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

• Two double data fields named width and height • One static string data field n

ID: 3625231 • Letter: #

Question

• Two double data fields named width and height
• One static string data field named color that specifies a color for the rectangle
• Two Constructor methods: 1st with no parameters, defaults of 1, 1, “white”; 2nd with parameters for width and height
• Accessor (getter) and Mutator (setter) methods for width, height and color
• A getArea() method that returns the area of this rectangle
• A getPerimeter() method that returns the perimeter of this rectangle

Draw a UML diagram for this class.
Write a second program that instantiates the Rectangle class and tests it.

Explanation / Answer

public class Rectangle { private double width; private double height; static private String color; public Rectangle() { width=1; height=1; color="white"; } public Rectangle(double width, double height) { this.width=width; this.height=height; } public double getWidth() { return width; } public double getHeight() { return height; } public String getColor() { return color; } public void setWidth(double width) { this.width=width; } public void setHeight(double height) { this.height=height; } public void setColor(String c) { color=c; } public double getArea() { return(height*width); } public double getPerimeter() { return(height*2+width*2); } } //In another file for the driver public class Driver { public static void main(String [] args) { Rectangle rect=new Rectangle(15,10); rect.setColor("Black"); System.out.println("Perimeter "+rect.getPerimeter()); System.out.println("Area "+rect.getArea()); } } //UML _________ Rectangle __________ -width: double -height: double -color: String _____________________ +Rectangle() +Rectangle(width: double, height: double) +getWidth(): double +getHeight(): double +getColor(): String +setWidth(width: double): void +setHeight(height: double): void +setColor(c: String): void +getArea(): double +getPerimeter(): double