Question
Java -- To write an Object-Oriented application that creates a Java class with several instance variables, a constructor to initialize the instance variables, several methods
Java --
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. In addition, Java Exception Handling is used to handle exceptions within the application. Also, write a test class that instantiates the first class and tests the classs constructor and methods.
Details:
I would need to: Rewrite the Rectangle class written for previous programming assignment to 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 previous assignment to handle the exceptions thrown by the Rectangle class.
Thoroughly tests the Rectangle classs 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 classs methods well. The thoroughness of your testing in will impact your grade.
Previous code:
Rectangle.java
public class Rectangle {
private double width; private double height;
public Rectangle(double width, double height) {
setWidth(width); setHeight(height);
}
public double getWidth() { return width; }
public double getHeight() { return height;
} public void setWidth (double width){ if(width < 0 || width > 20){ throw new IllegalArgumentException("Width is not in range"); } this.width = width; }
public void setHeight(double height) { if (height < 0 || height > 20) { throw new IllegalArgumentException("Height is not in range"); } this.height = height; }
public double calculatePerimeter() { return 2 * (width + height); }
public double calculateArea() { return width * height;
}
}
========================================
RectangleTest.java
public class RectangleTest {
public static void main(String[] args) { Rectangle rectangle = new Rectangle(6, 6); System.out.println("Height:" + rectangle.getHeight()); System.out.println("Width:" + rectangle.getWidth()); System.out.println("Area:" + rectangle.calculateArea()); System.out.println("Perimeter:" + rectangle.calculatePerimeter()); Rectangle rectangle1 = new Rectangle (22,15); System.out.println("Height:" + rectangle1.getHeight()); System.out.println("Width:" + rectangle1.getWidth()); System.out.println("Area: "+ rectangle1.calculateArea()); System.out.println("Perimeter: " + rectangle1.calculatePerimeter()); } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started