02 Teach : Team Activity
Practice with Interfaces
Objectives
Practice implementing a Java Interface.
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
-
First, define an interface called
Expensethat 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.
-
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".
-
Next, create a class called
Cruisewhose constructor takes aDestination.Add all of the code necessary to make
Cruiseimplement theExpenseinterface.The
Cruiseclass should calculate its costs this way:Destination Cost Mexico $1000.00 Europe $2000.00 Japan $3000.00
Part II: Create Your Driver Class
-
Create a Java class called
VacationCalculator. This class will contain yourmain()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) { } } -
Write the code for the
tallyExpensesfunction. It should loop through the list of items in theexpensesparameter and return the sum of their costs. -
Write the code for the
calculateVacationCostfunction. It should create anArrayListof Expenses, add aCruiseinstance for that list for the proper destination, then use thetallyExpensesmethod to determine the cost of that vacation. -
Finally, in
main, create an instance of theVacationCalculatorclass, and call thecalculateVacationCostfunction 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.
-
Create a class called
Diningthat also implements theExpenseinterface.The constructor for the
Diningclass should include two parameters, one for the destination, and one for the number of nights you'll be staying there. -
The
Diningclass should calculate its cost this way:Destination Cost Mexico $10.00 per day Europe $20.00 per day Japan $30.00 per day -
Add another class called
Lodgingthat also implements theExpenseinterface. Its constructor should also take the destination and number of nights as parameters. -
The
Lodgingclass should calculate its cost this way:Destination Cost Mexico $100.00 per day Europe $200.00 per day + 10% Hotel Tax Surchage Japan $300.00 per day + 30% Hotel Tax Surchage -
In your driver class, modify the
calculateVacationCostso 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. -
Update your
mainfunction 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.