1. What is/are the name(s) of the class file in this entire project? 2. Where is
ID: 3635832 • Letter: 1
Question
1. What is/are the name(s) of the class file in this entire project?
2. Where is the main method located?
3. Which of the classes is considered the driver program?
4. This line of code is found in the main method:
PigGame game = new PigGame (100);
What type of code is this?
Explain the function of this line of code.
5. This line of code is found in the main method:
game.play();
Where does control pass to when this line of code is executed?
6. Explain the scope of the variables created by the following section of code:
public class PigGame
{
private int goal;
private PairOfDice dice;
private PigPlayer computer, human, currentPlayer;
7. What type of code is the following line of code?
announceWinner();
8. What is the return type of this particular method?
private void takeTurn ()
9. What event will cause this method to be executed?
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());
}
10. Explain the use of the logical negation in this block of code:
if (! rollAgain)
{
total += round;
round = 0;
}
11. Where is this code found?
if (! rollAgain)
{
total += round;
round = 0;
}
12. What tpe of data is contained in the variable total? How do you know based on this code alone?
public int getPoints()
{
return total;
}
13. What type of data is being passed into this particular method?
public boolean roll (PairOfDice dice, int goal)
14. Why are these two import statements required for this class and not the Pig class?
import java.util.Scanner;
import java.util.*;
public class PigPlayer
{
15. First – where is this block of code found? Second – Why is there no return type for this particular block of code?
public PigPlayer (int max)
{
total = 0;
round = 0;
limit = max;
}
16. This method returns rollAgain. Where does it return rollAgain to?
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;
}
17. Which method displays to the screen if someone is a winner?
//********************************************************************
// PigPlayer.java Java Foundations
//
// Solution to Programming Project 5.11
//
// The PigPlayer class represents one player of the game, either the
// human player or the computer. Each player tracks his total points
// and the points accumulated this round. The player's limit is the
// number of points he is willing to accumulate in one round before
// passing to the next player. This is dynamic for the human player.
//********************************************************************
import java.util.Scanner;
import java.util.*;
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;
}
}
//********************************************************************
// PigGame.java Java Foundations
//
// Solution to Programming Project 5.11
//********************************************************************
public class PigGame
{
private int goal;
private PairOfDice dice;
private PigPlayer computer, human, currentPlayer;
//-----------------------------------------------------------------
// Sets up the game including the point goal for the game.
//-----------------------------------------------------------------
public PigGame (int target)
{
goal = target;
dice = new PairOfDice();
computer = new PigPlayer (20);
human = new PigPlayer (PigPlayer.ASK);
}
//-----------------------------------------------------------------
// This method contains the primary game loop, which terminates
// only once a player has won the game.
//-----------------------------------------------------------------
public void play()
{
boolean noWinnerYet = true;
currentPlayer = computer;
while (noWinnerYet)
{
takeTurn();
// Check for winner; otherwise switch players and continue
if (currentPlayer.getPoints() >= goal)
noWinnerYet = false;
else
if (currentPlayer == computer)
currentPlayer = human;
else
currentPlayer = computer;
}
announceWinner();
}
//-----------------------------------------------------------------
// Plays one player's complete turn, allowing the player to
// roll the dice multiple times.
//-----------------------------------------------------------------
private void takeTurn ()
{
boolean stillRolling = true;
System.out.println ("****************************************");
System.out.println ("Current Status:");
System.out.println (" Computer: " + computer.getPoints());
System.out.println (" You: " + human.getPoints());
while (stillRolling)
stillRolling = currentPlayer.roll (dice, goal);
}
//-----------------------------------------------------------------
// Announces the winner and prints the results.
//-----------------------------------------------------------------
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());
}
}
//********************************************************************
// Pig.java Java Foundations
//
// Solution to Programming Project 5.11
//********************************************************************
public class Pig
{
//-----------------------------------------------------------------
// Creates the PigGame object and plays the game.
//-----------------------------------------------------------------
public static void main (String[] args)
{
PigGame game = new PigGame (100);
game.play();
}
}
Explanation / Answer
1) Class Names
a)PigPlayer
b)PigGame
c)Pig
As per given code there is one more class(PairOfDice), but this class you haven't created.if you created that class also, then class names will be 4(including this class PairOfDice)
2)
Main Method located in Pig Class.
3)
PigGame is the Driver class.
4)
This is called creation of Object (Instance of) for PigGame class.
Functionality:
Here we are passing 100 to the parameterized constructor in PigGame class.that will be initialized goal varaible.
using that insatnce(game) we can call methods in PigGame class.
5)
first controle will go to the PigGame class and will check for the play() method, if it's available then it will call that method and it will excute logic and return some values, if return type is void then it won't return just it will excute as per logic only.
6)
within the same class only you can access, by using public& private methods. out side the class you can't access.
7)
this is a method calling. to call one method from another method we can use this type of code.
8)
return type is void.
10)
here you are making round variable as 0 if rollAgain is true, initially you can add round value to total, after that you will add every time 0 to the total variable. this is negotion.
11)
you can find that code in public boolean roll(PairOfDice dice, int goal) method in PigPlayer class.
12)
Ineteger type data will be available. based on the return type of this method i can say.
13)
we can pass integer type and reference typpe of PairOfDice.
14)
here we are using Scanner class to read the input from user, so for that purpose we have to import that package.
util package is used to implement collection classes like List,Set,Map.
15)
a) In PigPlayer class you can find.
b) this is a Construtor, Construtor can't return any value, it is used to initialize the varaibles, So we can't use Return type for the Constructor.
16)
At the end of the line Return statement will return.
17) In PigGame class, private void announceWinner () method displays to the screen if someone is a winner.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.