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 ten 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 */}

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 Transformation

A transformation gets its new value always with the same calculation from value(s) of other variable(s) and attribute(s). The example program asks the user for the capital and calculates both the interest and the total capital for ten years. The variable interest is a transformation and it is calculated (on line 8) using the capital.

/* 1 */ public class Growth {
/* 2 */    public static void main(String[] args) {  
/* 3 */       float capital, interest;
/* 4 */       int i;  
/* 5 */       System.out.print("Enter capital (positive or negative): ");
/* 6 */       capital = UserInputReader.readFloat();
/* 7 */       for (i = 1; i <=10; i++) {
/* 8 */          interest = 0.05F * capital;
/* 9 */          capital += interest;
/* 10 */         System.out.println("After " + i + " years interest is " 
/* 11 */                 + interest + " and total capital is " + capital);
/* 12 */      }
/* 13 */   }
/* 14 */}

(The variable capital is a gatherer and i is a counter.)

A transformation can be used for computing some result based on other items or coding the kind or type of an item.

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 */}

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 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 Temporary

A temporary is always needed only for a very short period. The example program outputs first the bigger number and then the smaller number out of the two numbers given as inputs. The values of the variables are changed with each other by using a temporary variable tmp (on lines 8-10), whose value is later meaningless (no matter how long the program would run).

/*  1 */ public class Swap {
/*  2 */   
/*  3 */    public static void main(String[] args) { 
/*  4 */       int number1, number2, tmp;
/*  5 */       System.out.print("Enter a number: ");  number1 = UserInputReader.readInt();
/*  6 */       System.out.print("Enter another number: ");  number2 = UserInputReader.readInt();
/*  7 */       if (number1 < number2) {
/*  8 */          tmp = number1;
/*  9 */          number1 = number2;
/* 10 */          number2 = tmp;
/* 11 */       }
/* 12 */       System.out.println("The bigger of the numbers is " + number1 + " and the smaller is " 
/* 13 */                          + number2 + ".");
/* 14 */    }
/* 15 */ }

A temporary is often used for changing the places of two data elements of an organizer.

Icon for the role Organizer

An organizer is an array 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, 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 an array or for some other reorganization.


Last update: 15.5.2005

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