Institute of Computer Science
  1. Courses
  2. 2017/18 spring
  3. Object-Oriented Programming (Narva College) (P2NC.01.083)
ET
Log in

Object-Oriented Programming (Narva College) 2017/18 spring

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

Tasks (to be submitted by Sun 6.05 23:55) 1 point

Task 1.

Use Map to solve the following task. Write a program which reads the data in from a file, makes statistics on the used words and displays the result on the screen. (Hint: if a word already exists in the map, replace its value.)

The program should have a class called TextAnalyzer. In the class, there should be two constructors: one takes in the file name as an argument; the second constructor should take in two parameters - java.io.File (from where to read data in) and String (for the file encoding). The result of the analysis should return a method without parameters called wordFrequency.

Hint: it is possible to get a text from the file using only one expression. The required components are:

  • java.nio.Files#readAllBytes
  • java.io.File#toPath
  • java.lang.String(byte[], String)

Task 2.

Create a class called Pair who has two parameter types. The class should have two instance fields (one for each type), a constructor which instantiates the fields and the get and set methods (getFirst, getSecond, setFirst, setSecond). Add the toString method which returns the content of the fields. Create a client class. In the main method of the class, create a pair of String,Integer (e.g. a person's name and his age) and a pair of String,String (e.g. a person's name and his address) and test the instance methods.

Task 3.

Create a generic class called Comparer which takes in one parameter type. Bound the parameter type so that it can take only the Comparable interface implementing types. Add a method called findLargest which takes in two parameters. The method has to compare the objects and return the largest object based on the compareTo method.

Hint 1: leave the initial parameter type out and write the using a specific data type. Then add the parameter type and replace the data types. Hint 2: <T extends Comparable<T>> is a nice solution - T is the data type whose objects can be compared.

Session 12 
  • 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