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

Using the example for the circle class: class Circle { /** The radius of this ci

ID: 3631761 • Letter: U

Question

Using the example for the circle class:
class Circle {
/** The radius of this circle */
double radius = 1.0;

/** Construct a circle object */
Circle () {
}

/** Construct a circle object */
Circle ( double newRadius) {
radius = new Radius;
}

/** Return the area of this circle */
double getArea () {
return radius * radius * Math.PI;
}
}

Use the above example to design a class name Rectangle to represent a rectangle. The class contains:
- Two double data fields named width and height that specify the width and height of the
rectangle. The default values are 1 for both width and height.
- 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.
Implement the class. Write a program in Java that creates two Rectangle objects—one with width 4 and height 40 and the other with width 3.5 and height 35.9. Display the width, height, area, and perimeter of each rectangle in this order.

Explanation / Answer

//Create class Rectangle class Rectangle{ // Declare two variables Width and Height and assign default value as 1 double Width=1.0; double Height=1.0; // Create empty constructor Rectangle() { } // Create a constructor with two parameters width and height Rectangle(double newWidth, double newHeight) { Width=newWidth; Height=newHeight; } // Method getArea() retuns the area of rectangle public double getArea() { return Width*Height; } //Method getPerimeter() retuns the perimeter of given rectangle public double getPerimeter() { return 2*(Width+Height); } }//End class //Create a Test class to test the methods in Rectangle class class TestRectangle{ //Main Method public static void main(String[] args){ //Create two objects r1 and r2 Rectangle r1=new Rectangle(4.0, 40.0); Rectangle r2=new Rectangle(3.5, 35.9); //Display Output System.out.println("Width :"+r1.Width); System.out.println("Height :"+r1.Height); System.out.println("Area :"+r1.getArea()); System.out.println("Perimeter :"+r1.getPerimeter()); System.out.println(); System.out.println("Width :"+r2.Width); System.out.println("Height :"+r2.Height); System.out.println("Area :"+r2.getArea()); System.out.println("Perimeter :"+r2.getPerimeter()); }//End main method }//End class

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Chat Now And Get Quote