Arvutiteaduse instituut
  1. Kursused
  2. 2025/26 sügis
  3. Objektorienteeritud programmeerimine (MTAT.03.130)
EN
Logi sisse

Objektorienteeritud programmeerimine 2025/26 sügis

  • Introduction
  • Week 3: Classes, Objects
  • Week 4: Strings

Homework

Exercise 1

Create a class called Story. Add these fields to this class:

  1. Story name (String)
  2. Story text (String)

Now add get and set methods, a constructor and a toString() method to this class. Also add these methods:

  1. An instance method for seeing how many words there are in a story text field. The return type should be int. Tip: use split() method.
  2. An instance method for adding data to an existing story text. This method should take a String parameter that represents the text we want to add to the field. You can use the StringBuilder class. This method should return the modified text.

Create a class called Book. This class’s main method should have the following:

  • Three class Story objects
  • An array of Story objects
  • Use the toString method for all the array objects
  • For every object in the array, call out the method for seeing how many words there are in a story text field and calculate the sum of them. Print out the result.

Example: If three Story objects have 5 words in the text then the result should be 15.

  • Use the method for adding data to an existing story text with one of the Story objects and print out the result.

Example: If the text was like this “this is the first story” and we wanted to add this “ this is new part” then the method should return the following text “this is the first story this is new part”

Exercise 2

Create a class called Files. Add the following fields to it:

  1. File path (String absolute path)
  2. Text to a file (String)

This class should have a constructor and a toString method. Add the following instance methods:

  1. Method for creating a new text file with the same path as the field in this class. Write the value that is in the text field into the file. Split the data so there are at least three lines (if you use “.” as a separator you should write it like that “\\.”). Use try-with-resources when writing into a file. This method should be void type and print out if the file was created and written into.
  2. A method for analyzing the data in the file. Read in the file and for every line try to find words that are equal with each other in the same line. You can use the equalsIgnoreCase() method, so the comparison is not case-sensitive. Write the word that appeared multiple times in a line into an ArrayList. The arraylist should not contain duplicates. This method should return an ArrayList of words that were not unique in the sentence. (Tip: use double for loops.)

Create a test class with the name TestFiles. This class’s main method should have the following:

  • One class Files object.
  • Call out the method that creates and writes into a file (there should be at least three sentences)

Example:
The result could be something like this: “File was created and data was written successfully”

  • Call out the method that analyses the file data.

Example:
The result could look like this: [it, into]

Exercise 3

We have a file called results.txt (download this file). This file contains data of competition results. The data is in the following order: Name,place,age_group. So for example some of the data is like this:

Mati Karu,15,junior
Mark Suli,27,mini

Now make a class called CompetionResults. The class should contain the following fields:

  1. Name of the file we read the data from (String)

This class should have a constructor and a get method. Add the following methods:

  1. Instance method for reading the data from the file. This method should return the ArrayList of all the lines in the file. You can use this method in all the following methods.
  2. An instance method for making a list of all the same age group competitors (this list should contain the lines of the file). This method should take a parameter that says what age group competitors we want to see and return an ArrayList. The age groups are the following: mini, senior, junior.
  3. An instance method for calculating which age group had the most competitors. This method should return the age group name

Make a class called TestCompetitionResults. This class is for using all of the previously made methods and should have the following:

  • One CompetitionResults class object that uses the file results.txt as a parameter
  • Use the method that makes a list of the same age group competitors at least 2 times.

Example (this is only the first four competitors of the list):
[Mark Suli,27,mini, Kaarel Tamm,11,mini, Mari-Liis Aas,34,mini, Raimond Kalju,25,mini]

  • Use the last method that returns which age group had the most competitors.

Example:
Mini

Please leave your feedback for homework ->

<- Reading MaterialsPractice materials ->
  • 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