In: Computer Science
Purpose: To write an application using the list data structure that sorts objects in ascending order.
Details:
Create a class called Rectangle containing the following:
Create a second class called RectangleTest that contains the main method, and thoroughly tests the Rectangle class’s methods. This test class does not need to ask users for input. Just create the needed Rectangle objects to ensure that you test the Rectangle class’s methods well. The thoroughness of your testing is important.
***Throw an IllegalArgumentException indicating there is a problem with the input parameters in the constructor and the set methods.Also, rewrite the RectangleTest class from Chapter 8 to handle the exceptions thrown by the Rectangle class. Thoroughly tests the Rectangle class’s methods. This test class does not need to ask users for input. Just create the needed Rectangle objects to ensure that you test the Rectangle class’s methods well. The thoroughness of your testing in will impact your grade.***8
Then---create a second class called RectangleComparator that implements the Comparator interface to compares two Rectangle objects according to area.
Create a third class called RectangleSortTest that contains the main method. The method should create a List containing five Rectangle objects, not in order by area. The method should print the list, displaying each rectangle’s area. Then sort the list, and print the list again showing the list has been sorted by area. This test class does not need to ask users for input, nor does it need to do any additional error checking.
Thanks for the question. Below is the code you will be needing Let me know if you have any doubts or if you need anything to change. Thank You ! =========================================================================== public class Rectangle { private double width; private double height; public Rectangle(double width, double height) { if (0.0 < width && width <= 20.0 && 0 < height && height <= 20.0) { this.width = width; this.height = height; } else { throw new IllegalArgumentException("Error: Invalid dimensions provided"); } } public double getWidth() { return width; } public void setWidth(double width) { if (0.0 < width && width <= 20.0 && 0 < height && height <= 20.0) { this.width = width; } else { throw new IllegalArgumentException("Error: Invalid width provided"); } } public double getHeight() { return height; } public void setHeight(double height) { if (0.0 < width && width <= 20.0 && 0 < height && height <= 20.0) { this.height = height; } else { throw new IllegalArgumentException("Error: Invalid height provided"); } } public double calculatePerimeter() { return 2 * (width + height); } public double calculateArea() { return width * height; } }
=============================================================
public class RectangleTest { public static void main(String[] args) { // testing exception is getting thrown when width or length // are outside the range and printing the exception message try { Rectangle invalid = new Rectangle(-10, 21); } catch (IllegalArgumentException e) { System.out.println(e.getMessage()); } // testing the perimeter and area Rectangle rectangle = new Rectangle(4.5, 8.7); System.out.println("Perimeter = " + rectangle.calculatePerimeter()); System.out.println("Area = " + rectangle.calculateArea()); } }
=============================================================
import java.util.Comparator; public class RectangleComparator implements Comparator<Rectangle> { @Override public int compare(Rectangle o1, Rectangle o2) { return Double.compare(o1.calculateArea(),o2.calculateArea()); } }
=============================================================
import java.util.ArrayList; import java.util.Collections; public class RectangleSortTest { public static void main(String[] args) { ArrayList<Rectangle> rectangles = new ArrayList<>(); rectangles.add(new Rectangle(4.6, 2)); rectangles.add(new Rectangle(14.6, 5.4)); rectangles.add(new Rectangle(1.6, 6.4)); rectangles.add(new Rectangle(1.9, 17.8)); rectangles.add(new Rectangle(16.9, 5.4)); Collections.sort(rectangles, new RectangleComparator()); System.out.println("Printing Sorted Rectangle by Area: "); for (Rectangle rectangle : rectangles) { System.out.println(rectangle.getWidth() + " x " + rectangle.getHeight() + ", Area: " + String.format("%.2f", rectangle.calculateArea())); } } }
=============================================================
=============================================================