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

Please somebody assist me. I want to write a java program designing a class name

ID: 3641270 • Letter: P

Question

Please somebody assist me. I want to write a java program designing a class named Rectangle to represent rectangle. The class contains: two double data fields named width and height that specify the width and height of a rectangle. The default values are 1 for both width and height. A no-arg constructor that creates a 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 of this rectangle.
This should be implemented writing a java program that creates two objects: one with width 4 and height 40 and the other with width 3.5 and height 35.9. I need to display the height, width, area, and perimeter of each rectangle in this order.

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());
        
        
    }            
}