Question: Using the Rectangle class you created in Chapter 8s programming assignment, create another class called RectangleComparator that implements the Comparator interface to compares two Rectangle
Using the Rectangle class you created in Chapter 8s programming assignment, create another class called RectangleComparator that implements the Comparator interface to compares two Rectangle objects according to area.Create a third class, RectangleSort, to be used as a test class. The main method should create a List containing at least five Rectangle objects. The method should print the list, displaying each rectangles area. Sort the list, and then print the list again to show the list has been sorted by area.
Chapter 8 programming assignment
Create a class called Rectangle with two instance variables width and height that are of type double. Provide a method called calculatePerimeter that calculates the perimeter of the rectangle, and provide a method called calculateArea that calculates the area of the rectangle. Both methods should return doubles. The class should also have set and get methods for setting and getting the width and height instance variables. The set methods should verify that the length and width specified should be within the range of 0.0 and 20.0. Ensure you code is commented well, especially the set methods explaining the input restrictions and what happens if the restrictions are not met.
Write a program to test the Rectangle class.
public class Rectangle {
private double width; private double height;
/** * Creates a Rectangle class, insuring that the specified width and height * are greater then 0.0 and less then or equal to 20.0. * * @param width the width * @param height the height */ public Rectangle(double width, double height) { this.checkWidthValue(width); this.checkHeightValue(height); this.width = width; this.height = height; }
/** * Returns the width * * @return the width */ public double getWidth() { return this.width; }
/** * Sets the width ensuring it is greater then 0.0 and less or equal to 20.0. * * @param width the width */ public void setWidth(double width) { this.checkWidthValue(width); this.width = width; }
/** * Returns the height. * * @return the height */ public double getHeight() { return this.height; }
/** * Sets the height ensuring it is greater then 0.0 and less or equal to * 20.0. * * @param height the height */ public void setHeight(double height) { this.checkHeightValue(height); this.height = height; }
/** * Calculates the perimeter of the rectangle. * * @return the perimeter */ public double calculatePerimeter() { return this.width * 2 + this.height * 2; }
/** * Calculates the area of the rectangle. * * @return the area */ public double calculateArea() { return this.width * this.height; }
/** * Check that the height value is within range. * * @param height the height * * @throws IllegalArgumentException if not */ private void checkHeightValue(double height) { if (height < 0.0 || height > 20.0) { throw new IllegalArgumentException("height is out of bounds"); } }
/** * Check that the width value is within range. * * @param width the width * * @throws IllegalArgumentException if not */ private void checkWidthValue(double width) { if (width < 0.0 || width > 20.0) { throw new IllegalArgumentException("width is out of bounds"); } } }
//RECTANGLETEST.JAVA
public class RectangleTest {
public static void main(String[] args) { System.out.println("-- Create a 4.0 by 5.0 rectangle --"); Rectangle rectangle = new Rectangle(4.0, 5.0);
System.out.println("area = " + rectangle.calculateArea()); System.out.println("perimeter = " + rectangle.calculatePerimeter()); System.out.println("-- Change width and height of rectangle to 8.0 by 10.0 --"); rectangle.setWidth(8.0); rectangle.setHeight(10.0); System.out.println("area = " + rectangle.calculateArea()); System.out.println("perimeter = " + rectangle.calculatePerimeter()); System.out.println("-- Out of bounds checking --"); try { rectangle.setHeight(-1.0); } catch (IllegalArgumentException e) { System.out.println("calling setHeight method with -1.0: " + e.getMessage()); }
try { rectangle.setHeight(20.5); } catch (IllegalArgumentException e) { System.out.println("calling setHeight method with 20.5: " + e.getMessage()); }
try { rectangle.setWidth(-1.0); } catch (IllegalArgumentException e) { System.out.println("calling setWidth method -1.0: " + e.getMessage()); }
try { rectangle.setWidth(20.5); } catch (IllegalArgumentException e) { System.out.println("calling setWidth method with 20.5: " + e.getMessage()); }
try { rectangle = new Rectangle(-1.0, 15); } catch (IllegalArgumentException e) { System.out.println("constructor with -1.0, 15: " + e.getMessage()); }
try { rectangle = new Rectangle(15, -1.0); } catch (IllegalArgumentException e) { System.out.println("constructor 15, -1.0: " + e.getMessage()); } } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
