Reputation: 7
// Create a method called displayHighScorePosition
// it should a players name as a parameter, and the 2nd parameter as a position in the high score table
// You should display the player's name along with a message like " managed to get into position " and the
// position they got and a further message " on the high score table".
//
// Create a 2nd method called calculateHighScorePosition
// it should be sent one argument only, the player score
// it should return an in
// the return data should be
// 1 if the score is >=1000
// 2 if the score is >=500 and < 1000
// 3 if the score is >=100 and < 500
// 4 in all other cases
// call both methods and display the results of the following
// a score of 1500, 900, 400 and 50
public class Challenge {
public static void main(String[] args) {
int position=calculateHighScorePosition(1500);
displayHighScorePosition("Stone",position);
position=calculateHighScorePosition(900);
displayHighScorePosition("shawn",position);
position=calculateHighScorePosition(400);
displayHighScorePosition("Simon",position);
position=calculateHighScorePosition(50);
displayHighScorePosition("sks",position); }
public static void displayHighScorePosition(String playerName, int position) {
System.out.println(playerName + " managed to get into position");
System.out.println(position + " on the high score table");
}
public static int calculateHighScorePosition(int playerScore) {
if(playerScore>1000){
return 1;
}else if(playerScore>500&&playerScore<1000) {
return 2;
}else if(playerScore>100&&playerScore<500) {
return 3;
}else {
return 4;
}
}
}
Upvotes: 0
Views: 93
Reputation: 4403
After the edits, the basic program seems OK, with the following caveat:
the return data should be
1 if the score is >=1000
2 if the score is >=500 and < 1000
3 if the score is >=100 and < 500
4 in all other cases
But the current tests omit the =
part of the test. As an example, try running a test case with the score equal to 500. What is the result?
So the checks should probably be like the following, incorporating both the =
and the note from @JohnyMopp about not needing the parts beyond the &&
:
if(playerScore >= 1000){
return 1;
} else if (playerScore >= 500) {
return 2;
} else if (playerScore >= 100) {
return 3;
} else {
return 4;
}
Writing some test cases at the boundaries is the best way to ensure the program logic functions correctly. So, in addition to the specified results, add some tests at 1000, 500, 100, 499, 99. Basically, start to think of the corner cases that can affect the outcome.
Some stdout examples here: https://ideone.com/sGi48C
Upvotes: 1