Free University of Bolzano/Bozen
Faculty of Computer Science - Bachelor in Applied Computer Science
Bachelor in Production Engineering
Introduction to Programming - A.A. 2006/2007
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 net fare (without VAT) for the ticket (a double)
The Ticket
class should have the following methods:
- A constructor to create Ticket objects, with three
parameters: one for the first name of the passenger, one for the last
name of the passenger and one for the net fare (without VAT).
- 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.
- getFare(), which takes one prameter of type
double
(representing the current VAT rate - for example: 0.20) and returns the
ticket fare including the VAT.
- printFullForm(), which prints the ticket's information in a full
form (in which names are not truncated and the ticket fare, including 20%
VAT is shown). For example:
''
Johnny English: Milan (Italy) - Paris (France), 300.00 EUR
''
- 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 keyboard.
- 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 keyboard.
- 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 keyboard.
- 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