Hi, Please could someone help with the following, I am a beginner at Java :) Thanks


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:

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
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();
}
}

The verses can be done very easily using a for loop.

int bottles;
    //have user enter number of bottles and store it in the 
    //bottles  variable
    for(int i=bottles; i>=1; i--)
    {
          //print the verses using i for the current amount of bottles
          // and use i-1 for the remaining bottles
     }

When the loop variable i reaches 1, it will stop because the bottles remaining will reach 0.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.