Java - Using the output of one method in the statement body of another
method is not producing the expected result
I have 2 classes, LotSelection and LotGen, in a package called
lotterynumberselector. LotSelection has 2 methods: LotPool() and
WinningSequence(). LotPool() is meant to return an ArrayList of 50
integers from 0 to 49 and scramble it. WinningSequence() is meant to
create a 6-element array containing the first 6 integers from the
ArrayList generated in LotPool().
This is the code for LotSelection.
package lotterynumberselector;
import java.util.ArrayList;
import java.util.Collections;
public class LotSelection {
ArrayList<Integer> LotPool() {
ArrayList<Integer> sequencedraw = new ArrayList<Integer>();
for(int i = 0; i < 49; i++) {
sequencedraw.add(i);
}
Collections.shuffle(sequencedraw);
return sequencedraw;
}
int[] WinningSequence() {
int[] WinningSequence = new int[6];
int j = 0;
while (j < 6) {
WinningSequence[j] = LotPool().get(j);
j++;
}
return WinningSequence;
}
}
The purpose of LotGen is to test if the outputs created by LotSelection
were doing their expected tasks. However, the output from
WinningSequence() didn't match the first six numbers created from
LotPool() and I'm wondering why. I'm not sure if it's because the code in
LotGen or LotSelection is creating an unexpected result. I suspect it's
because LotPool() is producing one 50-element ArrayList and
WinningSequence() is creating ANOTHER LotPool() so it's making it's array
from a DIFFERENT 50-element ArrayList, but I'm not sure.
Here is the code for LotGen:
package lotterynumberselector;
import java.util.ArrayList;
import java.util.Arrays;
public class LotGen {
public static void main(String [] args) {
LotSelection a = new LotSelection();
ArrayList<Integer> LotPool = new ArrayList<Integer>();
LotPool = a.LotPool();
System.out.println(LotPool);
int[] WinSeq = new int[6];
WinSeq = a.WinningSequence();
System.out.println(Arrays.toString(WinSeq));
}
}
No comments:
Post a Comment