CSCI 201 Lab 11 -- Arrays

Getting ready

If a directory called C:\files\lab11 presently exists on your computer, delete it.

Download a self-extracting archive of the Lab 11 project and store it in the C:\files directory. In Windows Explorer, double-click on the file and extract the project folder to the c:\files directory. This should create a C:\files\lab11 directory with two subdirectories, ArrayOp and ArrayMod

Operating on Arrays

Open, compile, and run the workspace c:\files\lab11\ArrayOp\ArrayOp.jcw. Notice that there is ASCII output generated from both MyClass1 and MyClass2. Now, let's look at the code for MyClass1 because you will have to modify this class as well as use it as a template for creating other similar classes.


package edu.unca.cs.csci201.ArrayOp ;

public class MyClass1 implements SpecArrayOp {
	public int TestFunc(int[] V) {
		int N = V.length ;
		return V[N/2] ;
	}
}
The first line includes MyClass1 in the package edu.unca.cs.csci201.ArrayOp so that it can use (and be used by) other classes defined in that package. In the next line, MyClass1 is declared to be a class that implements the SpecArrayOp interface.

As you see, MyClass1 consists of just one method called TestFunc() (the method specified in the interface). TestFunc() has one parameter, V, that is an array of integers, and TestFunc()'s purpose is to return an integer value. Right now, TestFunc() returns the middle element of the array that it receives as input. (Can you see why?)

In short, the ArrayOp program works as follows. The main() method, located in the class TestArrayOp creates an array (look at line 36 of that file) and calls the TestFunc() method in each class that implements the SpecArrayOp interface. In each call to TestFunc(), main() passes TestFunc() the array created on line 36. The main() method then outputs to the monitor the value returned by TestFunc(), as you observed when you first ran the program. Both the call to TestFunc() and the call to System.out.println occur in the for-loop that begins on line 41 of main()

Your Task

You are to modify the program as described in the table below. Remember the value returned by TestFunc() is output so that you can check your work.
Class Name  TestFunc() Return Value 
MyClass1 Adds all elements of the array and returns the total 
MyClass2 Returns the largest element of the array 
MyClass3 Returns the smallest element of the array 
MyClass4 Adds all the even elements of the array and returns the total 
MyClass5 Returns an integer average of all elements in the array 
MyClass6 Adds the smallest and largest elements of the array and returns their sum 

Once you have modified MyClass1 and MyClass2 to return the values listed in the table above, you will need to create new classes to produce the additional return values. Here is how to create a new class in your ArrayOp project. Call the first new class MyClass3:

Repeat the procedure stated above for MyClass4 through MyClass6.

Lab check-off I

When you are done, show your lab instructor the output of your program.

Modifying Arrays

Open, compile, and run the workspace c:\files\lab11\ArrayOp\ArrayMod. As in the earlier project, notice that there is ASCII output generated from both MyClass1 and MyClass2. Now, look at the code in the file MyClass1.java. The TestFunc() method still receives an array of integers as input, but it has no return value. In the ArrayMod project, TestFunc() actually modifies the values stored in the array that it receives as input. (Why is this possible?) The modified array is then output to the monitor by the for-loop on line 47 of the main() method located in TestArrayMod.

You job is to modify the program as described in the table below. Remember the modified array is output so that you can check your work.
Class Name  Operation Performed in TestFunc() 
MyClass1 Adds one to each element of the array 
MyClass2 Multiplies each element by three 
MyClass3 Sets all array elements to zero 
MyClass4 Sets the i'th array element to i
MyClass5 Reverses the array, that is, switches the first and last elements, etc. 

Once you have modified MyClass1 and MyClass2, you will have to create new classes as described in the section above to complete this part of the lab.

Lab check-off II

When you are done, show your lab instructor the output of your program.