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

Given this code: public class Rectangle implements Comparable{ public final int

ID: 3603146 • Letter: G

Question

Given this code:

public class Rectangle implements Comparable{
  
   public final int NUM_OF_SIDES = 4;
   private double length;
   private double width;
  
   public Rectangle(){
       length = 2;
       width = 2;
   }
  
   public Rectangle(double l, double w){
       length = l;
       width = w;
   }
  
   public void setLength(double l){
       length=l;
   }
  
   public void setWidth(double w){
       width = w;
   }
  
   public double getLength(){
       return length;
   }
  
   public double getWidth(){
       return width;
   }
  
   public double getPerimeter(){
       return 2*length + 2*width;
   }
  
   public double getArea(){
       return length * width;
   }
  
   public boolean equals(Object obj){
       if(obj instanceof Rectangle){
           Rectangle that = (Rectangle) obj;
           if(this.length == that.length && this.width == that.width){
               return true;
           }
       }
       return false;
   }
           public int compareTo(Object obj){
               if(obj instanceof Rectangle){
                   Rectangle that = (Rectangle) obj;
                  
           double diff = this.getArea() - that.getArea();
          
           if(Math.abs(diff) <=.001){
               return 0;
           } else if (diff > 0){
               return 1;
           } else {
               return -1;
           }
      
       }
       return 1;
   }
   public String toString(){
       return "Rectangle: "
       +" length: " + length + " "
       +" width: " + width + " ";
   }
}

answer this:

What value is stored in this variable result:

1)

Rectangle r1 = new Rectangle(4,2);

Rectangle r2 = new Rectangle();

double result = r2.getArea(2);

if(r1.equals(r2)){

result += 10;

}

2)

Rectangle r1 = new Rectangle(3,3);

Rectangle r2 = new Rectangle(2,2);

double result = 3;

result = r1.getPerimeter() + r1.compareTo(r2)*(4/r2.compareTo(r1));

(if you can please explain how you got answer, thanks)

Explanation / Answer

//Lets answer 2 question one by one.

1. The 1st question where r1 is a rectengle with dimension 4,2 and r2 is a rectengle with dimension 2,2 ( as default constructor is getting called and it will set dimension 2,2).

Now result variable contains r2.getArea() (note here if the argument 2 is passed while calling getArea method it will give a direct compiler error)  which is 4 as the dimension of r2 is 2,2. equals() method return true or false after comparing the object dimension. In this case we are comparing r1 (3,2) and r2 (2,2) whose dimensions are not same. So it will return false and the program will not go inside the if block. So result will contain of value 4 at the end .

result=4 ...... ans

2. In case of 2nd quesion trick is the compareTo() method. It returns +1 if the object invoking the method is bigger than the object passed as argument, returns 0 if the object invoking the method is equal to the object passed as argument, and returns -1 if the object invoking the method is lesser than the object passed as argument. Now when it will execute

result = r1.getPerimeter() + r1.compareTo(r2)*(4/r2.compareTo(r1));

r1.getPerimeter() will give 12 , r1.compareTo(r2) will give +1 as r1 have bigger dimension than r2, r2.compareTo(r1) will give -1 as r2 have lesser dimension than r1. So

result=12+1*(4/-1) = 12+ (1*-4) = 12-4= 8

so the end result is 8

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