// // Description: // // NamedArray defines an "integer array" class in which each // object contains an integer array and a (string) name. // Extra functionality is provided; see individual method // definitions. // // Usage: // // See method definitions for usage of individual methods. // // main() tests the other methods: // "java NamedArray aName N a1 ... aN" creates two NamedArrays // aa, bb (one using each constructor method) with // name aName, length N, and elements a1, ... aN and // writes the following to system output: // aa.getName() // aa.getLength() // aa.getElement(i), for i = 0 ... aa.getLength()-1 // aa.sumArray() // aa.productArray() // bb.getName() // bb.getLength() // bb.getElement(i), for i = 0 ... bb.getLength()-1 // It is assumed that input arguments are valid (a1 ... aN are // integers). // public class NamedArray { private String name ; private int[] nums ; // ---- constructor methods ------------------------------------ public NamedArray(String s, int l) { name = s ; nums = new int[l] ; } public NamedArray(String s, int[] a) { name = s ; nums = new int[a.length] ; for (int i = 0 ; i < a.length ; i++) { nums[i] = a[i] ; } } // ---- methods to set, retrieve elements ---------------------- // set element i to n public void setElement(int i, int n) { nums[i] = n ; } // return value of element i public int getElement(int i) { return nums[i] ; } // return length public int getLength() { return nums.length ; } // return name public String getName() { return name ; } // ---- other methods ------------------------------------------ // return sum of array elements public int sumArray() { int sum = 0 ; for (int i = 0 ; i < nums.length ; i++) { sum += nums[i] ; } return sum ; } // return product of array elements public int productArray() { int product = 1 ; for (int i = 0 ; i < nums.length ; i++) { product *= nums[i] ; } return product ; } // ---- main method -------------------------------------------- public static void main(String[] args) { NamedArray aa ; NamedArray bb ; // get name ; String name = args[0] ; // get length of array int l = Integer.parseInt(args[1]) ; // get elements int[] elems = new int[l] ; for (int i = 0 ; i < l ; i++) { elems[i] = Integer.parseInt(args[2+i]) ; } // construct NamedArray with first constructor aa = new NamedArray(name, l) ; for (int i = 0 ; i < l ; i++) { aa.setElement(i, elems[i]) ; // note that if we had not needed elems for // the second test, we could have used this: // aa.setElement(Integer.parseInt(args[2+i])) ; } // construct NamedArray with second constructor bb = new NamedArray(name, elems) ; // test methods on aa System.out.println("Constructed with first method:") ; System.out.println("Array name: " + aa.getName()) ; System.out.println("Array length: " + aa.getLength()) ; System.out.print("Array elements: ") ; for (int i = 0 ; i < aa.getLength() ; i++) System.out.print(aa.getElement(i) + " ") ; System.out.println("") ; System.out.println("Sum of elements: " + aa.sumArray()) ; System.out.println("Product of elements: " + aa.productArray()) ; // test methods on bb System.out.println("Constructed with second method:") ; System.out.println("Array name: " + bb.getName()) ; System.out.println("Array length: " + bb.getLength()) ; System.out.print("Array elements: ") ; for (int i = 0 ; i < bb.getLength() ; i++) System.out.print(bb.getElement(i) + " ") ; System.out.println("") ; } }