JavaGently Chapter 6 Solutions


Chapter 6, Question 8
 
Useful methods. Write a class with methods which take an array of integers as one of their parameters and do the following:
 
find the maximum of all the elements;
determine whether all the quantities in the array are equal;
determine the number of times a value greater than a given level occurs.


We would also like to add the following facilities:

 
find the maximum of all the elements, plus all the positions where it occurs;
determine the range of values spanned by the array.


Discuss why this may be more difficult and propose a class-array design that might be able to provide for the methods.


Solution

 
We declare a constant array as follows:
 
final static int[] data={10,9,8,7,6,5,4,3,2,1};
Finding the maximum is easy if you remember the special case on the first iteration:
 
static int maximum(int[] data) {
        
        int max=0;
      
        for (int i=0; i < data.length; i++)
             if (data[i]>max | i==0)
                 max=data[i];
                    
        return max;
}
We compare adjacent entries to determine whether all entries are equal (take note of the bounds of the loop!)
 
static boolean equal(int[] data) {
      
        for (int i=1; i < data.length; i++)
                     
                if (data[i]!=data[i-1])
                 return false;
                   
        return true;            

}
Counting the elements above a certain value is also very easy:
 
static int elementsAbove(int[] data, int level) {
       
        int count=0;

    for (int i=0; i < data.length; i++)
             if (data[i]>level)
                      count++;
                        
        return count;           

}
Now comes the more difficult bit: We are asked to return more than one value. This is not possible unless we define our own class or we return the values in an array.

Since we already have a method to find the maximum of the array, we pass this as parameter to occurancesOfElement to count the number of elements:
 

static Vector occurancesOfElement(int[] data, int max) {
        Vector result=new Vector();
     
        for (int i=0; i < data.length; i++)
             if (data[i]==max)
                       result.addElement(new Integer(i));
                      
        return result;
  
}
We now try our hand at returning multiple values in an array. We declare the constants
 
final static int MAX=0;
final static int MIN=1;
so that we can refer to the values in the array to be return symbolically (I like to do things this way because it makes the code more readable).

As you will have guessed, the upper limit is kept at index 0 and the lower at 1. The code for range is given below:
 

static int[] range(int[] data) {
        
        int[] result=new int[2];
        
        result[MAX]=result[MIN]=data[0];
        
        for (int i=0; i < data.length; i++) {
           if (data[i]>result[MAX])
                        result[MAX]=data[i];
            if (data[i]                     result[MIN]=data[i];
    }
       
        return result;
          
}
 



[CoolArray.java
Please mail any corrections or suggestions to Graeme