public class Level
{
private int points;
private boolean goalReached;
public Level( int p, boolean gReached )
{
points = p;
goalReached = gReached;
}
/** Returns true if the player reached the goal on this level
and returns false otherwise */
public boolean goalReached()
{
return goalReached;
}
/** Returns the number of points (a positive integer) recorded
for this level */
public int getPoints()
{
return points;
}
}
|
public class Game
{
private Level levelOne;
private Level levelTwo;
private Level levelThree;
private boolean bonus;
/** Postcondition: All instance variables have been initialized. */
public Game()
{ /* Ignore the implementation */
}
/** Returns true if this game is a bonus game and returns false otherwise */
public boolean isBonus()
{ /* Ignore the implementation */
return bonus;
}
/** Simulates the play of this Game (consisting of three levels) and updates all relevant
* game data
*/
public void play()
{ /* Ignore the implementation */
levelOne = new Level( points[nextGame][0], gReach[nextGame][0]);
levelTwo = new Level( points[nextGame][1], gReach[nextGame][1]);
levelThree = new Level( points[nextGame][2], gReach[nextGame][2]);
bonus = bonusStatus[nextGame];
nextGame++;
}
/** Returns the score earned in the most recently played game, as described in part (a) */
public int getScore()
{ /* to be implemented in part (a) */
return 0; // return statement required for compilation
// delete it when you answer this question
}
/** Simulates the play of num games and returns the highest score earned, as
* described in part (b)
* Precondition: num > 0
*/
public int playManyTimes(int num)
{ /* to be implemented in part (b) */
return 0; // return statement required for compilation
// delete it when you answer this question
}
// ----------------- Data to simulate 6 game plays -------
public int[][] points = { {7, 8, 2},
{7, 7, 7},
{6, 7, 4},
{7, 6, 9},
{1, 3, 8},
{8, 8, 8},};
public boolean[][] gReach = { {true, true, false},
{true, true, true},
{true, true, false},
{true, true, true},
{false, false, true},
{true, true, true}};
public boolean[] bonusStatus = { false, true, true, false, false, true };
public int nextGame = 0;
}
|
Use this Java program to test your answer to question A:
public class TestPartA
{
public static void main(String[] args)
{
Game x = new Game();
x.play();
System.out.println( x.getScore() );
x.play();
System.out.println( x.getScore() );
}
}
|
The correct answer is: 15 and 63
Use this Java program to test your answer to question B:
public class TestPartB
{
public static void main(String[] args)
{
Game x = new Game();
System.out.println( x.playManyTimes( 4 ));
Game y = new Game();
System.out.println( y.playManyTimes( 6 ));
}
}
|
The correct answer is: 63 and 72