next up previous
Next: A Working Program Up: Learning the Language Previous: Defining Our Own 'types'

Operations With Those Types?

We'd like to be able to specify some operations to go along with the data structure that we just specified. For example, let's say that a student pays her fees. We would like to be able to set the feesOwing variable to 0. In fact, before we do that, maybe we'd like to find out how much she owes. We can build operations like these into the class definition as follows:


public class Student {

    public void reportFeesOwing() {
        System.out.println("Owing: " + feesOwing);
    }

    public void PayFees() {
        feesOwing = 0;
    }

    private String name;
    private int age;
    private String courses;
    private double feesOwing;
    private boolean scholarship;
}

Again we're going to leave discussion of public and private until later. What we've added to the class definition are


      public void reportFeesOwing() {
        System.out.println("Owing " + feesOwing);
    }
and

    public void PayFees() {
        feesOwing = 0;
    }

These are called methods or functions. For now, let's ignore the void keyword; we'll come back to it later. The point is that these are 'operations' for the Student type in the same way that increment, decrement, and so on were the operations for the int type. In this case, the programmer has full control over what the operations are and how they are defined.

Let's look at the methods more closely. The first method, reportFeesOwing(), consists of one statement. You can probably guess what this statement does -- it prints the word 'Owing:' and then the value of the variable feesOwing to the computer screen. This method is called a query because it is asking for the value of one of the member variables.

The second method, PayFees(), also consists of one statement. This statement says 'write the value 0 in the memory location associated with the variable name feesOwing.' Because it changes the value of one of the member variables, this method is called a command.

More generally, the set of values associated with all the member variables is called the state. Therefore, any method that reads a value of the state is called a query, and any method that changes or writes a value of the state is called a command. Don't get too concerned about all the names here; the point is the idea. And the idea is that we can define operations to read and write the memory locations of the member variables, or attributes.

Here is a comparison of the int type and a user defined type, or class:

  int Class
Number of variables 1 User defined
Amount of Memory 4 bytes; fixed User defined: sum of storage of member variables
How Memory is Allocated built-in program of compiler Class definition selects built-in programs of compiler
Operations increment, decrement, addition,... built-in programs user defined: methods/functions in class definition

As you can see, the idea of a class generalizes the idea of built-in types, allowing the user to define new data structures and the operations that go with them.


next up previous
Next: A Working Program Up: Learning the Language Previous: Defining Our Own 'types'
Chris Trendall
Copyright ©Chris Trendall, 2001. All rights reserved.

2001-12-09