diff --git a/TwoLiterStack/src/main/TwoLiter.java b/TwoLiterStack/src/main/TwoLiter.java index 9a6d0d0..e18e4fb 100644 --- a/TwoLiterStack/src/main/TwoLiter.java +++ b/TwoLiterStack/src/main/TwoLiter.java @@ -43,12 +43,20 @@ public TwoLiter(TwoLiter twoLiter) { * @param twoLiterStack The Stack * @param count The number of TwoLiter objects to add */ +/* +EDIT: +Random.nextInt(int n) generates a random number in the range 0(inclusive) to n(exclusive). +We want to generate index for arrays flavors and UPCs randomly. So we need indexes from +0 to flavors.length-1 (both inclusive). So for upper bound we will have to add 1 to the upper bound +and the pass this to nextInt() method. So, correct call should be nextInt(flavors.length), then only it +will generate number from 0 to flavors.length-1 (both inclusive). +*/ public static void add(Stack twoLiterStack, int count ) { // Random r = new Random(42); Random r = new Random(); for (int i = 0; i < count; i++) { - twoLiterStack.add(new TwoLiter(UPCs[r.nextInt(UPCs.length - 1)], - flavors[r.nextInt(flavors.length - 1)], + twoLiterStack.add(new TwoLiter(UPCs[r.nextInt(UPCs.length )], + flavors[r.nextInt(flavors.length)], 1.00 + r.nextFloat())); } } diff --git a/bugFix.txt b/bugFix.txt new file mode 100644 index 0000000..6c676ee --- /dev/null +++ b/bugFix.txt @@ -0,0 +1,5 @@ +Random.nextInt(int n) generates a random number in the range 0(inclusive) to n(exclusive). +We want to generate index for arrays flavors and UPCs randomly. So we need indexes from +0 to flavors.length-1 (both inclusive). So for upper bound we will have to add 1 to the upper bound +and the pass this to nextInt() method. So, correct call should be nextInt(flavors.length), then only it +will generate number from 0 to flavors.length-1 (both inclusive). \ No newline at end of file