Free University of Bolzano/Bozen
Faculty of Computer Science - Bachelor in Applied Computer Science
Bachelor in Production Engineering
Introduction to Programming - A.A. 2005/2006
Exercise 4
Class and method definitions
Exercise 4A
Define a class for representing journey tickets. Each ticket should contain
the following information:
- The first name and the last name of the passenger (two strings)
- The name of the city where the journey begins (a string)
- The name of the country where the journey begins (a string)
- The name of the city where the journey ends (a string)
- The name of the country where the journey ends (a string)
The Ticket
class should have the following methods:
- A constructor to create Ticket objects, with two
parameters: one for the first name of the passenger, and one for the last
name of the passenger.
- setDeparturePoint(), which takes two parameters of
type
String
(representing the name of a city and of a country)
and sets the name of the city and of the country where the journey begins.
- setArrivalPoint(), which takes two parameters of
type
String
(representing the name of a city and of a country)
and sets the name of the city and of the country where the journey ends.
- printFullForm(), which prints the ticket's information in a full
form (in which names are not truncated). For example: ''
Johny
English: Milan (Italy) - Paris (France)
''
- printCompactForm(), which prints the ticket's information in a
compact form, in which the first name is replaced with its first letter and
a dot, while the name of a country is abbreviated with its first two
letters in upper case. For example: ''
J. English: Milan (IT)
- Paris (FR)
''.
For the design of the class, follow the methodology presented in the lectures.
Solution
Exercise 4B
Define a Java class implementing an example client for the class
Ticket
of Exercise 4A. The example class should perform
the following operations:
- Read from the keyboard the first and the last name of a passenger, and
create a
Ticket
object. Use two dialog windows
of JOptionPane to read the input from the keyborad.
- Read from the keyboard the names of a city and of a country, and use them
to update the departure point of the
Ticket
object. Use two
dialog windows of JOptionPane to read the input from the keyborad.
- Read from the keyboard the names of a city and of a country, and use them
to update the arrival point of the
Ticket
object. Use two
dialog windows of JOptionPane to read the input from the keyborad.
- Display the ticket's information in full form.
- Display the ticket's information in compact form.
Solution
Exercise 4C
Repeat Exercise 4B using the Scanner
class to read the input from
the keyboard.
Solution