| RHS => Allan => Basic Programming => Exercises => Arrays |
Updated:
02/02/2004 23:36 WebMaster: allanhn@rhs.dk |
Arrays
The Mission
Together we are going to make a program, which can perform several elementary
operations on integer arrays.
Question 1 Worker + application classes
You are to create a program which can initialize an integer array with random
numbers in the interval 0 - 1000 using the Math.random() method.
Type in the Arrays class, which must resemble the following template:
public class Arrays {
private int[] data = new int[10];
//initializing the array
public Arrays()
for (int i=0; i < data.length; i++) {
data[i] = (int)(Math.random()*1000);
}//for
}//Arrays()
public int getElement(int index) {
//fill in code to return an element from the array
//the index for the element is passed as parameter
}//getElement(int)public void setElement(int index, int value) {
//fill in the code to set the element at the position
//given by the first parameter (index) to the value
//given by the second parameter (value)
}//setElement(int, int)//printing indices and values of an array
public String toString() {
for (int i=0; i < data.length; i++) {
System.out.print(i + ": " + data[i] + "\n");
}//for
}//toString()
}//Arrays
Compile.
Now create the application class, ArraysApp with the main:
public static void main(String[] args) {
Arrays myArray = new Arrays();
System.out.println(myArray);
}
Question 2
Now add the following code in main() before the println statement:
myArray.setElement(5, 5);
myArray.setElement(6, 8);
myArray.setElement(7, myArray.getElement(5) + getElement(6));
myArray.setElement(8, myArray.getElement(7) + getElement(6));
Run the program and see the difference in the array content.
Question
3 Find maximum
To the Arrays class add a method, findMax(),
to find the maximum value in the array.
The method should resemble
the following template:
public int findMax() {
int max = data[0];
for (int i = 1; i < data.length; i++) {
if (data[i] > max) {
max = data[i];
}//if
}//for
return max;
}//findMax
In ArraysApp add the following statement to the
main() method:
System.out.println( "Maximum in myArray
is: " +
myArray.findMax() );
Question 4 Find minimum
Extend the Arrays class with a new method, findMin(),
which returns the minimum value in the array.
Also use this method in ArraysApp.
Question 5 Find sum
Find the sum of all numbers in the array by adding a method findSum().
Follow the style from question 4.
Question 6 Find average
Find the average of all numbers in the array by adding a method findAverage().
Follow the style from assignment 4.
Hint: Use the findSum() method!
Question 7 Optional
In Arrays declare and construct three other int arrays
named x, y and z.
x and y
must hold 10 values whereas the number of values that z must hold is to
be user defined; i.e. a value of
the length must be read in.
x must initalized the same way as we did above.
y must be initialized with the values 0, 1, 2, ..9 (i.e. y[i] = i)
z[i] must be initialized with the value y[i] * y[i] for all index
In ArraysApp change the println statement so it prints the data in all three
arrays: x, y, z
Save, Compile and run this program.
Question 8 Extra - only
for the fast ones!
Here we want to calculate some numbers called Fibonacci numbers and save them
in the array y.
Fibonacci numbers are defined as:
i y[i] formula
--- ------ --------
0 0 constant
1 1 constant
2 1 1 + 0
3 2 1 + 1
4 3 1 + 2
5 5 3 + 2
6 8 5 + 3
7 13 8 + 5
... ... ...
As you can see a Fibonacci number at a certain point
is the sum of the two previous numbers.
In Arrays add a method, findFibonacci(), where you use a loop to assign the
sum of two previous elements to the elements of y - remember that you must
manually initialise y[0] = 0 and y[1]
= 1 !!