CS 1054 Introduction to Programming in Java (Spring 2008)
Lab 6: Review: Conditional Statements in Methods
Lab Objective
- Explore the different ways to vary
method behavior using conditional
statements
Instructions
For this lab,
no webcat submission is required,
but attendance will be checked.
The TA will demonstrate and discuss the solutions
to the tasks described during the lab period,
but you will be given some time
during the lab to attempt some of these tasks yourself.
Consider this lab as a review session for the programming
portion of the midterm exam.
Begin by downloading
Lab6.zip,
save the file, and then extract it in a folder you can locate.
Open this project in BlueJ.
You will find two familiar classes:
CellPhone and Car.
The methods in this class do not use if statements,
so there are no guards present, for instance,
to check against
calling on a CellPhone object with insufficient credits
or invoking the drive method on a Car object
when it doesn't have enough fuel.
- In Lab 5, you arranged it so that the call method of CellPhone prints an error message
when there are insufficient credits to make the call.
This time, arrange it so that if this condition occurs and there is at least 1 minute/credit remaining,
proceed with the call but limit it to the minutes that the credits will allow.
Test your revised CellPhone class by carrying out the following on a newly created CellPhone object:
- Load 10 credits/minutes
- Make a 5 minute call
- Make a 3 minute call
- Check credits (should be 2), and number of calls (should be 2)
- Make a 5 minute call (should work up to 2 minutes)
- Check credits (should be 0), and number of calls (should be 3)
- Make a 3 minute call (should print an error message because there are 0 credits left)
- Check credits (should be 0), and number of calls (should be 3, still)
- Load 10 credits/minutes
- Make a 5 minute call
- Check credits (should be 5), and number of calls (should be 4)
- For the Car object, you have (once again) a couple of options for
refining the drive method in case the car does not have enough fuel.
Let's try the "error message" option first:
print an error message (the car does not move),
if there isn't enough fuel to travel the distance provided.
Test your revised Car class by carrying out the following on a newly created Car object
(has 15 gallons of gas initially):
- Drive 50 miles (consumes two gallons)
- Check gas level (should be 13 gallons)
, and distance travelled (50 miles)
- Drive 200 miles
- Check gas level (should be 5 gallons)
, and distance travelled (250 miles)
- Drive 200 miles
(should print an error message)
- Check gas level (should be 5 gallons)
, and distance travelled (250 miles)
- Now, let's try the other option,
where we let the car drive as far as it can,
in the event that the miles specified cannot all be traversed.
The test should now go as follows:
- Drive 50 miles (consumes two gallons)
- Check gas level (should be 13 gallons)
, and distance travelled (50 miles)
- Drive 200 miles
- Check gas level (should be 5 gallons)
, and distance travelled (250 miles)
- Drive 200 miles
- Check gas level (should be 0 gallons)
, and distance travelled (375 miles)
- It also makes sense to check for invalid values for arguments
(e.g., negative values).
In such cases, an error message is usually printed
(e.g., "Positive values only").
Incorporate
guards like these for the
load and call methods of CellPhone
and for the loadGas and drive methods of Car.
(In fact, for loadGas, you could also guard against
exceeding gas tank capacity).