Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Create a Flight class that uses the Plane and Time class. This class will repres

ID: 3928848 • Letter: C

Question

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).

Below is the code so far (only part of the final test FLIGHT is missing):

/**
*
* @author (your name), Acuna
*/
public class Driver {
    public static void main(String[] args) {
       
        testAirline();
        testAirport();
        testPlane();
        testTime();
        testFlight();
    }
   
    public static void testAirline() {
        System.out.println("==testAirline()==");
       
        Airline a1 = Airline.American;
        Airline a2 = Airline.United;
        Airline a3 = Airline.Delta;
        Airline a4 = Airline.United;
       

        System.out.println("a1: " + a1);
        System.out.println("a2 == a3: " + (a1 == a2));
        System.out.println("a2 == a4: " + (a2 == a4));

    }
   
    public static void testAirport() {
        System.out.println("==testAirport()==");
       

        Airport a1 = Airport.PHX;
        Airport a2 = Airport.LAX;
        Airport a3 = Airport.SFO;
        Airport a4 = Airport.NRT;
        Airport a5 = Airport.SIN;
       
        System.out.println("a1: " + a1);
        System.out.println("a2 == a3: " + (a1 == a2));
        System.out.println("a2 == a4: " + (a2 == a4));
        System.out.println("a1: " + Airport.getAirportCity(a1));
        System.out.println("a3: " + Airport.getAirportCity(a3));
        System.out.println("a5: " + Airport.getAirportCity(a5));
    
    }
   
    public static void testPlane() {
        System.out.println("==testPlane()==");

        Plane p1 = new Plane(Airline.Delta, "Boeing 717");
        Plane p2 = new Plane(Airline.United, "Airbus A321");
       
        System.out.println(p1.getAirline());
        System.out.println(p1.getModel());
        System.out.println(p1);
        System.out.println(p2);
  
    }
   
    public static void testTime() {
        System.out.println("==testTime()==");
       

        //Test 1: use default constructor.
        Time t1 = new Time();
       
        //Test 2: use overloaded constructor.
        Time t2 = new Time(9, 0);
        Time t3 = new Time(1, 15);
       
        //Test 3: use clone operation.
        Time t4 = t3.getCopy();
       
        //Test 4: run toString on AM times.
        System.out.println(new Time(0, 5));
        System.out.println(new Time(1, 15));
        System.out.println(new Time(2, 45));
        System.out.println(new Time(10, 5));
        System.out.println(new Time(11, 15));
       
        //Test 5: run toString on PM times.
        System.out.println(new Time(12, 45));
        System.out.println(new Time(13, 5));
        System.out.println(new Time(22, 15));
        System.out.println(new Time(23, 45));
       
        //Test 6: run toString on object from default constructor.
        System.out.println("t1: " + t1);
       
        //Test 7: testing addTime operation
        System.out.println("t2: " + t2);
        t2.addTime(t3);
        System.out.println("t2: " + t2);
       
        //Test 8: testing addMinutes operation
        t2.addMinute(181);
        System.out.println("t2: " + t2);
       
        //Test 9: testing8 addHours operation
        t2.addHours(2);
        System.out.println("t2: " + t2);
       
        //Test 10: testing cloned copy.
        t4.addHours(1);
        System.out.println("t3: " + t3);//original
        System.out.println("t4: " + t4);//clone
       
        //Test 11: testing isEarlierThan.
        System.out.println("t3 < t4: " + t3.isEarlierThan(t4));
        System.out.println("t4 < t3: " + t4.isEarlierThan(t3));
        System.out.println("t2 < t4: " + t2.isEarlierThan(t4));
        System.out.println("t4 < t2: " + t4.isEarlierThan(t2));
        System.out.println("t2 < t2: " + t2.isEarlierThan(t2));
       
        //Test 12: testing isLaterThan.
        System.out.println("t2 > t4: " + t2.isLaterThan(t4));
        System.out.println("t4 > t2: " + t4.isLaterThan(t2));
        System.out.println("t4 > t4: " + t4.isLaterThan(t4));
       
        //Test 13: testing isSameTime.
        System.out.println("t2 = t4: " + t2.isSameTime(t4));
        System.out.println("t4 = t4: " + t4.isSameTime(t4));
        System.out.println("t4 = 2:15AM: " + t4.isSameTime(new Time(2, 15)));

       
    }

   
    public static void testFlight() {
        System.out.println("==testFlight()==");

        Flight f1 = new Flight(new Plane(Airline.American, "Airbus A321"),
                               "495",
                               79,
                               new Time(11,5), 100,
                               Airport.PHX, Airport.LAX);
       
        Flight f2 = new Flight(new Plane(Airline.Delta, "Boeing 717"),
                               "1063",
                               79,
                               new Time(7, 10),
                               95,
                               Airport.PHX,
                               Airport.LAX);
       
        Flight f3 = new Flight(new Plane(Airline.American, "Airbus A321"),
                               "400",
                               44,
                               new Time(21, 25),
                               127,
                               Airport.PHX,
                               Airport.SFO);
       
        Flight f4 = new Flight(new Plane(Airline.United, "Boeing 787"),
                               "400",
                               525,
                               new Time(10, 50),
                               715,
                               Airport.LAX,
                               Airport.NRT);
       
        Flight f5 = new Flight(new Plane(Airline.United, "Boeing 737"),
                               "414",
                               59,
                               new Time(6, 50),
                               85,
                               Airport.LAX,
                               Airport.SFO);
       
       
        System.out.println(f1.toDetailedString());
        System.out.println();
        System.out.println(f1.toOverviewString());
       
        System.out.println();
        System.out.println();
       
        System.out.println(f5.toDetailedString());
        System.out.println();
        System.out.println(f5.toOverviewString());

    }
}

Explanation / Answer

