Results 1 to 2 of 2
- 12-12-2012, 12:48 AM #1
Member
- Join Date
- Oct 2012
- Posts
- 46
- Rep Power
- 0
Need help figuring out where my program is falling apart - not getting errors
For this program I am trying to figure out the duration and cost of travel for 3 separate items - car, train and plane. Everything is working correctly except the plan duration and cost. The cost is wrong because the duration is wrong. I am only including the plane and tester program as the car and train are working.Here is what I have:
Arguments: 400 200 Boston 11 60 224 20110103 0743 20110103 1153
Java Code:{ //attributes private String departureDate; private String departureTime; private String arrivalDate; private String arrivalTime; private String description; private double airFare; private double hotelCost; private double totalCost; private double duration; private double lodging; //constructor //@ param public AirTravelCost(String des, double af, double hc, String dd, String dt, String ad, String at) { description = des; airFare = af; hotelCost = hc; departureDate = dd; departureTime = dt; arrivalDate = ad; arrivalTime = at; } public double getDuration() { int year1 = Integer.parseInt(departureDate.substring(0, 4)); int month1 = Integer.parseInt(departureDate.substring(4, 6)); if (month1 == 01) { month1 = 0; } else if (month1 == 02) { month1 = 1; } else if (month1 == 03) { month1 = 2; } else if (month1 == 04) { month1 = 3; } else if (month1 == 05) { month1 = 4; } else if (month1 == 06) { month1 = 5; } else if (month1 == 07) { month1 = 6; } else if (month1 == 8) { month1 = 7; } else if (month1 == 9) { month1 = 8; } else if (month1 == 10) { month1 = 9; } else if (month1 == 11) { month1 = 10; } else { month1 = 11; } int day1 = Integer.parseInt(departureDate.substring(6)); int hour1 = Integer.parseInt(departureTime.substring(0, 3)); int minute1 = Integer.parseInt(departureTime.substring(3)); GregorianCalendar time1 = new GregorianCalendar(year1, month1, day1, hour1, minute1); int year2 = Integer.parseInt(arrivalDate.substring(0, 4)); int month2 = Integer.parseInt(arrivalDate.substring(4, 6)); if (month2 == 01) { month2 = 0; } else if (month2 == 02) { month2 = 1; } else if (month2 == 03) { month1 = 2; } else if (month2 == 04) { month2 = 3; } else if (month2 == 05) { month2 = 4; } else if (month2 == 06) { month2 = 5; } else if (month2 == 07) { month2 = 6; } else if (month2 == 8) { month2 = 7; } else if (month2 == 9) { month2 = 8; } else if (month2 == 10) { month1 = 9; } else if (month2 == 11) { month2 = 10; } else { month2 = 11; } int day2 = Integer.parseInt(arrivalDate.substring(6)); int hour2 = Integer.parseInt(arrivalTime.substring(0, 3)); int minute2 = Integer.parseInt(arrivalTime.substring(3)); GregorianCalendar time2 = new GregorianCalendar(year2, month2, day2, hour2, minute2); int milliTime1 = (int) time1.getTimeInMillis(); System.out.println(milliTime1); int milliTime2 = (int) time2.getTimeInMillis(); System.out.println(milliTime2); System.out.println(milliTime2 - milliTime1); duration = ((milliTime2 - milliTime1)/60000)/60; return duration; } public double getLodgingCost() { lodging = (duration/24) * hotelCost; return lodging; } public double getTotalCost() { totalCost = airFare + lodging + AGENT_FEE; return totalCost; } public String getDescription() { return description; } public String toString() { return "Air travel to "+ description +" will take "+ duration +" hours and cost "+ totalCost; } }Result: Car travel Boston will take 7.2727272727272725 and cost 210.0Java Code://Attribute String totalTrip; int length = args.length; String d = ""; double m = 0; double h = 0; double f = 0; double du = 0; double af = 0; String depd = ""; String dept = ""; String arrd = ""; String arrt = ""; //Accepts user input Scanner test = new Scanner(System.in); //System.out.print("TestTravelCost "); //totalTrip = test.nextLine(); if(length < 10) { System.out.println("Error: Invalid number of command line arguments"); //System.out.print("\nTestTravelCost "); //totalTrip = test.nextLine(); } else { m = Double.parseDouble(args[0]); h = Double.parseDouble(args[1]); d = args[2]; du = Double.parseDouble(args[3]); f = Double.parseDouble(args[4]); af = Double.parseDouble(args[5]); depd = args[6]; dept = args[7]; arrd = args[8]; arrt = args[9]; CarTravelCost cost = new CarTravelCost(d, h, m); cost.getDuration(); cost.getLodgingCost(); cost.getTotalCost(); cost.getDescription(); System.out.println(cost.toString()); TrainTravelCost train = new TrainTravelCost(d, f, du); train.getDuration(); train.getLodgingCost(); train.getTotalCost(); train.getDescription(); System.out.println(train.toString()); AirTravelCost plane = new AirTravelCost(d, af, h, depd, dept, arrd, arrt); plane.getDuration(); plane.getLodgingCost(); plane.getTotalCost(); plane.getDescription(); System.out.println(plane.toString()); } } }
Train travel to Boston will take 11.0 hours and cost 70.0
1512223904
1659823904
147600000
Air travel to Boston will take 41.0 hours and cost 575.6666666666666
Should be: Car travel Boston will take 7.2727272727272725 and cost 210.0
Train travel to Boston will take 11.0 hours and cost 70.0
1512223904
1659823904
147600000
Air travel to Boston will take 4.166666666667 hours and cost 234.0
Thank you!!!
- 12-12-2012, 02:20 AM #2
Senior Member
- Join Date
- Jun 2007
- Location
- Bali, Indonesia
- Posts
- 696
- Rep Power
- 6
Re: Need help figuring out where my program is falling apart - not getting errors
Hello,
This is not a fix to your problem but this lines are not needed:
You can change it into something like this:Java Code:int month1 = Integer.parseInt(departureDate.substring(4, 6)); if (month1 == 01) { month1 = 0; } else if (month1 == 02) { month1 = 1; } else if (month1 == 03) { month1 = 2; } else if (month1 == 04) { month1 = 3; } else if (month1 == 05) { month1 = 4; } else if (month1 == 06) { month1 = 5; } else if (month1 == 07) { month1 = 6; } else if (month1 == 8) { month1 = 7; } else if (month1 == 9) { month1 = 8; } else if (month1 == 10) { month1 = 9; } else if (month1 == 11) { month1 = 10; } else { month1 = 11; }
Java Code:int month1 = Integer.parseInt(departureDate.substring(4, 6)) - 1;
Website: Learn Java by Examples
Similar Threads
-
How do I fix this program (2 errors)?
By Logik22 in forum New To JavaReplies: 17Last Post: 07-17-2011, 09:04 PM -
Help with some errors in my program
By kbud123 in forum Java AppletsReplies: 5Last Post: 05-11-2011, 05:08 AM -
First Java Program-Compile Errors (errors are posted)-simple GUI
By cc11rocks in forum AWT / SwingReplies: 4Last Post: 01-04-2011, 12:36 AM -
computes and returns the n:th falling power of base
By Danieldcc in forum New To JavaReplies: 4Last Post: 10-16-2010, 04:38 AM -
I'm falling in love with java...
By IndioDoido in forum IntroductionsReplies: 2Last Post: 08-30-2009, 03:38 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks