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

Design a class named Rectangle that represents a rectangle. The class should con

ID: 3632854 • Letter: D

Question

Design a class named Rectangle that represents a rectangle. The class should contain the following:
Two private double data fields named “width” and “height” where the default width and height values are 1
- A no-arg constructor that creates a default rectangle
- A constructor that creates a rectangle with the specified width and height
- A method named getArea() that returns the area of this rectangle
- A method named getPerimeter() that returns the perimeter
- Two methods to get the width and height of the rectangle, named getWidth() and getHeight()

Explanation / Answer

public class Rectangle { private double width; private double height; private String color = "white"; public Rectangle () { width = 1.0; height = 1.0; } private Rectangle (double w, double h) //init width to w, init height to h { width = w; height = h; } public double getWidth() // called a getter { return width; } public void setWidth(double w) // this is a mutator method. Also called a setter { width = w; } public double getHeight() // called a getter { return height; } public void setHeight(double w) // this is a mutator method. Also called a setter { height = w; } public double getArea() { return width*height; } public double getPerimeter() { return height* 2 + width* 2; // was "return 0.0;" } } public class RectangleTester { public static void main (String[] args) { Rectangle rect1 = new Rectangle(4.0, 40.0); Rectangle rect2 = new Rectangle(3.5, 35.9); double result = rect1.getWidth(); System.out.println (result); result = rect2.getWidth(); System.out.println (result); } }

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