Java

OOP GUIDE / WORK

 

GAME CLASS

 

Topics

  • Class design

 

Source

  • This question was part of the 2022 AP exam free response questions.  You can find a link to all free response questions on the previous page.

 

 

TASK – PART 1 – SETUP – STUDYPRACTICE INTERFACE

 

Copy and paste the following code into your IDE:

 

 

public class Level

{

     private boolean goalReached;

     private int points;

    

     /* Creates a random Level result */

    

     public Level()

     {

           if (Math.random() <= 0.5)

                goalReached = true;

           else

                goalReached = false;

          

           points = (int)(Math.random() * 6) * 100;  //0, 100, 200, ..., 500

     }

    

     /* Create a Level result based on parameters */

    

     public Level (int pts, boolean gr)

     {

           points = pts;

           goalReached = gr;

     }

    

     public boolean goalReached()

     {

           return goalReached;

     }

    

     public int getPoints()

     {

           return points;

     }

}

 

 

public class Game

{

     private Level levelOne;

     private Level levelTwo;

     private Level levelThree;

     private boolean isBonus;

    

     /** This constructor creates a random Game result. */

    

     public Game()

     {

           play(); 

     }

    

     /** This constructor creates a Game with provided parameters.

      *  This is required to test the getPoints results from the question.

      */   

    

     public Game(Level l1, Level l2, Level l3, boolean isBonus)

     {

           levelOne = l1;

           levelTwo = l2;

           levelThree = l3;

           this.isBonus = isBonus;

     }

    

     /** Returns true if this game is a bonus game and returns false otherwise. */

    

     public boolean isBonus()

     {

           return isBonus;

     }

    

     /** Simulates the play of this Game (consisting of three levels) and

      *  updates all relevant game data

      */

    

     public void play()

     {

           levelOne = new Level();

           levelTwo = new Level();

           levelThree = new Level();

           if (Math.random() <= 0.50)

                isBonus = true;

           else

                isBonus = false;

     }

    

     /** Returns the score earned in the most recently played game,

      *  as described in part (a)

      */

    

     public int getPoints()

     {

           //TO IMPLEMENTS IN PART A

     }

    

     /** Simulates the play of num games and returns the highest

      *  score earned, as described in (b)

      *  Precondition: num > 0

      */

    

     public int playManyTimes(int num)

     {

           //TO IMPLEMENTS IN PART B

     }

}

 

 

 

TASK – PART 2 – WORK

 

Click here for a PDF containing the work that you must do.

 

 

TASK – PART 3 – TESTING YOUR CLASS

 

Copy and paste the following code that will test your Game class.  Note that the program below matches the examples from the questions so you should compare your results with the results in the examples.  You should also consider adding your own tests.

 

public class GameTester

{

     public static void main(String[] args)

     {

           Level l1 = new Level(200, true);

           Level l2 = new Level(100, true);

           Level l3 = new Level(500, true);

           Game g1 = new Game(l1, l2, l3, true);

           System.out.println(g1.getPoints());

          

           Level l4 = new Level(200, true);

           Level l5 = new Level(100, true);

           Level l6 = new Level(500, false);

           Game g2 = new Game (l4, l5, l6, false);

           System.out.println(g2.getPoints());

          

           Level l7 = new Level(200, true);

           Level l8 = new Level(100, false);

           Level l9 = new Level(500, true);

           Game g3 = new Game(l7, l8, l9, true);

           System.out.println(g3.getPoints());

          

           Level l10 = new Level(200, false);

           Level l11 = new Level(100, true);

           Level l12 = new Level(500, true);

           Game g4 = new Game(l10, l11, l12, false);

           System.out.println(g4.getPoints());

          

           //The following is not from the question but rather

           //just a test of the playManyTimes method.

          

           Game g5 = new Game();

          int maxPoints = g5.playManyTimes(5);

           System.out.println("Max points: " + maxPoints);

     }

}