Given this code: public class Rectangle implements Comparable{ public final int
ID: 3602572 • 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 r = new Rectangle(2, 4);
double result = r.getPerimeter()*r.getArea()%r.NUM_OF_SIDES;
2)
Rectangle r = new Rectangle(3,3);
double result = 2;
if(r.equals(r.toString())){
result -= 5;
}else{
result *= 2;
}
result += r.getArea(2);
(if you can please explain how you got answer, thanks)
Explanation / Answer
What value is stored in this variable result:
1)
Rectangle r = new Rectangle(2, 4);
double result = r.getPerimeter()*r.getArea()%r.NUM_OF_SIDES;
Answer: 0.0
Rectangle area here is 8 and perimeter is 16
So result = (16 * 8) % 4 will return 0.0 value
2)
Rectangle r = new Rectangle(3,3);
double result = 2;
if(r.equals(r.toString())){
result -= 5;
}else{
result *= 2;
}
result += r.getArea();
Answer; 13.0
Rectanle width and height is 3 and 3 so area is 9
nitially result value is 2
then if condition will return false so else block will execute so result = result + 2 means result = 2+ 2 = 4
finally we are adding result value with area so result = 4 + 9 = 13.0
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.