JAVA QUESTION Using only Classes arrays reading files loops


JAVA QUESTION

Using only: Classes, arrays, reading files, loops, and selection


In this problem you will write a set of classes which keep track of a set of player’s scores during a game of bowling. To do this program you will need to create a Player Class, to represent one or more players during the game. Each player plays 10 frames. In each frame, a player can bowl a maximum of 2 balls (except for the last), and their score is calculated for each frame, subject to the following rules:

A “strike” is when the first ball thrown knocks down all 10 pins. The player will NOT throw another ball during this frame. To calculate the score for this frame, the 10 pins knocked down this frame PLUS the # of pins knocked down with each of the next two balls thrown in future frames is added to the total score.

A “spare” is when the total pins knocked down in this frame with BOTH balls equals 10. To calculate the score for this frame, the 10 pins knocked down this frame PLUS the # of pins knocked down with the NEXT ball is added to the total score.

An “open frame” occurs when the player fails to knock down all 10 pins with both balls. The total pins knocked down are added to the total score.

The 10thframe is special, Normally each player only throws 2 balls, in the 10thframe, HOWEVER, if the first ball is a strike, the player will have an opportunity to throw 2 additional balls (for a total of 3). Also, if with the player gets a spare with the first 2 balls, he/she will be allowed to throw a 3rdball.

There are many sample bowling score sheets online that you can view to help you learn the rules. We are playing a simplified game with no handicaps.

A perfect game where every ball thrown results in a strike, should yield a score of 300

A “gutter ball” is a ball that knocks down no pins, and scores a zero.


An execution of your main program will represent a “game” where 1 or more players will play a game of bowling. To aid with the grading of your program you will be given a data file containing the information for each player, and the number of pins knocked down by each of their balls. Your program will calculate each player’s score as the game progresses, frame by frame (if possible). The data file will have the following format:


Integer # of Players

player 1 first and last name

player 2 first and last name

player n first and last name


frame# Player1 balls 1 and 2, player 2 balls 1 and 2.. player n balls one and 2


!!!!!!!!!! Note if a player throws a strike, eg: ball1 = 10, ball2 will be 0

The following is a sample data file

3

Sam Sneed

Kim Jones

Donald Duck

1 10 0 5 5 0 8

2 10 0 4 6 10 0

3 0 10 4 5 3 6

10 10 10 10 4 6 5 3 4

// remember in the 10thframe a player can throw 3 balls if they get a spare or strike



Player class

The player class contains the information about a single player, it has the following private instance data members:

String firstName

String lastName

int[] frameScores; // 10 element array of bowling scores for each frame

int[] ballsThrown; // An integer array containing a list of balls thrown for each frame. There will be 2

// per frame except for the last which would have 3, for a total of 21 elements


CONSTRUCTOR:

In this class you need a single constructor that will receive the player’s first and last name as parameters, and is responsible for instantiating the arrays for frameScores and ballsThrown, to the appropriate size.


public Player( String first, String last)


METHODS: Your class will have the following instance methods:

you will need getters for the first and last name

public void storeBallsThrown( int ball1, int ball2, int frameNum): For the given frame this method stores the balls thrown into the correct elements of the ballsThrown array. IE: for frame 1, ball1 would be stored in element 0, and ball 2 into element 1, for frame2, ball 1 would be stored into element 2, and ball 2 into element 3, for frame 3 ball 1 is in element 4, and ball2 in element5 …..

HINT: for a give frame, the element # of the first ball = (frame#-1 * 2)

public void calculateFrameScore( int frame) -- This method will calculate a player’s score up through the current frame. It will revisit all previous frames, in case their score could not finish being calculated. Remember if a player throws a strike for the first ball, he/she will not throw a second ball for that frame, thus you must want for the next two balls thrown in future frames. The same is true for a spare, however, if the balls thrown in a frame is less than 10, then simply add the total to the total for the previous frame. Note frame 1, is element 0 of your frameScores array.

public int getBall1( int frame) -- for the given frame this returns a player’s throw for ball 1

public int getBall2(int frame) -- for a give frame this returns a player’s throw for ball 2

public int getScore(int frame) -- this returns a player’s cumulative score for a particular frame


Your main program/tester class


Your main program is more complex than in past assignments. It must perform the following steps

Prompt the user for a file name, and then using a try/catch block sequence open the file via a Scanner. YOU MUST TAKE ALL STEPS NECESSARY to insure that your program forces the user to enter a correct file name.

Once the file is opened, read the first line to get the number of players playing in this game, and declare/instantiate an array of Players.

Then for each player:

Read their first and last name from the file

Create a player instance by calling the constructor for class player and saving the object into the array. The first player is in element 0 [ hint you might need a for loop]. IE:

players[i] = new Player(firstName, lastName);

Next for 10 frames you must read the pins knocked down by balls 1 and 2 for each player, and save them into the player’s instance using storeBallsThrown. As the score for the balls thrown by each player are read and saved you need to call the calculateFrameScore method for that player. [HINT: you will need two loops, one that loops through 10 frames, and one that loops through the number of players]

in each frame after reading ball 1 and ball 2 for a player you will do something similar to :

player[i].storeBallsThrown(ball1, ball2, frame);

player[i].calculateFrameScore(frame);

Finally, as the last step you will be provided with a static method called drawScoreCard that you will call sending in your player array as an argument. This method will print off a score sheet for your game. YOU WILL NEED TO MAKE MODIFICATIONS to this METHOD.. so that it will print all players. This method will be provided to you by Monday 10/24





Sample output

For data file #2, Sam Sneed bowled a perfect game and his score should be 300, Jane Jone\'s final score should be 149, and Jack Cassidy should get a 187



3

Sam Sneed

Jane Jones

Jack Cassidy

1 10 0 8 2 10 0

2 10 0 5 4 9 1

3 10 0 9 0 5 5

4 10 0 10 0 7 2

5 10 0 10 0 10 0

6 10 5 5 10 0

7 10 5 3 10 0

8 10 6 3 9 0

9 10 9 1 8 2

10 10 10 9 1 10 9 1 10



Sample data file


3

Sam Sneed

Lora Lately

John Stump

1 10 0 4 5 6 4

2 10 0 4 6 5 5

3 10 0 3 5 0 0

4 10 0 3 7 8 2

5 10 0 3 4 10 0

6 10 0 8 0 3 5

7 10 0 3 5 8 2

8 10 0 7 1 8 0

9 10 0 4 3 7 2

10 10 10 10 4 6 3 7 1


Solution

package Player Class;

import junit.framework.TestCase;

public class TestBowling extends TestCase {

    public void testFirstFrame() {

        //setup

        BowlingLane scorer = new BowlingLane();

        //assert

        assertFrame(scorer, false, 1);

    }

    public void testDropPins_Strike() {

        //setup

        BowlingLane scorer = new BowlingLane();

        //execute

        scorer.dropPins(10);

        //assert

        assertFrame(scorer, false, 2);

    }

    public void testDropPins_IncompleteFrame() {

        //setup

        BowlingLane scorer = new BowlingLane();

        //execute

        scorer.dropPins(0);

        assertFrame(scorer, false, 1);

    }

    public void testDropPins_CompleteFrame() {

        //setup

        BowlingLane scorer = new BowlingLane();

        scorer.dropPins(0);

        //execute

        scorer.dropPins(0);

        //assert

        assertFrame(scorer, false, 2);

    }

    public void testDropPins_IncompleteSecondFrame() {

        //setup

        BowlingLane scorer = new BowlingLane();

        scorer.dropPins(0);

        scorer.dropPins(0);

        //execute

        scorer.dropPins(0);

        //assert

        assertFrame(scorer, false, 2);

    }

    public void testDropPins_CompleteSecondFrame() {

        //setup

        BowlingLane scorer = new BowlingLane();

        scorer.dropPins(0);

        scorer.dropPins(0);

        scorer.dropPins(0);

        //execute

        scorer.dropPins(0);

        //assert

        assertFrame(scorer, false, 3);

    }

