Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Alter your Rectangle class to include compareTo and make any other modifications you need. DO NOT alter the toString method. Write a main program so

Alter your Rectangle class to include compareTo and make any other modifications you need. DO NOT alter the toString method. Write a main program so that it will first read an int to say how many rectangles you will input. Then read that many lengths and widths from the keyboard, create rectangles, add to an ArrayList. Finally sort and output the ArrayList. Note: One rectangle is bigger than another if its area is bigger.

public class Lab7Num2 {

public static class Rectangle { private double length, width; public Rectangle() { length=0; width=0; } public Rectangle(double len, double wid) { length=len; width=wid; }

public double getLength() { return length; }

public void setLength(double length) { this.length = length; }

public double getWidth() { return width; }

public void setWidth(double width) { this.width = width; } public double area() { return length*width; } public double perimeter() { return 2*(length+width); } public String toString() { return "Length: " + length + " Width: " + width; } public int compareTo(Rectangle r) { //put your code here } } public static void main(String[] args) { Scanner keyboard = new Scanner(System.in); //declare an ArrayList to hold your Rectangles //input the number of rectangles int howMany = keyboard.nextInt(); //loop howMany times, //each time input a length and width, create a rectangle, add it to the ArrayList //sort the ArrayList //output the ArrayList } }

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

MongoDB Applied Design Patterns Practical Use Cases With The Leading NoSQL Database

Authors: Rick Copeland

1st Edition

1449340040, 978-1449340049

More Books

Students also viewed these Databases questions

Question

Distinguish between poor and good positive and neutral messages.

Answered: 1 week ago

Question

Describe the four specific guidelines for using the direct plan.

Answered: 1 week ago