AP Computer Science Java: Lesson 1.6a
Beginning Objects and Class Design - A Student Class Design

Lesson 1.6a - A Student Class Design
Purpose: To learn the basics of class design in Java

A First Class
At the beginning of the course we worked with robot classes in Karel J. Robot. Now it is time to revisit this concept in a more abstract setting. In Object-Oriented Programming design (OOP design), we create and use structures or objects that are capable of storing information and performing actions on that information. We will begin with a class that manages data for a student.

When designing a class, we typically have the following sections that make up that class:

1. Instance variables
Constructors (default, initializing and copy)
Methods (accessors, modifiers and output)

Let's look at each section in depth:

Instance Variables The instance variables consist of whatever data this class needs to maintain. We declare these variables just like regular variables, with one exception - we put the keyword private in front of them. For example, in the student class, we will maintain a student's name and three test scores, so we declare the following as instance variables:

private String name;
private int test1, test2, test3;

So what does the keyword private do? By making instance variables private, we protect their values from outside manipulation. It also means that any attempt to use those variables outside of this file will meet with an undeclared identifier error. In other words, these variables are unknown outside this code (the opposite of private is public, but we will not use that keyword).

Constructors A constructor is a method whose purpose is to instanciate or construct a new object. It is essentially a means to declare an object in a way similar to the way we declare variables, but involves more detail. Recall that when we declared a new robot object in Karel J. Robot, we had to give the robot a name and some initial information (coordinates, direction and number of beepers). That statement used a Robot constructor. In our example, we will construct a Student. This requires giving the student a name as well as values for the three test scores.

There are three types of constructors:

Default - sets instance variables to default values
Initializing - sets instance variables to user-defined values
Copy - sets instance variables to values copied from another object of the same type

See the file Student.java at this time to see how constructors are implemented in the Student class.

Methods The methods of a class give the programmer functions that can be performed on and by the object. Recall that in Karel J. Robot we created methods to give the robot new skills to perform specific tasks. It is the same here, only more abstract.

As with constructors, there are several types of methods:

Accessors - allow the client code access to the instance variables
Modifiers - allow the client code to safely make changes to the values of the instance variables
Other - perform various miscellaneous actions on the object (either public or private) including such tasks as output of the object

See the file Student.java at this time to see how methods are implemented in the Student class.

© DanShuster.com