NB! The practical session exercises are always provided enough and to spare with an eye to fast / experienced students have exercises to solve. To this end, do not get upset if you do not get on all the task in class. Take your time and complete the tasks at home!
Task 1. Dancing couples
In DancingCouple
class, there are private instance fields: boy's name (String
) and his birth year (int
) as well as girls' name (String
) and her birth year (int
). The name of the dancer contains his/her first name(s) and last name. The class also has a constructor and at least the following methods:
- all instance fields have
get
andset
methods; - an instance method of
int
return type and one parameter for the current year (int
); the method returns the age of the oldest partner; - an instance method of
boolean
return type and without parameters; the method shows if the partners were born in the same year; - an instance method of
String
return type and without parameters; the method returns the last names of the partners delimited by a space as one string; toString
method which shows the partners' last names using the previous method.
In the main class, create several dancing couples and test the instance methods.
Task 2. ID card
IDCard
class has a private instance field for the id card number (String
), a private instance field for the card owner's name (String
) and a private instance field for the id number (String
). The class has a constructor and the following methods:
- all instance fields have
get
andset
methods; gender
method returns the card owner's gender: if the first number of the id number is 1, 3 or 5 - it is a man's card; if the first number of the id number is 2, 4 or 6 - it is a female's card;birthDate
method returns the birthday date of the card owner in the format DD.MM.YYYY (the birthday date is encoded in the 2-7 symbols of the id number; more info about it card can be found inWikipedia
);toString
method shows info about the card owner in a readable way usinggender
andbirthDate
methods.
In the main class, create different id cards and test the instance methods.
Task 3. Returnable empties
After a birthday party, friends want to give away empty beverage containers. All containers are marked with a label which indicates how much money one can get for an empty container: A label - 0.04 euros, B, C and D - 0.08 euros. The friends also have a file about containers in the format:
Kelluke A Joosep Valge klaar B Teele Tarhun A Jaan Mõmmi C Joosep
Each line of the file has the name of the beverage (a single word or several words), the label and the name of the friend who drank it (one word).
Write a program which meets the following requirements.
1. Container
class has the following private instance fields: name of the beverage (String
), label on the container (char
) and the name of the friend who drank it (String
). The class has a constructor and the following methods:
- all instance fields have
get
andset
methods; - a static method of
double
return type and withchar
type parameter; the method returns the amount of money a person can get on return of the container; if the label is something else but A, B, C or D, the amount of money is equal to 0; - an instance method of
double
return type and without parameters; the method returns the refund for the container; the method utilises the previous method; toString
method which shows the info about the container in a readable way (the refund must be shown in euro cents).
2. Returner
class has a private instance field for the name of the returner (String
). The class has a constructor and the following methods:
- all instance fields have
get
andset
methods; - a method of
double
return type whose parameter is an array of containers; the method returns the total refund for the containers in the array; - a method of
int
return type whose parameter is an array of containers; the method counts and returns the number of containers that the returner drank him/herself; toString
method which shows the info about the returner in a readable way.
3. The main class has the following variables and methods:
- four static variables of
double
type (e.g.containerA
etc.) which hold the values of the containers: A 0.04; B, C and D 0.08. - a static
void
type method which reads the data from the file (given as an argument) and puts in three lists (given as arguments): beverages, labels and the name of the friend who drank it. (To process a char given as a string usecharAt(0)
method). The data about containers are given in the file in format shown above. It is not known beforehand the number of lines in the file (the program should work even if the file is empty). - The main class has the following procedures:
- Create three
List
type variables: one for the beverage names, the second one for the labels and the third one for the friend's name. - Use an appropriate static method that reads the data from the file and puts into the lists.
- Divide the containers in three parts. In other words, create three
Container[]
type arrays (the capacity of the arrays depends on the capacity of the lists; e.g. if there are 7 lines in the file, the arrays capacity might be 2, 2 and 3). - Create as many instances of
Container
class as there are elements in the lists using the data from the appropriate lists. Consequently fill the arrays. - Create three friends (three instances of
Returner
type). At least one name match the name from the file. - Each returner has to calculate how much he/she would get if gives aways the containers. Each of the returners has his/her array.
- Show how many bottles each returner drank.
- Show info about each instance in a readable way using the different methods.
- Create three
The file is available here
. Copy the file on your computer.