Name___________________
CSE148 Object Oriented Programming
Quiz 02
• Design an interface called “MyIntArrayInterface” which has the following method declared:
getDifference () : int
• Design a class “MyIntArray”, implement the following interfaces: (1) MyIntArrayInterface; (2) Comparable; (3) Cloneable.
Data members:
-data: int [] /* array of integer numbers, default size is 16 */
Methods:
// constructors
+MyIntArray () : /* default constructor: (1) generate data array with default size which is 16; (2) randomly initialize this array with numbers in range [-100, 100] */
+MyIntArray (size:int )
/* (1) generate data array with size ‘size’; (2) randomly initialize this array with numbers in range [-100, 100] */
// methods
+getAverage():double /* return average value of data array elements */
+getMaximum():double /* return the maximum element in data array */
+getMinimum():double /* return the minimum element in data array */
// override equals(), toString()
+equals(o:Object):Object
/* return true if 2 instances have the same array (same size, and same elements at same location */
+toString():String /* return instance information, which includes (1) the size of “data” array; (2) the value of maximum element and minimum element in the array */
// implements MyArrayInterface interface
+getDifference(): int /* find the maximum number and minimum number in data array, return difference between these 2 numbers */
// implements Comparable interface
+compareTo(other:MyIntArray) int; /* compare “this” object with “other” object by comparing average values of data array in these 2 instances. Return 1 (greater than), 0 (equal to), -1 (less than) to indicate different results of comparison. */
// Implements Cloneable interface
+clone():Object /* override clone() method to perform Deep-Copy */
• Test
• Create an ArrayList of type MyDataArray, name it “myDataCenter”.
• Create 3 MyIntArray instances with different size of array, add them into “myDataCenter”.
• Create the 4th MyIntArray instances which is cloned from the 2nd MyIntArray instances; add it to “myDataCenter”.
• Visit all elements in myDataCenter to display element (MyIntArray instance) information.
• Sort myDataCenter.
• Visit all elements in myDataCenter again to display elements information.