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 C:\Files, on Windows, or the directory csci/201, on Linux.
To extract the projects, type the following commands in the windows labs.
mkdir C:\Files\Lab11 cd C:\Files\Lab11 mkdir ArrayOp cd ArrayOp C:\j2sdk1.4.0_01\bin\jar xfv C:\Files\ArrayOp.jar cd .. mkdir ArrayMod cd ArrayMod C:\j2sdk1.4.0_01\bin\jar xfv C:\Files\ArrayMod.jar
And the following commands in the Linux labs.
mkdir -p ~/csci/201/Lab11 cd ~/csci/201/Lab11 mkdir ArrayOp cd ArrayOp jar xfv ~/csci/201/ArrayOp.jar cd .. mkdir ArrayMod cd ArrayMod jar xfv ~/csci/201/ArrayMod.jar
You might pause and give some thoughts to what you just did. If you understand it, you're becoming an accomplished user of the command processor and are on your way to a high paying career as a system administrator. If you don't understand it, you might ask your lab instructor some questions. By the way the "cd .." means to connect to the parent directory.
Open the jGRASP project file stored in Lab11/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 specification, just like those
classes you wrotes for Lab 10.
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.