Results 1 to 3 of 3
Thread: Need help with Java Answer
- 05-09-2010, 09:15 PM #1
Member
- Join Date
- Sep 2009
- Posts
- 9
- Rep Power
- 0
Need help with Java Answer
So I have the following classes that both work.
The Time Class
The Flight ClassJava Code:public class Time { // instance variables - replace the example below with your own private int hours; private int minutes; /** * Constructor for objects of class Time */ public Time(int h, int m) { // initialise instance variables hours = h; minutes = m; } public void setTime (int h, int m) { hours = h; minutes = m; } public int getHours() { return hours; } public int getMinutes() { return minutes; } public boolean isValidTime() { if ((hours >= 0 && hours <= 23) && (minutes >= 0 && minutes <= 59)) { return true; } else { return false; } } public String toString() { return hours + ":" + minutes; } public int minutesUntil (Time other) { int x = (this.hours * 60) + this.minutes; int y = 1440 - x; int a = (other.hours * 60) + other.minutes; int b = 1440 - a; return y - b; } }
The Trip ClassJava Code:public class Flight { // instance variables - replace the example below with your own private String num; private Time departure; private Time arrival; /** * Constructor for objects of class Flight */ public Flight(String f, Time d, Time a) { num = f; departure = d; arrival = a; } public void setFlight (String f, Time d, Time a) { num = f; departure = d; arrival = a; } public Time getDepartureTime() { return departure; } public Time getArrivalTime() { return arrival; } public String toString() { int x = departure.getHours(); int a = arrival.getHours(); int test = (x - a) * 60; int test_2 = test + (departure.getMinutes() - arrival.getMinutes()); return num + "-" + departure + "-" + arrival + "-" + test_2; } }
Java Code:import java.util.*; public class Trip { private String time; private ArrayList<Flight> a; public Trip (String t, ArrayList<Flight> f) { time = t; a = f; } public int getDuration () { // Need help with this method } }
I need help completing the getDuration method.
The getDuration method returns the number of minutes, from the departure of the first flight to the arrival of the last flight.
I'm not sure how to do this. Thanks for any help I can get.
- 05-10-2010, 01:05 AM #2
Member
- Join Date
- Sep 2009
- Posts
- 9
- Rep Power
- 0
I was thinking about using the arraylist function get, but I'm not sure.
- 05-10-2010, 07:17 AM #3
Senior Member
- Join Date
- Feb 2010
- Location
- Ljubljana, Slovenia
- Posts
- 470
- Rep Power
- 4
If the flights in the arraylist are already sorted, you just need to do this:
If the flights are not sorted, then you first need to find the earliest departure and the latest arrival time in your array, the return statement remains the same.Java Code:public int getDuration() { Time t1 = a.get(0).getDepartureTime(); Time t2 = a.get(a.size()-1).getArrivalTime(); return t1.minutesUntil(t2); }Ever seen a dog chase its tail? Now that's an infinite loop.
Similar Threads
-
Need answer asap!!!
By uranis_khai in forum New To JavaReplies: 3Last Post: 07-07-2009, 09:48 AM -
Java Interview Question and Answer
By gnomeom in forum Java SoftwareReplies: 3Last Post: 06-04-2009, 11:53 AM -
Why is the answer not coming out
By anonymous18 in forum New To JavaReplies: 4Last Post: 11-12-2008, 03:10 AM -
i want my answer to a whole number or i think an int, please help?
By soc86 in forum New To JavaReplies: 3Last Post: 11-02-2008, 01:29 AM -
Plz answer this question ...
By raghu2114 in forum Advanced JavaReplies: 2Last Post: 09-19-2008, 06:36 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks