Question

Design and implement a class called Flight that represents an airline flight. It should contain instance data that represents the airline name


Design and implement 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 called FlightTest, whose main method instantiates and updates several Flight objects.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Algorithm :

STEP1: Start the program
STEP2: Create a class with Flight with four instance variables
called airLineName, flightName, originCity and destinationCity.
STEP3: Define a constructor of Flight class that takes the values
and the values for the instance variables of the Flight class.
STEP4: Write accessor and mutator methods for all the instance variables of Flight class.
STEP5: Define a method toString that returns the string representation of the instance
variables of the class Flight.
STEP6: Create a test program called FlightTest.java
STEP7: In main method of the class, instantiate two Flight class objects.
STEP8:Call the method toString on each Flight objects that prints the description of
Flight class.
STEP9:Stop the program.

Java program :

/**
* The java program that demonstrates the Flight class . The main method
* instantiates the Flight objects with the airlines name, flight name, origin name
* and destination name. Then call toString method to display the one line
* description of the Flight on console output.
*
* */
//FlightTest.java
public class FlightTest {
   public static void main(String[] args) {
      
       Flight indiObject=new Flight("Indgo", "ABC", "HYD", "DEL");
       System.out.println(indiObject.toString());
      
       Flight kingObject=new Flight("KinFisherAirLines", "XYZ", "NY", "DLS");
       System.out.println(kingObject.toString());
   }
}//end of the class

----------------------------------------------------------------------------------------------------------------------------
//Flight class
//Flight.java
public class Flight
{
   //declare instance variables of class
   private String airLineName;
   private String flightName;
   private String originCity;
   private String destinationCity;
   /*Constructor that sets the values for the instance variables*/
   public Flight(String airLineName, String flightName,
           String originCity, String destinationCity) {
       this.airLineName=airLineName;
       this.flightName=flightName;
       this.originCity=originCity;
       this.destinationCity=destinationCity;
   }
   /**
   * Method to get air line name
   */
   public String getAirLineName() {
       return airLineName;
   }
   /**
   * Method to set air line name
   */
   public void setAirLineName(String airLineName) {
       this.airLineName = airLineName;
   }
   /**
   * Method to get flight name
   */
   public String getFlightName() {
       return flightName;
   }
   /**
   * Method to set the flight name
   */
   public void setFlightName(String flightName) {
       this.flightName = flightName;
   }
   /**
   * Method to get origin city
   */
   public String getOriginCity() {
       return originCity;
   }
   /**
   * Method to set the origin city
   */
   public void setOriginCity(String originCity) {
       this.originCity = originCity;
   }
   /**
   * Method to get destination city
   */
   public String getDestinationCity() {
       return destinationCity;
   }
   /**
   * Method to set the destination city
   */
   public void setDestinationCity(String destinationCity) {
       this.destinationCity = destinationCity;
   }
  
   /*Method returns the one line desription of flight*/
   public String toString() {
       return String.format("Airline Name: %s Flight Name: %s Origin Name: %s Destination Name: %s",
               airLineName,flightName,originCity,destinationCity);
   }
}//End of Flight class

Sample Output:

Airline Name: Indgo Flight Name: ABC Origin Name: HYD Destination Name: DEL
Airline Name: KinFisherAirLines Flight Name: XYZ Origin Name: NY Destination Name: DLS

Add a comment
Answer #2



CODE:

class FlightTest {

public static void main(String[] args)

{

Flight obj1 = new Flight("Chegg",1234,"London","New york");

//Flight obj1 = new Flight();

System.out.println(obj1.name+"\n"+obj1.number+"\n"+obj1.origin+"\n"+obj1.destination);

System.out.println("get name - "+obj1.getName());

System.out.println("get nuber - "+obj1.getName());

System.out.println("get Origin - "+obj1.getName());

System.out.println("get Destination - "+obj1.getName());

obj1.setName("HomeworkLib1");

obj1.setNumber(2345);

obj1.setOrigin("HomeworkLib2");

obj1.setDestination("India");

System.out.println(obj1.name+"\n"+obj1.number+"\n"+obj1.origin+"\n"+obj1.destination);

}

static class Flight {

String name;

int number;

String origin;

String destination;

Flight(String name_t,int number_t,String origin_t,String destination_t)

{

name = name_t;

number = number_t;

origin = origin_t;

destination = destination_t;

}

public void setName(String name_t)

{

name = name_t;

}

public void setNumber(int number_t)

{

number = number_t;

}

public void setOrigin(String origin_t)

{

origin = origin_t;

}

public void setDestination(String destination_t)

{

destination = destination_t;

}

public String getName()

{

return name ;

}

public int getNumber()

{

return number;

}

public String setOrigin()

{

return origin ;

}

public String setDestination()

{

return destination;

}

}

}

