CPSC-115 Fall 2008
Lab 7
October 14/15, 2008
Professor Heidi Ellis

Be sure that you hand in the printouts of your work before leaving!!!

Objective:

Pre-lab: The prelab for Lab 7 has two parts:
  1. Write the modified UML class diagram for the Flex class that includes the getStudentID, getNumberOfMeals and getMealFlex methods for part 3 below.
  2. Write the code for the getNumberOfMeals and getMealFlex methods in part 3 below.
To provide you an example, the code for the getStudentID method is below:
public String getStudentID()
{
  return studentID;
}

In this lab, you will be working with a Flex class which represents the form of credit used by Trinity students to purchase food including meals and snacks at food service locations throughout the Trinity College campus. You will create a FlexDriver class to create instances of the Flex class and to manipulate the instances.

Pair Programming:

  1. Locate your partner and introduce yourself:
  2. Tuesday Lab   Wednesday Lab
    Kristen AndersonJeff Young  Chelsea Bainbridge-DonnerRyan Ersland
    Jake ElderNick Dragu  Catherine DoyleJohn Wilsterman
        Jesse VazquezGreg Vaughan
        Corazon IrizarryJin Feng Liu
  3. Select one person to start as "driver". This person will type at the keyboard for the first 20 mintues.
  4. Proceed through the process of completing the lab as described below. Be very careful to ensure that every item for both programs is completed.
  5. Swap pairs every 20 minutes.
  6. When you are done, be sure to email a copy of the code to the person whose account you are not working in. In other words, make sure that both partners have a copy of the code.
In this lab, we will be working with a Flex.java class. This class represents the form of credit used by Trinity students to purchase food including meals and snacks at food service locations throughout the Trinity College campus. The Flex class supports two distinct forms of credit. One is a set number of meals that may be purchased at Trinity dining halls. The second is called "meal flex" and it represents a dollar amount that may be used at any Trinity location to purchase meals or snacks. Meal flex provides flexibility in the meal plan, allowing students to purchase meals and snacks at the location of their choice.

The UML class diagram for the Flex class is shown below:

Flex
- studentID: String
- numberOfMeals: int
- mealFlex: double
+ setStudentID(n: String):void
+ setNumberOfMeals(meals: int):void
+ setMealFlex(c: double):void
+ buyMeal(): void
+ buySnack(double amt): void
+ addFlex(double amt): void
+ toString(): String


Step 1: Create a FlexDriver Class

For step 1, you must download the Flex.java class. Study the class and the javadoc documentation for the class. Make sure that you understand the methods available for use and how to call them. Note that the Flex class has no main method. This is because you will be using the FlexDriver class to create and manipulate instances of the Flex class within a main method.

Once you are familiar with the Flex class, create a FlexDriver class. Your FlexDriver class should contain only a main method which should do the following:

  1. Create two instances of the Flex class. Remember that you will need to use the new operator.
  2. For each instance, set the student ID.
  3. For each instance, set the number of meals.
  4. For each instance, set the amount of meal flex.
  5. Print each instance.
  6. Call the buyMeal, buySnack and addFlex methods to modify the state of one or more of the Flex instances.
  7. Print both instances again.
The output should look something like:
The first Flex:
Student ID: 1234
Number of meals remaining: 121
Amount of meal flex remaining: 100.0

The second Flex:
Student ID: 5678
Number of meals remaining: 200
Amount of meal flex remaining: 200.0

AFTER MODIFICATIONS:
The first Flex:
Student ID: 1234
Number of meals remaining: 121
Amount of meal flex remaining: 79.5

The second Flex:
Student ID: 5678
Number of meals remaining: 199
Amount of meal flex remaining: 230.0
Show the instructor your FlexDriver class before continuing to part 2.

Step 2: Modify the buyMeal, buySnack and addFlex methods to check for incorrect data

In order to ensure that the state of the Flex instances is correct, you must modify the buyMeal, buySnack and addFlex methods to check to make sure that if the methods are executed, that the objects are not left in an incorrect state (e.g., a negative number of meals available). Specifically, you must:
  1. Update the buyMeal method to ensure that the number of meals is not decremented if the number of meals is zero. If the number of meals is zero, you should print an error message indicating that the student cannot purchase a meal.
  2. Update the buySnack method to check that the amount to be deducted is greater than zero and that the amount to be deducted is less than the current flex balance. If the amount is less than zero or greater than the current flex balance, an error message should be printed.
  3. Update the addFlex method to ensure that the amount added to the flex balance is positive. If the amount is not positive, an error message should be printed.
  4. Modify the FlexDriver class to call the buyMeal, buySnack, and addFlex methods with data that will trigger the error messages.
  5. Modify the addFlex method so that if more than $25.00 is added to the account, an additional 20% over the original amount to be added is added to the account.
The output should look something like:
The first Flex:
Student ID: 1234
Number of meals remaining: 0
Amount of meal flex remaining: 10.0

The second Flex:
Student ID: 5678
Number of meals remaining: 200
Amount of meal flex remaining: 200.0

There is an insufficient number of meals.
Incorrect amount or insufficient funds.
Cannot add negative funds.

AFTER MODIFICATIONS:
The first Flex:
Student ID: 1234
Number of meals remaining: 0
Amount of meal flex remaining: 10.0

The second Flex:
Student ID: 5678
Number of meals remaining: 200
Amount of meal flex remaining: 200.0
Show the instructor your modified Flex and FlexDriver classes before continuing to part 3.

Step 3: Implement the getStudentID, getNumberOfMeals and getMealFlex Methods

When examining the Flex class, you should have noticed that the Flex class has an equals method that has been commented out. This method takes another instance of the Flex class and compares the values in all of the attributes of the other instance to the local instance. If the values are the same, then the equals method returns true. If any one of the attribute values do not agree, the method returns false.

Note that the equals method calls several methods that have not yet been implemented on the Flex class: getStudentID, getNumberOfMeals and getMealFlex. Add these methods to your Flex class and uncomment the equals method.

Now test the equals method in the FlexDriver class by creating three Flex instances. Two of the instances should contain the same data while the third should contain different data. Print out the result of calling the equals method on each combination of Flex instances. The output should look something like:

The first Flex:
Student ID: 1234
Number of meals remaining: 121
Amount of meal flex remaining: 100.0

The second Flex:
Student ID: 5678
Number of meals remaining: 200
Amount of meal flex remaining: 200.0

The third Flex:
Student ID: 1234
Number of meals remaining: 121
Amount of meal flex remaining: 100.0

The first and second flex are equal is: false
The second and third flex are equal is: false
The first and third flex are equal is: true
Test your code carefully to make sure that it operates correctly under a variety of test cases. Your code should have good programming style and follow all Java conventions. Don't forget to use constants where appropriate.

You're done!! Don't forget to log out and hand in your printouts to the professor before you leave!!