Tutorial 2 - Class With Its Methods
Tutorial 2 - Class With Its Methods
Tutorial 2 – Creating the methods of a class
Purpose:
The purpose of this tutorial is to illustrate the creation of a concrete class with its
methods. In the previous tutorial we created the data members of the class, so it will not
be looked at again. You will be provided with the UML class diagram from which you
need to create the methods the class.
You are provided with the following UML class diagram, that represents a Student class.
A student must have a surname, student number, and an age. This was created in the
previous tutorial. Note the third row of the UML class diagram that contains the
methods. It shows that the class have a constructor, mutator and accessor methods for
the data members.
Student
‐ surname: String
‐ studentNumber: String
‐ age: int
+ Student()
+ setSurname(String): void
+ setStudentNumber(String): void
+ setAge(int): void
+ getSurname(): String
+ getStudentNumber(): String
+ getAge(): int
Student UML Class Diagram
1. Open the already started Student class from tutorial 1. The class as it was from the
previous tutorial is shown in figure 1.
2. The name of our class is “Student” so declare the class with its body as shown in
figure 1 below. Save the file as Student.java.
Figure 1
Page 1
3. We start to create the methods of the class one at a time underneath the already
created data members. Looking at the UML class diagram you will note the first listed
method is the constructor. This is the default constructor. It has the same name as the
class and have no return type, not even void. Refer to the notes for the purpose of a
constructor. The “ + “ sing in front of the constructor name, indicates that the access
modifier is public. Create the default constructor as shown in figure 2. The complete
method will include the method header as well as the body of the method.
Figure 2
4. NetBeans IDE will allow you to insert the constructors for you, you do not need to
code it. Somewhere in the source right click and the following options will appear.
Select the Insert Code.. option.
Page 2
5. In the next window you will be able to select what it is that you want to insert like
constructor, get, set, or other types of method. For this tutorial you can select
Constructor…
6. The “Generator Constructor” window will appear from which you can select the fields
you want to initialize with the constructor. For this tutorial we will not select any of
the fields, we will create an empty constructor as was shown in figure 2.
7. For the remaining figures we will not show the data members. Just remember that
they are still there.
8. The next three listed methods are the mutator methods, also known as “set”
methods. Refer to the notes for the purpose of mutator methods. Note the syntax for
declaring a method as a comment in figure 3. And then you code the three methods
as indicated in figure 3. Make use of the Insert Code… feature again and insert the
setters methods for all three data members.
Page 3
Figure 3
9. The next three listed methods are the accessor methods, also known as “get”
methods. Refer to the notes for the purpose of accessor methods. Following the same
syntax for declaring a method, declare the 3 accessor methods as shown in figure 4.
Please note that these methods in figure 4 were added under the mutator methods.
Make use of the Insert Code… feature again and insert the getters methods for all
three data members.
Figure 4
NB – There is no order in which you have to code the methods. Try to just follow the
order in which they appear in the UML class diagram.
Follow these steps and complete the class declaration with its methods for the
Competition class as shown in the UML class diagram. A Competition must have a name,
a basic registration fee and number of competitors. Continue with the class you started in
tutorial 1.
Page 4
Competition
‐ BASIC_REG_FEE: double
‐ name: String
‐ competitors: int
+ Competition()
+ setName(String): void
+ setCompetitors(int): void
+ getName(): String
+ getCompetitors(): int
+ registrationFee(int): double
Competition UML Class Diagram
A few things to note in this class:
Since the BASIC_REG_FEE is a constant, it will not require a mutator method since
we cannot change its value. It is also not a common practice to have an accessor
method for it, but it is possible.
The registrationFee() method is used to compute and return the registration fee
for a competitor. This is seen as a helper method. A competitor in the age range of
20 to 25 will pay the basic fee plus an additional R100. A competitor in the age
range of 26 to 40 will pay the basic fee plus an additional R150. A competitor older
than 40 will only pay the basic fee.
Page 5
Page 6