Results 1 to 3 of 3
  1. #1
    alexisasoxfan is offline Member
    Join Date
    Sep 2009
    Posts
    9
    Rep Power
    0

    Default Need help with Java Answer

    So I have the following classes that both work.

    The Time Class

    Java 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 Flight Class

    Java 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;
            
        }
    
    }
    The Trip Class

    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.

  2. #2
    alexisasoxfan is offline Member
    Join Date
    Sep 2009
    Posts
    9
    Rep Power
    0

    Default

    I was thinking about using the arraylist function get, but I'm not sure.

  3. #3
    m00nchile is offline Senior Member
    Join Date
    Feb 2010
    Location
    Ljubljana, Slovenia
    Posts
    470
    Rep Power
    5

    Default

    If the flights in the arraylist are already sorted, you just need to do this:
    Java Code:
    public int getDuration() {
      Time t1 = a.get(0).getDepartureTime();
      Time t2 = a.get(a.size()-1).getArrivalTime();
      return t1.minutesUntil(t2);
    }
    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.
    Ever seen a dog chase its tail? Now that's an infinite loop.

Similar Threads

  1. Need answer asap!!!
    By uranis_khai in forum New To Java
    Replies: 3
    Last Post: 07-07-2009, 09:48 AM
  2. Java Interview Question and Answer
    By gnomeom in forum Java Software
    Replies: 3
    Last Post: 06-04-2009, 11:53 AM
  3. Why is the answer not coming out
    By anonymous18 in forum New To Java
    Replies: 4
    Last Post: 11-12-2008, 03:10 AM
  4. Replies: 3
    Last Post: 11-02-2008, 01:29 AM
  5. Plz answer this question ...
    By raghu2114 in forum Advanced Java
    Replies: 2
    Last Post: 09-19-2008, 06:36 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •