Assignment #4

Create a separate class for each problem.

As we’ve discussed in class, I can not stress the following enough:

1.     DO NOT PROCRASTINATE! This assignment is going to take more time than the previous ones. It is not necessarily harder, but it requires you to apply all of the various techniques we’ve discussed over the course to date.

2.     PRACTICE and get comfortable with the various concepts we have learned in class before attempting to do the homework. I promise you that this will make your lives much, much easier in terms of getting the assignment done!

3.     Remember to start small and build each individual piece of your program. Test to make sure it’s working properly before moving on to the next bit of functionality.

4.     Spend some time in advance thinking out your strategy and write out the steps on a piece of paper. Think these steps through carefully before beginning to code. There is nothing wrong with making changes to your plan as you proceed.

Problem #1

The following line of code generates a random integer number between 1 and 6 and assigns the random number to the variable.

int dieRoll = (int) (Math.random()*6) + 1;

Experiment with this code a little bit.

You are going to design an (admittedly simple) game in which you will “roll” two die and will output the results of the roll and whether or not a “doubles” was rolled. For those who aren’t familiar, “doubles” means that the same value was rolled in both die.  So a 4 and a 4 would be a doubles. A 4 and a 5 would not.

As a hint to help get you started: Create two variables, one to represent the first die, and another to represent the second die.

After each roll, as the user if they want another roll. Read in their answer as a Y or  an N character. Your program should also accept ‘y’ or ‘n’.

Your output might look something like this:

You rolled a 5 and a 3. Not a doubles.

Would you like to play again? Enter Y or N:  y

You rolled a 6 and a 6. Doubles!

Would you like to play again? Enter Y or N: N

Press any key to continue…

Problem #2

Working off of problem number 1, figure out how to modify the line of code provided so that it represents a coin flip.

As a rather generous hint to get you started: You will want to generate a random number between 1 and 2. If the number is one, you can say that a “heads” was flipped. If the number is two, you can say that a “tails” was flipped. You should output the results of 50 coin flips.  Your output MUST be identical to mine shown here, although, of course, the results of the flips will be different:

Flip #3: Tails

etc

etc

etc

There were 27 Heads and 23 tails.

Press any key to continue…

You must also determine how many heads were flipped and how many tails.

SUBMISSION REQUIREMENTS:

Attach both classes as a ZIP file.

The assignment is due Thursday at 10:00 AM Central Time.