Question
The NetBeans software and requires no QUI. This lab uses a series of classes that compare the cost of travel by plane, train and auto
The NetBeans software and requires no QUI.
This lab uses a series of classes that compare the cost of travel by plane, train and auto from Rochester to some other destination. An interface is required and input is received through command line arguments.
Exercise 1 The cost of car travel
Step 1:
Creating the interface - Create an interface, TravelCost, that has the following characteristics: A constant double AGENT_FEE value in US dollars ($s) of 10.00 An abstract method getDuration that returns a double trip length in hrs An abstract method getTotalCost that returns a double trip cost in $s
An abstract method getLodgingCost that returns a double lodging cost in $s An abstract method getDestination that returns a String destination An abstract method toString that returns a string formatted by String.format()
Step 2:
Create a class, CarTravelCost, with the characteristics:
Attributes are trip distance int numMiles, nightly lodging cost double hotelCost and trip destination String destination A normal accessor for destination A constructor that accepts and updates all the instance variables getDestination returns: numHours=(double)numMiles/65, (uses car speed of 65 mph) The getLodgingCost() returns: numDays*hotelCost. numDays, which is the number of days on the road, is calculated getDuration()/8.0 (8 hours of driving per day). There is no need to include fractional days as a whole day (why?); the method Math.floor() method will help. getTotalCost() returns: (numMiles * 0.45) + getLodgingCost() + AGENT_FEE A toString method returns: destination, getDuration() and getTotalCost() formatted by String.format()
Step 3:
Create a test class , TestCarTravelCost, that takes in three command line arguments: number of miles, hotel cost per night and destination. Issue an error message if there are not three arguments: ERROR: Invalid number of command line arguments. All data conversions should be done in the test class (e.g. String to double). If there is a valid number of arguments then:
Instantiate an object of the CarTravelCost class using the three arguments
Print out the toString
Sample Output
Janeway:Lab12_Interfaces Key jim$ java TestCarTravelCost 400 Boston
ERROR: Invalid number of command line arguments
Janeway:Lab12_Interfaces Key jim$ java TestCarTravelCost 400 200 Boston
Car travel to Boston will take 6.15 hours and cost $190.00.
Exercise 2 Add train travel
Step 1:
Create a class, TrainTravelCost, with the following characteristics: Attributes of double duration, double trainFare, and String destination. A constructor that accepts and updates all the attributes Normal accessors for duration and destination The getLodgingCost method returns: 0.0 (passengers can sleep on the train) The getTotalCost method returns: travelFare + AGENT_FEE A toString method returns: Train travel to description will take duration and cost totalCost formatted by String.format()
Step 2:
Modify the test class TestCarTravelCost Add two more command line arguments, train duration and train fare, for a total of 5 arguments. Put out an error message if there are not five arguments (ERROR: Invalid number of command line arguments)
All data conversions should be done in the test class (e.g. String to double).
If there is a valid number of arguments then:
Instantiate an object of the CarTravelCost class using the arguments (as before)
Add an instantiation of the class TrainTravelCost using the necessary arguments
Print out the toString from the CarTravelCost object (as before)
Add a toString method for the TrainTravelCost object and print it out; as before, formatted
by String.format()
Sample Output
Janeway:Lab12_Interfaces Key jim$ java TestCarTravelCost 400 200 Boston 7 70.0
Car travel to Boston will take 6.15 hours and cost $190.00.
Train Travel to Boston will take 7.00 hours and cost $80.00.
Exercise 3 Add air travel
Step 1:
Create a class, AirTravelCost, that has the following characteristics: Attributes of departure/arrival date (String in the form YYYYMMDD), departure/arrival time (String in the form HHMM), destination, travelFare, and hotelCost (cost per night). A constructor that accepts and updates all the attributes A normal accessor for destination Duration can be calculated. Use the GregorianCalendar class. Create two GregorianCalendar objects, one for each of the departure and arrival dates and times. Use the getTimeInMillis method. Subtract the two for duration in milliseconds, Divide the result by 60,000 to get minutes and divide by 60 to get hours, etc. Calculate the lodging (hotel) cost by dividing the duration by 24 hours and use the number of whole days only (no partial days). Multiply the number of days by the hotel cost per night. The total cost can be calculated as travelFare + getLodgingCost() + AGENT_FEE A toString method returns a string formatted by String.format()whos output looks like Air travel to destination will take duration and cost totalCost
Step 2:
Modify the test class TestTravelCost to add five more command line arguments, airFare, departureDate (String formatted as YYYYMMDD), departureTime (String formatted as HHMM - in 24 hour time), arrivalDate, arrivalTime for a total of 10 arguments. All data conversions should be done in the test class (e.g. String to double). Put out an error message if there are not ten arguments: ERROR: Invalid number of command line arguments. If there is a valid number of arguments then:
Instantiate an object of the CarTravelCost class using the arguments (as before)
Instantiate an object of the TrainTravelCost using the necessary arguments (as before)
Add an instantiation of the class AirTravelCost using the necessary arguments
Add each of these objects to an ArrayList. Think about the datatype (i.e. class used to define
the ArrayList objects.
Print out the return of the toString method from the CarTravelCost object (as before)
Print out the return of the toString method from the TrainTravelCost object (as before)
Add a print of the return of the toString method from the AirTravelCost object
Search the ArrayList to see which travel method is cheapest and which is of shortest duration
and print out those results.
Sample Output
Car travel to Boston will take 6.15 hours and cost $190.00.
Train Travel to Boston will take 11.00 hours and cost $70.00.
Air travel to Boston will take 4.17 hours and cost $234.00.
LOWEST COST: Train Travel to Boston will take 11.00 hours and cost $70.00.
Car travel to Los Angeles will take 40.17 hours and cost $2184.95.
Train Travel to Los Angeles will take 60.00 hours and cost $235.00.
Air travel to Los Angeles will take 8.80 hours and cost $410.00.
LOWEST COST: Train Travel to Los Angeles will take 60.00 hours and cost $235.00.
SHORTEST DURATION: Air travel to Los Angeles will take 8.80 hours and cost $410.00.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started