Starting from:

$25

CS352 - Lab 7 - Solved

Each program should include comments that explain what each block of code is doing. Additionally, the programs should compile without errors, and run with the results described in the exercise. The following deductions will be made from each exercise if any of the following is incorrect or missing:

Proper formatting [5 points]

Proper names for classes and variables [5 points]

Comments [5 points per class]

Program doesn't compile [ 10 points]

Source code (java file) missing [ 10 points]

Executable (class file) missing [10 points]

Missing array where an array was required [5 points each]

Missing loop where a loop was required [5 points each]

Missing class from the design provided [10 points each]

Missing method from the design provided [ 5 points each]
Lab (100 Points)

Create the BatterUp system using object oriented programming, following the design provided on the following pages.    Your output should match the format shown on the last page, although your actual output will vary based on the the outcome of the game.

Submit the java and class files via Canvas (as a single zipfile). Include a comment block at the top of each Java file that includes your name, student id number, and “Lab 7Fall 2018”. Also be sure to include the answers to the post-lab questions found in these instructions.

This Lab is due Saturday November 17 at 6:00am.    

 

                       Launcher                                          1                          BatterUp

Field

                                                                              Player changes:--outs : score: intint          1                     1

+main(String[]):void

-nextPlayerIndex: int

+Field()
•     Add two +BatterUpint attributes to count total ()        hits and total theFieldatBats this player has had+getDugout() : Base

                          Player                                      +createPlayers• Initialize these to 0 in the Constructor():void      +getBatterBox() : Base

                                                                                        +getNextPlayer• Increment the values in the ():Player  takeTurn method for each +moveAheadatBat and each hit(Base, int) : Base

 --name: Stringstrikes: int            9 •1 Create a new method named ++play():voidmovePlayers• Move the dice behavior from the (int):void                                              roll bat method into this new method1

