|
|
The "setIO" code presented here refers back to code introduced in the previous lecture.
1. OVERVIEW |
A constrained array is an array where the index is specified (and hence the number of components is specified), we say that the bounds are static, hence constrained arrays are sometimes referred to as static arrays. So far we have only looked at such constrained arrays. Most programming languages (including Java) support the concept of unconstrained arrays. When an unconstrained array is declared the number of elements is not supplied untill run time --- typically by user input. In a previous example we considered a class SetIO which included methods to allow us to input the values making up a set of integers (ensuring that duplicates were not accepted) and output those values. However, because a set was represented as an 8 element static array, the class could only be used in relation to sets comprising 8 elements. The class and the methods within it would be much more useful if it operated with respect to sets (arrays) of any size from 0 (the empty set) up to some maximum. This would make the class much more general (generic) and therefore much more useful. |
To do this we must amend the code so that the upper bound constant becomes a variable so that a value can be supplied (using the constructor) for this data item at run time. The proposed changes are presented in Table 1. Code for an application test class to exercise the revised SetIO class is presented in Table 2. Consider the code fragment given in Table 1. This is the start of the implementation for the SetIO class but this time we have included a constructor which allows the user to "load" values into sets of any size (in the previous version a default constructor was used). In this case, although the lower bound will remain constant, the set length will vary for each instance of SetIO thus it cannot be a (static) class field. Similarly it cannot be a (final) constant field because we will not know the value till run time. We also need to make an adjustment to the SetIOtestApp class with which we tested the SetIO class as shown in Table 2. Here we have included some lines of code to allow the user the opportunity to input an appropriate upper bound. Note that the constant ARRAY_LENGTHarrayLength. |
// SET IO, VERSION 2 // Frans Coenen // 7 July 1999 // Revised: Tuesday 30 august 2005 // Dept Computer Science, University of Liverpool import java.util.*; class SetIOver2 { // ------------------ FIELDS ------------------------ private static Scanner keyboardInput = new Scanner(System.in); private static final int LOWER_BOUND = 0; private int arrayLength; private int[] integerSet; // ------------------ CONSTRUCTOR ------------------------ public SetIOver2(int numElements) { arrayLength = numElements; integerSet = new int[arrayLength]; } // ------------------ METHODS ------------------------< AND SO ON AS BEFORE EXCEPT THAT THE ARRAY_LENGTH CONSTANT IS NOW A VARIABLE> |
Table 1: Version 2 of SetIO class with constructor to allow definition of set of any size
// SET IO VERSDION 2 TEST APPLICATION // Frans Coenen // 7 July 1999 // Revised: Tuesday 30 august 2005 // Dept Computer Science, University of Liverpool import java.util.*; class SetIOver2testApp{ // ------------------- FIELDS ------------------------ // Create Scanner class instance private static Scanner keyboardInput = new Scanner(System.in); // ------------------ METHODS ------------------------ /* Main method */ public static void main(String[] args) { // Input number of elements in set and then create instance of // set IO class. System.out.println("Input number of elements in set"); int setSize = keyboardInput.nextInt(); SetIOver2 newSet = new SetIOver2(setSize); // Input for set System.out.println("Input values for set."); newSet.inputSet(); // Output values for set newSet.outputSet(); } } |
Table 2: Version 2 of SetIOver2 test application class
2. THE THIS OBJECT |
We have seen that we call an instance method by linking it (using the '.' operator) to a particular instance of the class to which the method belongs: myInstance.myMethod() In this manner the method has access to all the instance fields peculiar to the "calling instance". In addition the method may have some arguments (formal parameters) associated with it. In some cases these arguments are instances. Let us assume we have a method, lets call it myMethod, which is an instance method of the class myClass. To use this method we would create an instance of the class, (say) myInstance, and then call the method in the normal way as shown above. Now, suppose that embedded within this method there is a call to another method, anotherMethod, that requires as its argument the current instance: public void myMethod() { anotherMethod (< The Current Instance >); } |
How does Java know what this instance is? The answer is that we have to tell it, but how? One option is to "hard code" the desired instance name into the method: public void myMethod() { anotherMethod(myInstance); } But what if we wish to call myMethod with a different instance in which case we would want the anotherMethod to be also called with this alternative instance and not the one that we have "hard coded" in. To do this we must use the keyword this which refers to the current object. Thus: public void myMethod() { anotherMethod(this); } Consequently we often refer to the this keyword as the this object. In the following unconstrained array handling example problem we will make use of the this keyword to pass the current object/instance. |
Created and maintained by Frans Coenen. Last updated 10 February 2015