Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stuck trying to use Random object for my Intro to Computer Programming with Java homework

Briefly, I have textbook lab assignments for this class and I'm currently doing a module on objects. This lab has me using a random number generator but due to auto grading it needs repeatability. It does this by seeding with 2 but I can't seem to get what the lab wants even though my program functions exactly the way it should under normal circumstances. Here's the directions and my code:

Write a program that simulates flipping a coin to make decisions. The input is how many decisions are needed, and the output is either heads or tails. Assume the input is a value greater than 0.

Ex: If the input is 3, the output is:

tails 
heads
heads

For reproducibility needed for auto-grading, seed the program with a value of 2. In a real program, you would seed with the current time. In that case, every program's output would be different, which is what is desired but can't be auto-graded.

Note: A common student mistake is to create an instance of Random before each call to rand.nextInt(). But seeding should only be done once, at the start of the program, after which rand.nextInt() can be called any number of times.

Your program must define and call the following method that returns "heads" or "tails".

public static String HeadsOrTails(Random rand)

And here's what I have so far:

import java.util.Scanner;
import java.util.Random;

public class LabProgram {

   public static String HeadsOrTails(Random rand) {
      String coinFlipVal = "tails";

      if (rand.nextInt(2) == 0) {
         coinFlipVal = "heads";
         }

      return coinFlipVal;
   }

   public static void main(String[] args) {
      Scanner key = new Scanner(System.in);
      Random randGen = new Random(2); // Unique seed
      int iDecisions = key.nextInt();

      for (int i = 0; i < iDecisions; i++) {
         System.out.println(HeadsOrTails(randGen));
      }
   }
}

This does exactly what it's supposed in that it yields a random set of heads or tails results for however many iterations I want but not in the order that the program is looking for. I've played with my if statement, setting it to 1 instead of zero, using an else if statement for tails and declaring coinFlipVal as "", etc. I just don't know how to get what they're looking for. Any help as to what I'm overlooking is greatly appreciated. And here is the submission results (which gives what the program expects from the output):

1: Compare output 0 / 2 Output differs. See highlights below. Input:

3

Your output

tails
heads
tails

Expected output

tails
heads
heads

2: Unit test 2 / 2 HeadsOrTails() input 1

Your output:

HeadsOrTails() with input 1 correctly returned:
tails

3: Unit test 0 / 3 HeadsOrTails() input 5

Your output:

HeadsOrTails() with input 5 incorrectly returned:
tails
heads
tails
heads
heads

4: Unit test 0 / 3 HeadsOrTails() input 10

Your output:

HeadsOrTails() with input 10 incorrectly returned:
tails
heads
tails
heads
heads
tails
tails
heads
tails
tails
like image 580
Boog Avatar asked Dec 29 '25 11:12

Boog


1 Answers

I had the same answer as you and got 10/10, the only difference is that I added an else Statement after the if;

public static String headsOrTails(Random rand){
      String coinFlip; // no need to "initialize" as it is definitely assigned just below

      if (rand.nextInt(2) == 0)
         coinFlip = "heads";
      else  // Added code
         coinFlip = "tails";

   return coinFlip;
   }
like image 138
Eduardo Moreno C Avatar answered Dec 31 '25 02:12

Eduardo Moreno C



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!