You should have TWO files not one. Write a class called Flight that represents a
ID: 3673233 • Letter: Y
Question
You should have TWO files not one.
Write a class called Flight that represents an airline flight. It should contain instance data that represents the airline name, 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 call PP4_8 , whose main method instantiates and updates several Flight objects.
-After completing this assignment you should have two files PP4_8.java and Flight.java
-You should have a total of 10 methods when you have created the Flight class
1) One constructor method
2) Four setter methods- one for each piece of flight data
3) Four getter methods- one for each piece of flight data
4) One toString method
-The assignment states that you should instantiate (create) and updates several objects. I want you to specifically:
1) Create 7 flight objects.
2) Update the flight number on 2 of the flights after the objects have been created using a setter method that you created.
3) Update the airline name on 1 other flight object
4) Using a System.out.println method in PP4_8, print information about the three objects that you updated
5) Using getter methods, retrieve the flight number and destination of one of the non-modified flights and report to the user via a println statement that it has been delayed. Since this will not call the toString method, you should add an appropriate description with the data.
Explanation / Answer
Hi, you can print any information using getter function.
public class Flight {
private String name;
private int number;
private String origin;
private String destination;
/**
* @param name
* @param number
* @param origin
* @param destination
*/
public Flight(String name, int number, String origin, String destination) {
this.name = name;
this.number = number;
this.origin = origin;
this.destination = destination;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getNumber() {
return number;
}
public void setNumber(int number) {
this.number = number;
}
public String getOrigin() {
return origin;
}
public void setOrigin(String origin) {
this.origin = origin;
}
public String getDestination() {
return destination;
}
public void setDestination(String destination) {
this.destination = destination;
}
@Override
public String toString() {
return "["+name+", "+number+", "+origin+", "+destination+"]";
}
}
class PP4_8{
public static void main(String[] args) {
// 7 flight objests
Flight f1 = new Flight("Airline", 1, "Delhi", "Bangalore");
Flight f2 = new Flight("SpiceJet", 2, "Chennai", "Bhopal");
Flight f3 = new Flight("GoAir", 3, "Hydrabad", "Noida");
Flight f4 = new Flight("Metro", 4, "Delhi", "Patna");
Flight f5 = new Flight("AirZet", 5, "Ranchi", "Pune");
Flight f6 = new Flight("Domestic", 6, "Pune", "Bangalore");
Flight f7 = new Flight("Indigo", 7, "Delhi", "Mumbai");
// updatin flight no. 2 with 10;
f2.setNumber(10);
//updating flight 1 with flight 4
f1 = f4;
System.out.println(f2);
System.out.println(f1);
System.out.println(f4);
System.out.println(f5.getNumber()+" "+f5.getOrigin());
}
}
/*
Output:
[SpiceJet, 10, Chennai, Bhopal]
[Metro, 4, Delhi, Patna]
[Metro, 4, Delhi, Patna]
5 Ranchi
*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.