In: Computer Science
Purpose: To write an Object-Oriented application that creates a Java class with several instance variables, a constructor to initialize the instance variables, several methods to access and update the instance variables’ values, along with other methods to perform calculations. Also, write a test class that instantiates the first class and tests the class’s constructor and methods.
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 in will impact your grade.
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. If you are satisfied with the solution, please leave a +ve feedback : ) Let me know for any help with any other questions. Thank You! =========================================================================== public class Rectangle { private double width; private double height; //Provide a constructor with two parameters used to initializes each instance variable. public Rectangle(double width, double height) { if (width < 0 || width > 20 || height < 0 || height > 20) { throw new IllegalArgumentException("Error: Invalid dimension(s) provided."); } this.width = width; this.height = height; } //Provide get methods that return the values of each instance variables. public double getWidth() { return width; } public double getHeight() { return height; } //Provide set methods that set the instance variables to new values. public void setHeight(double height) { if (height < 0 || height > 20) { throw new IllegalArgumentException("Error: Invalid height provided."); } this.height = height; } public void setWidth(double width) { if (width < 0 || width > 20) { throw new IllegalArgumentException("Error: Invalid width provided."); } this.width = width; } //Provide a method called calculatePerimeter that calculates the perimeter public double calculatePerimeter() { return 2 * width + 2 * height; } //Provide a method called calculateArea that calculates the area of the rectangle a public double calculateArea() { return width * height; } }
==============================================================
public class RectangleTest { public static void main(String[] args) { System.out.println("Test 1: Invalid height"); try { Rectangle r1 = new Rectangle(10, -10); } catch (IllegalArgumentException e) { System.out.println(e.getMessage()); } System.out.println("\nTest 2: Invalid width"); try { Rectangle r1 = new Rectangle(22, 10); } catch (IllegalArgumentException e) { System.out.println(e.getMessage()); } System.out.println("\nTest 3: Set invalid height"); try { Rectangle r1 = new Rectangle(12, 10); r1.setHeight(-8); } catch (IllegalArgumentException e) { System.out.println(e.getMessage()); } System.out.println("\nTest 4: Set invalid width"); try { Rectangle r1 = new Rectangle(12, 10); r1.setWidth(-8); } catch (IllegalArgumentException e) { System.out.println(e.getMessage()); } System.out.println("\nTest 5: calculate area"); Rectangle r = new Rectangle(12, 15); System.out.println("Expected area = 180.0" + ", Actual area = " + r.calculateArea()); System.out.println("\nTest 6: calculate perimeter"); System.out.println("Expected perimeter = 54.0" + ", Actual perimeter = " + r.calculatePerimeter()); } }
==============================================================