I am trying to create a java program that calculates the average score of a team of n athletes. Each athlete has 3 scores.
1) Create an athlete class with the following fields:
- name
- array of 3 scores
- setters and getters (they must have the score number as argument.)
- a constructor that clears the data
2) another class that has:
- an array of athletes.
Note: the user operations to perform:
- add an athlete. the user can add athletes without scores and add scores later.
- add scores (the user must be prompt to enter the score number then add scores for all athlete for that score.)
Here is my code.
package demo;
public class Athlete {
private String name;
private int[] scores = new scores[3];
Athlete(){
this.name = "";
this.scores= new int[]{-1.0, -1.0, -1.0};
}
Athlete(String name, int[] scores){
this.name=name;
this.scores[0]= getscores(1);
this.scores[1]= getscores(2);
this.scores[2]= getscores(3);
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public void setscores(int scores, int scoresNumber){
grades[scoresNumber-1] = scores;
}
public int getscores(int scoresNumber){
return grades[scoresNumber-1];
}
public int[] getscores(){
scores[0]= getscores(1);
scores[1] = getscores(2);
scores[2] = getscores(3);
return scores;
}
}
//In this test, I am just testing for one athlete
My driver class is
package demo;
import java.util.Arrays;
import java.util.Scanner;
public class AthleteDriver {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
Athlete d = new Athlete();
String name;
int scoresNumber;
int score;
System.out.println("Name ");
name = sc.nextLine();
System.out.println("Score number ");
scoresNumber = sc.nextInt();
System.out.println("score ");
score = sc.nextInt();
d.setscoreGrades(score, scoresNumber);
System.out.println(d.getscores(scoresNumber));
System.out.println(d.getscores();
}
My problem is that I can't print out the array of scores. d.getscores()
gives me a reference value.
So how am I able to print out the array of scores?