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.

CHAPTER 8

/* * File: Rectangle.java * Name: Mike Boehmer */

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

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

The Accidental Data Scientist

Authors: Amy Affelt

1st Edition

1573877077, 9781573877077

Students also viewed these Databases questions