The Rectangle class
Design a class named Rectangle to represent a rectangle. The class contains:
- Two double data fields named width and height that specify the width and height of the rectangle. The default values are 1 for both width and height.
- A no-arg constructor that creates a default rectangle.
- A constructor that creates a rectangle with the specified width and height.
- A method named getArea() that returns the area of this rectangle.
- A method named getPerimeter() that returns the perimeter.
Write a test program that allows the user to enter the data for the rectangles width and height. The program should include try-catch blocks and exception handling. Write the program so that after the user enters the input it is validated in the Class and if valid the appropriate results are shown. If not valid, the Class should throw an exception to the catch block in the Test class which notifies the user with a message about the error and then program should return to the input portions.
The Rectangle class I have created is below:
public class Rectangle {
//two double data fields width and height, default values are 1 for both.
private double width = 1;
private double height = 1;
private String errorMessage = "";
//no-arg constructor creates default rectangle
public Rectangle() {
}
//fpzc, called by another program with a statement like Rectangle rec = new Rectangle(#, #);
public Rectangle (double _width, double _height) throws Exception {
setWidth(_width);
setHeight(_height);
}
//get functions
public double getArea(){
return (width * height);
}
public double getPerimeter() {
return (2*(width + height));
}
public String getErrorMessage() {
return errorMessage;
}
//set functions
public void setWidth(double _width) throws Exception {
if( !isValidWidth(_width)){
Exception e = new Exception(errorMessage);
throw e;
//System.out.println(errorMessage);
//return false;
}
width = _width;
}
public void setHeight(double _height) throws Exception {
if ( !isValidHeight(_height)){
Exception e = new Exception(errorMessage);
throw e;
//System.out.println(errorMessage);
//return false;
}
height = _height;
}
//isValid methods
public boolean isValidWidth(double _width) {
//default check
//if(_width == 1) {
// return true;
//}
if(_width > 0){
return true;
}
else {
errorMessage = "Invalid value for width, must be greater than zero";
return false;
}
}
public boolean isValidHeight(double _height) {
//default check
//if(_height == 1){
// return true;
//}
if(_height > 0){
return true;
}
else {
errorMessage = "Invalid value for height, must be greater than zero";
return false;
}
}
}
And the test program i have so far below:
import java.util.Scanner;
import java.util.InputMismatchException;
public class TestRectangle {
//default constructor
public TestRectangle() {
}
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
Rectangle rec = new Rectangle();
boolean Continue = true;
double _width = 1;
double _height = 1;
do {
try {
System.out.println("Please enter a numerical value for the rectangle's width:");
_width = input.nextDouble();
rec.setWidth(_width);
Continue = false;
}
catch (Exception e){
rec.getErrorMessage();
Continue = true;
}
} while (Continue);
do{
try {
System.out.println("Please enter a numerical value for the rectangle's height:");
_height = input.nextDouble();
rec.setHeight(_height);
Continue = false;
}
catch (Exception e){
rec.getErrorMessage();
Continue = true;
}
} while (Continue);
System.out.println("The rectangle has a width of " + _width + " and a height of " + _height);
System.out.println("the area is " + rec.getArea());
System.out.println("The perimeter is " + rec.getPerimeter());
}
}
Te main issue I am having is that when the exception is caught it does not print out the respective errorMessage. Not sure what I am doing wrong on that one. I cannot just add a print statement into the catch method because the professor wants the error message to be sent from the isValid method in the rectangle class.
The second small issue I am having is how to add another step into the isValid method for both the width and height that makes sure the input from the user is not a letter or other character. And in turn how to add that additional exception as another catch in my try-catch block.
Any help would be much appreciated!