Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions