Role List

Variables and attributes have in programs some typical behaviors that are called roles. For example, a variable whose value is never changed after initialization is called a fixed value. Roles must not be mixed up with the basic types (e.g., in Java int, char, float, etc.) since the concept of roles is a classification telling what kind of behavior the variable or attribute has in a certain program.

The following eleven roles are enough to cover almost all variables and attributes in simple programs. Roles appearing the most often are fixed value, stepper and most-recent holder which cover about 70 % of all variables. On the other hand, all variables and attributes don't necessary have any of the roles listed below.

The roles (presented more accurately below) are:

The meaning of line numbering in example programs is to ease refering to lines. Line numbers have no effect on program execution since they are comments in Java.

User input from the command line is read in the examples with the class UserInputReader. The methods UserInputReader.readFloat(), UserInputReader.readInt(), and UserInputReader.readChar() read a float, an int, and a character respectively. If the user gives erroneus input, for example a character to UserInputReader.readInt(), then the UserInputReader methods prints an error message and asks for a valid value.

Icon for the role Fixed value

The role of a variable or an attribute is a fixed value, if its value is not changed after initialization. The example class models a bank account. The atttribute accountNumber is a fixed value. It gets its value (on line 7) during the run-time only once. The value never changes after that. Fixed value can be used in different places in a program - on line 10 in the example.

/*  1 */ public class BankAccount {
/*  2 */
/*  3 */    String accountNumber;
/*  4 */    float balance=0;
/*  5 */
/*  6 */    public BankAccount (String nr) {
/*  7 */       accountNumber = nr;
/*  8 */    } 
/*  9 */
/* 10 */    public String getAccountNumber () { return accountNumber; }
/* 11 */
/* 12 */    public float transaction (float change) {
/* 13 */       balance += change; 
/* 14 */       return balance;
/* 15 */    } 
/* 16 */}

Icon for the role Stepper

Stepper goes through a succession of values in some systematic way. Below is an example of a loop structure where the variable multiplier is used as a stepper. The example program outputs a multiplication table while the stepper goes through the values from one to ten.

/* 1 */ public class MultiplicationTable {
/* 2 */    public static void main(String[] args) {
/* 3 */    int multiplier;
/* 4 */    for (multiplier = 1; multiplier <= 10; multiplier++)
/* 5 */       System.out.println(multiplier + " * 3 = " + multiplier * 3);
/* 6 */    }
/* 7 */ }
A stepper can also be used, for example, for counting and traversing the indexes of an array.

Icon for the role Most-recent holder

The value of a most-recent holder is the latest gone through value of a certain group or simply the latest input value. The example program repeteadly (on line 7) asks the user for the input until the input is valid. In this program the variable s is a most-recent holder since it holds the latest input value at the time.

/*  1 */ public class AreaOfSquare {
/*  2 */ 
/*  3 */    public static void main(String[] args) {  
/*  4 */       float s = 0f;
/*  5 */       while (s <= 0) {
/*  6 */          System.out.print("Enter the length of the side of a square: ");
/*  7 */          s = UserInputReader.readFloat();
/*  8 */       }
/*  9 */       System.out.println("The area of the square is " + s * s);
/* 10 */    }
/* 11 */ }

Icon for the role Most-wanted holder

The value of a most-wanted holder is the "best" or otherwise the most-wanted value out of the values gone through so far. The exists no restrictions for measuring the superiority of the values: the most-wanted can mean, for example, the smallest or the biggest number or a number closest to a certain value.

The example class finds out the largest of the temperatures included in a statistics. The attribute maxTemperature is a most-wanted holder since it is given (on line 15) the current value if it is larger than the largest one so far.

/*  1 */ import java.util.Vector;
/*  2 */
/*  3 */ public class TemperatureStatistics {
/*  4 */
/*  5 */    Vector temperatures;
/*  6 */    int maxTemperature;
/*  7 */
/*  8 */    public TemperatureStatistics () {
/*  9 */       temperatures = new Vector();
/* 10 */       maxTemperature = -99999;
/* 11 */    }
/* 12 */
/* 13 */    public void addData (int temperature) {
/* 14 */       temperatures.add(new Integer(temperature));
/* 15 */       if (temperature > maxTemperature) maxTemperature = temperature;
/* 16 */    }
/* 17 */}
(The attribute temperatures is a container.)

