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

2. (25 points] Define class Rectangle and test it in class RectangleTester. (A)

ID: 3595469 • Letter: 2

Question

2. (25 points] Define class Rectangle and test it in class RectangleTester. (A) Write complete code to define class Rectangle, which represents a rectande shape. (1) The data member we are interested in is length and width, an int. which is defirned as 1. There are two constructors, one is default (set data member length and width to be 1), the other is non-default with two parameter (f the input parameters are positive integers, use the first one to initialize data member Length, use the second one to initialize data member width, otherwise, set default setting for data members) tLength, getwidth, getArea, getPerimeter.The f a rectangle is the product of length and width, the perimeter of a rectangle 2. The getters are ge is twice the sum of length and width. A setter is setLength. if the input parameter is positive, then use that parameters value to reset data member length; otherwise, do not change. 3. Define setWidth similarly Override tostring and equals method inherited from class Object. In tostring method, return information like length, width, area, and perimeter of that rectangle. Two rectangles are equivalent if their lengths are equivalent and their widths are equivalent. 4. 5. (B) Write Rectangleclient method to test Rectangle class you defined. (1) Create a rectangle with default constructor, (2) Change its length to 5. (3) Change its width to -5. (4) Print out the rectangle. (5) Create another rectangle with length 5 and width 1, compare the two rectangles equals or not.

Explanation / Answer

Please find my implementation.

public class Rectangle {

   private int length;

   private int width;

   // constructors

   public Rectangle() {

       length = 1;

       width = 1;

   }

   public Rectangle(int len, int wid) {

       if(len > 0)

           length = len;

       else

           length = 1;

       if(wid > 0)

           width = wid;

       else

           width = 1;

   }

   // setters and getters

   public int getLength() {

       return length;

   }

   public int getWidth() {

       return width;

   }

   public void setLength(int len) {

       if(len > 0)

           length = len;

       else

           length = 1;

   }

   public void setWidth(int wid) {

       if(wid > 0)

           width = wid;

       else

           width = 1;

   }

   public int getArea() {

       return length*width;

   }

   public int getPerimeter() {

       return 2*(length + width);

   }

   @Override

   public String toString() {

       return "Lenght: "+length+", Widht: "+width+", Area: "+getArea()+", Perimeter: "+getPerimeter();

   }

   @Override

   public boolean equals(Object obj) {

       if(obj instanceof Rectangle) {

           Rectangle r = (Rectangle)obj;

           if(r.length == length && r.width == width)

               return true;

       }

       return false;

   }

}

############

public class RectangleClient {

   public static void main(String[] args) {

       Rectangle r1 = new Rectangle(); // with default constructor

       r1.setLength(5);

       r1.setWidth(-5);

       System.out.println(r1);

       Rectangle r2 = new Rectangle(); // with parameterized constructor

       r2.setLength(5);

       r2.setWidth(1);

       System.out.println(r2);

      

       if(r1.equals(r2))

           System.out.println("R1 and R2 are same");

       else

           System.out.println("R1 and R2 are NOT same");

   }

}

/*

Sampel run:

Lenght: 5, Widht: 1, Area: 5, Perimeter: 12

Lenght: 5, Widht: 1, Area: 5, Perimeter: 12

R1 and R2 are same

*/

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