Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Rewrite the Rectangle class you wrote for Chapter 8 to throw an IllegalArgumentException for the set methods when the parameter value is out of range.

Rewrite the Rectangle class you wrote for Chapter 8 to throw an IllegalArgumentException for the set methods when the parameter value is out of range. Use the IllegalArgumentException constructor that takes a String as the parameter. Use your favorite Internet search tool to search for the IllegalArgujmentException API to read the details about the class and how to use it. Write a RectangleTester class that asks the user for length and width values before performing the calculations.

Original Chapter 8 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.

Rectangle Class:

public class Rectangle { private double width; private double height;

public Rectangle(double width, double height) { this.checkWidthValue(width); this.checkHeightValue(height); this.width = width; this.height = height; }

public double getWidth() { return this.width; }

public void setWidth(double width) { this.checkWidthValue(width); this.width = width; }

public double getHeight() { return this.height; }

public void setHeight(double height) { this.checkHeightValue(height); this.height = height; }

public double calculatePerimeter() { return this.width * 2 + this.height * 2; }

public double calculateArea() { return this.width * this.height; }

private void checkHeightValue(double height) { if (height < 0.0 || height > 20.0) { throw new IllegalArgumentException("height is out of bounds"); } }

private void checkWidthValue(double width) { if (width < 0.0 || width > 20.0) { throw new IllegalArgumentException("width is out of bounds"); } } }

Rectangle Test:

public class RectangleTest { public static void main(String[] args) { 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(e.getMessage()); }

try { rectangle.setHeight(20.5); } catch (IllegalArgumentException e) { System.out.println(e.getMessage()); }

try { rectangle.setWidth(-1.0); } catch (IllegalArgumentException e) { System.out.println(e.getMessage()); }

try { rectangle.setWidth(20.5); } catch (IllegalArgumentException e) { System.out.println(e.getMessage()); }

try { rectangle = new Rectangle(-1.0, 15); } catch (IllegalArgumentException e) { System.out.println(e.getMessage()); }

try { rectangle = new Rectangle(15, -1.0); } catch (IllegalArgumentException e) { System.out.println(e.getMessage()); } } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Concepts of Database Management

Authors: Philip J. Pratt, Mary Z. Last

8th edition

1285427106, 978-1285427102

More Books

Students also viewed these Databases questions

Question

Provide examples of KPIs in Human Capital Management.

Answered: 1 week ago

Question

What are OLAP Cubes?

Answered: 1 week ago