Question: Problem 1 Write a class called Rectangle that implements the Comparable interface with the following features. a. Defines the following private instance variables: int width

Problem

1

Write a class called Rectangle that implements the Comparable interface with the following features.

a.

Defines the following private instance variables:

int

width

int

length

b.

Defines a constructor that initializes the instance variables. The header

for the constructor is

public Rectangle(int width int length)

c.

A method with the header

public String toString()

that returns a String that contains the values stored in the instance variables.

Example: If

width = 4 and length = 6 then your method returns " width = 4 length= 6"

d.Defines a compareTo method

that compares Rectangle objects by perimeter.

If the perimeter of Rectangle1 is greater than the perimeter of Rectangle2 then Rectangle1 is greater than Rectangle2

If the perimeter of Rectangle1 = the perimeter of Rectangle2 then Rectangle1 equals Rectangle2

If the perimeter of Rectangle1 is less than the perimeter of Rectangle2 then Rectangle1 is less than Rectangle2

Math 140 Interface Worksheet

Problem 2:

How would you change the code for

Problem

1, part d

if we ordered

Rectangle objects

with the following rules

1.Compare two Rectangle objects by width.

The Rectangle object with the larger width is considered to by the larger Rectangle object.

(This rule sorts Rectangle objects in ascending order by width.)

2. Compare two Rectangle objects by length.

The Rectangle object with the smaller length is considered to by the larger Rectangle object.

(This rule sorts Rectangle objects in descending order by length.)

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!