Flight Exercise Write a class called Flight that contains instance data to repre
ID: 3699134 • Letter: F
Question
Flight Exercise Write a class called Flight that contains instance data to represent the airline name, flight number, and the flight's origin and destination cities. Define the flight constructor to accept or initialize all instance data. Include mutator(setter) methods for all instance data, and a toString method that returns the data of the Flight. Create a static variable to keep track of the number of flight objects and a static method to return the number of flights recorded. Create a driver class called FlightTest, whose main method instantiates and updates several Flight objects. In the Flight class you will need: Private members of the class to store the airline name, flight number, flight origin and destination cities A static variable to store the number of Flight objects Two constructors: The first constructor should not accept any values during instantiation (This constructor can initialize the members to an empty string or dash-ie. “ o or 0) o The second constructor should accept all values for the members during instantiation. o The static variable should be incremented in each version of the constructor Mutator methods for each member of the class You will need these to pass values to the second object. A static method to return the number of flights o This will also serve as the accessor example A toString method to output the information on each flight In the FlightTest driver, you will need: At least two flight objects At least one object must be initialized with the constructor that accepts no parameters At least one object can be "hardcoded" by passing values through the constructor A Scanner object to get information for one (or more) of the objects Local variables (as needed) to store information To referene the mutators for one of your objects To reference the static method created in the class A sample of the output is shown below. (Output does not have to be exact - you can create any situation that you like but your code must be able to show successful use of all components of the class and driver programs.)Explanation / Answer
import java.util.Scanner;
class Flight
{
private String fName;
private String fNumber;
private String fOrigin;
private String fDest;
static int numOfFlights = 0;
Flight() //Default Constructor
{
this.fName="-";
this.fNumber = "-";
this.fOrigin = "-";
this.fDest = "-";
this.numOfFlights++;
}
Flight(String name,String num,String origin,String dest) //Paramertarized Constructor
{
this.fName = name;
this.fNumber = num;
this.fOrigin = origin;
this.fDest = dest;
this.numOfFlights++;
}
public void setfName(String fName) //Mutator function to set flight name
{
this.fName = fName;
}
public void setfNumber(String fNumber) //Muatator to set Flight Number
{
this.fNumber = fNumber;
}
public void setfOrigin(String fOrigin) //Mutator to set Flight Origin
{
this.fOrigin = fOrigin;
}
public void setfDest(String fDest) //Mutator to set Flight Destination
{
this.fDest = fDest;
}
public String toString() // method to return all the flight class data as a String
{
return this.fName+" "+this.fNumber+" -- From: "+this.fOrigin+" To: "+this.fDest;
}
}
class FlightTest
{
public static void main(String args[])
{
Scanner in = new Scanner(System.in);
String name,num,origin,dest;
System.out.println(" Please enter the information of the First flight");
System.out.print("Airline : ");
name = in.nextLine();
System.out.print(" Origin for connecting flight : ");
origin = in.nextLine();
System.out.print(" Destination for connecting flight : ");
dest = in.nextLine();
System.out.print(" Flight Number : ");
num = in.nextLine();
Flight first = new Flight();
first.setfName(name);
first.setfNumber(num);
first.setfOrigin(origin);
first.setfDest(dest);
System.out.println(" Please enter the information of the Second flight");
System.out.print("Airline : ");
name = in.nextLine();
System.out.print(" Origin for connecting flight : ");
origin = in.nextLine();
System.out.print(" Destination for connecting flight : ");
dest = in.nextLine();
System.out.print(" Flight Number : ");
num = in.nextLine();
Flight second = new Flight(name,num,origin,dest);
System.out.println(" Here is the information for the "+second.numOfFlights+" flights :");
System.out.println(first.toString());
System.out.println(second.toString());
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.