String Array Object in Java

I am trying to print the first element on two arrays in class, country and name Athlete. I also need to create an object that simulates the three dives that the athlete had (which was originally set to zero). I am new to OOP and I don't know how to do this in my main ... as far as constructors go. This is what I have done so far ...

this is the main thing:

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'm just not sure what to do ...

And this is a class with 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; } } 

in advance for help and input! By the way, there are other classes that simply do not correspond to atm ..

+6
source share
6 answers

First , for your Athlete class, you can remove your Getter and Setter methods, because you have declared your instance variables with the public access public . You can access variables through <ClassName>.<variableName> .

However, if you really want to use this Getter and Setter , replace the public modifier with private .

Secondly, for the constructor you are trying to make a simple technique called shadowing . Shadowing is when you have a method that has 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 } 

For example, in your main method, you create an instance of the Person class as follows:
Person person = new Person("theolc");

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

Back to your question: if you just want to print the first element with your current code, you can remove Getter and Setter . Remove your options 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 can do this.

 public static void main(String[] args) { Athlete art = new Athlete(); System.out.println(art.name[0]); System.out.println(art.country[0]); } } 
+5
source

Currently, you cannot access arrays named name and country because they are member variables of your Athelete class.

Depending on how it looks, you are trying to do it, it will not work.

These arrays belong to your main class.

+4
source

Your attempt at an athlete class seems to be related to a group of athletes, which is a design mistake.

Define a class to represent one athlete with fields representing athlete 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 will use ti as follows:

 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 } } } 
+4
source

I think you are a bit confused with what you are doing. An athlete is an object, an athlete has a name, I have a city in which 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); } } 
+1
source

public static void main (String [] args) {

  public String[] name = {"Art", "Dan", "Jen"}; public String[] country = {"Canada", "Germant", "USA"}; // initialize your performance array here too. //Your constructor takes arrays as an argument so you need to be sure to pass in the arrays and not just objects. Athlete art = new Athlete(name, country, performance); } 
0
source

Firstly, arrays are meaningless, they allow you to get rid of them: all they do is provide values โ€‹โ€‹for the layout data. How you build a mock of objects was discussed ad nauseum, but obviously the code for creating fake athletes should be inside the unit test. I would use Joshua Bloch's static builder for the Athlete class, but you only have two attributes right now, so just pass them in the constructor. It will look like this:

 class Athlete { private String name; private String country; private List<Dive> dives; public Athlete(String name, String country){ this.name = name; this.country = country; } public String getName(){ return this.name; } public String getCountry(){ return this.country; } public String getDives(){ return this.dives; } public void addDive(Dive dive){ this.dives.add(dive); } } 

Then for the Dive class:

 class Dive { private Athlete athlete; private Date date; private double score; public Dive(Athlete athlete, double score){ this.athlete = athlete; this.score = score; this.date = new Date(); } public Athlete getAthlete(){ return this.athlete; } public Athlete getAthlete(){ return this.athlete; } public Athlete getAthlete(){ return this.athlete; } } 

Then create a unit test and just create classes and manipulate them, make sure they work. They are not doing anything right now, so all you can do is say that they keep the dives that you put into them. Example:

 @Test public void testThatDivesRetainInformation(){ Athlete art = new Athlete("Art", "Canada"); Dive art1 = new Dive(art, 8.5); Dive art2 = new Dive(art, 8.0); Dive art3 = new Dive(art, 8.8); Dive art4 = new Dive(art, 9.2); assertThat(art.getDives().size(), is(5)); } 

You can then go through the tests and add tests for such things to make sure you cannot build a dive without an athlete, etc.

You can move the design of the athletes into the test setup method so you can use it everywhere. Most IDEs have support for this with refactoring.

0
source

All Articles