Institute of Computer Science
  1. Courses
  2. 2018/19 spring
  3. Object-Oriented Programming (Narva College) (LTAT.NR.003)
ET
Log in

Object-Oriented Programming (Narva College) 2018/19 spring

  • Home
  • Materials
  • Java Glossary
  • Source Example
  • Cheat sheet (S1-S6)
  • Grading
  • Links

slides_s4.pdf

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. 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 person's id number (String). The class has a constructor and the following methods:

  • all instance fields have get and set methods;
  • gender method returns the card owner's gender: if the first number of the id number is 1, 3 or 5 - it is man's card; if the first number of the id number is 2, 4 or 6 - it is woman'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 person's id number; more info about it card can be found in Wikipedia);
  • toString method shows info about the card owner in a readable way using gender and birthDate methods.

In the main class, create different id cards and test the instance methods.

Task 2. 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 a 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: the name of a beverage (String), a 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 and set methods;
  • a static method of double return type and with char 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 and set 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 use charAt(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.

The file is available here. Copy the file on your computer.

Task 3. 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 and set 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 calculates 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.

  • Institute of Computer Science
  • Faculty of Science and Technology
  • University of Tartu
In case of technical problems or questions write to:

Contact the course organizers with the organizational and course content questions.
The proprietary copyrights of educational materials belong to the University of Tartu. The use of educational materials is permitted for the purposes and under the conditions provided for in the copyright law for the free use of a work. When using educational materials, the user is obligated to give credit to the author of the educational materials.
The use of educational materials for other purposes is allowed only with the prior written consent of the University of Tartu.
Terms of use for the Courses environment