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

Java Program Practice: (The rectangle class) Following the example of the Circle

ID: 3671745 • Letter: J

Question

Java Program Practice:

(The rectangle class) Following the example of the Circle class, design and implement a class name Rectangle to represent 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.

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.

Write a test program 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

// Rectangle.java

/**
* Rectangle Area and Perimeter.
*/

public class Rectangle {
    // Declare and initialize default values.
   double width     = 1.0;
   double height    = 1.0;

    // Create constructor with default values.
    public Rectangle (){

    }

    // Create constructor with new width and height values.
    public Rectangle (double width, double height){
        if(width > 0 && height > 0){
            this.width = width;
            this.height = height;
        }else{
            this.width = 0;
            this.height = 0;
        }
    }

    // Calculate Area.
    public double getArea(){
        return this.width * this.height;
    }

    // Calculate Perimeter.
    public double getPerimeter(){
        return 2 * (this.width + this.height);
    }

    // Pepresentation method of Rectangle object.
    public String toString(){
        String result;
        if(this.width > 0 && height > 0){
            result = "The Rectangle Width: " + this.width + ", Height: " + this.height + ", Area: " + this.getArea() + ", and Perimeter: " + this.getPerimeter();
        }else if(this.width > 0 && height <= 0){
            result = "The height must be greater than zero.";
        }else if(this.width <= 0 && height > 0){
            result = "The width must be greater than zero.";
        }else{
            result = "The width and height must be greater than zero.";
        }
        return result;
    }

} // End of Rectangle class.

====================================================================================

//Test.java

/**
* Rectangle Area and Perimeter.
*/
public class Test{

    /** main Method */
    public static void main(String [] args){

        // Create Rectangle object.
        Rectangle rect1 = new Rectangle(4,40);
        Rectangle rect2 = new Rectangle(3.5,35.9);

            // Print Rectangle object values.
            System.out.println(rect1);
            System.out.println(rect2);

    } // End of main method

} // End of TestRectangle class.

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