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

Implement the following steps: 1. Modify the class Rectangle from chapter 9 such

ID: 3941430 • Letter: I

Question

Implement the following steps:

1. Modify the class Rectangle from chapter 9 such that it implements the Comparable interface. Comparable interface defines only one method, compareTo(). Read API documentation to learn the requirements for this method. Add a toString() method to help with the output.

2. Write an application that defines an array of 5 Rectangle objects. This application should initialize the 5 objects with different data and then sort the array by calling Arrays.sort() method. Finally, the application should display the sorted array. Arrays class is located in the java.util package.

Explanation / Answer

Hi, PLease find my implementation.

Please let me know in case of any issue.

############ Rectangle.java ############

/**

* The Rectangle class stores and manipulates

* data for a rectangle.

*/

public class Rectangle implements Comparable<Rectangle>

{

   private double length;

   private double width;

   /**

   * Constructor

   */

   public Rectangle(double len, double w)

   {

       length = len;

       width = w;

   }

   /**

   * The setLength method accepts an argument

   * which is stored in the length field.

   */

   public void setLength(double len)

   {

       length = len;

   }

   /**

   * The setWidth method accepts an argument

   * which is stored in the width field.

   */

   public void setWidth(double w)

   {

       width = w;

   }

   /**

   * The set method accepts two arguments

   * which are stored in the length and width

   * fields.

   */

   public void set(double len, double w)

   {

       length = len;

       width = w;

   }

   /**

   * The getLength method returns the value

   * stored in the length field.

   */

   public double getLength()

   {

       return length;

   }

   /**

   * The getWidth method returns the value

   * stored in the width field.

   */

   public double getWidth()

   {

       return width;

   }

   /**

   * The getArea method returns the value of the

   * length field times the width field.

   */

   public double getArea()

   {

       return length * width;

   }

   @Override

   public int compareTo(Rectangle o) {

       if(getArea() < o.getArea())

           return -1;

       else if(getArea() > o.getArea())

           return 1;

       else

           return 0;

   }

  

   @Override

   public String toString() {

       return "Length: "+length+", Width: "+width+", Area: "+getArea();

   }

}

############## RectangleTest.java ###############

import java.util.Arrays;

public class RectangleTest {

  

   public static void main(String[] args) {

      

       // creating an array of Rectangle

       Rectangle[] rectangles = {

               new Rectangle(11, 5.7),

               new Rectangle(8.0, 7.5),

               new Rectangle(4, 5.5),

               new Rectangle(6, 12.5),

               new Rectangle(9.5, 3)

       };

      

       // sorting

       Arrays.sort(rectangles);

      

       // printing

       for(Rectangle r : rectangles)

           System.out.println(r);

   }

}

/*

Sample run:

Length: 4.0, Width: 5.5, Area: 22.0

Length: 9.5, Width: 3.0, Area: 28.5

Length: 8.0, Width: 7.5, Area: 60.0

Length: 11.0, Width: 5.7, Area: 62.7

Length: 6.0, Width: 12.5, Area: 75.0

*/