60 likes | 208 Views
Class ArrayTest contains main() method declare array a[5] declare array b[5] initialize both array with data invoke method addArray () collect the array return by this method print the returned array by invoking printArray () method. Class ArrayMethod addArray(array 1, array 2)
E N D
Class ArrayTest contains main() method declare array a[5] declare array b[5] initialize both array with data invoke method addArray() collect the array return by this method print the returned array by invoking printArray() method Class ArrayMethod addArray(array 1, array 2) adds array 1 and array 2 returns an array printArray(array) prints array returns nothing Programming Problem: Passing and Returning Array to/from a Method Write Java code to define the class “ClassArrayMethod”. Then use the methods of this class in the driver class “ClassArrayTest” according to the specifications given below:
Difference Between Class Variables and Instance Variables Source: Ivor Horton, Beginning Java™ 2, JDK™ 5 Edition, Wiley Publishing, Inc.
Controlling Access to Class Members Source: Ivor Horton, Beginning Java™ 2, JDK™ 5 Edition, Wiley Publishing, Inc.
Controlling Access to Class Members Source: Ivor Horton, Beginning Java™ 2, JDK™ 5 Edition, Wiley Publishing, Inc.
Controlling Access to Class Members Source: Ivor Horton, Beginning Java™ 2, JDK™ 5 Edition, Wiley Publishing, Inc.
publicclassObjectData { int [] data; publicObjectData(inti){ data = newint[i]; for (int j=0;j<i;j++) data[j]=j; }//end of constructor }//end of class data Example: Passing Object to a Method Returning Object from a Method publicclassObjectMethod { publicObjectDatasortData(ObjectData ob){ int [] a = newint[ob.data.length]; inti = 0; for(int j=(ob.data.length-1);j>=0;j--){ a[i]=ob.data[j]; i++; }// end of for ob.data = a; return ob; }//end of sortData }//end of class publicclassTestObjectMethod { publicstaticvoid main(String[] args) { ObjectData ob1, ob2; ObjectMethod ob3; ob3 = newObjectMethod(); ob1 = newObjectData(6); ob2 = ob3.sortData(ob1); for(inti=0;i<ob2.data.length;i++){ System.out.println(ob2.data[i]); }// end of for }//end of main() }//end of class