0

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?

Michael
  • 41,989
  • 11
  • 82
  • 128
  • 2
    Possible duplicate of [What's the simplest way to print a Java array?](http://stackoverflow.com/questions/409784/whats-the-simplest-way-to-print-a-java-array) – Jérôme Apr 03 '17 at 16:13
  • Have you actually tried to run this code? It is full of compilation errors. I would fix those first before asking a question here. – Luke Bajada Apr 03 '17 at 16:14
  • where do grades[] come from? I didnt see it declared in the Athelete class – 0xDEADBEEF Apr 03 '17 at 16:16
  • @bajada93 considering OP has mentioned his/hers current code displays the reference of the array rather than its contents that could potentially mean it's compiling fine. – Ousmane D. Apr 03 '17 at 16:20
  • @jobethbillien considering OP has mentioned his/hers current code is displaying the reference of the array rather than its contents that could potentially mean it's compiling fine hence he/she probably didn't include that array within the provided code. – Ousmane D. Apr 03 '17 at 16:21

1 Answers1

0

my problem is that I can't print out the array of scores d.getscores() it gives me a reference value

use Arrays.toString(type[ ] a), which returns a string representation of the contents of the specified array.

note - this method is overloaded so it should work for most types (if not all).

So how am I able to print out the array of scores?

replace this:

System.out.println(d.getscores());

with this:

System.out.println(Arrays.toString(d.getscores()));
Ousmane D.
  • 54,915
  • 8
  • 91
  • 126