CS 1054 Introduction to Programming in Java (Spring 2008)

Lab 4: Conditional Statements

Lab Objectives

Instructions

Similar to the previous lab, Lab 4 is composed of two exercises (Part I and Part II) where you will write two different classes. One of these classes (TollLane) is brand-new, while the other (EggTrader) has appeared most recently in Lab 3. Please note that unlike Lab 3, Lab 4 will not be split into parts; both classes are due together by the end of your lab section for the week of February 17th. For part I, you will be writing the TollLane class from scratch. For part II, you will be extending your original EggTrader class with additional functionality.

Begin by downloading Lab4.zip , save the file, and then extract it in a folder you can locate. Open this project in BlueJ.

Part I

  1. Check out the TollLane and TollLaneTester classes. The TollLane class itself is empty. This means that the TollLaneTester class (which has been provided for your convenience) cannot be compiled until the methods of the TollLane class have been sufficiently defined.
  2. Define the TollLane class. Some toll lanes in this country have unmanned toll baskets where drivers deposit coins as they pass through a lane. Violators are automatically detected and tagged (in fact, pictures of their license plates are automatically taken if they don't deposit the correct amount). You will come up with an approximate representation of such a toll lane. Assume that the toll is 0.60. You will need to implement the following methods for a TollLane object: For a complete description of these methods, please visit this Java documentation page .
  3. Don't forget to comply with the formatting guidelines and include your Javadoc-formatted method comments.
  4. Compile both classes and then execute TollLaneTester.
  5. When you see that your own output matches the expected output as indicated, you are done with this portion of the lab. Proceed to the next section.
Part II
  1. Observe the EggTrader and EggTraderTester classes (they should be very familiar to you by now!) and note that both classes are empty. For this section you will be building on the original EggTrader class that you wrote for Lab 3B, so feel free to copy and paste your old code into the empty class to get started.
  2. You may have noticed during testing of the original EggTrader class that there is nothing preventing you from purchasing eggs when you don't have enough money, or selling more eggs than you actually own, causing both values to become negative over time. For a wide variety of reasons, this is generally not an acceptable industry practice. Your objective is to implement additional program logic (using conditional statements) that will prevent the purchase or sale of eggs in the following situations: Here is the documentation page for the EggTrader class.
  3. In the event where a sale or purchase of eggs is blocked, simply print a line to the screen indicating as such (e.g. "Not enough cash" or "Not enough eggs").
  4. For testing purposes, you may choose to write your own test cases using the EggTraderTester class (no explicit testing code is provided), or utilize BlueJ's built-in object instances and manipulate the EggTrader methods from there. Regardless of how you choose to test, you should be checking to see whether or not your code guards against invalid purchase and sales of eggs.
  5. Don't forget to comply with the formatting guidelines and include your Javadoc-formatted method comments.
Submission
  1. Since this lab requires submitting two class files at the same time (EggTrader.java, TollLane.java), you need will need to archive these two files. Consult your TA for instructions on how this is done.
  2. Upload the generated .zip file to Web-CAT for grading, which will automatically extract the files for grading.