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 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)
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started