In: Computer Science
JAVA - Design and implement a class called Flight that represents an airline flight. It should contain instance data that represent the airline name, the flight number, and the flight’s origin and destination cities. Define the Flight constructor to accept and initialize all instance data. Include getter and setter methods for all instance data. Include a toString method that returns a one-line description of the flight. Create a driver class called FlightTest, whose main method instantiates and updates several Flight objects (at least 5).
class Flight{
private String name;
private String number;
private String origin;
private String destination;
/**
* @param aName
* @param aNumber
* @param aOrigin
* @param aDestination
*/
public Flight(String aName, String aNumber, String aOrigin, String aDestination) {
super();
name = aName;
number = aNumber;
origin = aOrigin;
destination = aDestination;
}
/**
* @return the name
*/
public String getName() {
return name;
}
/**
* @return the number
*/
public String getNumber() {
return number;
}
/**
* @return the origin
*/
public String getOrigin() {
return origin;
}
/**
* @return the destination
*/
public String getDestination() {
return destination;
}
/**
* @param aName the name to set
*/
public void setName(String aName) {
name = aName;
}
/**
* @param aNumber the number to set
*/
public void setNumber(String aNumber) {
number = aNumber;
}
/**
* @param aOrigin the origin to set
*/
public void setOrigin(String aOrigin) {
origin = aOrigin;
}
/**
* @param aDestination the destination to set
*/
public void setDestination(String aDestination) {
destination = aDestination;
}
@Override
public String toString() {
return "Flight [name=" + name + ", number=" + number + ", origin=" + origin + ", destination=" + destination
+ "]";
}
}
public class FlightTest {
public static void main(String[] args) {
//creating 5 Flight objects
Flight f1 = new Flight("AIR123", "12314", "Hyderabad", "AUS");
Flight f2 = new Flight("JET123", "45435", "Bombay", "Newyork");
Flight f3 = new Flight("JET123", "23413", "Chennai", "UK");
Flight f4 = new Flight("IND123", "42323", "Hyderabad", "USA");
Flight f5 = new Flight("IND123", "12312", "Banlgore", "Hyderabad");
//printing 5 objects data
System.out.println(f1);
System.out.println(f2);
System.out.println(f3);
System.out.println(f4);
System.out.println(f5);
//updating flight data
f5.setDestination("Begumpet");
System.out.println(f5);
}
}