Java-使用多个速率计算时间
我有一个家庭作业问题,我需要一些帮助。 我有一个探索机器人,我需要创建一个类,在这个类中,我可以命令它移动并返回有关其状态的各种不同信息 它的初始移动速率为5.0个单位。我用这个速率和它经过的距离来计算它探索的时间 我必须创建一个方法,允许用户多次更改速率,并且仍然计算它已经行驶的时间,每个部分都按照它当时行驶的速率计算 例如:机器人以5.0的速率移动50个字段,然后将20个字段的速率更改为6.0,然后将80个字段的速率更改为2.0。 沿着这些路线的东西 以下是我到目前为止对object类的介绍Java-使用多个速率计算时间,java,Java,我有一个家庭作业问题,我需要一些帮助。 我有一个探索机器人,我需要创建一个类,在这个类中,我可以命令它移动并返回有关其状态的各种不同信息 它的初始移动速率为5.0个单位。我用这个速率和它经过的距离来计算它探索的时间 我必须创建一个方法,允许用户多次更改速率,并且仍然计算它已经行驶的时间,每个部分都按照它当时行驶的速率计算 例如:机器人以5.0的速率移动50个字段,然后将20个字段的速率更改为6.0,然后将80个字段的速率更改为2.0。 沿着这些路线的东西 以下是我到目前为止对object类的介绍
private int xcoord, ycoord;
private int identification;
private double rate;
private double traveled;
//Sets up a robot with the given ID number and beginning x and y coordinates
public Robot (int id, int x, int y)
{
identification = id;
xcoord = x;
ycoord = y;
traveled = 0;
rate = 5.0;
}
//Has the robot travel to the set coordinates
public double setDestination (int x, int y)
{
double distance = Math.pow(x - xcoord, 2) + Math.pow(y - ycoord, 2);
traveled += Math.sqrt(distance);
xcoord = x;
ycoord = y;
return traveled;
}
//Gets the time spent travelling
public double getTimeSpent()
{
return traveled/rate;
}
//Sets the rate at which the robot travels
public void setRate(double setrate)
{
rate = setrate;
}
//Returns the ID of the robot
public int getID()
{
return identification;
}
我想我需要更改gettimespend()方法,但我不确定如何更改它,以便它以各自的速率完成旅程的每一段。现在设置时,它将以上次设置的速率返回整个旅程的时间
谢谢你的帮助。你就不能按照这个思路做点什么吗?几乎可以创建一个实例变量
timeSpent
并在setDestination
中使用该时间内行驶的距离而不是整个距离,使用当前速率更新该变量,并在gettimespend
中返回该变量
private int xcoord, ycoord;
private int identification;
private double rate;
private double traveled;
private double timeSpent;
//Sets up a robot with the given ID number and beginning x and y coordinates
public Robot (int id, int x, int y)
{
identification = id;
xcoord = x;
ycoord = y;
traveled = 0;
rate = 5.0;
}
//Has the robot travel to the set coordinates
public double setDestination (int x, int y)
{
double distance = Math.pow(x - xcoord, 2) + Math.pow(y - ycoord, 2);
traveled += Math.sqrt(distance);
xcoord = x;
ycoord = y;
timeSpent += Math.sqrt(distance)/rate;
return traveled;
}
//Gets the time spent travelling
public double getTimeSpent()
{
return timeSpent;
}
//Sets the rate at which the robot travels
public void setRate(double setrate)
{
rate = setrate;
}
//Returns the ID of the robot
public int getID()
{
return identification;
}
向该类介绍另一个成员,例如
travelLog
,这将是一个成对的列表TravelLogEntry:(rate,DistanceTraveledAtate)
——这可能是您必须创建的另一个类
大概是这样的:
List<TravelLogEntry> travelLog = new ArrayList<TravelLogEntry>();
在gettimespend()
中,迭代travelLog
并计算每个条目花费的时间,同时将时间累积到某个临时变量中:
long totalTimeSpent = 0;
// go throught all the previous rates/distances
for(TravelLogEntry entry: travelLog){
totalTimeSpent += entry.traveled/entry.rate;
}
// and don't forget the current rate:
totalTimeSpent += traveled/rate;
最后,totalTimeSpent
将包含机器人行驶的总时间,并根据不同的行驶速度进行调整
这应该能奏效
更新:没关系,我想,@austin的解决方案要简单得多。更新你在旅行中花费的时间是的,这很有效。就在你发帖的时候,我只是想用另一种方法尝试一下,而你却抢先一步。谢谢亲爱的,很高兴你也想到了!哈哈,这会管用的,如果OP需要查看它们的位置,它可能会比我的解决方案更好。我们还没有真正使用数组,尽管从我对Java的了解来看,这将是一个更有用的解决方案。奥斯汀的解决方案更简单,而且现在能满足我的需要。谢谢你的回答!
long totalTimeSpent = 0;
// go throught all the previous rates/distances
for(TravelLogEntry entry: travelLog){
totalTimeSpent += entry.traveled/entry.rate;
}
// and don't forget the current rate:
totalTimeSpent += traveled/rate;