Help Please!!!! For the following three programs annotate it line by line with a
ID: 3760207 • Letter: H
Question
Help Please!!!!
For the following three programs annotate it line by line with an explanation of what each line does. Do NOT comment on the obvious lines in the program.
Program 1
public class Pig
{
//-----------------------------------------------------------------
// Creates the PigGame object and plays the game.
//-----------------------------------------------------------------
public static void main(String[] args)
{
PigGame game = new PigGame(100);
game.play();
}
}
Program 2
private void announceWinner()
{
System.out.println();
if (currentPlayer == computer)
System.out.println("The computer has won!");
else
System.out.println("Congratulations, you have won!");
System.out.println();
System.out.println("Final Results:");
System.out.println(" Computer: " + computer.getPoints());
System.out.println(" You: " + human.getPoints());
}
}
Program 3
public class PigPlayer
{
public final static int ASK = -1; // prompt for round termination
private int total; // total points accumulated in game
private int round; // points accumulated in current round
private int limit; // pass tolerance
//-----------------------------------------------------------------
// Initializes the point accumulators to zero, and the round
// limit as specified.
//-----------------------------------------------------------------
public PigPlayer(int max)
{
total = 0;
round = 0;
limit = max;
}
//-----------------------------------------------------------------
// Rolls the dice once and deals with the results. Returns true
// if the player should roll again and false otherwise. The
// player will not roll again if he busts or wins, or if his
// round limit is reached.
//-----------------------------------------------------------------
public boolean roll(PairOfDice dice, int goal)
{
boolean rollAgain = true;
dice.roll();
System.out.println (dice);
int die1 = dice.getDie1FaceValue();
int die2 = dice.getDie2FaceValue();
if (die1 == 1 || die2 == 1)
{
System.out.println("Busted!!!");
rollAgain = false;
round = 0;
if (die1 == 1 && die2 == 1)
total = 0;
}
else
{
round += die1 + die2;
System.out.println("Current Round: " + round);
System.out.println("Potential Total: " + (total+round));
if ((total+round) >= goal)
rollAgain = false;
else
if (limit == ASK)
{
System.out.print("Take another turn (y/n)? ");
Scanner scan = new Scanner(System.in);
String again = scan.nextLine();
rollAgain = again.equalsIgnoreCase("y");
}
else
if (round >= limit)
rollAgain = false;
if (! rollAgain)
{
total += round;
round = 0;
}
}
return rollAgain;
}
//-----------------------------------------------------------------
// Returns the total number of points accumulated by this player.
//-----------------------------------------------------------------
public int getPoints()
{
return total;
}
}
Explanation / Answer
public class Pig
{
//-----------------------------------------------------------------
// Creates the PigGame object and plays the game.
//-----------------------------------------------------------------
public static void main(String[] args)
{
PigGame game = new PigGame(100);// Declare Function
game.play();
}
}
Program 2
private void announceWinner()
{
System.out.println();
if (currentPlayer == computer)
System.out.println("The computer has won!");
else
System.out.println("Congratulations, you have won!");
/// Resultof the game...
System.out.println();
System.out.println("Final Results:");
System.out.println(" Computer: " + computer.getPoints());
System.out.println(" You: " + human.getPoints());
}
}
Program 3
public class PigPlayer
{
public final static int ASK = -1; // prompt for round termination
// variable declaration
private int total; // total points accumulated in game
private int round; // points accumulated in current round
private int limit; // pass tolerance
//-----------------------------------------------------------------
// Initializes the point accumulators to zero, and the round
// limit as specified.
//-----------------------------------------------------------------
public PigPlayer(int max)
{
total = 0;
round = 0;
limit = max;
}
//-----------------------------------------------------------------
// Rolls the dice once and deals with the results. Returns true
// if the player should roll again and false otherwise. The
// player will not roll again if he busts or wins, or if his
// round limit is reached.
//-----------------------------------------------------------------
public boolean roll(PairOfDice dice, int goal)
{
boolean rollAgain = true;
// For true instance
dice.roll();
System.out.println (dice);
// Result of Throwdice
int die1 = dice.getDie1FaceValue();
int die2 = dice.getDie2FaceValue();
if (die1 == 1 || die2 == 1)
{
System.out.println("Busted!!!");
rollAgain = false;
round = 0;
if (die1 == 1 && die2 == 1)
total = 0;
}
else
{
round += die1 + die2;
System.out.println("Current Round: " + round);// increament result
System.out.println("Potential Total: " + (total+round));
if ((total+round) >= goal) // Once we reach the goal
rollAgain = false;
else
if (limit == ASK)
{
System.out.print("Take another turn (y/n)? ");
Scanner scan = new Scanner(System.in);
String again = scan.nextLine();
rollAgain = again.equalsIgnoreCase("y");
}
else
if (round >= limit)
rollAgain = false;
if (! rollAgain)
{
total += round;
round = 0;
}
}
return rollAgain;
}
//-----------------------------------------------------------------
// Returns the total number of points accumulated by this player.
//-----------------------------------------------------------------
public int getPoints()
{
return total;
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.