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;

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: " +"\tlength:\t" + length + " " +"\twidth:\t" + 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)

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!