You are on page 1of 2

University Of Jordan

Computer Engineering Department


Object-Oriented Problem Solving
Assignment-5
Eng. Asma Abdel Karim

❖ Write a JAVA program to build an appropriate implementation for the UML diagram below,
which models flights and passengers.

Flight
- passengers: Passenger[]
Passenger - origin: String
+ name: String - destination: String
- ticketNo: int + number: int
- maxNumOfPassengers: int =40
+ Passenger()
+ Passenger(name: String, ticketNo: int) + Flight()
+ getTicketNo(): int + Flight(passengers: Passenger[])
+ setTicketNo(ticketNo: int):void + addPassenger(): boolean
+ removePassenger(): boolean
+ getPassengers(): Passenger[]
+ getOriginAndDestination(): String
+ getMaxNumOfPassengers():int
+ setMaxNumOfPassengers(maxNumOfPassengers:int): void

1. Passenger:
• The no-arg constructor Passenger() must read the name and the ticktNo from the user. You
must prompt the user to enter the name and ticketNo with appropriate messages. The
ticketNo must consist of exactly 5 digits, if the user enters a number that does not consist of
5 digits, your code must keep on prompting the user until he enters a valid number.
• The constructor Passenger(name:String, ticketNo: int) must initialize the name and ticketNo
with the passed argument.
2. Flight:
• The no-arg constructor of the Flight class must read the origin, the destination, and the flight
number from the user. You must prompt the user to enter the required info with appropriate
messages. The Passengers array must be created with 0 elements.
• The constructor Flight(passengers: Passenger[]) must invoke the no-arg constructor of the
same class. Then, initialize the Passengers array data field with the passed array by creating
a new array and new objects.
• The method addPassenger must add a new passenger (created using the no-arg constructor
of class Passengers) to the array passengers. The method must check that the size of the
passengers array has not reached the maxNoOfPassngers before adding the passenger. If
it’s not reached, it must add the passenger and return true. If it’s reached it doesn’t add the
passenger and returns false.
• The method removePassenger must read the ticketNo from the user, and remove the
passenger with the entered ticketNo from the array passengers. The method returns true if a
passenger with the entered ticket number is found and removed. If no passenger with the
entered ticket number is found the method does not remove anything and returns false.
• The method getOriginAndDestinaton must return a String that includes the origin value and
destination value as follows: origin – destination.

3. In your main class:


• Define a method named maxFlightPassengers that takes an array of flights and returns the
flight with the largest number of passengers.
• Define a method named printFlight that takes an object of type flight and print its info as
follows:
Max number of passengers allowed for flights: …..
Flight number: ….
Origin & destination:…..
Passenger ticketNo Passenger Name
.
.
.

• In your main method:


o Define an object of type Flight using the no-arg constructor.
o Add three passengers to it.
o Define a new object of type Flight and initialize it with passengers of the first flight
object.
o Print info of the two flights by invoking the printFlight method,

You might also like