header

Lab 5

In this lab, you will modify two classes that you wrote in earlier labs. In both cases, the changes will help insure that objects created from the corresponding classes will always be in a valid state.

Modify the Circle Class

If you think about it, the radius of a circle should be a positive value. In the Circle class you wrote for lab 3, there was nothing to prevent us from constructing a new circle with a radius less than or equal to zero. Nor was there anything to prevent us from setting the radius of an existing circle to a value less than or equal to zero.

Rewrite the initialization constructor so that it tests the value of its parameter and if it is less than or equal to zero then the radius is set equal to 1.0 (the default value). If the value of the parameter is positive then the constructor will construct a circle with the specified radius as it did before. Otherwise, it will construct a circle with the default radius.

Rewrite your setRadius method so that it tests the value of its parameter and if it is less than or equal to zero it does nothing. That is, it changes the value of the radius only if the new value is positive. In effect, the modified setter method will change the radius of an existing circle only if the new value is valid.

Modify the Car Class

Modify the Car class you wrote for homework 2 so that the number of cylinders is stored as an integer value rather than as a string value.

Modify the initialization constructor so that it tests the number of cylinders. Acceptable values are 4, 6, and 8. If the parameter is any other value then ignore it and set the number of cylinders equal to the default number (as specified in your default constructor).

Modify your setCylinders method to also test the number of cylinders specified by its parameter. Again, the only acceptable values are 4, 6, and 8. If the number of cylinders is any other value, then the setCylinders method should do nothing. The modified setter method changes the number of cylinders in an existing Car object only if the new value is valid.

For you purists, I know there are cars in which the number of cylinders is something other than 4, 6, or 8 but those cars are not very common so we won't worry about them.

Note

Anytime I give you an assignment to modify code you wrote in an earlier assignment, it is expected that you will have corrected any mistakes noted in that earlier assignment.

Submitting Your Work

Submit your Circle.java and Car.java files as attachments to an email message whose subject is "Lab05".