OUTPUT:

Add a comment
Know the answer?
Add Answer to:
Design and implement a class called Flight that represents an airline flight. It should contain instance data that represents the airline name
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a class called Shelf that contains instance data that represents the length, breadth, and capacity...

    Write a class called Shelf that contains instance data that represents the length, breadth, and capacity of the shelf. Also include a boolean variable called occupied as instance data that represents whether the shelf is occupied or not. Define the Shelf constructor to accept and initialize the height, width, and capacity of the shelf. Each newly created Shelf is vacant (the constructor should initialize occupied to false). Include getter and setter methods for all instance data. Include a toString method...

  • cs55(java) please. Write a class called Book that contains instance data for the title, author, publisher,...

    cs55(java) please. Write a class called Book that contains instance data for the title, author, publisher, price, and copyright date. Define the Book constructor to accept and initialize this data. Include setter and getter methods for all instance data. Include a toString method that returns a nicely formatted, multi-line description of the book. Write another class called Bookshelf, which has name and array of Book objects. Bookself capacity is maximum of five books. Includes method for Bookself that adds, removes,...

  • Chapter 5 Assignment Read directions: In JAVA design and implement a class called Dog that contains...

    Chapter 5 Assignment Read directions: In JAVA design and implement a class called Dog that contains instance data that represents the dog's name and dog's age. Define the Dog constructor to accept and initialize instance data. Include getter and setter methods for the name and age. Include a method to compute and return the age of the dog in "person years" (seven times age of dog. Include a toString method that returns a one-time description of the dog (example below)....

  • Can you please help me with with problem. please follow all the specific insturctions, and try...

    Can you please help me with with problem. please follow all the specific insturctions, and try to make simple for a beginner in java. Write a class called Shape that contains instance data that represents the name and number of sides of the shape. Define a constructor to initialize these values. Include mutator(setter) methods – with the this reference – for the instance data, and a toString method that returns a the shape data. Create a static variable to keep...

  • A java class named Book contains: instance data for the title, author, publisher and copyright year...

    A java class named Book contains: instance data for the title, author, publisher and copyright year a constructor to accept and initialize the instance data variables set and get methods a toString() method to return a formatted, multi-line description of the book Produce a child class that inherits from Book. The class is called Dictionary. Dictonary must include: instance data that describes the number of words in the dictionary a constructor that takes in all information needed to describe a...

  • In Java* Please implement a class called "MyPet". It is designed as shown in the following...

    In Java* Please implement a class called "MyPet". It is designed as shown in the following class diagram. Four private instance variables: name (of the type String), color (of the type String), gender (of the type char) and weight(of the type double). Three overloaded constructors: a default constructor with no argument a constructor which takes a string argument for name, and a constructor with take two strings, a char and a double for name, color, gender and weight respectively. public...

  • Please help me with this code. Thank you Implement the following Java class: Vehicle Class should...

    Please help me with this code. Thank you Implement the following Java class: Vehicle Class should contain next instance variables: Integer numberOfWheels; Double engineCapacity; Boolean isElectric, String manufacturer; Array of integers productionYears; Supply your class with: Default constructor (which sets all variables to their respective default values) Constructor which accepts all the variables All the appropriate getters and setters (you may skip comments for this methods. Also, make sure that for engineCapacity setter method you check first if the vehicle...

  • In a project named 'DogApplication', create a class called 'Dog' 1. declare two instance variables in...

    In a project named 'DogApplication', create a class called 'Dog' 1. declare two instance variables in 'Dog' : name (String type) age (int type) 2. declare the constructor for the 'Dog' class that takes two input parameters and uses these input parameters to initialize the instance variables 3. create another class called 'Driver' and inside the main method, declare two variables of type 'Dog' and call them 'myDog' and 'yourDog', then assign two variables to two instance of 'Dog' with...

  • Write an entire class called Movie that stores an instance variable named title for the title...

    Write an entire class called Movie that stores an instance variable named title for the title of the movie as a string. Write the corresponding getter and setter methods (getTitle, setTitle), and a constructor that takes one parameter (string for the title). Include both the class definition and method definitions! Be sure to use appropriate visibility modifiers. Assume all includes and using namespace std are already in the file. Do not try to make header files.

  • Create an abstract class “Appointment” that represents an event on a calendar. The “Appointment” class will...

    Create an abstract class “Appointment” that represents an event on a calendar. The “Appointment” class will have four instance variables: • An instance variable called “year” which will be of type int • An instance variable called “month” which will be of type int • An instance variable called “day” which will be of type int • An instance variable called “description” which will be of type String The “Appointment” class must also implement the following methods: • A getter...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT