Airplane Class in Java

Posted by Samath
  3459
April 30, 2015

 

Design and implement a class called AirPlane that will store data about a commercial aircraft. Data
for an airplane includes:
- make (type of plane, such as, “Boeing”)
- model (model number, for example, 707)
- capacity (seating capacity of the plane – a whole number up to 400)
- pilot (name of pilot assigned to the aircraft)

Your implementation should include:

a) A constructor that accepts parameters in the order given above and initializes the corresponding instance variables;     
b) Getter methods as given below:
     getMake() – return a string with the make of the airplane
     getModel() – return a number being the airplane’s model
     capacity() - return a number being the capacity of the airplane.

c) A method called assignPilot which accepts the name of a pilot and assigns the pilot to the
airplane.

d) A toString() method which should return a string with the format:
       Airplane Information:
                    Aircraft : <<insert make>> <<insert model>>
                    Capacity: ___ seats
                    Pilot: _____

AirPlane Class:

public class AirPlane 
{
    private String make;
    private int model;
    private int capacity;
    private String pilot;
    
    public AirPlane(String mk, int mdl, int cp, String pl)
    {
        make = mk;
        model = mdl;
        capacity = cp;
        pilot = pl;
    }          
    
    public  String getMake()
    {
        return make;
    }
    
    public int getModel()
    {
        return model;
    }
    
    public int capacity()
    {
        return capacity;
    }
    
    public void assignPilot(String name)
    {
        pilot = name;
    }
    
    public String toString()
    {
        String result = "";
        
        result += "Airplane Information: \n"+"\t\tAircraft : "+make+" "+model+"\n\t\tCapacity: "+capacity+" seats"+"\n\t\tPilot: "+pilot;
        
        return result;
    }
    
}

Design and implement a class called Flight that will store data about a commercial flight. Data includes:
- origin (e.g. “Kingston”)
- destination (e.g. “New York”)
- departure time (e.g. “10:10”)
- arrival time (e.g “13:30”)
- date of flight (e.g. “12/04/2012”)
- aircraft assigned (this should be an object of class AirPlane);

Your implementation should include:
a) A constructor which accepts data (except for departure time and arrival time), in the order listed
above and initializes each relevant instance variable accordingly.

b) A toString()method which should return a string with the format:
         Flight Information:
                   Date: <<insert date of flight>>
                   From: <<insert origin>> to <<insert destination>>
                   Flight time: <<insert departure time>> to <<insert arrival time>>
                   Plane Information:
                            Aircraft : <<insert make>> <<insert model>>
                            Capacity: ___ seats
                            Pilot: _____

In the Flight class, define the following methods that will set the scheduled time for a flight:
void schedule(String arrivalTime)

void schedule(String arrivalTime, String departureTime)

Flight Class:

public class Flight 
{
  private String origin; 
  private String destination; 
  private String departure_time; 
  private String arrival_time;
  private String date_of_flight;
  private AirPlane aircraft_assigned; 
  
  
  public Flight(String ori, String des, String dof, AirPlane airassign)
  {
      origin = ori;
      destination = des;
      date_of_flight = dof;
      aircraft_assigned = airassign;   
  }
  
     public String toString()
    {
        String result = "";
        
        result += "\n\nFlight Information: \n"
                    +"\tDate: "+date_of_flight
                    +"\n\tFrom: "+origin+ " to "+ destination
                    +"\n\tFlight time: "+departure_time +" to "+arrival_time
                    +"\n\t"+aircraft_assigned.toString();
        
        return result;
    }
     
     public void schedule(String arrivalTime)
     {
         arrival_time = arrivalTime;
     }
  
     void schedule(String arrivalTime, String departureTime)
     {
         arrival_time = arrivalTime;
         departure_time = departureTime;
     }
  
}

Implement a driver class, called AirJamaica that will do the following:
a. Declare and initialize a list of flights.
b. Add up to 5 flights of your choice.
c. Use the second version of the schedule method to set the time schedule for the third flight in
the list.
d. Use the first version of the schedule method to update the arrival time for the third flight in
the list.
e. Use a while loop to display all flights.

AirJamaica Class:

import java.util.*;
public class AirJamaica 
{
    public static void main(String[] args) 
    {
        AirPlane ap1 = new AirPlane("Boeing", 505, 400, "Roy Jones");
        AirPlane ap2 = new AirPlane("Boeing", 968, 400, "Mark Scott");
        AirPlane ap3 = new AirPlane("Boeing", 756, 400, "John Palmer");
        
        Flight f1 = new Flight("New York", "Miami", "3-4-2015", ap1);
        Flight f2 = new Flight("London", "Miami", "3-8-2015", ap2);
        Flight f3 = new Flight("New York", "London", "9-4-2015", ap2);
        Flight f4 = new Flight("New York", "Toronto", "4-8-2015", ap3);
        Flight f5 = new Flight("Paris", "Miami", "9-6-2015", ap1);
        
       ArrayList<Flight> flights = new ArrayList<Flight>();
       flights.add(f1);
       flights.add(f2);
       flights.add(f3);
       flights.add(f4);
       flights.add(f5);
       
       flights.get(2).schedule("2:00PM", "6:00PM");
       flights.get(2).schedule("2:15PM");
       
       int count = 0;
       while(flights.size()!=count)
       {
           System.out.print(flights.get(count).toString());
           count++;
       }
    }
    
}