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

Problem: Write a class named Rectangle to represent rectangles. The data fields

ID: 3864644 • Letter: P

Question

Problem: Write a class named Rectangle to represent rectangles. The data fields are width, height, and color. Use double for width and height, and String for color. Suppose that all the rectangles are the same color. You need to provide the accessor methods for the properties and a findArea() method for computing the area of the rectangle.

The outline of the class is given as follows:

The outline of the class is given as follows:

public class Rectangle {

private double width = 1;

private double height = 1;

private String color = "white";

public Rectangle( ) { }

public Rectangle(double widt, double height, String color) { }

public double getWidth( ) { }

public void setWidth( double width) { }

public double getHeight( ) { }

public void setHeight (double height) { }

public String getColor( ) { }

public void setColor ( String color) { }

public double findArea ( ) { }

public String toString ( ) { }

}

Write a client program(test class) to test the class Rectangle. In the client program, create two Rectangle objects. Assign any width and height to the two objects. Assign the first object the color red, and the second, yellow. Display the properties of both objects and find their areas.

Explanation / Answer

public class RectangleBase {
   private double base ;
   private double height;
  
   private String color ;
  
   //Initializing rectangle object with height base and color
  
   public RectangleBase(double height,double base ,String color)
   {
       this.height=height;
       this.base=base;
       this.color=color;
   }
  
   public double getHeight()
   {
       return height;
   }
   public String getColor()
   {
       return color;
   }
   public double getBase()
   {
       return base;
   }
   //Checking for equality of rectangle
   public static boolean triangleEquality(RectangleBase r1 , RectangleBase r2 )
   {
       if(r1.height==r2.height && r1.base==r2.base&& r1.color.equalsIgnoreCase(r2.color))
       {
           System.out.println("Triangles are equal");
           return true;
       }
       else
           {
           System.out.println("Triangles are not equal");
           return false;
           }
         
   }


   //Area calculation
   public double getArea()
   {
      
       return this.base*this.height;
   }//getArea()
};//class

Client class:-

public class RectangleClient {

   public static void main(String[] args) {
       // TODO Auto-generated method stub
       RectangleBase rectangle1 = new RectangleBase(2,3,"red");
       RectangleBase rectangle2 = new RectangleBase(2,5.2,"yellow");
       System.out.println("Height="+rectangle1.getHeight()+" Base= "+rectangle1.getBase()+"Color of triangle= "+rectangle1.getColor());
       System.out.println("Height="+rectangle2.getHeight()+" Base= "+rectangle2.getBase()+"Color of triangle= "+rectangle2.getColor());
           RectangleBase.triangleEquality(rectangle1, rectangle2);
           System.out.println("Area= "+rectangle1.getArea());
           System.out.println("Area= "+rectangle2.getArea());
   };//main

};//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