Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Problem: Write a class named Rectangle to represent rectangles. The data fields

ID: 3641209 • Letter: P

Question

Problem: Write a class named Rectangle to represent rectangles. The data fields are width, height, and color. Use double for width and height, and String for color. Suppose that all the rectangles are the same color. You need to provide the accessor methods for the properties and a findArea() method for computing the area of the rectangle.

The outline of the class is given as follows:

public class Rectangle {
private double width = 1;
private double height = 1;
private String color = “white”;

public Rectangle( ) { }

public Rectangle ( double width, double height, String color ) { }

public double getWidth ( ) { }

public void setWidth ( double width ) { }

public double getHeight ( ) { }

public void setHeight ( double height ) { }

public String getColor ( ) { }

public void setColor ( String color ) { }

public double findArea ( ) { }

public String toString ( ) { }
}

Write a client program(test class) to test the class Rectangle. In the client program, create two Rectangle objects. Assign any width and height to the two objects. Assign the first object the color red, and the second, yellow. Display the properties of both objects and find their areas.

Explanation / Answer

public class Rectangle { private double width = 1; private double height = 1; private String color = "white"; public Rectangle( ) { } public Rectangle ( double width, double height, String color ) { this.width = width; this.height = height; this.color = color; } public double getWidth ( ) { return width; } public void setWidth ( double width ) { this.width = width; } public double getHeight ( ) { return height; } public void setHeight ( double height ) { this.height = height; } public String getColor ( ) { return color; } public void setColor ( String color ) { this.color = color; } public double findArea ( ) { return width * height; } public String toString ( ) { return "Rectangle with width " + width + ", height " + height + ", and color " + color; } } public class rectangleTest extends Rectangle { public static void main(String[] args) { Rectangle first = new Rectangle(2,4,"red"); Rectangle second = new Rectangle(3,7, "yellow"); System.out.println(first.toString()); System.out.println(second.toString()); System.out.println("Area of first: " + first.findArea()); System.out.println("Area of second: " + second.findArea()); } }

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote