Overview Schedule Announcements Resources Assignments Home

CSC 121: Computer Science I, Spring 2005

Lab 7

Laboratory 7 - Simulation with Random Numbers

Goals:
To construct a class from scratch. To develop a simulation driven by random numbers.
Getting started:
The Steps of the Lab:
  1. Open the simulation1 project. (Test each step as you complete it)
    Read

    The project simulation1 has an empty class called Bowling. You will implement a simple bowling simulation in stages. The game of bowling consists of throwing a heavy ball down an alley at a set of ten pins. A player bowls ten frames and, except for the tenth frame, has up to two throws per frame. If the player knocks down all ten pins on the first throw, the frame is complete. Otherwise, the player gets one more chance to knock down the rest of the pins. In the tenth frame, a bowler who knocks down ten pins on the first throw gets two additional throws; a bowler who knocks down a total of ten pins on the first two throws gets one additional throw. Otherwise, a bowler gets a total of two throws, as in any other frame.

    Note: Read the description above carefully, because our simulation simplifies the real-world rules of bowling; for example, there is no extra scoring for strikes or spares (apart from the handling of the tenth frame). In particular, be sure that you understand how the maximum score in our simulation is 120.

    Do

    Create the Bowling class by opening the class editor (double-click the Bowling class diagram) and create the class shell:

        public class Bowling {
    
    
        }
                       
    

    Add the following fields: firstBall, secondBall, extraBall, frame, and score, all of which are of type int. The class constructor should initialize all of these to 0.

     

    Write a method called updateScore. It will add firstBall and secondBall to score. If it is the 10th frame, it will also add extraBall to score.

     

    Write a void method called displayFrame. It will display the frame number, the first and second throws, and the total score so far in the following format:

        Results for frame # 1
          First ball: 8
          Second ball: 2
          Score so far: 10
                       
    

    If it is the 10th frame, it will display the frame as follows:

        **** This game is over - Final Score ****
        Results for frame # 10
          First ball: 9
          Second ball: 1
          Extra ball: 6
          Total for game: 95
                       
    

    Test this method before going to the next step of the lab. You may need to write temporary mutator methods to set firstBall, secondBall, extraBall and frame for the purpose of testing.

     

    Write a void method called bowlFrame:

    • If frame is 10 when entering the method, just invoke the displayFrame() method and exit.
    • Otherwise, increment frame and throw the first ball, (int)(Math.random()*11), recording the number of pins knocked down in firstBall.
    • If less than ten pins are knocked down, throw the second ball, knowing that it can knock down at most the number of pins left after the first ball was thrown (10 - firstBall). Record the number of pins in secondBall. Note: You will need to initialize secondBall to 0 each time in this method, in case a second ball is not thrown.
    • Also throw a second ball if the value of frame after incrementing it is 10, and all ten pins were knocked down on the first throw. In this case, all ten pins are available again on the second throw.
    • If the value of frame after incrementing it is 10, and all the pins were knocked down in the second throw, throw an extra ball in an attempt to knock down up to ten more pins. Record the result in extraBall. Note: You will need to initialize extraBall to 0 in case an extra ball is not thrown.
    • Finally, update the score by calling the method updateScore, display the frame results by calling displayFrame and exit the method.
     

    Write a method called reset. This method will initialize firstBall, secondBall, extraBall, score, and frame to 0.

    Play

    To play a game, simply create an instance of Bowling, reset, and invoke bowlFrame 10 times. Play the game two or three times to make sure it works. Make sure you invoke reset between games.

    Do

    Write an int method for the Bowling class called playGame that invokes bowlFrame 10 times. playGame should reset before playing, and should return the score of the game as an int.

Overview Schedule Announcements Resources Assignments Home

Valid HTML 4.01!Valid CSS!DePauw University , Computer Science Department , Spring 2005
Maintained by Brian Howard ( bhoward@depauw.edu ). Last updated