Lab 27
Hip to be Square
Objective:
Write two classes: One class draws a square, and an
exception that occurs when the user puts in an invalid dimension.
- First download the driver and put it in your
project
- Write a class file called DimensionException that
inherits from Exception
- Create the following constructors
- Default – calls the parent’s constructor and pass a
message that it is an invalid dimension
- A constructor that takes one string parameters that is
passed to the parent’s constructor
- Write a class file called Square that DOES NOT
HAVE a main method
- An attribute of this class is
- Length – corresponding to the length of each side
- Create the following constructors
- Default – creates a 1x1 square (or a single star)
- One that has a integer parameter that corresponds to its
length and width
- Accessors and Mutators for each variable
- MAKE SURE THE MUTATORS CHECK FOR VALID VALUES!
- If the dimension is set improperly throw the DimensionException
- Create the following Methods
- draw – this method will draw a square of asterisks (*) by
the given dimensions.
- getArea – returns the area of the square
- getPerimeter – returns the perimeter of the square
Example Dialog:
Welcome to the easy square program
Enter the length of the side of a square or enter QUIT to quit
5
*****
*****
*****
*****
*****
The area is 25
The perimeter is 20
Enter the length of the side of a square or enter QUIT to quit
0
Dimensions must be of length one at least
Enter the length of the side of a square or enter QUIT to quit
Lab Report Questions:
- Describe what an exception does.
- Describe the order in which a series of “catches” need to
be.
Finally:
Upload DimensionException and Sqaure file to the dropbox