CS 1054 Introduction to Programming in Java (Spring 2008)

Lab 6: Review: Conditional Statements in Methods

Lab Objective

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.
  1. 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:
  2. 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):
  3. 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:
  4. 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).