Reputation: 15
I was wondering if when I pass a value into a method call. Is there a way to grab the value directly from the method call?
int playerPosition = calculateHighScorePosition(1500); //<-- Is there a way to grab this passed value?
displayHighScorePosition("Perseus", playerPosition, 1500);//<-- Instead of hardcoding it in?
Here is the full code in question I was wondering about:
package com.company;
import org.w3c.dom.ls.LSOutput;
public class Main {
public static void main(String[] args) {
/*boolean gameOver = true;
int score = 800;
int levelCompleted = 5;
int bonus = 100;
int highScore = calcScore(gameOver, score, levelCompleted, bonus);
System.out.println("Your final score was " + highScore);
score = 10000;
levelCompleted = 8;
bonus = 200;
highScore = calcScore(gameOver, score, levelCompleted, bonus);
System.out.println("Your final score was " + highScore);*/
int playerPosition = calculateHighScorePosition(1500); //<-- Is there a way to grab this passed value?
displayHighScorePosition("Perseus", playerPosition, 1500);//<-- Instead of hardcoding it in?
playerPosition = calculateHighScorePosition(900);
displayHighScorePosition("Achilles", playerPosition, 900);
playerPosition = calculateHighScorePosition(400);
displayHighScorePosition("Hercules", playerPosition, 400);
playerPosition = calculateHighScorePosition(50);
displayHighScorePosition("Leonidas", playerPosition, 50);
}
/*public static int calcScore(boolean gameOver, int score, int levelCompleted, int bonus){
if(gameOver){
int finalScore = score + (levelCompleted * bonus);
finalScore += 2000;
return finalScore;
} else {return -1;}
}*/
public static int calculateHighScorePosition(int highScore){
if (highScore >= 1000){
return 1;
} else if (highScore >= 500){
return 2;
} else if (highScore >= 100){
return 3;
} return 4;
}
public static void displayHighScorePosition(String playerName,int playerPosition, int highScore){
System.out.println(playerName + " managed to get into position "
+ playerPosition + " by scoring " + highScore + " points.");
}
}
Upvotes: 0
Views: 56
Reputation: 361585
This is a good opportunity to extract a method, a bread and butter refactoring technique.
private static void calculateAndDisplayHighScorePosition(String name, int highScore) {
int playerPosition = calculateHighScorePosition(highScore);
displayHighScorePosition(name, playerPosition, highScore);
}
(You can probably come up with a better, shorter name.)
This gets rid of the repetitive logic in main()
:
calculateAndDisplayHighScorePosition("Perseus", 1500);
calculateAndDisplayHighScorePosition("Achilles", 900);
calculateAndDisplayHighScorePosition("Hercules", 400);
calculateAndDisplayHighScorePosition("Leonidas", 50);
You could further reduce the repetition by saving the threshold scores in parallel arrays:
String[] names = {"Perseus", "Achilles", "Hercules", "Leonidas"};
int[] scores = {1500, 900, 400, 50};
for (int i = 0; i < names.length && i < scores.length; i++) {
calculateAndDisplayHighScorePosition(names[i], scores[i]);
}
Upvotes: 5
Reputation: 1152
You would do this by assigning it to a variable and passing that to both methods
int perseusPos = 1500;
int playerPosition = calculateHighScorePosition(perseusPos);
displayHighScorePosition("Perseus", playerPosition, perseusPos);
Upvotes: 2