Question
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
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