by using blueJ : Rectangle Class For this exercise, you need to implement two (2
ID: 3689612 • Letter: B
Question
by using blueJ
: Rectangle Class
For this exercise, you need to implement two (2) classes, the Rectangle class and the RectangleTest class, as
follows:
Rectangle class:
o Instancevariables:
A variable named width that stores the width of the rectangle as a double.
A variable named length that stores the length of the rectangle as a double. o Defaultconstructor:
Initializes all data fields to default values. o Overloadedconstructor
Parameters: a width and a length.
Initializes all data fields to the given values passed by the parameters. o Method getWidth
Returns the width of the calling object. o Method getLength
Returns the length of the calling object. o Method setWidth
Parameter: A width
Re-sets the value of the object's width to the value passed by the parameter. o Method setLength
Parameter: A length
Re-sets the value of the object's length to the value passed by the parameter. o Method calculateArea
Returns the area of the object (does NOT print; it only performs a calculation). o Method calculatePerimeter
Returns the perimeter of the object (does NOT print; it only performs a calculation). o Method printDimensions
Prints out all the data related to the object (see output below for format).
RectangleTest class:
o Perform the following actions in the main method:
Create an object named r1 of the Rectangle class using the default constructor.
Using the set methods, initialize r1 to have width 3.0 and length 5.2.
Use the printDimensions method to output all the data related to r1.
Create another object named r2 of the Rectangle class with width 2.6 and length 5.4
using the overloaded constructor.
Print out r2 using the get methods.
Print out the area and the perimeter of r1 using the calculateArea and
calculatePerimeter methods.
Print out the area and the perimeter of r2 using the calculateArea and
calculatePerimeter methods.
Expected Output:
Explanation / Answer
/**
* @author Srinivas Palli
*
*/
public class Rectangle {
double width;
double length;
public Rectangle() {
// TODO Auto-generated constructor stub
}
/**
* @param width
* @param length
*/
public Rectangle(double width, double length) {
this.width = width;
this.length = length;
}
/**
* @return the width
*/
public double getWidth() {
return width;
}
/**
* @param width
* the width to set
*/
public void setWidth(double width) {
this.width = width;
}
/**
* @return the length
*/
public double getLength() {
return length;
}
/**
* @param length
* the length to set
*/
public void setLength(double length) {
this.length = length;
}
/**
* method to calculate the area of the object
*
* @return
*/
public double calculateArea() {
return (length * width);
}
/**
* method to perimeter the area of the object
*
* @return
*/
public double calculatePerimeter() {
return (2 * (length + width));
}
public void printDimensions() {
System.out.printf("Width: %.2f Length: %.2f ", width, length);
}
}
/**
* @author Srinivas Palli
*
*/
public class RectangleTest {
/**
* @param args
*/
public static void main(String[] args) {
Rectangle r1 = new Rectangle();
r1.setLength(5.2);
r1.setWidth(3.0);
r1.printDimensions();
Rectangle r2 = new Rectangle(2.6, 5.4);
System.out.printf("Width : %.2f ", r2.getWidth());
System.out.printf("Length : %.2f ", r2.getLength());
System.out.printf("R1 Perimeter: %.2f ", r1.calculatePerimeter());
System.out.printf("R1 Area: %.2f ", r1.calculateArea());
System.out.printf("R2 Perimeter: %.2f ", r2.calculatePerimeter());
System.out.printf("R2 Area: %.2f ", r2.calculateArea());
}
}
OUTPUT:
Width: 3.00
Length: 5.20
Width : 2.60
Length : 5.40
R1 Perimeter: 16.40
R1 Area: 15.60
R2 Perimeter: 16.00
R2 Area: 14.04
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.