1

I am building a program that simulates a dive competition. I have a class Athlete that essentially is trying to access an array element in another class. Would anyone know how I can access the data?

Here is the Athlete class:

public class Athlete {

    public static String[] name = { "Art Class", "Dan Druff", "Jen Tull" };
    public static String[] country = { "Canada", "Germany", "USA" };
    Performance[] performance = new Performance[2];

    public Athlete(String[] name, String[] country) {
        this.name = name;
        this.country = country;
        // this.event = event;
    }

    public void perform() {
        Dive mydive = new Dive(Dive.diveName, Dive.difficulty);
        Performance event = new Performance(event.dive, Performance.judgeScores);
        performance[0] = event(event.dive.diveNames[0], event.judgeScores); // here
        performance[1] = event; // Here
        performance[2] = event; // Here
    }

    public void printResults() {
    }
}

Where indicated with "//Here" I am trying to access the data from the classes that are shown below, and I'm wondering how can I do that?

Performance:

public class Performance {

    Dive dive = new Dive(Dive.diveName, Dive.difficulty);
    public static float[] judgeScores = new float[7];

    public Performance(Dive dive, float[] judgeScores) {
        this.dive = dive;
        // this.dive=difficulty;
        this.judgeScores = judgeScores;
        // this.judgeScores = judgeScores;
    }
}

Dive:

import java.util.Random;

public class Dive {
    public static String diveName;
    public static int difficulty;

    public static final String[] diveNames = { "reverse pike", "forward pike",
            "reverse armstand with double somersault", "reverse triple twist",
            "double forward with triple somersault", "cannon ball" };
    public static final int[] diveDifficulties = { 3, 2, 2, 4, 4, 1 };

    public static Dive chosenRandomly() {
        Random rand = new Random();
        int num = rand.nextInt(6) + 1;
        String diveName = diveNames[num];
        int difficulty = diveDifficulties[num];

        Dive dive = new Dive(diveName, difficulty);
        return dive;
    }

    public Dive(String diveName, int difficulty) {
        this.diveName = diveName;
        this.difficulty = difficulty;
    }
}

Are my constructors all wrong?

PS: This is homework so some people might not find my method to be the appropriate way to tackle the problem, but please keep in mind that I'm following the instructions.

Thanks in advance for any input!

jww
  • 97,681
  • 90
  • 411
  • 885
choloboy
  • 795
  • 4
  • 16
  • 38
  • 1
    If you are using an IDE that can automatically format your code, I suggest you to repost formatted code to enhance readability of your question. – giampaolo Jan 20 '13 at 19:50

2 Answers2

1

Usually this is not a good idea to access fields from another classes (even public). You should create getters for your array fields and access them.

Community
  • 1
  • 1
Archer
  • 5,073
  • 8
  • 50
  • 96
1

Assume that you are working in a directory called Javadir, and that you create four files, whose contents are shown below.

file 1

package ListPkg;
public class List { ... }
class ListNode {...}

file 2

package ListPkg;
public class NoNextItemException { ... }

file 3

public class Test { ... }
class Utils { ... }

file 4

class Test2 { ... }

Here are the directories and file names you must use:

File 1 must be in a subdirectory named ListPkg, in a file named List.java.

File 2 must also be in the ListPkg subdirectory, in a file named NoNextItemException.java.

File 3 must be in a file named Test.java (in the Javadir directory).

File 4 can be in any .java file (in the Javadir directory).

For more guidelines, go through this notes on java packages

sr01853
  • 6,043
  • 1
  • 19
  • 39