AP Computer Science Java: Lesson 1.6b
Beginning Objects and Class Design - Working with the Student Class


Lesson 1.6b - Working with the Student Class
Purpose: To learn how to implement Java classes with client code

Client Code
Now that we have seen the code for the Student class, we need to learn how to use or implement the methods in a client program. A client is a program that makes use of a class. To state it another way, the program is a client of the class.

When writing client code, we must first declare an object of the type defined in the class. That means we will first make use of one of the class's constructors. Since there are two constructors, there are two ways to construct a student object. See the examples below.

Student s1 = new Student();
Student s2 = new Student("Bob", 82, 89, 64);

The first statement above utilizes the default constructor. Since no initial values were given to this object, it received the default values (those specified in the default constructor). For our design, that means the object s1 has its name initialized as an empty string and all 3 of its test scores are set to zero.

In the second statement, the object s2 is given initial values for the name and the test scores (the scores are assigned in the order they are given). This is an example of the use of an initializing constructor.

In general, the form of a constructor is

ClassName identifier = new ClassName(list of values - if any);

Accessor Methods Once an object is instanciated, we can make use of its built-in methods. Some of the methods in our class are accessor methods, which are used to access the private instance variables. Typically, the names of accessor methods begin with get, since they are used to get data from the object. One such method in our class is getName(). The method getName() returns the name for the given Student object. Its method header is shown below.

public String getName()

What is this method header saying? First it tells us that this method is public, meaning that it can be invoked in any client of the Student class (a private method could only be used within the context of the class). Second, the use of the keyword String means that the method will return a String value (if a method only performs a task and does not have a return value, we make it a void method). Lastly, getName() is the name of the method and must adhere to the same naming rules for all identifiers. Since the parentheses are empty, this method has no parameters or input values. In general, method headers take this form:

public returnType methodName(parameters)

The returned value of a method has to go somewhere. Since this method is returning a String, the value has to go to a String variable or to an output statement. Here are two ways to invoke this method for the Student object s2:

System.out.println(s2.getName());
String studentName = s2.getName());

See the Student.java file for more examples of accessor methods.

Modifier Methods A method that safely changes the value of one of the private instance variables is called a modifier. The Student class method setName() is an example. The purpose of this method is to allow the user to change the internal name of a Student object. See the header of setName() below:

public void setName(String nm)

This method header tells us that it is public and void (has no return value). It is called setName and has one parameter, a String. This means we must supply the method call with a String that is intended to become the new name of the Student object. Note that the identifier tm is a temporary placeholder for this value and the value in tm gets transferred internally to the instance variable name in the Student class.

See the Student.java file for more examples of modifier methods.

Other Methods In the Student class, there are other methods that are neither accessors or modifiers. These are getAverage() and toString(). These are examples of methods that perform tasks on the instance variables. Let's look at each one individually.

While getAverage() appears to be an accessor due to its name, it doesn't actually access data - it simply supplies more information about the data. Specifically, it computes and returns the average of the three test scores as a decimal (see Student.java to see how this is done).

The method toString() gives the class a way to output a visual representation of the object. It does so by returning a String value with all of the data from the object, including the average of the tests (using getAverage()). That String is displayed using a System.out.println() statement. For example, if we have a Student object named s, we can do the following:

System.out.println(s);

When an object is placed in a System.out.println() statement, the toString() method is automatically invoked and the output described in that method is displayed.

See the Student.java file for specifics on the toString() method.

© DanShuster.com