Methods - Assignment 4
Implement a class Rectangle with the instance variables and methods mentioned below.
Method Description
calculateArea()
Calculate and return the area of the rectangle. The area should be rounded off to two decimal digits.
calculatePerimeter()
Calculate and return the perimeter of the rectangle. The perimeter should be rounded off to two decimal digits.
Test the functionalities using the provided Tester class.
Sample Input and Output
class Rectangle{
public float length;
public float width;
public double calculateArea()
{
double a=length*width;
double area=Math.round(a*100.0)/100.0;
return area;
}
public double calculatePerimeter()
{
double p=2*(length+width);
double perimeter=Math.round(p*100.0)/100.0;
return perimeter;
}
}
class Tester{
public static void main(String args[]){
Rectangle rectangle=new Rectangle();
//AssignvaluestothemembervariablesofRectangleclass
rectangle.length=12f;
rectangle.width=5f;
//InvokethemethodsoftheRectangleclasstocalculatetheareaandperimeter
double x=rectangle.calculateArea();
double y=rectangle.calculatePerimeter();
//Displaytheareaandperimeterusingthelinesgivenbelow
System.out.println("Areaoftherectangleis"+x);
System.out.println("Perimeteroftherectangleis"+y);
}
}
No comments:
Post a Comment