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

Practice Materials

Exercise 1

Create a class called Student. Add the following fields/attributes to it:

  1. name of the student (String)
  2. personal identity code of student (String)
  3. the year student is at (int)

All of these fields should have get and set methods and this class should have a constructor. The set method of the personal identity code field should control if the size of it is right before changing. Add these methods to the class:

  1. method that according to identity code returns the time of birth (dd/mm/yyyy). Return type should be String. The meaning of identity code numbers is described here: https://learn.e-resident.gov.ee/hc/en-us/articles/360000624498-How-to-use-your-digital-ID), (you can get each number with a charAt() method)
  2. method that according to identity code returns if the student is male or female

Exercise 2

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

  1. classroom number (int)
  2. classroom capacity (int)
  3. classroom availability (boolean)

Now make get and set methods for all of the fields. This class should have two different constructors – one that does not take any parameters and one that uses all of the fields. Add the following method:

  1. method for controlling if a room can fit all of the students, this method should take one parameter: number of students and return if there are any open spots left. The return type should be String and it should return a whole sentence that says if there are any spots left and how many.

Exercise 3

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

  1. teacher name (String)
  2. the lesson this teacher is giving (String)
  3. the home classroom of this teacher (class Classroom object)
  4. the year this teacher is the class teacher at (int)
  5. the number of lessons in a day (int)

Add set and get methods if necessary. This class should also have a constructor and a toString method. Add the following methods to this class:

  1. method for seeing if the home classroom is available (return type boolean)
  2. method for calculating the time teacher is teaching in a day (each lesson is 45 min), return type double

Exercise 4

Amend class Student so it has a method that returns the name of the student's class teacher. This method takes one parameter that is an array of teachers (the array consists of class Teacher objects). Use a loop to go through all of the teachers. You can use the field in class Teacher that defines what year this teacher is teaching at.

Exercise 5

Create a class called Teaching. In this class we are going to use all of the classes we created. Make the following objects:

  1. one array of class Teacher objects
  2. three class Student objects
  3. one array of Student objects
  4. three class Classroom objects

Now use the all of these methods and print out the values with whole sentences:

  1. Method in class Classroom to control if a classroom fits all of the students.
  2. Try to change the students’s identity code with the set method.
  3. Print out all of the Student array objects’s time of birth and if they are male or female.
  4. Print out all of the students’s class teacher’s name
  5. Method in class Teacher to calculate how many hours a day they work
  6. Method in class Teacher to see if the home class is available.

Please leave your feedback for practice materials ->

<- Homework

  • 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