Arvutiteaduse instituut
  1. Kursused
  2. 2017/18 kevad
  3. Objektorienteeritud programmeerimine (Narva Kolledž) (P2NC.01.083)
EN
Logi sisse

Objektorienteeritud programmeerimine (Narva Kolledž) 2017/18 kevad

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

First test: sample task

First, read the task to the end and then start programming!

The library lends out different prints (books and magazines) for different periods. The period depends on the label on the print: green label - 24h (1 day), without any label (blank label) - 14 days, yellow label - 30 days and blue label - 60 days. If the print is not returned on time, the reader has to pay a penalty. The penalty for one delayed day is imposed according to the label on the print: yellow or blue label - 0.05 euros, without any label - 0.15 euros, green label – 2 euros.

Usually readers return books on time, but sometimes there are delays. Write a program which helps detect the readers with large penalty charges and prints out the reader with the largest penalty charge. The program should send a warning message to such readers.

The data about the borrowed books and their readers are given in the file in the following format:

 H. Schildt, Java: The Complete Reference; yellow; Joosep Kask; 125
 Guardians/2013,10; yellow; Teele Tamm; 59
 Oskar Luts, Kevade; blue; Teele Tamm; 39
 Artur Jürisson, Eesti-inglise õigussõnaraamat; blank; Jaan Aruhein; 224
 M. Lutz, Learning Python; green; Joosep Kask; 22
 Wired/2010,15; blank; Joosep Kask; 4

Each print is added into the file on a separate line. Each line contains the data about one print: the description of the print, the label, the reader and the number of days the borrowed book has already been in the reader's pocket (do not worry how this number is updated each day). These data is delimited by a semicolon and a space. Note that the description of a book contains information about the author and the title separated by a comma and a space. The description of a magazine contains the title, a slash, the publication year, a comma, and the issue. It can be assumed that if the description of a print does not contain a slash, it is a book.

Write a program which meets the following requirements (even if you do not like them or the requirements seem to be weird). The program must have java files for:

  • an abstract class called Print;
  • an interface called PenaltyCharge;
  • a class called Magazine which extends class Print;
  • a class called Book which extends class Print;
  • a class called ChargeWarning;
  • a class called LargestPenaltyCharge;
  • a test class.

1. PenaltyCharge is an interface which has a void return type method called saveCharge. The method takes in the following parameters: reader's name (String), print's description (String) and the penalty charge (double).

2. Print class is abstract. It has a constructor which instantiates the print's description (String), the print's label (String), the print's borrower (String) and the number of days the book has already been borrowed for (int).

  • The class has an abstract boolean method called moreInfo which informs if the book should be ordered from the repository.
  • The class has an int return type instance method (without parameters) called daysToLend which returns the period (the number of days) within which the book must be returned to the library (note the period depends on the label of the print). If the label is something else but green, yellow, blue, blank, the method returns value 0.
  • The class has a double return type method (without parameters) called chargePerDay which returns the penalty charge for one day associated with the label on the print.
  • The class has a void return type method calculateCharge which takes in an argument of PenaltyCharge type. The method calculates the total penalty charge associated with the print and let the PenaltyCharge store the charge.
  • The class has toString method which returns the data about the print in a readable way. The output must also contain the additional information of the print (use moreInfo method).
  • The class has to implement Comparable<Print> interface. Its method compareTo has to compare the prints based on the print's description.

3. Book class is a subclass of Print class. The class has a constructor which instantiates the instance fields, and the following methods:

  • moreInfo method which returns true if the book has a yellow or a blue label; otherwise, the method returns false.
  • toString method which returns the result of superclass's method toString complemented with a note that the print is a book.

4. Magazine class is a subclass of Print class. The class has a constructor which instantiates the instance fields, and the following methods:

  • moreInfo method which returns true if the magazine was printed in 2000 or earlier; otherwise, the method returns false.
  • toString method which returns the result of superclass's method toString complemented with a note that the print is a magazine.

5. ChargeWarning class implements PenaltyCharge interface. The class has a private instance field for the admissible penalty charge (double) and a private list for the debtors (ArrayList<String>).

  • The class has a constructor with one double type parameter for the admissible penalty charge.
  • The class has get method for the list of debtors.
  • The method saveCharge saves the reader's name in the list if the amount of the penalty charge (per book) exceeds the admissible level and reader's name is not in the debtor's list yet.

6. LargestPenaltyCharge class implements PenaltyCharge interface. The class has three private instance fields: the largest penalty charge (double), the reader (String) and the description of the print (String) associated with the largest penalty charge.

  • The class has a void return type method (without parameters) called sendWarning which outputs the reader's name and the print's description associated with the largest penalty charge.
  • The class has a void return type method called saveCharge. The method saves only the data about the largest penalty charge and associated with it reader's name and print's description.

7. The main class should have a public static List<Print> return type method called readPrintsIn. The method takes in a file name as an argument and returns the data from the file. The method may throw out an exception; therefore, add at the end of the method signature the following note: throws Exception. The number of lines in the file is not known. The program should not crash if the file is empty.

The main class should perform the following operations:

  • Use the static method to read in the data from the file.
  • Sort the prints using java.util.Collections.sort(List<T> list) method according to the rules stated in compareTo method.
  • Create an instance of ChargeWarning class (the admissible penalty charge is 0.2€).
  • Create an instance of LargestPenaltyCharge class.
  • Apply calculateCharge method to each print using the instance of ChargeWarning class as an argument. After the calculation of the penalty charges, output the names of the debtors.
  • Apply calculateCharge method to each print using the instance of LargestPenaltyCharge class as an argument. Output a warning message about the person who has the largest penalty charge.
  • 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