CSC211 LAB 8
- Read the section on Arrays in the lecture notes for this week. Make sure you understand before you continue.
- Edit, Compile and Run the following program. You don't have to type the comments make sure you read them.
class ArrayEx1 {
public static void main(String [] args) {
//declaring a reference variable to an integer array
int [] data;
//creating an array object of length 5. returning location
// of array to variable data.
// This of this a creating 5 named buckets,
// where each bucket is assigned an integer and the integers start at 0.
data = new int[5];
// assigning random integers to cells of the array
// index to the array starts at 0
data[0] = (int)(Math.random() * 100);
data[1] = (int)(Math.random() * 100);
data[2] = (int)(Math.random() * 100);
data[3] = (int)(Math.random() * 100);
data[4] = (int)(Math.random() * 100);
// Print out the array.
for(int i = 0; i < data.length; i++)
{
System.out.print(data[i] + " " );
}
System.out.println(); // blank line
}
For the following, add the statements to ArrayEx1 that you typed in above.
- Write the statement to output the first value in the array.
- Write the statement to decrement the last value in the array by one and output that value. The following piece of code shows you how to increment the first value by one. Add it to the above code and output the first element
++data[0];
The following piece of code shows you how to swap the second and last values in the array. Add it to the above code.
// need a temp variable
int temporaryHoldingCell;
// assign to the temp the second value in the array
// remember the index starts at 0.
temporaryHoldingCell = data[1];
// assign the second cell of the array the last element
data[1] = data[data.length - 1];
// assign the temp to the last
data[data.length - 1] = temporaryHoldingCell;
Write the code to output all the values of the array after the swap.
Write the code to swap the first and second values, then output all the values again.
Write an if .. else statement to see if 7 is the first value in the list.
Make sure you understand all the code before you continue.
Edit, Compile and Run the following piece of code.
class ArrayEx2 {
public static void main(String [] args) {
// using initializer list to create
// and initialize the array.
int [] data = {5,7,1,9,20};
/*
* data[0] contains 5
* data[1] contains 7
* data[2] contains 1
* data[3] contains 9
* data[4] contains 20
*
*/
printArray(data);
}
public static void printArray(int [] data){
for(int i = 0; i < data.length; i++)
{
System.out.print(data[i] + " ");
}
System.out.println();
}
}
The following piece of code will search the array for the value 9 and print out wether 9 is in the array or not. Add it to the above code.
boolean isNine = false;
for(int j = 0; j < data.length; j++)
{
if( data[j] == 9 )
{
isNine = true;
}
}
if (isNine)
System.out.println("9 is in the list ");
else
System.out.println("9 is not in the list");
Write the code that will replace all occurrences of the value 1 with 10.
Print out the array. Just call the function.
Study the print function. Write a method that will take in an array and return the average of the array.
Add another method that will take in an array and return the minimum value in the array. You can assume the array has at least one value in it.