Fundamental Programming Concepts
Summer 2000

Lab 8

Overview

This lab requires the use of:

  • Classes (Part I)
  • Arrays (Part II)

As the size of your programs grows, you will find that you need to use the same (or very similar) code in more than one place in a program. When this occurs, you should create a separate method that is called each time you need the code. You should not cut and paste the code into your program in several different places -- this leads to poor maintainability. We will penalize duplicated code when we see it.

 

Part I: TimeTester

Create a class called Time according to the specification given below -- read carefully! Also write a driver program, called TimeTester, to test your class. 

This will be a more complicated Metrowerks project than you've created so far. You'll have to add two source files to it (Time.java and TimeTester.java). Your main method will go in TimeTester -- Time will not have a main method. You only need to import CS99 into the files in which you use Console methods, which will either be just TimeTester (if you use console input in your testing), or no files. Each of your classes should have a header comment.

The Driver Program

A driver program is a program that is used to test the functionality of classes and methods. For your driver program, you should test all of the methods in the Time class to make sure they work correctly in all cases. When we test your classes, we will substitute our own driver program for yours. This means two things for you:

  1. You must stick exactly to the following specification for the Time class. Otherwise, we won't be able to compile our program with your class. This will result in very large penalties. To help you with this, we are providing a template for your Time class.
  2. Our driver will be very thorough, so you should try and make yours test as many cases as possible, too.

The Time Class

This class represents military time to the precision of seconds. Military time does not use AM or PM; instead, it uses hours from 0-23. So in military time midnight is 00:00:00, whereas in standard time midnight is 12:00:00 AM. Military time always uses two digits for hours, minutes, and seconds. Standard time uses the minimum number of digits for hours (either one or two), but always uses two digits for minutes and seconds (e.g., 5:00:00 PM).

The Time class should have the following public methods (none should be static):
Time()
Default constructor, should initialize object to midnight
Time(int hr, int min, int sec)
Constructor to initialize object to desired time. 
void setTime(int hr, int min, int sec)
Set the time to the given parameters.
void advanceSec()
Advance the time by one second
String toString()
Return the time formatted as military time.
String toStringStandard()
Return the time formatted as standard time.

It should have at least the following private fields (none should be static):
int hour
Hour component of time, ranges from 0 to 23
int minute
Minute component of time, ranges from 0 to 59
int second
Second component of time, ranges from 0 to 59

You may also use any other private methods and fields you wish in implementing the above specification.

Hints:

  • It is possible to pass invalid arguments to some of the methods above (for example, someone could call setTime(24, 0, 0), which is not a legal time). If this happens, your object still needs to remain in a valid state -- remember that objects should be self-governing. To accomplish this, set the illegal component(s) to whatever the default constructor would initialize them (continuing the previous example, you would set the hour to 0 instead of 24). 
  • Note that Java drops the leading zero from an integer when it prints it out. To avoid this, use a DecimalFormat object. The format string you want is "00".
  • Advancing one second should also cause the minute and hour to advance, as necessary. For example, advancing "23:59:59" by one second yields "00:00:00".
  • You can test toString using concatenation, e.g.: 
    System.out.println("" + t);    // tests toString()
    System.out.println(t.toStringStandard());

 

Part II: Airline
Create an airline reservation program. The program is for a small company with only one airplane. The plane has just 8 seats numbered 1-8. Passengers may only reserve seats on the current flight (i.e., they cannot make reservations in advance). The airline wants the program to allow them to make reservations, cancel reservations, and print a seating chart for the current flight. When making a reservation, the only information the airline needs is the passenger's name. Passengers are automatically assigned the next available seat and may not request a particular seat. The assigned seat should be output as part of the reservation process. Reservations are canceled by seat number, not by passenger name.

Your program should be menu-based. For example, the main menu for the program might look like:

Main Menu

1. Make a reservation
2. Cancel a reservation
3. Print seating chart
4. Quit

===> 

Use an array of strings to represent the airplane. Each element is a seat, and is either equal to the passenger's name (if the seat is reserved), or the empty string. Be sure to read section 6.2 for information about arrays of objects. In particular, you must not only declare and create the arrays of seats, but you must also create each of the strings in the array. See the hint below.

You may use static fields only if they are also final (in other words, class-wide constants). For example, you could create a constant that represents the number of seats on the plane. In particular, your array of seats may not be static.

Hints:

  • Be sure to validate all input. See the lab 6 grading comments to make sure you understand what validate means!
  • Make effective use of methods to separate your code into small modules that perform one well-defined task. For example, each choice off of the main menu should probably be handled by its own method. Your array will probably be passed as an argument to many of these methods.
  • If the plane is full, any attempted reservation should cause an error message to be printed.
  • If a cancellation for a seat that is empty is attempted, an error message should be printed.
  • The seating chart should be a list of seat numbers and passenger names. Empty seats should be included in the list, even though they won't have a name by them.
  • Before displaying the menu for the first time, call a method that initializes your seat array. It should assign every element of it a newly-created empty string.
  • To find empty seats, loop through the array looking for empty strings. (This is a good candidate for a method.)
Submitting

Submit exactly the following:

  • Lab8
    • Time.java
    • TimeTester.java
    • Airline.java