I am working on a program for myself that calculates a user's golf handicap and I am currently stuck on getting the Average Scores. All it does is print out an empty Array. The issues are happening in my HandicapIndexCalculator class. Any help is greatly appreciated. I haven't been coding for long so the problem might be apparent for some. Below is the code in order. MCVE at the end. The empty Array should contain the average of all scores.
My main class:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
UserProfile user = new UserProfile();
Attributes stats = new Attributes();
HandicapIndexCalculator handicap = new HandicapIndexCalculator();
System.out.print("\nWelcome to GolfMax! Your personal Handicap Calculator. \nTo begin, please enter your first and last name: ");
String userName = input.nextLine();
user.setName(userName);
user.readAndValidateName();
stats.getCourseAttributes(input);
handicap.getAverageScoreDifferential();
}
}
My Attributes class:
import java.util.Scanner;
public class Attributes {
Scanner input = new Scanner(System.in);
private final int MIN_COUNT = 5; private final int MAX_COUNT = 20;
private boolean validCount = false; public int count;
public double[] userCourseRating;
public double[] userSlopeRating;
public double[] userScores;
final int AVERAGE_DIFFICULTY = 113;
public void getCourseAttributes(Scanner input) {
System.out.print("\nPlease enter a minimum of 5 scores & maximum of 20 scores: ");
int userCount = input.nextInt();
while(!validCount) {
userScores = new double[userCount];
userCourseRating = new double[userCount];
userSlopeRating = new double[userCount];
if(userCount >= MIN_COUNT && userCount <= MAX_COUNT) {
validCount = true;
for(int i = 0; i < userCount; i ) {
System.out.printf("\nEnter Score #%d: ", i 1);
if(input.hasNextInt()) {
userScores[i] = input.nextInt();
}
System.out.printf("Enter Course Rating #%d: ", i 1);
if(input.hasNextDouble()) {
userCourseRating[i] = input.nextDouble();
}
System.out.printf("Enter Slope Rating #%d: ", i 1);
if(input.hasNextDouble()) {
userSlopeRating[i] = input.nextDouble();
}
}
}
else {
System.out.print("\nInvalid Input. \nPlease try again. \nPlease enter a minimum of 5 scores & maximum of 20 scores: ");
userCount = input.nextInt();
setScoreCount(userCount);
getScoreCount();
}
}
}
public void setScoreCount(int userCount) {
this.count = userCount;
}
public int getScoreCount() {
return count;
}
}
Lastly, this is where the problem is... I think :
import java.util.Arrays;
public class HandicapIndexCalculator {
private final int AVERAGE_DIFFICULTY = 113;
private double[] handicapIndex;
double[] averageScoreDifferential;
Attributes atr = new Attributes();
public void getAverageScoreDifferential() {
averageScoreDifferential = new double[atr.count];
for(int i = 0; i < averageScoreDifferential.length; i ) {
averageScoreDifferential[i] = (atr.userScores[i] - atr.userCourseRating[i]) * (AVERAGE_DIFFICULTY / atr.userSlopeRating[i]);
averageScoreDifferential[i] = Math.round(averageScoreDifferential[i] * 1000.0) / 1000.0;
}
Arrays.sort(averageScoreDifferential);
System.out.print(Arrays.toString(averageScoreDifferential));
}
}
MCVE:
Welcome to GolfMax! Your personal Handicap Calculator.
To begin, please enter your first and last name: Bob
User: Bob ID #66e038cf-1014-4d18-9e60-672f19f2ff6b
Please enter a minimum of 5 scores & maximum of 20 scores: 5
Enter Score #1: 78
Enter Course Rating #1: 61
Enter Slope Rating #1: 103
Enter Score #2: 80
Enter Course Rating #2: 61
Enter Slope Rating #2: 103
Enter Score #3: 82
Enter Course Rating #3: 61
Enter Slope Rating #3: 103
Enter Score #4: 85
Enter Course Rating #4: 61
Enter Slope Rating #4: 103
Enter Score #5: 90
Enter Course Rating #5: 61
Enter Slope Rating #5: 103
[]
CodePudding user response:
Because your Attributes in Main class and HandicapIndexCalculator are diffrent objects, so you need to assign in contructor of HandicapIndexCalculator or use setter as below:
class HandicapIndexCalculator { private final int AVERAGE_DIFFICULTY = 113; private double[] handicapIndex; double[] averageScoreDifferential; Attributes atr = null; public void setAttributes(Attributes atr) { this.atr = atr; } public void getAverageScoreDifferential() { ... } }
Main class:
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
UserProfile user = new UserProfile();
Attributes stats = new Attributes();
HandicapIndexCalculator handicap = new HandicapIndexCalculator();
handicap.setAttributes(stats);
...
}
}
Need to update count first time
class Attributes { ... public void getCourseAttributes(Scanner input) { System.out.print("\nPlease enter a minimum of 5 scores & maximum of 20 scores: "); int userCount = input.nextInt(); setScoreCount(userCount); ... } ... }
CodePudding user response:
(6th line of HandicapIndexCalculator):
Attributes atr = new Attributes();
That makes, well, the text spells it out, really, a new class. Which is not the same one you made during your main
. This new attributes starts out the way it is constructor - with counts at 0, the arrays uninitialized. You'd have to invoke method 'getCourseAttributes(Scanner)' on it for it to contain anything.
The solution seems trivial. Do not make a new one. Instead, pass the Attributes object you made in main which is filled in to the calculateHandicap
method.