Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

String Array object in Java

Tags:

java

arrays

oop

I am trying to print the first element on the two arrays in my Athlete class, country and name. I also need to create a object that simulates three dive attemps an athlete had (that is initially set to zero). I am new to OOP and I dont know how to go abouts doing this in my main... as far as constructors go. This is what i have done so far...

this is the main:

import java.util.Random;
import java.util.List;


public class Assignment1 {
public static void main(String[] args) {
            Athlete art = new Athlete(name[0], country[0], performance[0]);   
    }
}

I just really am not sure what to do...

And this is the class with the arrays.

 import java.util.Random;
 import java.util.List;

 public class Athlete {

public String[] name = {"Art", "Dan", "Jen"};
public String[] country = {"Canada", "Germant", "USA"};
    //Here i would like to create something that would be representing 3 dive attemps  (that relate to dive and score. eventually.)

 Athlete(String[] name, String[] country, Performance[] performance) {
    this.name = name;
    this.country=country;
    this.performance=performance;

}



public Performance Perform(Dive dive){
    dive.getDiveName();
    return null;        
}

public String[] getName() {
    return name;
}

public void setName(String[] name) {
    this.name = name;
}

public String[] getCountry() {
    return country;
}

public void setCountry(String[] country) {
    this.country = country;
}

    }

thanks in advance for any help and input! btw there is other classes too, just not relevant atm..

like image 429
choloboy Avatar asked Jan 20 '13 01:01

choloboy


4 Answers

First, as for your Athlete class, you can remove your Getter and Setter methods since you have declared your instance variables with an access modifier of public. You can access the variables via <ClassName>.<variableName>.

However, if you really want to use that Getter and Setter, change the public modifier to private instead.

Second, for the constructor, you're trying to do a simple technique called shadowing. Shadowing is when you have a method having a parameter with the same name as the declared variable. This is an example of shadowing:
----------Shadowing sample----------
You have the following class:

public String name;

public Person(String name){
    this.name = name; // This is Shadowing
}

In your main method for example, you instantiate the Person class as follow:
Person person = new Person("theolc");

Variable name will be equal to "theolc".
----------End of shadowing----------

Let's go back to your question, if you just want to print the first element with your current code, you may remove the Getter and Setter. Remove your parameters on your constructor.

public class Athlete {

public String[] name = {"Art", "Dan", "Jen"};
public String[] country = {"Canada", "Germany", "USA"};

public Athlete() {

}

In your main method, you could do this.

public static void main(String[] args) {
       Athlete art = new Athlete();   

       System.out.println(art.name[0]);
       System.out.println(art.country[0]);
    }
}
like image 194
Michael 'Maik' Ardan Avatar answered Oct 13 '22 18:10

Michael 'Maik' Ardan


Currently you can't access the arrays named name and country, because they are member variables of your Athelete class.

Based on what it looks like you're trying to do, this will not work.

These arrays belong in your main class.

like image 21
jahroy Avatar answered Oct 13 '22 20:10

jahroy


Your attempt at an athlete class seems to be dealing with a group of athletes, which is a design fault.

Define a class to represent a single athlete, with fields that represent the athlete's attributes:

public class Athlete {
    private final String name;
    private final String country;
    private List<Performance> performances = new ArrayList<Performance>();
    // other fields as required

    public Athlete (String name, String country) {
        this.name = name;
        this.country = country;
    }
    // getters omitted

    public List<Performance> getPerformances() {
        return performances;
    }

    public Performance perform(Dive dive) {
        // not sure what your intention is here, but something like this:
        Performance p = new Performance(dive, this);
        // add new performance to list
        performances.add(p);
        return p;
    }
}

Then your main method would use ti like this:

public class Assignment1 {
    public static void main(String[] args) {
        String[] name = {"Art", "Dan", "Jen"};
        String[] country = {"Canada", "Germant", "USA"};
        Dive[] dive = new Dive[]{new Dive("somersault"), new Dive("foo"), new Dive("bar")};
        for (int i = 0; i < name.length; i++) {
            Athlete athlete = new Athlete(name[i], country[i]);
            Performance performance = athlete.perform(dive[i]);   
            // do something with athlete and/or performance
        }
    }
}
like image 29
Bohemian Avatar answered Oct 13 '22 18:10

Bohemian


I think you are a little messed up with what you doing. Athlete is an object, athlete has a name, i has a city where he lives. Athlete can dive.

public class Athlete {

private String name;
private String city;

public Athlete (String name, String city){
this.name = name;
this.city = city;
}
--create method dive, (i am not sure what exactly i has to do)
public void dive (){} 
}




public class Main{
public static void main (String [] args){

String name = in.next(); //enter name from keyboad
String city = in.next(); //enter city form keybord

--create a new object athlete and pass paramenters name and city into the object
Athlete a = new Athlete (name, city);

}
}
like image 1
lejadina Avatar answered Oct 13 '22 19:10

lejadina