frq problems

  • Part a:
  • Write the getScore method, which returns the score for the most recently played game. Each game consists of three levels. The score for the game is computed using the following helper methods. The isBonus method of the Game class returns true if this is a bonus game and returns false otherwise. The goalReached method of the Level class returns true if the goal has been reached on a particular level and returns false otherwise. The getPoints method of the Level class returns the number of points recorded on a particular level. Whether or not recorded points are earned (included in the game score) depends on the rules of the game, which follow. The score for the game is computed according to the following rules. Level one points are earned only if the level one goal is reached. Level two points are earned only if both the level one and level two goals are reached. Level three points are earned only if the goals of all three levels are reached. The score for the game is the sum of the points earned for levels one, two, and three. If the game is a bonus game, the score for the game is tripled
  • Part b:
  • Write the playManyTimes method, which simulates the play of num games and returns the highest game score earned. For example, if the four plays of the game that are simulated as a result of the method call playManyTimes(4) earn scores of 75, 50, 90, and 20, then the method should return 90. Play of the game is simulated by calling the helper method play. Note that if play is called only one time followed by multiple consecutive calls to getScore, each call to getScore will return the score earned in the single simulated play of the game. Complete the playManyTimes method. Assume that getScore works as intended, regardless of what you wrote in part (a). You must call play and getScore appropriately in order to receive full credit.
public class Level {
    int value;
    String name;
    boolean reached;
    public Level(int Value, String Name){ //sets up everything for the levels
        value = Value;
        name = Name;
    }
    public boolean goalReached() {
        return true;
    }
    public int getPoints() {    
        return value;
    }
}
import java.util.*;
public class Game {
    private static Level levelOne = new Level(100, "One"); //some minor edits to the three levels
    private static Level levelTwo = new Level(200, "Two");
    private static Level levelThree = new Level(500,"Three");
    public static int count = 0;    
    public static int winCount = 0;

    public Game() {
        
    }


    public boolean isBonus() {
        if (levelThree.goalReached()) {
            return true;
        } else {
            return false;
        }
    }


    public static void play() {
        Scanner Input = new Scanner(System.in);
        int integer = Input.nextInt();
        if (integer==1){
            levelOne.goalReached();
        }
        else if (integer == 2){
            levelTwo.goalReached();
        }
        else if (integer ==3){
            levelThree.goalReached();
        }
    }


    public static int getScore() {
        play();
        int score = 0;
        if (levelOne.goalReached()) {
            score += 200;
            if (levelTwo.goalReached()) {
                score += 100;
                if (levelThree.goalReached()) {
                    score += 500;
                }
            }
        }
        if (levelThree.goalReached()) {
            score *= 3;
        }
        return score;
    }


    public static int playManyTimes(int num) {// You can play however many times you want
        int max = 0;
        for (int i = 0; i < num; i++) {
            int score = getScore();
            if (score > max) {
                max = score;
            }
        }
        return max;
    }
    public static void main(String[] args){ //Main function helps run the code
        System.out.println("How many times do you want to play?");
        Scanner input = new Scanner(System.in);
        int playTimes = input.nextInt();
        System.out.println(playManyTimes(playTimes));
    }

}
Game.main(null);

Output: “How many times do you want to play?”

Summary:

  • 0.95/1
  • The code was able to run, however there were some bugs that I couldn’t fix: Always adding 2400 points everytime, all levels return true for no reason