Icon for the role Gatherer

The value of a gatherer accumulates all the values gone through so far. The example program accepts integers given as an input one by one until the user inputs the number -999 after which the program calculates the mean value of the inputs. The variable sum is a gatherer: the total of the inputs is gathered (on line 9) in it.

/* 1 */ public class MeanValue { 
/* 2 */
/* 3 */    public static void main(String[] argv) {   
/* 4 */       int count=0;
/* 5 */       float sum=0, number=0;
/* 6 */       while (number != -999) {
/* 7 */          System.out.print("Enter a number, -999 to quit: ");
/* 8 */          number = UserInputReader.readFloat();
/* 9 */          if (number != -999) {sum += number; count++; }
/* 10 */      }
/* 11 */      if (count>0) System.out.println("The mean is " + sum / count);
/* 12 */   }
/* 13 */}

(The variable count is a stepper and number is a most-recent holder.)

Icon for the role Follower

A follower always gets the old value of another known variable or attribute as its new value. The example program asks the user for twelve integers and finally tells the biggest difference between the two successive input integers. The variable previous is a follower: it follows the variable current (on line 9).

/*  1 */ public class BiggestDifference {
/*  2 */   
/*  3 */    public static void main(String[] args) {  
/*  4 */       int month, current, previous, biggestDiff;
/*  5 */       System.out.print("Enter the 1. value: "); previous = UserInputReader.readInt();
/*  6 */       System.out.print("Enter the 2. value: "); current = UserInputReader.readInt();
/*  7 */       biggestDiff = current - previous;
/*  8 */       for (month = 3; month <= 12; month++) {
/*  9 */          previous = current;
/* 10 */          System.out.print("Enter the " + month + ". value: ");
/* 11 */          current = UserInputReader.readInt();
/* 12 */          if (current - previous > biggestDiff)
/* 13 */             biggestDiff = current - previous;
/* 14 */       }
/* 15 */       System.out.println("The biggest difference was " + biggestDiff);
/* 16 */    }  
/* 17 */ }

(The variable month is a stepper, current is a most-recent holder and biggestDiff is a most-wanted holder.)

Followers are used a lot with linked data structures to point the data element previous to the one in process.

Icon for the role One-way flag

A one-way flag has two possible values but cannot get its original value anymore after it has been once changed. The example class finds out whether any of the temperatures included in a statistics is negative. The one-way flag hasBeenBelowZero watches (on line 15) whether there appears any negative numbers among the inputs and if at least one negative value is found, the variable will never return to value false.

/*  1 */ import java.util.Vector;
/*  2 */
/*  3 */ public class TemperatureStatistics2 {
/*  4 */
/*  5 */    Vector temperatures;
/*  6 */    boolean hasBeenBelowZero;
/*  7 */
/*  8 */    public TemperatureStatistics2 () {
/*  9 */       temperatures = new Vector();
/* 10 */       hasBeenBelowZero = false;
/* 11 */    } 
/* 12 */
/* 13 */    public void addData (int temperature) {
/* 14 */       temperatures.add(new Integer(temperature));
/* 15 */       if (temperature < 0) hasBeenBelowZero = true;
/* 16 */    } 
/* 17 */}

(The attribute temperatures is a container.)

A one-way flag can also be used, for example, to watch errors occuring in input data so that the program could ask the inputs again.

Icon for the role Temporary

The value of a temporary is always needed only for a very short period. The example program models an item with a fixed tax-free price but whose tax rate may be changed. The tax is computed into the temporary tax (on line 11) which used only on the following two lines immediately after the value has been computed.

/*  1 */ public class Item {
/*  2 */ 
/*  3 */    float taxfreePrice, priceWithTax;
/*  4 */
/*  5 */    public Item (float price) { 
/*  6 */        taxfreePrice = price;
/*  7 */    }
/*  8 */
/*  9 */    public float setTax (float taxRate) { 
/* 10 */       float tax;
/* 11 */       tax = taxRate * taxfreePrice / 100;
/* 12 */       priceWithTax = taxfreePrice + tax;
/* 13 */       return tax;
/* 14 */    }
/* 15 */ }

