Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Alter your Rectangle class to include compareTo and make any other modifications

ID: 3788356 • Letter: A

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

/**
*
* @author Jean Mehta
*/
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

}
  
  
}

Explanation / Answer


// Lab7Num2.java
import java.util.Scanner;
import java.util.ArrayList;
import java.util.Collections;

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)
{
double difference = area() - r.area();
if (difference < 0)
return -1;
else if (difference == 0)
return 0;
else // difference > 0
return 1;
}
}
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
//declare an ArrayList to hold your Rectangles
ArrayList<Rectangle> al = new ArrayList<Rectangle>();

//input the number of rectangles
System.out.print("Enter 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
for (int i = 0; i < howMany ;i++ )
{
System.out.print(" Enter length of rectangle " + (i+1) + ": ");
double length = keyboard.nextDouble();
System.out.print("Enter width of rectangle " + (i+1) + ": ");
double width = keyboard.nextDouble();
al.add(new Rectangle(length,width));
}

//sort the ArrayList
for (int i = 0; i < howMany ;i++ )
{
for (int j = 0; j < howMany ;j++ )
{
if(al.get(i).compareTo(al.get(j)) < 0)
Collections.swap(al,i, j);
}
}

//output the ArrayList
System.out.println(" Sorted rectangles by area: ");
for (int i = 0; i < howMany ;i++ )
{
System.out.println(al.get(i));
System.out.println("Area: " + al.get(i).area() + " ");   
}

}
  
  
}


/*
output:

Enter the number of rectangles: 3

Enter length of rectangle 1: 3.4
Enter width of rectangle 1: 5

Enter length of rectangle 2: 2.3
Enter width of rectangle 2: 4

Enter length of rectangle 3: 6
Enter width of rectangle 3: 1.3


Sorted rectangles by area:
Length: 6.0 Width: 1.3
Area: 7.8

Length: 2.3 Width: 4.0
Area: 9.2

Length: 3.4 Width: 5.0
Area: 17.0


*/

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote