1

ランナー情報をランナー情報の配列に追加する方法を見つけようとしています。最大で 100 個のランナーを含める必要があります。

これは、次の要件を満たす必要がある大規模なプロジェクトの一部です。

操作 (メソッド):

• レース名と距離を受け取るコンストラクター。

• 名前と距離の両方のインスタンス変数のゲッターとセッター。

• 配列に追加された RunnerResult オブジェクトの数を返すメソッド。

• RunnerResult を配列に追加するメソッド (Runner のインスタンスとランナーの終了時刻が与えられた場合)。

• RunnerResult オブジェクトを取得するメソッド。1 つは RunnerResult が追加された位置を受け取るもの (配列からオブジェクトに直接アクセスするため) であり、もう 1 つはランナー名を受け取るもの (一致するランナーを検索するために使用するため) です。最初のランナーのインデックスは 0、2 番目のランナーのインデックスは 1 などです。

• 整数 (1、2、3、4、5) として渡されたフラグによってトリガーされる、特定のカテゴリ (若者、成人、高齢者、男性、女性、すべて) のすべてのランナーの数を与える条件付きロジックを備えたメソッド。 、6、それぞれパブリック定数として実装)。同様の方法で、潜在的なカテゴリごとに平均レース結果 (レース終了までの時間) が得られます。

• 条件付きロジックを使用するメソッドは、レース時間が指定された分/マイル未満のランナーを見つけます。たとえば、1 マイルあたり 8 分未満のタイムでレースを終えたすべてのランナーを検索します。

• レース名、レース距離、レースの合計ランナー数、およびレースのすべてのランナーの平均時間を単純に与える toString メソッド。

これまでのところ、これは私が持っているものです:

public class Race
{
    // instance variables
    private String name;
    private double distance;
    private int nextPos;
    private RunnerResult [] results;


    // public constants

    /**
     * Flag to signify YOUTH.
     */
    public static final int YOUTH = 1;



    /**
     * Flag to signify ADULT.
     */
    public static final int ADULT = 2;

    /**
     * Flag to signify SENIOR.
     */
    public static final int SENIOR = 3;

    /**
     * Flag to signify MALE.
     */
    public static final int MALE = 4;

    /**
     * Flag to signify FEMALE.
     */
    public static final int FEMALE = 5;

    /**
     * Flag to signify ALL.
     */
    public static final int ALL = 6;

    /**
     * Array limit.
     */
    public static final int MAX_COUNT = 100;


    /**
     * Constructor for objects of class Race.
     *
     * @param  inName the race name.
     * @param  inDist the distance of the race.
     *
     */
    public Race(String inName, double inDist)
    {
        // initialize the instance variables and 
        // empty array of results, initalize nextPos
        this.name = inName;
        this.distance = inDist;
        RunnerResult[] results = new RunnerResult[100];
    }

    /**
     * Set the race Name.
     * 
     * @param  inName the race name.
     *  
     */
    public void setName(String inName)
    {
        this.name = inName;
    }

    /**
     * Get the race Name.
     * 
     * @return String The race name.
     *  
     */
    public String getName()
    {
        return this.name;
    }   

    /**
     * Set the race distance.
     * 
     * @param  inDist the distance of the Race.
     *  
     */
    public void setDist(double inDist)
    {
        this.distance = inDist;
    }

    /**
     * Get the race distance.
     * 
     * @return double the distance of the race.
     *   
     */
    public double getDist()
    {
        return this.distance;
    }    

    /**
     * Add a runner to the results
     * (runners are NOT entered in order of finish).
     * 
     * @param  inChip   the runner's chip id.
     * @param  inRunner a Runner object.
     * @param  inStart  the start time for the runner.
     * @param  inEnd    the end time for the runner.
     *  
     */
    public void addRunner(String inChip, Runner inRunner, Time inStart, Time inEnd)
    {
        if (this.nextPos < MAX_COUNT)
        {
            // set the instance field element to a "copy" of passed-in object
            // add to array, increment counter

            for(int i = 0; i < results.length; i++);
            {
                RunnerResult[] results = { copyinChip, copyinRunner, copyinStart,  
                    copyinEnd };

                i++;
            }
        }
    }  
}

これらの値を配列に入れる方法がわかりません。(互換性のない型エラーが発生します。入力をいただければ幸いです。

4

1 に答える 1

0

ここで2つのこと。

1.) を再宣言resultsすると、フィールドとして宣言したのと同じオブジェクトを参照するのではなく、まったく新しいオブジェクトを参照することになりますaddRunner

2.) 割り当てるときresults = { ---, ---, ---, ---};新しいランナーを配列に追加していません。むしろ、そのループを実行するたびに配列全体を再割り当てしています。新しいRunnerResultオブジェクトを作成し、それに必要なデータを追加してから、results[]; に配置します。

ここに例を示します:

public void addRunner(String inChip, Runner inRunner, Time inStart, Time inEnd)
{
    if (this.nextPos < MAX_COUNT)
    {
        // set the instance field element to a "copy" of passed-in object
        // add to array, increment counter

        for(int i = 0; i < results.length; i++);
        {      
            results[i] = new RunnerResult(<your params>);
        }
    }
}  
于 2013-11-29T23:52:54.943 に答える