我一直在试图弄清楚如何将跑步者信息添加到跑步者信息数组中。它最多应包含 100 名跑步者。
这是必须满足以下要求的大型项目的一部分:
操作(方法):
• 接受比赛名称和距离的构造函数。
• 名称和距离实例变量的getter 和setter。
• 返回添加到数组中的RunnerResult 对象的计数的方法。
• 将RunnerResult 添加到数组的方法(给定Runner 的实例和跑步者的完成时间)。
• 获取RunnerResult 对象的方法;一个接受添加 RunnerResult 的位置(直接从数组访问对象),另一个接受跑步者名称(用于搜索匹配的跑步者)。第一个跑步者的指数是0,第二个是1,等等。
• 一种具有条件逻辑的方法,用于计算某个类别(青年、成人、老年人、男性、女性、所有人)的所有跑步者,这些跑步者由作为整数传入的标志(1、2、3、4、5)触发, 6 分别作为公共常量实现)。类似的方法提供了每个潜在类别的平均比赛结果(完成比赛的时间)。
• 带有条件逻辑的方法可以找到比赛时间少于指定的每英里分钟数的跑步者。例如,查找所有完成比赛的每英里时间少于 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++;
}
}
}
}
我只是不知道如何将这些值放入数组中。(我得到一个不兼容的类型错误。任何输入将不胜感激。