We'll use Java arrays in this lab.
In this lab you'll complete two different Java projects. We'll package them in two different jar files. Download both, ArrayOp.jar and ArrayMod.jar and save them into the directory csci/201.
To extract the projects, type the following commands:
cd csci/201 jar xfv ArrayOp.jar jar xfv ArrayMod.jar
Open the jGRASP project file stored in Lab12/ArrayOp/ArrayOp.gpj.
Then compile and run the application class ArrayOp
.
Now take a look at the internal class MyClass1
of ArrayOp
.
private static class MyClass1 implements SpecArrayOp { public int TestFunc(int[] V) { int N = V.length ; return V[N/2] ; } }
This class implements a interface, just like those
classes you wrotes for Lab 11.
As you see, MyClass1
consists of just one method
TestFunc
and
TestFunc
has a single parameter, V
,
an array of integers.
TestFunc
's purpose is to return an
integer value. Right now, it returns the middle
element of the array that it receives as input. (Can you see why?)
The ArrayOp
class works as follows. Its
main
method
calls the TestArrayOperation
method of
TestArrayOp
with a single variable, a class
which implements SpecArrayOp
.
TestArrayOperation
creates a array, passes it
to the TestFunc
method of its interface and
then prints the result.
You are to modify the main
method of ArrayOp
so that it tests six different classes.
These six classes have a TestFunc
methods which implements
the six array operations shown below.
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 |
When you are done, show your lab instructor the output of your program.
Now open, compile, and run the ArrayMod project.
You should notice some fairly strong similarities between
the ArrayOp
and ArrayMod
classes.
ArrayMod
has six internal classes which are
supposed to modify the elements of an array.
Your job is to complete those six internal classes.
This table gives the details of your assigned tasks.
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. |
MyClass6 |
Relaxes the array, that is, sets each element to the average of its two surrounding neighbors, except for the first and last elements which are left unchanged. |
If the last Java class stresses you, ask your lab instructor about relaxation.
Again, when you are done, show your lab instructor the output of your program.