Below are three testing programs for array of integers, with increasing OOP flavor.

When developing your programs, try to follow the OOP conventions.

 

       Exercises: Define the following as methods.

a)      Add all the items in the array and return the sum.

b)      Reverse the values in the array.

 

----------------------------------------------------------------------

Approach 1: Quick and dirty, but not desirable

// This class is a test program for implementing an integer array

// Quick version

// command: javac IntArrayTest1.java

// command: java IntArrayTest1 -20

 

public class IntArrayTest1 {

   // attributes

                private final int SIZE = 10;

                private int A[] = {4, -20, 10, 5};

                //The default values are initialized when an IntArrayTest1 object is created.

                // not a good OOP technique

 

                private int currentPosition = 0; //the current index of the last array element

               

   // methods

                public int search (int key) {

                                for (int i=0; i< A.length; i++) {

                                                System.out.println (A[i]);

                                                if (A[i] == key) return i;

                                } //for

                                return -1; //not found

                } //search()

               

   // main( )

                public static void main (String args[] ) {

                                IntArrayTest1 ar = new IntArrayTest1();

 

                                //the value to search for, input from command line

                                int key = Integer.parseInt(args[0]);

 

                                int result = ar.search (key);

                                System.out.println ("result: " + result);

                                if (result == -1)

                                                System.out.println (key + " is not in the array.");

                                else

                                                System.out.println (key + " is stored at position " + result + ".");

                } //main()

 

} // IntArrayTest1 class

 

Approach 2: More OOP-like

// This class is a test program for implementing an integer array.

// A user-defined constructor, IntArrayTest2(), is used to assign data to A[].

// command: javac IntArrayTest2.java

// command: java IntArrayTest2 -14

 

public class IntArrayTest2 {

   // attributes

                private final int SIZE = 10;

                private int A[] = new int [SIZE]; // The array needs to be initialized

                private int currentPosition = 0;

               

   // methods

                // public void IntArrayTest2() //NOTE: A constructor should not have a return data type.

                public IntArrayTest2() {

                                //System.out.println("length: " + A.length);

                                for ( ; currentPosition < A.length; currentPosition++) {

                                                A[currentPosition] = 2*currentPosition + currentPosition - 20;

                                }

                                //return; // do not place a return here

                }

               

                public int search (int key) {

                                for (int i=0; i< A.length; i++) {

                                                System.out.println (A[i]);

                                                if (A[i] == key) return i;

                                } //for

                                return -1; //not found

                } //search()

               

   // main( )

                public static void main (String args[] ) {

                                IntArrayTest2 ar = new IntArrayTest2 ();

                                int key = Integer.parseInt(args[0]);

                                //the value to search for, input from command line

 

                                int result = ar.search (key);

                                System.out.println ("result: " + result);

                                if (result == -1)

                                                System.out.println (key + " is not in the array.");

                                else

                                                System.out.println (key + " is stored at position " + result + ".");

                } //main()

 

} // IntArrayTest2 class

 

Approach 3: even more OOP flavor

// This class is a test program for implementing an integer array.

// Data are added into the array by the caller using the add() method.

// command: javac IntArrayTest3.java

// command: java IntArrayTest3 -14

 

public class IntArrayTest3 {

   // attributes

                private final int SIZE = 10;

                private int A[] = new int [SIZE]; // The array needs to be initialized

                private int currentPosition = 0;

               

   // methods

                public int size () {

                                return SIZE;

                } //size

 

                public void add (int value) {

                                A[currentPosition++] = value;

                } //add()

 

                public int search (int key) {

                                for (int i=0; i< A.length; i++) {

                                                System.out.println (A[i]);

                                                if (A[i] == key) return i;

                                } //for

                                return -1; //not found

                } //search()

               

   // main( )

                public static void main (String args[] ) {

                                IntArrayTest3 ar = new IntArrayTest3 ();

 

                                for (int i=0 ; i < ar.size(); i++) {

                                                ar.add(2*i + i - 20);

                                }

 

                                int key = Integer.parseInt(args[0]);

                                //the value to search for, input from command line

 

                                int result = ar.search (key);

                                System.out.println ("result: " + result);

                                if (result == -1)

                                                System.out.println (key + " is not in the array.");

                                else

                                                System.out.println (key + " is stored at position " + result + ".");

                } //main()

 

} // IntArrayTest2 class