Software Design and Development :: CS 246

02 Teach : Team Activity

Practice with Interfaces

Objectives

Assignment

Before continuing, you should have completed this week's preparation assignment.

You're going to practice implementing interfaces in Java by creating a vacation cost calculator. There are lots of different components that contribute to the cost of a vacation, transportation, food, lodging etc...

In order to keep our class hierarchy flexible, we're going to create a common interface that will allow us to get the cost of these components.

Part I: Create the Expense Interface and Cruise Class

  1. First, define an interface called Expense that requires a single method:

    float getCost();
    

    Typically, interfaces in Java go in their own file, so this one would go in a file called Expense.java.

  2. Next, create an enum type called Destination, which contains three possible options for its values:

    enum Destination {
        Mexico, Europe, Japan
    }
    

    Like the interface, enums are defined in their own, separate files. This one should go in a file called Destination.java.

    It might seem overcomplicated to put the enum and interface in their own files, since they both contain such a small amount of code, but putting everything in its own file is the "Java Way".

  3. Next, create a class called Cruise whose constructor takes a Destination.

    Add all of the code necessary to make Cruise implement the Expense interface.

    The Cruise class should calculate its costs this way:

    DestinationCost
    Mexico$1000.00
    Europe$2000.00
    Japan$3000.00

Part II: Create Your Driver Class

  1. Create a Java class called VacationCalculator. This class will contain your main() method, which will create an instance of the class and invoke methods on that instance:

    class VacationCalculator {
    
        public static void main(String[] args) {
    
            VacationCalculator vc = new VacationCalculator();
            
            // Calculate some vacation costs...
            float japanCost = vc.calculateVacationCost(Destination.Japan);
    
            // Print the cost...
        }
    
        /**
         * Calculates the total cost for vacationing at a given location for 
         * a specific number of nights.
         *
         * @param  dest the destination of the vacation
         * @return      the total cost of the vacation
        */
        public float calculateVacationCost(Destination dest)
        {
    
        }
    
        /**
         * An internal method used by VacationCalculator to loop through
         * a List of items that implement the Expense interface and 
         * determine their cost 
         *
         * @param  expenses A list of items that implement the Expense interface
         * @return          the total cost calculated by the items
        */
        float tallyExpenses(List<Expense> expenses)
        {
    
        }
    }
    
  2. Write the code for the tallyExpenses function. It should loop through the list of items in the expenses parameter and return the sum of their costs.

  3. Write the code for the calculateVacationCost function. It should create an ArrayList of Expenses, add a Cruise instance for that list for the proper destination, then use the tallyExpenses method to determine the cost of that vacation.

  4. Finally, in main, create an instance of the VacationCalculator class, and call the calculateVacationCost function in order to determine the cost of going on a vacation to Japan. Print the result to console using a format specifier.

Once you've made it this far, move on the stretch challenge.

🌟Stretch Challenge🌟

Going on a cruise is great, but we might also want to stay at our destination for a certain length of time. So, we'll need to calcuate those expenses as well and include them in our vacation cost.

  1. Create a class called Dining that also implements the Expense interface.

    The constructor for the Dining class should include two parameters, one for the destination, and one for the number of nights you'll be staying there.

  2. The Dining class should calculate its cost this way:

    DestinationCost
    Mexico$10.00 per day
    Europe$20.00 per day
    Japan$30.00 per day
  3. Add another class called Lodging that also implements the Expense interface. Its constructor should also take the destination and number of nights as parameters.

  4. The Lodging class should calculate its cost this way:

    DestinationCost
    Mexico$100.00 per day
    Europe$200.00 per day + 10% Hotel Tax Surchage
    Japan$300.00 per day + 30% Hotel Tax Surchage
  5. In your driver class, modify the calculateVacationCost so that you can specify how many days you'll be spending at your destination and have the function include a lodging and dining as part of the itenerary.

  6. Update your main function to calculate the cost of traveling to each destination and staying there for 5 days.

Congratulations! You now know how to use Java interfaces.

Make sure everyone on your team understands and receives a copy of this code.

Compare your answer to the teacher's solution, then complete the accompanying quiz for this assignment on I-Learn.