0

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;
    }
}
Community
  • 1
  • 1
Alex Brown
  • 35
  • 5
  • 1
    Why not use ``generator(5)`` to create your numbers? You don't need to use Math.abs(). – NomadMaker Jun 02 '21 at 00:33
  • The first section of code was given to me for the assignment. I am not allowed to change it, the second part is what I am responsible for. – Alex Brown Jun 02 '21 at 00:51
  • If this a schoolwork assignment, you should say do explicitly in your question. – Basil Bourque Jun 02 '21 at 03:23
  • Should I repost with that clarification? Sorry its only my second time posting. – Alex Brown Jun 02 '21 at 03:28
  • 1
    Are you aware that you just violated your disclaimer “*I agree not to share my solution with others.*”? – Holger Jun 02 '21 at 11:45
  • 3
    Regarding your problem, you’re referring to a field `vals` with `this.vals = vals;` and within the other methods that has not been declared anywhere. Add a field declaration. Then, to add the values of an `int[]` array to a Collection you’d need a loop, e.g. `for(int i: vals) ref.add(i);`. But it’s possible that your teacher does not understand that code, as apparently they stopped keeping up with technology more than 15 years ago. To write it in a way matching their template, you’d have to use `for(int index = 0; index < vals.length; index++) ref.add(Integer.valueOf(vals[index]));` – Holger Jun 02 '21 at 12:00

0 Answers0