Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

JAVA HELP

s4rd59s4rd59 Posts: 1Member
Hi,
Please could someone help me with the following:

There is a rhyme (a bit like 'Ten Green Bottles' ) called 'Bottles of Beer' which runs thus:

4 bottles of beer on the wall,
4 bottles of beer,
Take one down, pass it around (hic),
3 bottles of beer on the wall.

3 bottles of beer on the wall,
3 bottles of beer,
Take one down, pass it around (hic),
2 bottles of beer on the wall.

2 bottles of beer on the wall,
2 bottles of beer,
Take one down, pass it around (hic),
1 bottles of beer on the wall.

1 bottles of beer on the wall,
1 bottles of beer,
Take one down, pass it around (hic),
0 bottles of beer on the wall.

... well apart from the slight grammatical problem. Clearly the starting number can vary.

Start a new project by re-saving the headsTails project as bottleBeer. Delete the class Die from it (you won't need it). Alter class Game so that it:

[italic]Requests the starting value for 'Bottles of Beer' . (For this question assume the user always enters a sensible value.)

Loops the appropriate number of times

Prints a verse of the rhyme in each iteration[/italic]
This is the code for the class Game:

import java.util.*;

/**
* Flips a coin a number of times
*
* Uses head = 1 tails = 0
*
* @author Lisa Payne
* @version Dec 2007
*/

public class Game
{
private Die coin;
private Scanner myScanner;
private int headScore;
private int tailsScore;
private int noThrows;

// set up constants
private final int HEAD = 1;
private final int TAILS = 0;

/**
* Constructor for objects of class Game
*/
public Game()
{
// generates random 0/1's
coin = new Die(2);
myScanner = new Scanner( System.in );
headScore = 0;
tailsScore = 0;
noThrows = 0;
}

/**
* playGame plays the required number of coin throws
*/
public void playGame()
{
headScore = 0;
tailsScore = 0;

System.out.println();
System.out.println("How many coin throws in the next game?");
System.out.print("--> ");

noThrows = myScanner.nextInt();

for (int i = 1; i <= noThrows; i++) {
this.throwCoin();
}

this.printResults();

}

public void throwCoin()
{
int result;
result = coin.roll();
if (result == HEAD) { // if HEAD
headScore++;
}
else { // else assume TAILS
tailsScore++;
}
}

public void printResults()
{
System.out.println();
System.out.println("*************************************************");
System.out.print( "Of the " + noThrows + " throws in this game " );
System.out.println( "there were " + headScore + " heads");
System.out.println("*************************************************");
System.out.println();
}
}


Sign In or Register to comment.