    public void testDropPins_LastFrame_IncompleteGame() {

        //setup

        BowlingLane scorer = new LastFrameBowlingLane();

        //execute

        scorer.dropPins(0);

        //assert

        assertFrame(scorer, false, 10);

    }

    public void testDropPins_LastFrame_CompleteGame() {

        //setup

        BowlingLane scorer = new LastFrameBowlingLane();

        scorer.dropPins(0);

        //execute

        scorer.dropPins(0);

        //assert

        assertFrame(scorer, true, 10);

    }

    public void testDropPins_LastFrame_Strike_IncompleteGame() {

        //setup

        BowlingLane scorer = new LastFrameBowlingLane();

        //execute

        scorer.dropPins(10);

        //assert

        assertFrame(scorer, false, 10);

    }

    public void testDropPins_LastFrame_TwoStrikes_IncompleteGame() {

        //setup

        BowlingLane scorer = new LastFrameBowlingLane();

        scorer.dropPins(10);

        //execute

        scorer.dropPins(10);

        //assert

        assertFrame(scorer, false, 10);

    }

    public void testDropPins_LastFrame_Strike_CompleteGame() {

        //setup

        BowlingLane scorer = new LastFrameBowlingLane();

        scorer.dropPins(10);

        scorer.dropPins(10);

        //execute

        scorer.dropPins(10);

        //assert

        assertFrame(scorer, true, 10);

    }

    public void testDropPins_LastFrame_Spare_IncompleteGame() {

        //setup

        BowlingLane scorer = new LastFrameBowlingLane();

        scorer.dropPins(5);

        //execute

        scorer.dropPins(5);

        //assert

        assertFrame(scorer, false, 10);

    }

    public void testDropPins_LastFrame_Spare_CompleteGame() {

        //setup

        BowlingLane scorer = new LastFrameBowlingLane();

        scorer.dropPins(5);

        scorer.dropPins(5);

        //execute

        scorer.dropPins(0);

        //assert

        assertFrame(scorer, true, 10);

    }

