Q4 Create a Time class This class will represent a point in

Q4: Create a Time class. This class will represent a point in time, such as a departure time. It should contain 2 constructors, 2 instance variables (hour and minute), and 10 methods (see below). All methods but toString should be in terms of the 24 hour format. [30 points]

default constructor: Creates a Time object for 12:00AM.

overloaded constructor: Creates a Time object at a specific hour and minute.

getHour(): Returns an integer representing the hour of the Time object.

getMinute(): Returns an integer representing the minute of the Time object.

addHours(...): Updates the object by moving it forward a number of hours.

addMinute(...): Updates the object by moving it forward a number of minutes. (Hint: Be careful that you don\'t allow minutes to be more than 59.)

addTime(...): Updates the object by moving it forward by the hour and minute from another Time object.

getCopy(...): Returns a new Time object that has the same hour and minute of the existing Time object.

isEarlierThan(...): Returns true if this Time object is earlier than another Time object.

isSameTime(...): Returns true if this Time object is the same as another Time object.

isLaterThan(...): Returns true if this Time object is later than another Time object.

toString(): Returns a string representing the Time object. Uses 12 hour AM/PM format and pads minutes to be two digits. See the sample output for an example.

Q5: Create a Flight class that uses the Plane and Time class. This class will represent a flight between two airports, using a specific Plane, and departing at a specific Time. It should contain a constructor, 7 instance variables (plane, flight number, cost, departure, duration, source, destination), and 9 methods (see below). [25 points]

overloaded constructor: Creates a Flight object that is setup up with a Plane, a flight number, a cost, a departure Time, a duration time, a source Airport, and a destination Airport.

getPlane(): Returns the Plane that operates this flight.

getNumber(): Returns the flight number.

getCost(): Returns the flight cost.

getDestination(): Returns the destination Airport.

getDeparture(): Returns the departure Time.

getArrival(): Returns a Time object with the arrival time (computed from the departure time and duration).

getSource(): Returns a Airport object for the departure location.

toOverviewString(): Returns a String representing an overview of the flight. Use NumberFormat to display the price. See the sample output for an example.

toDetailedString(): Returns a String representing the flight\'s detail information. See the sample output for an example.

Included below is an overall UML diagram that describes the three classes you will be constructing. It provides a useful summary of all of the methods you are expected to implement, and their corresponding types and visibility. Notice that one private method is listed here (formatDigits in Time) that isn\'t mentioned above. This is a method that was in our solution, you may not need it in your answer. Conversely, you may implement additional private methods that you find useful.

Solution

Answer:

Note: Methods are named as per user requirements.

Q4: Time class

//import needed files

import java.io.*;

import java.util.*;

import java.lang.*;

//Time class

class Time

{

     //Variables for Time

     int timeMinute;

     int timeHour;

     //Default-constructor

     public Time()

     {

          timeMinute=0;

          timeHour=0;

     }

     //Overloaded constructor

     public Time(int hh,int mm)

     {

          timeMinute=mm;

          timeHour=hh;

     }

     //Return hour

     public int getHour()

     {

          //return timeHour

          return timeHour;

     }

     //return minute

     public int getMinute()

     {

          //return timeMinute

          return timeMinute;

     }

     //adds aHrs to timeHour

     public void addHours(int aHrs)

     {

          timeHour=timeHour+aHrs;

     }

     //adds aMts to timeMinute

     public void addMinute(int aMts)

     {

          timeMinute=timeMinute+aMts;

          if(timeMinute>59)

          {

              addHours(1);

              timeMinute=timeMinute-60;

          }

     }

     //Add this time to t2

     public void addTime(Time t2)

     {

          addHours(t2.getHour());

          addMinute(t2.getMinute());

     }

     //Return copy of this time

     public Time getCopy()

     {

          //Return new time object

          return new Time(this.getMinute(),this.getHour());

     }

     //Return true if this time > t2

     public boolean isEarlierThan(Time t2)

     {

          if(this.getHour()>t2.getHour())

              return true;

          return false;

     }

     //Return true if this time=t2

     public boolean isSameTime(Time t2)

     {

if((this.getHour()==t2.getHour())&&(this.getMinute()== t2.getMinute()))

              return true;

          return false;

     }

     //Return true if this time>t2

     public boolean isLaterThan(Time t2)

     {

          if(this.getHour()<t2.getHour())

              return true;

          return false;

     }

     //overridden toString

     public String toString()

     {

          String tt=\"\";

          if(timeMinute<10)

              tt=\"0\";

          if(timeHour==0)

              return \"12 :\"+tt+timeMinute+\"AM\";

          else if(timeHour<12)

              return timeHour+\":\"+tt+timeMinute+\"AM\";

          else if(timeHour==12)

              return timeHour+\":\"+tt+timeMinute+\"PM\";   

          return (timeHour-12)+\":\"+tt+timeMinute+\"PM\";   

     }

}

//Driver class

public class HelloWorld

{

     //main

     public static void main(String[] args)

     {

          //create time1

          Time time1=new Time(12,4);

          System.out.println(\"Time 1 \"+time1.toString());

          time1.addHours(3);

System.out.println(\"Time 1 After adding 3 hours \"+ time1.toString());                                  

          //create time2

          Time time2=new Time(2,48);

          System.out.println(\"Time 2 \"+time2.toString());

          //compare time 2 with time1

System.out.println(\"Is time2 same time as time1?\"+time2.isSameTime(time1));

          System.out.println(\"Is time2 earlier than time1?\"+             time2.isEarlierThan(time1));

          System.out.println(\"Is time2 later than time1?\"+               time2.isLaterThan(time1));

     }

}

Sample output:

sh-4.3$ javac HelloWorld.java                                                                                                      

sh-4.3$ java -Xmx128M -Xms16M HelloWorld                                                                                          

Time 1 12:04PM                                                                                                                     

Time 1 After adding 3 hours 3:04PM                                                                                                

Time 2 2:48AM                                                                                                                      

Is time2 same time as time1?false                                                                                                 

Is time2 earlier than time1?false                                                                                               

Is time2 later than time1?true                                                                                                 

sh-4.3$  

Q4: Create a Time class. This class will represent a point in time, such as a departure time. It should contain 2 constructors, 2 instance variables (hour and m
Q4: Create a Time class. This class will represent a point in time, such as a departure time. It should contain 2 constructors, 2 instance variables (hour and m
Q4: Create a Time class. This class will represent a point in time, such as a departure time. It should contain 2 constructors, 2 instance variables (hour and m
Q4: Create a Time class. This class will represent a point in time, such as a departure time. It should contain 2 constructors, 2 instance variables (hour and m
Q4: Create a Time class. This class will represent a point in time, such as a departure time. It should contain 2 constructors, 2 instance variables (hour and m

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site