#include<stdio.h>
#include<ctype.h>

#define MAX_NUM_SEATS (20)
#define MAX_NUM_SEATS_IN_FIRST_CLASS (5)
#define MAX_NUM_SEATS_IN_ECONOMY            (MAX_NUM_SEATS - MAX_NUM_SEATS_IN_FIRST_CLASS)

int main()
{
      int plane[MAX_NUM_SEATS] = {0}, i=0;
      int nNumSeatsInFirst = 0;
      int nNumSeatsInEconomy = 0;
      int nSeatAssignmentFirstClass = 1; // start at 1;
      int nSeatAssignmentEconomy = MAX_NUM_SEATS_IN_FIRST_CLASS + 1; // start at 6
      int choice;
      int nClass;
      int nCurrentSeatAssignment;//      firstClass=1,economy=6,choice;
      char response[2];
      char firstname[35], lastname[35];
      int       bPrintTicket;
      
      while( i < MAX_NUM_SEATS )
      {
            printf(" %s %s ", "Please type 1 for "First class"","Please type 2 for"Economy"");
            scanf("%d", &choice);
            
            if( !( ( 1 == choice ) || ( 2 == choice ) ) )
            {
                  continue;
            }
            
            // start out assuming we will print ticket!
            bPrintTicket = 1;
            
            printf(" Enter first name:");
            scanf("%s",firstname);
            
            printf(" Enter lastname :");
            scanf("%s", lastname);
            
            nClass = choice; // store the class before doing all the logical checks

            /****************************************************************/            
            /*************** ECONOMY SECTION ***************/
            /****************************************************************/
            if( 2 == choice )
            {
                  // check for economy full
                  if( nNumSeatsInEconomy >= MAX_NUM_SEATS_IN_ECONOMY )
                  {
                        // economy full
                        // check for first class (plane ) full
                        if( nNumSeatsInFirst <= MAX_NUM_SEATS_IN_FIRST_CLASS )
                        {
                              printf("The economy section is full. ");
                              printf("would you like to sit in first class ");
                              printf("section( Y or N)?");
                              scanf("%s", response);
                              
                              if ( toupper(response[0])=='Y')
                              {
                                    // print ticket!
                                    bPrintTicket = 1;
                                    printf( "Your seat assignment is %d ", nSeatAssignmentFirstClass );
                                    nCurrentSeatAssignment = nSeatAssignmentFirstClass;
                                    plane[nSeatAssignmentFirstClass - 1] = 1;
                                    nSeatAssignmentFirstClass++;
                                    nNumSeatsInFirst++;
                                    nClass = 1;
                                    i++;
                              }
                              else                              
                              {
                                    // don't print ticket!
                                    bPrintTicket = 0;
                                    printf("Next flight leaves in 3 hours. ");
                              }
                        }
                        else
                        {
                              // don't print ticket!
                              bPrintTicket = 0;
                              // print out that plane is full
                              printf("Plane is full, next flight in 3 hours ");
                        }
                  }
                  else
                  {      
                        // economy is not full                        
                        printf("Your seat assignment is %d ", nSeatAssignmentEconomy );
                        plane[(nSeatAssignmentEconomy - 1)] = 1;
                        nCurrentSeatAssignment = nSeatAssignmentEconomy;
                        nSeatAssignmentEconomy++;
                        nNumSeatsInEconomy++;                        
                        i++;
                  }
            } // if( choice == 2 )
            

            /****************************************************************/            
            /*************** FIRST CLASS SECTION ***************/
            /****************************************************************/
            if( 1 == choice )
            {
                  // check for fisrt class full
                  if( nNumSeatsInFirst >= MAX_NUM_SEATS_IN_FIRST_CLASS )
                  {
                        // first class full
                        // check for economy class (plane) full
                        if( nNumSeatsInEconomy <= MAX_NUM_SEATS_IN_ECONOMY )
                        {
                              printf("The First Class section is full. ");
                              printf("Would you like to sit in the economy ");
                              printf("section (Y or N)?");
                              scanf("%s", response);
                              
                              
                              if(toupper(response[0])=='Y')
                              {                                    
                                    printf("Your seat assignment is %d ",nSeatAssignmentEconomy);
                                    plane[nSeatAssignmentEconomy - 1] = 1;
                                    nCurrentSeatAssignment = nSeatAssignmentEconomy;
                                    nSeatAssignmentEconomy++;
                                    nNumSeatsInEconomy++;
                                    i++;
                                    nClass = 2;
                              }
                              else
                              {
                                    // don't print ticket!
                                    bPrintTicket = 0;
                                    printf("Next flight leaves in 3 hours. ");
                              }
                        }
                        else
                        {
                              // don't print ticket!
                              bPrintTicket = 0;
                              // print out that plane is full
                              printf("Plane is full, next flight in 3 hours ");
                        }
                  }
                  else
                  {
                        // first is not full
                        printf("Your seat assignment is %d ", nSeatAssignmentFirstClass );
                        plane[(nSeatAssignmentFirstClass - 1)] = 1;
                        nCurrentSeatAssignment = nSeatAssignmentFirstClass;
                        nSeatAssignmentFirstClass++;
                        nNumSeatsInFirst++;
                        
                        i++;
                  }
            }
            
            if( 1 == bPrintTicket )
            {
                  printf(" **************************************** ");
                  printf(" * * ");
                  printf(" NAME:%s, %s ",lastname,firstname);
                  printf(" * * ");
                  printf(" CLASS:%d ",nClass);
                  printf(" * * ");
                  printf(" SEAT:%d ",nCurrentSeatAssignment);
                  printf(" * * ");
                  printf(" ");
                  printf(" **************************************** ");
            }
            
}
  
printf(" All the seats for this flight are sold ");
  
return 0;
}