Tutorial 5: Classes and Objects


Overview on this Tutorial:

1. A Simple Class (ClassTest1.java)

2. Static Variables and Functions (ClassTest2.java)

3. Public and Private (ClassTest3.java)

4. Constructor (ClassTest4.java)


A Simple Class (ClassTest1.java):

1. Actually, Java programmer doesn't think in function. They thinks in "Classes" and "Objects", so Java is said to be an "Object Oriented Programming" (OOP) language.

2. The syntax of a (simple) class in Java is:

class <Class Name> {
    <Member Functions>
    <Member Variables>
}

3. It is actually quite similar to "Record" in Pascal, because both of them contains member variables. However, Class can have member functions.

4. Here is an example:

class Student // Define a class called Student
{
    // Member functions
    void setName(String _name) {
        name = _name;
    }

    void setChiMark(int _chiMark) {
        chiMark = _chiMark;
    }

    void setEngMark(int _engMark) {
        engMark = _engMark;
    }

    void setMathMark(int _mathMark) {
        mathMark = _mathMark;
    }

    void printAve() {
        System.out.println(name + ": " + average() );
    }

    int average() {
        int ave = (chiMark + engMark + mathMark) / 3;
        return ave;
    }

    // Member variables;
    String name;
    int chiMark;
    int engMark;
    int mathMark;
}

5. The class "Student" have 6 member functions: setName(), setChiMark(), setEngMark(), setMathMark(), printAve() and average().

6. Member functions in a class can access its member variables.

7. The class "Student" have 4 member variables: chimark, engMark, mathMark.

8. To define a variable of type "Student", we have to use a new operator.

Student calvin = new Student();
Student simon = new Student();

9. We called the variables ("calvin" and "simon") as objects of class "Student".

10. Each objects have different set of member variables.

11. Note that "ClassTest1" is another class (similar to "Student").

12. There is only one "public" class in a Java Program, which having the same name as the Java source code file.

13. In fact, unlike Pascal and C++, every function in Java must belong to a class.

Practice: Try to add a member function "higherAverage" in the class "ClassTest1" as shown below:

public class ClassTest1 extends MIDlet // A class called ClassTest1
{
    public ClassTest1() { }

    public boolean higherAverage(Student s1, Student s2) {

            // Your code here..
    } 


    public void startApp() {
        Student calvin = new Student();
        calvin.setName("Calvin");
        calvin.setChiMark(71);
        calvin.setEngMark(65);
        calvin.setMathMark(80);

        Student simon = new Student();
        simon.setName("Simon");
        simon.setChiMark(72);
        simon.setEngMark(66);
        simon.setMathMark(81);

        calvin.printAve();
        simon.printAve();
    }

    public void pauseApp() { }
    public void destroyApp(boolean unconditional) { }
}

Implement the function body such that it will return true if and only if student "s1" have higher average marks than student "s2".


Static Variables and Functions (ClassTest2.java)

1. As shown in the previous example, for normal class variables and functions, we have to use an object to reference them.

2. For "static" class variables and functions, we should use the class name to reference them.

3. There is a "static" variable called "numOfStudent" in the class "Student", we should reference it by writing "Student.numOfStudent".

4. The idea is that a "static" variable or function belongs to a class, but not an object.

5. A "static" function can use "static" variables. For example, the function getNumOfStudent() can use the variable numOfStudent.

6. You can specify a constant variables using "static final".


Public and Private (ClassTest3.java)

1. We will usually add "private" to all the variables to indicate that it can only be used by function in the same class.

2. We will usually add "public" to all the functions to indicate that it can only be used by function in the same class.

3. Since the variable chiMark is private and the functions setChiMark() and getChiMark() is public, we have:

calvin.setChiMark(71);
int chiMark = calvin.getChiMark();

But not:

calvin.chiMark = 71;
int chiMark = calvin.chiMark;

4. In fact, if you don't added "public" or "private", Java will still treated all the functions as "public", all the variables as "default modifier" (similar to "private").

Question: Simplifier the program by changing all the variables as "public". (You can delete all the "get" and "set" functions).


Constructor (ClassTest4.java)

1. There is usually a public function in a class with the function name being the same with the class name. This function is called by the "new" operator, that is the function is called whenever an object is constructed.

Student calvin = new Student("Calvin",71,65,80);

2. The constructor is usually used to initialize the variables.

public Student(String _name, int _chiMark, int _engMark, int _mathMark) {
    name = _name;
    chiMark = _chiMark;
    engMark = _engMark;
    mathMark = _mathMark;
    numOfStudent++;
}

3. Note that the variable "numOfStudent" is incremented by one when a Student object is constructed. So the variable "numOfStudent" can keep track of the total number of students.