So my main program creates a random number generator and generates 6 small positive random numbers and stores them in an array object of type int named vals. I then create a runner class that has this array object passed to it. The main then prints off all of the values stored in the array and calls on the method runA to do the same. I should be using an array list that has added the array vals to it and return the value in variable named ref of type collection. I then call method runB that will print out the numbers from the array in ascending order with no repeating values by using a TreeSet. I am using the .addAll interface to add the values of the array named vals to both the TreeSet and the ArrayList. I believe I am using both the TreeSet and ArrayList correctly but I am having trouble passing the values of the array to the TreeSet and ArrayList.
import java.util.*;
class Proj01 {
public static void main(String[] args) {
//Create a pseudo-random number generator
Random generator = null;
if (args.length != 0) {
generator = new Random(Long.parseLong(args[0]));
} else {
generator = new Random(new Date().getTime());
};
//Generate some small positive random numbers
// and store them in an array object of type int.
int[] vals = {
Math.abs(generator.nextInt()) % 5,
Math.abs(generator.nextInt()) % 5,
Math.abs(generator.nextInt()) % 5,
Math.abs(generator.nextInt()) % 5,
Math.abs(generator.nextInt()) % 5,
Math.abs(generator.nextInt()) % 5};
System.out.println();//blank line
Proj01Runner obj = new Proj01Runner(vals);
for (int cnt = 0; cnt < vals.length; cnt++) {
System.out.print(vals[cnt] + " ");
}
System.out.println();
Collection ref = obj.runA();
//Display the return values from the runA method
Iterator iter = ref.iterator();
while (iter.hasNext()) {
System.out.print(iter.next() + " ");
System.out.println();
ref = obj.runB();
int sum = 0;
iter = ref.iterator();
while (iter.hasNext()) {
Integer myObj = (Integer) (iter.next());
System.out.print(myObj + " ");
sum += myObj.intValue();
}//end while loop
System.out.println("\nSum = " + sum);
System.out.println();//blank line
}//end main
}//end class Proj01
}//End program specifications.
import java.util.*;
class Proj01Runner {
Proj01Runner(int[] vals) {
this.vals = vals;
System.out.println(
"I certify that this program is my own work\n" +
"and is not the work of others. I agree not\n" +
"to share my solution with others.");
System.out.println("name_here");
}
public Collection runA() {
Collection ref = new ArrayList();
ref.addAll(vals);
return ref;
}
public Collection runB() {
Collection ref = new TreeSet();
ref.addAll(vals);
return ref;
}
}