Practice Materials
Exercise 1
Create a class called Student
. Add the following fields/attributes to it:
- name of the student (
String
) - personal identity code of student (
String
) - 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:
- 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 acharAt()
method) - 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:
- classroom number (
int
) - classroom capacity (
int
) - 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:
- 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:
- teacher name (
String
) - the lesson this teacher is giving (
String
) - the home classroom of this teacher (class
Classroom
object) - the year this teacher is the class teacher at (
int
) - 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:
- method for seeing if the home classroom is available (return type
boolean
) - 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:
- one array of class
Teacher
objects - three class
Student
objects - one array of
Student
objects - three class
Classroom
objects
Now use the all of these methods and print out the values with whole sentences:
- Method in class
Classroom
to control if a classroom fits all of the students. - Try to change the students’s identity code with the
set
method. - Print out all of the
Student
array objects’s time of birth and if they are male or female. - Print out all of the students’s class teacher’s name
- Method in class
Teacher
to calculate how many hours a day they work - Method in class
Teacher
to see if the home class is available.