(The attribute taxfreePrice is a fixed value and priceWithTax is a most-recent holder.)

A temporary is typically used for efficiency reasons (e.g., executing a computation, whose value is needed in several places, only once) or to clarify the program (e.g., computing the result in a variable even though the use of a variable would not be absolutely necessary). A temporary is also often used for swapping two data elements of an organizer.

Icon for the role Organizer

An organizer is a data structure which is used for reorganizing its elements after initialization. The example program asks the user ten characters one by one, assigns them into the organizer word that is implemented as an array, reverses the order of the characters and finally outputs the characters in this reversed order.

/*  1 */ public class Reverse {
/*  2 */    
/*  3 */    public static void main(String[] args) {
/*  4 */       char[] word = new char[10];
/*  5 */       char tmp; int i; 
/*  6 */       System.out.print("Enter ten letters: ");
/*  7 */       for (i = 0; i < 10; i++) word[i] = UserInputReader.readChar();
/*  8 */       for (i = 0; i < 5; i++) {
/*  9 */          tmp = word[i];
/* 10 */          word[i] = word[9-i];
/* 11 */          word[9-i] = tmp;
/* 12 */       }
/* 13 */       for (i = 0; i < 10; i++) System.out.print(word[i]);
/* 14 */       System.out.println();
/* 15 */    }
/* 16 */ }

(The variable tmp is a temporary and i is a stepper.)

An organizer can be used for sorting or for some other reorganization.

Icon for the role Container

Container is a data structure where elements can be added and removed. The example program implements a stack where the element that will be removed is the most recently added element within the stack. Objects of this class (e.g., the attribute myStack on line 28) are containers: elements can be added (with the method add on lines 8-15) and removed (with the method remove on lines 17-24).

/*  1 */ public class Stack {
/*  2 */ 
/*  3 */    private int contents;
/*  4 */    private Stack next; 
/*  5 */ 
/*  6 */    public Stack() { this.next = null; } 
/*  7 */ 
/*  8 */    public void add (int element) {
/*  9 */       Stack node;
/* 10 */        
/* 11 */       node = new Stack(); 
/* 12 */       node.contents = element;
/* 13 */       node.next = this.next;
/* 14 */       this.next = node; 
/* 15 */    }
/* 16 */ 
/* 17 */    public int remove () {
/* 18 */       int removed = 0;
/* 19 */       if (this.next != null) { 
/* 20 */          removed = this.next.contents;
/* 21 */          this.next = this.next.next;
/* 22 */       }
/* 23 */       return removed;
/* 24 */    }
/* 25 */ }
/* 26 */ 
/* 27 */ class StackTest {
/* 28 */    Stack myStack = new Stack();
/* 29 */    ...
/* 30 */ }

(The attribute contents is a fixed value, next is a most-recent holder in the object myStack and a fixed value in other objects, the variable node is a temporary and removed is a temporary.)

Icon for the role Walker

Walker traverses a data structure. The example program extends the class Stack above with the method size which returns the number of elements in the stack. The variable p is a walker: it traverses the whole stack (on line 16).

/*  1 */ public class Stack {
/*  2 */ 
/*  3 */    private int contents;
/*  4 */    private Stack next; 
/*  5 */ 
/*  6 */    public Stack() { this.next = null; } 
/*  7 */    public void add (int element) { ... }
/*  8 */    public int remove () { ... }
/*  9 */
/* 10 */    public int size () {
/* 11 */       int size = 0;
/* 12 */       Stack p = this.next;
/* 13 */
/* 14 */       while (p != null) {
/* 15 */          size++;
/* 16 */          p = p.next;
/* 17 */       }
/* 18 */       return size;
/* 19 */    }
/* 20 */
/* 21 */ }

(The variable size is a stepper.)


Last update: 24.1.2006

saja.fi@gmail.com
Marja.Kuittinen@UEF.Fi
Petri.Gerdt@UEF.Fi