-balls : int               ++displayFieldprintStats• Create an values 1..6) and store the two results in the () : void():voidint array to hold two values.  Simulate two die roll s(possible int array.

-hits : int                                       players

-atBats : int                                                                     • Return the int array to the calling method

•     Make the following changes to the bat() method

+Player(String, Base)     • Replace the die roll code with a call to the new roll method you just created

<+getters and settersisNotInDugout() : boolean>              •• Store the result of calling Change all the if statements to compare the two values in the roll in a newly declared int array in this method6int array that bases

+takeTurn() : int                             1      was returned by roll to determine whether the player got a hit, strike, or ball1

+bat() : int                                                          • Make the following changes to the takeTurn method                                                                                                                        Base

++roll() : getBattingAverageint[]                                                                                          () : double           •• Increment the value of If the player gets a hit (single, double, triple, or home run), increment the atBats at the beginning of each turnlocation                                                  -name: String

+toString() : String              hits counter.  The hits counter is not incremented for a strike out or a walk

•     Add a new method called getBattingAverage which return+s a Base(Stringdouble th)at is the result of diving the total atBats by the total hits (using the two new variables you +getName() : String

                                                                                                           added to Player.                           +setName(String) : void

+isDugout() : boolean

+isHome() : boolean

+toString(): String

                          Ringer                                                                  Dud

+Ringer(String,Base)                                       +Dud(String,Base)

+roll() : int[]                                                      +roll() : int[]

Players, Ringers and Duds
The odds of a player getting a hit using two 6-sided dice is 4/36 or 11%.   See the table below:

 

Let’s introduce a different type of player called a “Ringer” who is much better at hitting singles, doubles and triples, but never hits home runs.  Let’s simulate this by having a Ringer roll 3-sided dice.  This increases the odds of getting a hit to 3/9 or 33%.

 

Let’s also introduce another type of player called a “Dud” who can make any type of hit, but has a much better chance of getting balls and strikes. Let’s simulate this by having a Dud roll 10-sided dice.  This reduces the odds of getting a hit to 4/100 or 4%.

 

 

                       Launcher                                          1                          BatterUp

Field

                                                                                                                 -outs : int                                           1                     1

     +main(String[]):void                                              -score: int

-nextPlayerIndex: int

+Field()

                                                                                                                 +BatterUp()                                                   theField                                                              +getDugout() : Base

                          Player                                                      +createPlayers():void                +getBatterBox() : Base

                                                                                                                 +getNextPlayer():Player        +moveAhead(Base, int) : Base

--name: Stringstrikes: int            9                1 ++play():voidmovePlayers(int):void                                  1

--balls : hits : intint                      players ++displayFieldprintStats() : void():void

-atBats : int

+Player(String, Base)
<BatterUpgetters and settersisNotInDugoutchanges:() : boolean>                                                                                                                                                                               6                                                                                                                                    bases

+

+takeTurn() : int                             1                                                                                                               1

+•bat() : In the intplay method, create another outer loop to repeat for nine innings.  Print the inning number with the score at the end of each Base

 ++•roll() : getBattingAverageinning.In the intcreatePlayers[] () : doublemethod, read in the player name and type for the nine players from an external text file provided with the lab location  -name: String

+toStringinstructions.  This file is named () : String         players.txt.

•     Based on the type (Average, Dud or Ringer) create an instance of Player, Dud, or Ringer and ad+d to the Base(Stringplay)ers ArrayList.

•     Create a new method called printStats, which creates an external text file with a list of stats for each player that includes name, hits, +getName() : String at-bats, and batting-average.              +setName(String) : void

•     You will need to retrieve each Player from the players ArrayList and send the getters to those objects to retrieve the data +isDugout() : boolean needed to print in the output file.          +isHome() : boolean

•     Use printf substitution symbols to align the data in the output file.         +toString(): String

•     Call RingerprintStats in the play method after the end of the 9Dud         th inning

•     See sample output file to the right ->

+Ringer(String,Base)                                       +Dud(String,Base)
+roll() : int[]                                                      +roll() : int[]

$ java Launcher     Sample Output:

Inning 1

SCORE: 0 

 [ 1 ] empty [ 2 ] empty [ 3 ] empty

Amazing Amy is batting

Rolled 3 1 STRIKE!

Rolled 1 1 Single!

SCORE: 0 

[ 1 ] Amazing Amy [ 2 ] empty [ 3 ] empty

Bozo Bob is batting

Rolled 3 9 STRIKE!

Rolled 7 7 STRIKE! Rolled 7 6 BALL!

Rolled 4 8 STRIKE! Strike out!!

SCORE: 0 

[ 1 ] Amazing Amy [ 2 ] empty [ 3 ] empty

Quick Carl is batting Rolled 1 1 Single!

SCORE: 0 

[ 1 ] Quick Carl [ 2 ] Amazing Amy [ 3 ] empty

Dizzy Diana is batting Rolled 2 9 BALL!

Rolled 3 2 BALL!

Rolled 6 8 STRIKE!

Rolled 3 6 BALL!

Rolled 9 8 BALL!

Walk

SCORE: 0 

[ 1 ] Dizzy Diana [ 2 ] Quick Carl [ 3 ] Amazing Amy

Average Ed is batting Rolled 1 5 STRIKE!

Rolled 4 3 BALL!

Rolled 5 2 BALL!

Rolled 5 2 BALL!

Rolled 2 4 STRIKE!

Rolled 2 4 STRIKE! Strike out!!

SCORE: 0 

[ 1 ] Dizzy Diana [ 2 ] Quick Carl [ 3 ] Amazing Amy

Fast Francis is batting Rolled 2 2 Double!

Amazing Amy SCORED!! Quick Carl SCORED!!

SCORE: 2 

[ 1 ] empty [ 2 ] Fast Francis [ 3 ] Dizzy Diana

So-So Sarah is batting Rolled 3 5 STRIKE!

Rolled 2 5 BALL!

Rolled 3 6 BALL!

Rolled 2 4 STRIKE!

Rolled 1 3 STRIKE! Strike out!!

THREE OUTS!

INNING 1 OVER WITH A SCORE OF 2

Inning 2

<data omitted>

Expect a high score with a voluminous amount of  output for nine innings of play. 

[ 1 ] empty [ 2 ] Amazing Amy [ 3 ] empty

Quick Carl is batting Rolled 2 1 BALL!

Rolled 3 2 BALL!

 Rolled 2 2 Double! Amazing Amy SCORED!!

[ 1 ] empty [ 2 ] Quick Carl [ 3 ] empty

Dizzy Diana is batting

Rolled 2 5 BALL!

Rolled 10 8 STRIKE! Rolled 8 8 STRIKE!

Rolled 10 4 STRIKE! Strike out!!

THREE OUTS!

INNING 9 OVER WITH A SCORE OF 66

More products