CS 1054 Introduction to Programming in Java (Spring 2008)
Lab 4:
Conditional Statements
Lab Objectives
- Use conditional statements to control program flow (Part I).
- Use conditional statements to implement bounds checking (Part II).
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
- 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.
- 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:
- public void letCarPass(double amount): allows one car to enter the toll lane;
amount represents the total amount deposited in the basket; which could
be equal, less than, or greater than the toll of 0.60.
- public double getCash(): returns the total amount of cash generated by the toll lane.
- public int getCars(): returns the total number of cars that have entered through the toll lane
(including violators).
- public int getViolators(): returns the number of cars
designated as violators by failing to pay the full toll
.
For a complete description of these methods, please visit this
Java documentation page .
- Don't forget to comply with the formatting guidelines and include your Javadoc-formatted method comments.
- Compile both classes and then execute TollLaneTester.
- 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
- 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.
- 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:
- the purchasing of eggs using the buyOneDozen method causes the
value returned by the getCash method to be negative
- the selling of eggs using the sellEggs method causes the
value returned by the getEggCount method to be negative
Here is the documentation page
for the EggTrader class.
- 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").
- 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.
- Don't forget to comply with the formatting guidelines and include
your Javadoc-formatted method comments.
Submission
- 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.
- Upload the generated .zip file to Web-CAT for grading,
which will automatically extract the files for grading.