7

Athlete クラスの国と名前の 2 つの配列の最初の要素を印刷しようとしています。また、アスリートが行った 3 回のダイビング試行 (最初はゼロに設定) をシミュレートするオブジェクトも作成する必要があります。私はOOPが初めてで、メインでこれを行う方法がわかりません...コンストラクタに関する限り。これは私がこれまでに行ったことです...

これがメインです:

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]);   
    }
}

私は本当に何をすべきかわからない...

そして、これは配列を持つクラスです。

 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;
}

    }

ヘルプとご意見をお寄せいただきありがとうございます。ところで、他のクラスもありますが、関連するatmではありません..

4

6 に答える 6

5

まず、Athlete クラスに関してGetter and Setterは、インスタンス変数を のアクセス修飾子で宣言しているため、メソッドを削除できますpublic。から変数にアクセスできます<ClassName>.<variableName>

ただし、本当にそれを使用したい場合は、修飾子を代わりGetter and Setterに変更してください。publicprivate

次に、コンストラクターについては、 という単純な手法を実行しようとしていますshadowingShadowing宣言された変数と同じ名前のパラメーターを持つメソッドがある場合です。これは の例ですshadowing:
----------Shadowing sample----------
次のクラスがあります:

public String name;

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

たとえば、メイン メソッドでは、次のようにPersonクラスをインスタンス化します。
Person person = new Person("theolc");

変数nameは と等しくなり"theolc"ます。
----------End of shadowing----------

質問に戻りましょう。現在のコードで最初の要素を印刷したいだけの場合は、Getter and Setter. のパラメータを削除しますconstructor

public class Athlete {

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

public Athlete() {

}

あなたの主な方法では、これを行うことができます。

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

       System.out.println(art.name[0]);
       System.out.println(art.country[0]);
    }
}
于 2013-01-20T01:56:14.523 に答える
4

現在、 nameおよびcountryという名前の配列は、 Atheleteクラスのメンバー変数であるため、アクセスできません。

あなたがやろうとしているように見えることに基づいて、これは機能しません。

これらの配列はメインクラスに属しています。

于 2013-01-20T01:45:58.970 に答える
4

アスリート クラスでのあなたの試みは、アスリートのグループを扱っているように見えますが、これは設計上の問題です。

アスリートの属性を表すフィールドを使用して、1 人のアスリートを表すクラスを定義します。

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;
    }
}

次に、メイン メソッドは ti を次のように使用します。

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
        }
    }
}
于 2013-01-20T01:55:00.093 に答える
1

あなたは自分のしていることに少し混乱していると思います。アスリートはオブジェクトであり、アスリートには名前があり、私には彼が住んでいる都市があります。アスリートは潜ることができます。

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);

}
}
于 2013-01-20T01:51:52.983 に答える
0

まず、配列は無意味です。それらを取り除きましょう。配列が行っているのは、モック データに値を提供することだけです。モック オブジェクトを作成する方法についてはうんざりして議論されてきましたが、明らかに、偽のアスリートを作成するコードは単体テスト内にある必要があります。Joshua Bloch の Athlete クラス用の静的ビルダーを使用しますが、現時点では 2 つの属性しかないため、それらをコンストラクターに渡すだけです。次のようになります。

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);
    }
}

次に、ダイブ クラスの場合:

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;
    }

}

次に、単体テストを行い、クラスを構築して操作し、それらが機能していることを確認します。現在、彼らは何もしていないので、あなたができることは、あなたが入れているダイブを保持していると主張することだけです. 例:

@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));
    }

次に、アスリートなしでダイビングを構築できないことを確認するなどのテストを実行して追加できます。

アスリートの構成をテストのセットアップ メソッドに移動して、あらゆる場所で使用できるようにすることができます。ほとんどの IDE は、リファクタリングでそれを行うことをサポートしています。

于 2013-01-20T02:29:53.430 に答える
0

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);   

}
于 2013-01-20T01:52:40.823 に答える