Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do I search through an ArrayList for the lowest number and shortest duration?
    text
    copied!<p>I have 3 classes. I'm learning about interfaces and the interface class TravelCost must have public abstract and the method type and name so that it is consistent through all three classes. The three classes (AirTravelCost, TrainTravelCost, CarTravelCost) will implement TravelCost. I have all that set and tested to work. However, the test page which I assume is where you input a search through the arrayList for the lowest cost and shortest duration. I don't know how to do this as I've never done this before in an ArrayList. Here the sample code in the test class:</p> <pre><code>import java.util.*; public class TestTravelCost { public static void main(String [] args) { /*Scanner scn = new Scanner(System.in); //scanner object System.out.println("Number of Miles: "); double numOfMiles = scn.nextDouble(); System.out.println("Hotel cost per night: "); double cost = scn.nextDouble(); System.out.println("Description: "); String description = scn.nextLine();*/ TravelCost c = new CarTravelCost(400, 200, "Boston");//instantiate object for car travel TravelCost t = new TrainTravelCost(6, 60.0, "Boston"); //instantiate object for train travel TravelCost a = new AirTravelCost(224, "20110103", "0743" , "20110103", "1153", "Boston");//instantiate object for air travel ArrayList&lt;TravelCost&gt; AL = new ArrayList&lt;TravelCost&gt;();//array list for car travel AL.add(c); AL.add(t); AL.add(a); for(TravelCost tc : AL) { System.out.println(tc.toString()); } } } </code></pre> <p>Output: Car travel to Boston will take 7.2727272727272725 hours and cost 210.0 <br> Train travel to Boston will take 6.0 hours and cost 70.0 <br> Air travel to Boston will take 1.0166666666666666 and cost 243.48888888888888 //this is not the correct calculation, I don't know where I'm wrong but its suppose to be the same as the shortest duration.I guess I'm not good at math.</p> <p>Here's the calculation method I used for Air Travel</p> <pre><code> public double getDuration() { //---DEPARTURE---// int Dyear = Integer.parseInt(departureDate.substring(0,3)); //2011 int Dmonth = Integer.parseInt(departureDate.substring(4,5));//01 int Dday = Integer.parseInt(departureDate.substring(6,7));//03 int Dhour = Integer.parseInt(departureTime.substring(0,1));//0743 int Dminute = Integer.parseInt(departureTime.substring(2,3));//1153 //---ARRIVAL---// int Ayear = Integer.parseInt(arrivalDate.substring(0,3)); //2011 int Amonth = Integer.parseInt(arrivalDate.substring(4,5));//01 int Aday = Integer.parseInt(arrivalDate.substring(6,7));//03 int Ahour = Integer.parseInt(arrivalTime.substring(0,1));//0743 int Aminute = Integer.parseInt(arrivalTime.substring(2,3));//1153 GregorianCalendar date = new GregorianCalendar(Dyear, Dmonth, Dday, Dhour, Dminute);//departure date &amp; time GregorianCalendar time = new GregorianCalendar(Ayear, Amonth, Aday, Ahour, Aminute);//arrival date &amp; time //date = arrivalDate - departureDate;//2011-01-03 - 2011-01-03 = 0 //time = arrivalTime - departureTime;//0734 - 1153 = 410 double duration = (Math.abs(date.getTimeInMillis() - time.getTimeInMillis()) / 60000.0) / 60.0; return duration; `enter code here` } </code></pre> <p>How do I get this result in my code?</p> <p>LOWEST COST: Train Travel to Boston will take 11.0 hours and cost 70.0 <br> SHORTEST DURATION: Air Travel to Boston will take 4.166666666666667 hours cost 234.0</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload