Arvutiteaduse instituut
  1. Kursused
  2. 2019/20 kevad
  3. Objektorienteeritud programmeerimine (Narva Kolledž) (LTAT.NR.003)
EN
Logi sisse

Objektorienteeritud programmeerimine (Narva Kolledž) 2019/20 kevad

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

Week 5 session exercises

NB! The set of practice problems will always be somewhat larger, so that those who are quicker or have more experience still have something to do. Don't worry if you don't reach the end. Take your time and solve those exercises that seem interesting and/or reasonably challenging to you.

Task 1. ID card

Write a class IDCard that meets the following requirements.

The 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 sevaral methods:

  • All instance fields have get and set methods.
  • Method gender that returns the card owner's gender: if the first number of the id number is 1, 3 or 5, then the owner is male; if the first number of the id number is 2, 4 or 6, then the owner is female. (More info from Wikipedia.) Return type and value can be chosen freely.
  • Method birthDate that returns the birth date of the card owner in the format DD.MM.YYYY (birth date is encoded in the symbols 2 to 7 of person's id number.
  • Method toString that shows the info about card owner in a readable way using the methods gender and birthDate.

In the main class, create different id cards and demonstrate the working of the instance methods.

Task 2. Returning empty bottles

After a birthday party, some friends want to bring back away empty beverage bottles. All bottles are marked with a label (A, B, C, or D) which indicates how much money one can get for that type of bottle. For example: label A - 0.04 euros, B, C and D - 0.08 euros.

The friends also have a file about the bottles:

 Kelluke A Joosep
 Valge klaar B Teele
 Tarhun A Jaan
 Mõmmi C Joosep

Each line of the file consists of the name of the beverage (single word or several words), the label, and the name of the friend who drank it (single word).

Write a program that has the following structure.

1) Class Bottle has four class fields of double type, which hold the values of the bottles. For example, label A has worth 0.04 and labels B, C, D have worth 0.08. (Since the values may change in time, it is better to declare the values as variables). Also the class has three private instance fields: name of beverage (String), label on the bottle (char) and 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 one char type parameter; the method returns the amount of money a person can get when returning the bottle with the given label. If the label is something else than 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 this bottle. To find the size of the refund, the method uses the previous method.
  • Method toString which shows the information about the bottle in a readable way; the refund must be shown in eurocents.

2) Class Returner has a private instance field for the name of the person who returns the bottles (String) and another private instance field for the array of Bottle's.

The class has a constructor and the following methods:

  • All instance fields have get and set methods.
  • A method of double return type without parameters, which returns the total refund for the bottles in the array of this returner.
  • A method of int return type without parameters, which counts and returns the number of bottles that were consumed by the returner him/herself;
  • Method toString which shows the info about the returner in a readable way.

3) The main class has the following methods.

  • A static void type method which reads the data from the file (given as an argument) and puts the data in three lists (also given as arguments): beverages, labels and the names of the friends 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 method which:
    • Creates three ArrayList type variables: one for the beverage names, the second one for the labels and the third one for the friend's names.
    • Uses the method above to read the data from the file and put it into the arraylists.
    • Divides the bottles into three arrays, i.e. creates three Bottle[] type arrays. The capacity of the arrays depends on the capacity of the lists; e.g. if there are 7 lines in the file, then the arrays capacity might be 2, 2 and 3.
    • Creates as many instances of Bottle class as there are elements in the lists, using the data read from the file into the lists and fills the three arrays.
    • Creates three friends (three instances of Returner type) and assignes to each of them one of the three lists. At least one name matches the name in the file.
    • Each returner calculates how much he/she would get if gives away all his/her bottles.
    • Shows how many bottles each returner drank.
    • Shows the information about each instance in a readable way using the different methods.

Data file is available at http://kodu.ut.ee/~eno/taara.txt. 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 with 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 determines 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 demonstrate the working of the instance methods.

  • Arvutiteaduse instituut
  • Loodus- ja täppisteaduste valdkond
  • Tartu Ülikool
Tehniliste probleemide või küsimuste korral kirjuta:

Kursuse sisu ja korralduslike küsimustega pöörduge kursuse korraldajate poole.
Õppematerjalide varalised autoriõigused kuuluvad Tartu Ülikoolile. Õppematerjalide kasutamine on lubatud autoriõiguse seaduses ettenähtud teose vaba kasutamise eesmärkidel ja tingimustel. Õppematerjalide kasutamisel on kasutaja kohustatud viitama õppematerjalide autorile.
Õppematerjalide kasutamine muudel eesmärkidel on lubatud ainult Tartu Ülikooli eelneval kirjalikul nõusolekul.
Courses’i keskkonna kasutustingimused