Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

In Java

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.

import java.util.Scanner;

public class Lab7Num2 {

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

public 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 } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered 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

Students also viewed these Databases questions