    private void assertFrame(BowlingLane scorer, boolean gameOver, int currentFrame) {

        assertEquals(\"game over\", gameOver, scorer.gameOver());

        assertEquals(\"current frame\", currentFrame, scorer.currentFramePosition());

    }

    public void testPinsDropped_NormalFrame_OneThrow() {

        //setup

        BowlingLane scorer = new BowlingLane();

        scorer.dropPins(5);

        scorer.dropPins(4);

        //execute

        String[] pinsDropped = scorer.pinsDropped(1);

        //assert

        assertPins(pinsDropped, \"5\", \"4\");

    }

    public void testPinsDropped_TwoThrows() {

        //setup

        BowlingLane scorer = new BowlingLane();

        scorer.dropPins(5);

        //execute

        String[] pinsDropped = scorer.pinsDropped(1);

        //assert

        assertPins(pinsDropped, \"5\", \"\");

    }

    public void testPinsDropped_Strike() {

        //setup

        BowlingLane scorer = new BowlingLane();

        scorer.dropPins(10);

        //execute

        String[] pinsDropped = scorer.pinsDropped(1);

        //assert

        assertPins(pinsDropped, \"X\", \"\");

    }

    public void testPinsDropped_Spare() {

        //setup

        BowlingLane scorer = new BowlingLane();

        scorer.dropPins(4);

        scorer.dropPins(6);

        //execute

        String[] pinsDropped = scorer.pinsDropped(1);

        //assert

        assertPins(pinsDropped, \"4\", \"/\");

    }

    public void testPinsDropped_LastFrame_OneThrow() {

        //setup

        BowlingLane scorer = new LastFrameBowlingLane();

        scorer.dropPins(4);

        //execute

        String[] pinsDropped = scorer.pinsDropped(10);

        //assert

        assertPins(pinsDropped, \"4\", \"\", \"\");

    }

    public void testPinsDropped_LastFrame_TwoThrows() {

        //setup

        BowlingLane scorer = new LastFrameBowlingLane();

        scorer.dropPins(2);

        scorer.dropPins(6);

        //execute

        String[] pinsDropped = scorer.pinsDropped(10);

        //assert

        assertPins(pinsDropped, \"2\", \"6\", \"\");

    }

    public void testPinsDropped_LastFrame_Strike() {

        //setup

        BowlingLane scorer = new LastFrameBowlingLane();

        scorer.dropPins(10);

        //execute

        String[] pinsDropped = scorer.pinsDropped(10);

        //assert

        assertPins(pinsDropped, \"X\", \"\", \"\");

    }

    public void testPinsDropped_LastFrame_TwoStrikes() {

        //setup

        BowlingLane scorer = new LastFrameBowlingLane();

        scorer.dropPins(10);

        scorer.dropPins(10);

        //execute

        String[] pinsDropped = scorer.pinsDropped(10);

        //assert

        assertPins(pinsDropped, \"X\", \"X\", \"\");

    }

    public void testPinsDropped_LastFrame_ThreeStrikes() {

        //setup

        BowlingLane scorer = new LastFrameBowlingLane();

        scorer.dropPins(10);

        scorer.dropPins(10);

        scorer.dropPins(10);

        //execute

        String[] pinsDropped = scorer.pinsDropped(10);

        //assert

        assertPins(pinsDropped, \"X\", \"X\", \"X\");

    }

    public void testPinsDropped_LastFrame_TwoStrikesPlusExtraThrow() {

        //setup

        BowlingLane scorer = new LastFrameBowlingLane();

        scorer.dropPins(10);

        scorer.dropPins(10);

        scorer.dropPins(5);

        //execute

        String[] pinsDropped = scorer.pinsDropped(10);

        //assert

        assertPins(pinsDropped, \"X\", \"X\", \"5\");

    }

    public void testPinsDropped_LastFrame_Spare() {

        //setup

        BowlingLane scorer = new LastFrameBowlingLane();

        scorer.dropPins(7);

        scorer.dropPins(3);

        //execute

        String[] pinsDropped = scorer.pinsDropped(10);

        //assert

        assertPins(pinsDropped, \"7\", \"/\", \"\");

    }

    public void testPinsDropped_LastFrame_SparePlusExtraThrow() {

        //setup

        BowlingLane scorer = new LastFrameBowlingLane();

        scorer.dropPins(7);

        scorer.dropPins(3);

        scorer.dropPins(9);

        //execute

        String[] pinsDropped = scorer.pinsDropped(10);

        //assert

        assertPins(pinsDropped, \"7\", \"/\", \"9\");

    }

    public void testPinsDropped_LastFrame_SparePlusStrike() {

        //setup

        BowlingLane scorer = new LastFrameBowlingLane();

        scorer.dropPins(7);

        scorer.dropPins(3);

        scorer.dropPins(10);

        //execute

        String[] pinsDropped = scorer.pinsDropped(10);

        //assert

        assertPins(pinsDropped, \"7\", \"/\", \"X\");

    }

    private void assertPins(String[] pinsDropped, String firstThrow, String secondThrow) {

        assertEquals(\"number of throws\", 2, pinsDropped.length);

        assertEquals(\"first throw\", firstThrow, pinsDropped[0]);

        assertEquals(\"second throw\", secondThrow, pinsDropped[1]);

    }

    private void assertPins(String[] pinsDropped, String firstThrow, String secondThrow, String thirdThrow) {

        assertEquals(\"number of throws\", 3, pinsDropped.length);

        assertEquals(\"first throw\", firstThrow, pinsDropped[0]);

        assertEquals(\"second throw\", secondThrow, pinsDropped[1]);

        assertEquals(\"third throw\", thirdThrow, pinsDropped[2]);

    }

    public void testScore_EmptyFrame() {

        //setup

        BowlingLane scorer = new BowlingLane();

        //execute

        String score = scorer.score(1);

        //assert

        assertEquals(\"score\", \"\", score);

    }

    public void testScore_IncompleteFrame() {

        //setup

        BowlingLane scorer = new BowlingLane();

        scorer.dropPins(7);

        //execute

        String score = scorer.score(1);

        //assert

        assertEquals(\"score\", \"\", score);

    }

    public void testScore_CompleteFrame() {

        //setup

        BowlingLane scorer = new BowlingLane();

        scorer.dropPins(7);

        scorer.dropPins(2);

        //execute

        String score = scorer.score(1);

        //assert

        assertEquals(\"score\", \"9\", score);

    }

    public void testScore_Strike_MissingThrows() {

        //setup

        BowlingLane scorer = new BowlingLane();

        scorer.dropPins(10);

        //execute

        String score = scorer.score(1);

        //assert

        assertEquals(\"score\", \"\", score);

    }

    public void testScore_Spare() {

        //setup

        BowlingLane scorer = new BowlingLane();

        scorer.dropPins(5);

        scorer.dropPins(5);

        scorer.dropPins(1);

        //execute

        String score = scorer.score(1);

        //assert

        assertEquals(\"score\", \"11\", score);

    }

    public void testScore_Strike() {

        //setup

        BowlingLane scorer = new BowlingLane();

        scorer.dropPins(10);

        scorer.dropPins(5);

        scorer.dropPins(1);

        //execute

        String score = scorer.score(1);

        //assert

        assertEquals(\"score\", \"16\", score);

    }

    public void testScore_TwoStrikes() {

      //setup

        BowlingLane scorer = new BowlingLane();

        scorer.dropPins(10);

        scorer.dropPins(10);

        scorer.dropPins(3);

        //execute

        String score = scorer.score(1);

        //assert

        assertEquals(\"score\", \"23\", score);

    }

    public void testScore_LastFrame_Empty() {

        //setup

        BowlingLane scorer = new LastFrameBowlingLane();

        //execute

        String score = scorer.score(10);

        //assert

        assertEquals(\"score\", \"\", score);

    }

    public void testScore_LastFrame_OneThrow() {

        //setup

        BowlingLane scorer = new LastFrameBowlingLane();

        scorer.dropPins(1);

        //execute

        String score = scorer.score(10);

        //assert

        assertEquals(\"score\", \"\", score);

    }

    public void testScore_LastFrame_TwoThrows() {

        //setup

        BowlingLane scorer = new LastFrameBowlingLane();

        scorer.dropPins(1);

        scorer.dropPins(6);

        //execute

        String score = scorer.score(10);

        //assert

        assertEquals(\"score\", \"7\", score);

    }

    public void testScore_LastFrame_IgnoreThirdThrow() {

        //setup

        BowlingLane scorer = new LastFrameBowlingLane();

       scorer.dropPins(1);

        scorer.dropPins(6);

        scorer.dropPins(2);

        //execute

        String score = scorer.score(10);

        //assert

        assertEquals(\"score\", \"7\", score);

    }

    public void testScore_LastFrame_Strike() {

        //setup

        BowlingLane scorer = new LastFrameBowlingLane();

        scorer.dropPins(10);

        scorer.dropPins(6);

        scorer.dropPins(2);

        //execute

        String score = scorer.score(10);

        //assert

        assertEquals(\"score\", \"18\", score);

    }

    public void testScore_LastFrame_Spare() {

        //setup

        BowlingLane scorer = new LastFrameBowlingLane();

        scorer.dropPins(4);

        scorer.dropPins(6);

        scorer.dropPins(2);

        //execute

        String score = scorer.score(10);

        //assert

        assertEquals(\"score\", \"12\", score);

    }

    public void testScore_CumulativeScore() {

        //setup

        BowlingLane scorer = new BowlingLane();

        scorer.dropPins(4);

        scorer.dropPins(2);

        scorer.dropPins(10);

        scorer.dropPins(10);

        scorer.dropPins(5);

        //execute

        String score = scorer.score(2);

        //assert

        assertEquals(\"score\", \"31\", score);

    }

    public void testPerfectGame() {

        BowlingLane scorer = new BowlingLane();

        scorer.dropPins(10);

        scorer.dropPins(10);

        scorer.dropPins(10);

        scorer.dropPins(10);

        scorer.dropPins(10);

        scorer.dropPins(10);

      scorer.dropPins(10);

        scorer.dropPins(10);

        scorer.dropPins(10);

        scorer.dropPins(10);

        scorer.dropPins(10);

        scorer.dropPins(10);

        String score = scorer.score(10);

        assertEquals(\"score\", \"300\", score);

        assertFrame(scorer, true, 10);

        String[] pinsDropped = scorer.pinsDropped(1);

        assertPins(pinsDropped, \"X\", \"\");

        pinsDropped = scorer.pinsDropped(2);

        assertPins(pinsDropped, \"X\", \"\");

        pinsDropped = scorer.pinsDropped(3);

        assertPins(pinsDropped, \"X\", \"\");

        pinsDropped = scorer.pinsDropped(4);

        assertPins(pinsDropped, \"X\", \"\");

        pinsDropped = scorer.pinsDropped(5);

        assertPins(pinsDropped, \"X\", \"\");

        pinsDropped = scorer.pinsDropped(6);

        assertPins(pinsDropped, \"X\", \"\");

        pinsDropped = scorer.pinsDropped(7);

        assertPins(pinsDropped, \"X\", \"\");

        pinsDropped = scorer.pinsDropped(8);

        assertPins(pinsDropped, \"X\", \"\");

        pinsDropped = scorer.pinsDropped(9);

        assertPins(pinsDropped, \"X\", \"\");

        pinsDropped = scorer.pinsDropped(10);

        assertPins(pinsDropped, \"X\", \"X\", \"X\");

    }

}

 JAVA QUESTION Using only: Classes, arrays, reading files, loops, and selection In this problem you will write a set of classes which keep track of a set of pla
 JAVA QUESTION Using only: Classes, arrays, reading files, loops, and selection In this problem you will write a set of classes which keep track of a set of pla
 JAVA QUESTION Using only: Classes, arrays, reading files, loops, and selection In this problem you will write a set of classes which keep track of a set of pla
 JAVA QUESTION Using only: Classes, arrays, reading files, loops, and selection In this problem you will write a set of classes which keep track of a set of pla
 JAVA QUESTION Using only: Classes, arrays, reading files, loops, and selection In this problem you will write a set of classes which keep track of a set of pla
 JAVA QUESTION Using only: Classes, arrays, reading files, loops, and selection In this problem you will write a set of classes which keep track of a set of pla
 JAVA QUESTION Using only: Classes, arrays, reading files, loops, and selection In this problem you will write a set of classes which keep track of a set of pla
 JAVA QUESTION Using only: Classes, arrays, reading files, loops, and selection In this problem you will write a set of classes which keep track of a set of pla
 JAVA QUESTION Using only: Classes, arrays, reading files, loops, and selection In this problem you will write a set of classes which keep track of a set of pla
 JAVA QUESTION Using only: Classes, arrays, reading files, loops, and selection In this problem you will write a set of classes which keep track of a set of pla
 JAVA QUESTION Using only: Classes, arrays, reading files, loops, and selection In this problem you will write a set of classes which keep track of a set of pla
 JAVA QUESTION Using only: Classes, arrays, reading files, loops, and selection In this problem you will write a set of classes which keep track of a set of pla
 JAVA QUESTION Using only: Classes, arrays, reading files, loops, and selection In this problem you will write a set of classes which keep track of a set of pla
 JAVA QUESTION Using only: Classes, arrays, reading files, loops, and selection In this problem you will write a set of classes which keep track of a set of pla
 JAVA QUESTION Using only: Classes, arrays, reading files, loops, and selection In this problem you will write a set of classes which keep track of a set of pla
 JAVA QUESTION Using only: Classes, arrays, reading files, loops, and selection In this problem you will write a set of classes which keep track of a set of pla
 JAVA QUESTION Using only: Classes, arrays, reading files, loops, and selection In this problem you will write a set of classes which keep track of a set of pla

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site