1. Create the class ArrayTools.
2. Place the following functons in the ArrayTools class. These functions
will be useful for general arrays. Test these functions
in the NumberApp class.
a. Write the method printArray(Object[] array) that prints the elements
of an array where each item is seperated by commas.
ex. Consider the following code in the NumberApp class.
int N = ;
MyNumber[] nums = new Binary[N];
for (int i = 0; i < N; i++)
nums[i] = new Binary(i);
ArrayTools.printArray(nums);
// preconditon: array != null
// postcondition: prints the elements seperated by commas
// ex. String [] letters = {"a,"b","c"};
// printArray(letters) prints a,b,c
public static void printArray(Object[] array){
< YOUR CODE GOES HERE >
}
b. Can this function take an int, double, of boolean array?
If not, create the function or functions to print these primitive arrays.
c. It is important to note that arrays are mutable, if you pass an
array to a function, the function can change the state of the array.
// preconditon: data != null
// postcondition: randomly shuffles the data in the array
// ex. shuffle({"a","b"}) changes the array to either {"a","b"}
// or {"b","a"} with equal probability.
public static void shuffle(Object [] data){
< YOUR CODE GOES HERE >
}
Why must the shuffle method of a String return a value?
public static String shuffle(String s){ }
Def. A function produces a side effect if it changes the state of
its arguments or modifies the state of the program, i.e.
System.out.print().
ex. printArray() and shuffle(Object []) produce side effects
Def. A mathematical function takes an arugment, produces no side effects
and returns a value.
ex. shuffle(String) is a mathematical function.
d.Write the function badShuffle(Object [] array) that similar to shuffle
however chooses an random index from 0 to N on each iteration to perform
a swap. Why is this a bad shuffle?
e. Write the function sample(Object [] array, int m) that returns
a random sample without replacement of m items from array. Make
this function a mathematical function (NO SIDE EFFECTS).
// pre: array != null , 0 <= m < array.length
// post: returns a random sample of size m without replacement
f. Write the function linearSearch(Object [] data, Object key) that
returns -1 if key is not in data or the index position of the first
occurrence of key in data (read the data from index 0 to data.length -1).
// post condition: returns the first occurrence of key in data
public static int linearSearch(Object [] data, Object key){
< YOUR CODE GOES HERE >
}
g. Test your linearSearch method with different Objects.
Try Strings, Integers, Binarys.
h. Your linearSearch method should not work correctly for MyNumbers.
Why?