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

Question: (More Rectangle Class) Please continue work on our previous lab about

ID: 3695688 • Letter: Q

Question

Question: (More Rectangle Class) Please continue work on our previous lab about rectangle class and add the following:

1. One integer data field named numberOfObjects that specify the number of objects that the rectangle class created in test program, refer to the circle class on textbook.

2. A method called toSting() that returns a string that is one line description of each rectangle class.

Here is an example of toSting() for Circle class.

--------------------------

public String toString()

{

return "The radius is:" + this.radius+","+"The area is:" +this.getArea()”;

}

---------------------------------

// here is the statement that use this toString() method in test program.

Circle c1 = new Circle();

System.out.println(c1);

3: Modify your test program so that it will use the toString() method and print out the number of objects created by test program.

Bonus (20 points): Declares and creates an array of five Rectangle objects in your test program, initialize rectangle length and width use any number you want, then calculate the total area of the rectangles in the array. (Refer to sample code on our textbook).

--------------------------------------------------------------------------------------------------------------------------------------------

Rect.java

-------------------------------------

public class Rect {
double width,length;
    Rect()
    {
        width=0.0;
        length=0.0;
    }
    Rect(double w,double l)
    {
        if(w>0 && l>0)
        {
        width=w;
        length=l;
        }
        else
        System.out.println("Dimensions can't be negative");
    }
    public double getWidth()
    {
        return width;
    }
    public double getLength()
    {
        return length;
    }
    public void setLength(double l)
    {
        if(l<0)
        System.out.println("Length must be greater than zero");
        else
        length=l;
    }
    public void setWidth(double w)
    {
        if(w<0)
        System.out.println("Width must be greater than zero");
        else
        width=w;
    }
    public double getArea()
    {
        return width*length;
    }
    public double getPerimeter()
    {
        return 2*(width+length);
    }
}

--------------

TestRect.java

--------------------------

public class TestRect {
   public static void main(String []args){
         Rect r1=new Rect(40,4);
         Rect r2=new Rect(35.9,3.5);
         System.out.println(" =====Info of rectangle 1======");
         System.out.print("Width: "+r1.getWidth()+" Length: "+r1.getLength());
         System.out.print(" Area: "+r1.getArea()+" Perimeter: "+r1.getPerimeter());
          System.out.println(" =====Info of rectangle 2======");
         System.out.print("Width: "+r2.getWidth()+" Length: "+r2.getLength());
         System.out.print(" Area: "+r2.getArea()+" Perimeter: "+r2.getPerimeter());
         System.out.print(" ");
     }  
}

In Java

Explanation / Answer

################### Rect.java ################################

public class Rect {
  
   static int numberOfObjects = 0;
   double width, length;

   Rect() {
       width = 0.0;
       length = 0.0;
       numberOfObjects++;
   }

   Rect(double w, double l) {
       numberOfObjects++;// increasing count of object
       if (w > 0 && l > 0) {
           width = w;
           length = l;
       } else
           System.out.println("Dimensions can't be negative");
   }

   public double getWidth() {
       return width;
   }

   public double getLength() {
       return length;
   }

   public void setLength(double l) {
       if (l < 0)
           System.out.println("Length must be greater than zero");
       else
           length = l;
   }

   public void setWidth(double w) {
       if (w < 0)
           System.out.println("Width must be greater than zero");
       else
           width = w;
   }

   public double getArea() {
       return width * length;
   }

   public double getPerimeter() {
       return 2 * (width + length);
   }

   // toString method
   @Override
   public String toString() {
       return "The length is:" + this.length + ", The width is: " + this.width
               + ", The area is:" + this.getArea();
   }
}

########################### TestRect.java ########################################
public class TestRect {
   public static void main(String []args){
   Rect r1=new Rect(40,4);
   Rect r2=new Rect(35,3.5);
     
   Rect[] rectangles = {
       r1, r2, new Rect(4,5),
       new Rect(3.5, 6),
       new Rect(1,1)
   };
     
   for(Rect rectangle: rectangles){
       System.out.println(rectangle);
   }
     
     
   /*System.out.println(" =====Info of rectangle 1======");
   System.out.print("Width: "+r1.getWidth()+" Length: "+r1.getLength());
   System.out.print(" Area: "+r1.getArea()+" Perimeter: "+r1.getPerimeter());
   System.out.println(" =====Info of rectangle 2======");
   System.out.print("Width: "+r2.getWidth()+" Length: "+r2.getLength());
   System.out.print(" Area: "+r2.getArea()+" Perimeter: "+r2.getPerimeter());
   System.out.print(" ");*/
   }
   }

/*

Output:

The length is:4.0, The width is: 40.0, The area is:160.0
The length is:3.5, The width is: 35.0, The area is:122.5
The length is:5.0, The width is: 4.0, The area is:20.0
The length is:6.0, The width is: 3.5, The area is:21.0
The length is:1.0, The width is: 1.0, The area is:1.0

*/

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