I have to create the bowling class and I\'m having trouble creating the shot() m
ID: 3529250 • Letter: I
Question
I have to create the bowling class and I'm having trouble creating the shot() method. It is supposed to return a boolean value indicating whether the current frame is over or not. I know how to indicate whether or not a strike happens, but i'm not sure how to keep track of the score and then determine how many pins are left to determine if it is a gutter ball or spare... Here is my code so far for the class. import java.util.Scanner; public class BowlingGame { //instance variables protected String name; protected int currentScore; protected int currentFrame; protected int numStrikes; protected int numSpares; protected int numGutterBalls; protected int NumOfPins; //constructors public BowlingGame() { //Deafult Constructor name = ""; //sets name to null currentScore = 0; //Sets the current Score to 0 currentFrame = 1; //Sets the current Frame to 1 as the deafult value numStrikes = 0; //Sets the numStrikes to 0 numSpares = 0; //Sets to the Number of Spares to 0 numGutterBalls = 0; //Sets to the Number of Gutter Balls to 0 } public BowlingGame(String BowlerName) { name = BowlerName; } public BowlingGame(String BowlersName, int curScore, int curFrame, int Strikes, int Spares, int GutterBalls) { name = BowlersName; currentScore = curScore; currentFrame = curFrame; numStrikes = Strikes; numSpares = Spares; numGutterBalls = GutterBalls; } //observers(getter) public String getName() { return name; } public int getCurrentScore() { return currentScore; } public int getCurrentFrame() { return currentFrame; } public int getStrikes() { return numStrikes; } public int getSpares() { return numSpares; } public int getGutterBalls() { return numGutterBalls; } //transformers //other methods public boolean shot(int numOfPinsDown) { if (numOfPinsDown == 10) { return true; //Returning a True Value means that the current Frame for the Bowler is over and will move to the Next Frame } else if (numOfPinsDown < 10 & numOfPinsDown > 0) { return false; //Returning a False Value means that the current Frame is still } else if (numOfPinsDown) { return false; } //method shot } public String toString() { String str; str = name + "'s game after frame"; //+ ...... return str; } // end toString public static void main(String[] args) { } } //end main //End class BowlingGameExplanation / Answer
var pinhits;
var pinsdown;
var pinspergame;
var guttertotal;
var framenum;
var turnnum;
pinspergame = 10;
framenum = 0;
//framestart increases the frame number and resets number of pins down and gutter count to 0
function framestart()
{
framenum = framenum + 1;
pinsdown = 0;
guttertotal = 0;
turnnum = 1;
}
//shot function. no need to return a value as variable holds all necessary data
function shot(pinhits)
{
if (pinhits == 0)
{
guttertotal = guttertotal + 1;
}
else
{
pinsdown = pinsdown + pinhits;
if (pinsdown == pinspergame or turnnum == 2)
{
framestart();
}
else
{
turnnum = turnnum + 1;
}
}
}