Methods and Controls hw
my hw for methods and controls frq team teach
Part A
public int getScore() {
int score = 0;
if (levelOne.goalReached()) {
score += levelOne.getPoints();
if (levelTwo.goalReached()) {
score += levelTwo.getPoints();
if (levelThree.goalReached()) {
score += levelThree.getPoints();
}
}
}
if (isBonus()) {
score *= 3;
}
return score;
}
- Level One Condition: Points are added only if levelOne.goalReached() is true.
- Level Two Condition: Points for Level Two are added only if both levelOne.goalReached() and levelTwo.goalReached() are true.
- Level Three Condition: Points for Level Three are added only if levelOne.goalReached(), levelTwo.goalReached(), and levelThree.goalReached() are all true.
- Bonus Game Multiplier: If the game is a bonus game (isBonus() returns true), the total score is multiplied by 3.
Part B
public int playManyTimes(int num) {
int maxScore = 0;
for (int i = 0; i < num; i++) {
play();
int currentScore = getScore();
if (currentScore > maxScore) {
maxScore = currentScore;
}
}
return maxScore;
}
Initialization (maxScore = 0): Starts with maxScore set to 0 because scores are always non-negative.
Loop Through num Simulations: A for loop runs num times to simulate playing the game multiple times.
Play the Game: play() is called in each iteration to simulate one complete game.
Get the Current Score: getScore() retrieves the score from the most recent game simulation.
Update Maximum Score: If the currentScore is higher than the maxScore, it updates maxScore.
Return the Highest Score: After all games are simulated, the method returns the highest score obtained.