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

Design a class name Rectangle to represent a rectangle. Two double data fields n

ID: 3639904 • Letter: D

Question

Design a class name Rectangle to represent a rectangle.

Two double data fields named width and height that specify the width and the height of the rectangle. The default values are 1 for the width and the 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 getAre() that returns the area of the rectangle.
A method name gerPerimeter() that returns the perimeter.
Write a test program that creates two rectangle objects-one with width 4 and height 40 and the other with the width 3.5 and height 35.9. Display the width, height , area, and the perimeter of each rectangle in this order.

My instructor said do give the values in the class. He wants the user to be able to give the values.

Explanation / Answer


public class Rectangle {
    private double height;
    private double width;
    
    Rectangle(){
      this.height=1;
      this.width=1;
    }
    Rectangle(double width, double height) {
        this.width=width;
        this.height=height;
    }
    
    double getArea() {
        return this.width*this.height;
    }
    double getPerimeter() {        
        return  2 *(this.width+this.height);
    }
        
    public String toString() {
        // Prints output to two decimals.
        java.text.DecimalFormat decimalFormat = new java.text.DecimalFormat("#.##");
        String output= "Width : "+this.width+" "+
                       "Height : "+this.height+" "+
                       "Area : "+decimalFormat.format(getArea())+" "+
                       "Perimeter : "+decimalFormat.format(getPerimeter());
        return output;
    }
    public static void main(String[] args) {
        Rectangle objRectangle1 = new Rectangle(4,40);
        Rectangle objRectangle2 = new Rectangle(3.5,35.9);
        
        System.out.println(objRectangle1.toString());
        System.out.println(objRectangle2.toString());
        
        
    }            
}
public class Rectangle {
    private double height;
    private double width;
    
    Rectangle(){
      this.height=1;
      this.width=1;
    }
    Rectangle(double width, double height) {
        this.width=width;
        this.height=height;
    }
    
    double getArea() {
        return this.width*this.height;
    }
    double getPerimeter() {        
        return  2 *(this.width+this.height);
    }
        
    public String toString() {
        // Prints output to two decimals.
        java.text.DecimalFormat decimalFormat = new java.text.DecimalFormat("#.##");
        String output= "Width : "+this.width+" "+
                       "Height : "+this.height+" "+
                       "Area : "+decimalFormat.format(getArea())+" "+
                       "Perimeter : "+decimalFormat.format(getPerimeter());
        return output;
    }
    public static void main(String[] args) {
        Rectangle objRectangle1 = new Rectangle(4,40);
        Rectangle objRectangle2 = new Rectangle(3.5,35.9);
        
        System.out.println(objRectangle1.toString());
        System.out.println(objRectangle2.toString());
        
        
    }            
}
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