mardi 10 mars 2015

Java - how to get maximum result of a calculation



We are working on creating object and driver classes. I have an object class that does various things to a moving exploratory robot.


What I need to do now is create a method that returns the largest distance that the robot moved in one single move command. I also need to return the time that it took to move that distance.


Here's the relevant code for that so far:



{

private int xcoord, ycoord; //Cartesian coordinates of the robot
private int identification; //Identification number of the robot
private double rate; //Rate at which the robot explores
private double traveled; //Distance the robot has travelled
private double timeSpent; //Time spent travelling
private double longestLeg; //Longest leg of the journey
private double longestLegTime; //Time on the longest leg

//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 longest leg of the robot's travels
public int getLongestLeg()
{
return longestLeg;
}

//Returns time of longest leg
public double getLongestLegTime()
{
return longestLegTime;
}


I'm not allowed to use if statements or loops yet, so it will have to be using Math.max I'm guessing. I tried using it, but it gave me an error saying that it required an int but I supplied a double.


Any suggestions would be awesome. Thanks!


If you are able, I have one final problem with the code as well. I need to create a method that would get the distance between two Robot objects. I'm not even sure how to start this one since we haven't really worked with it yet. A suggestion on how to even start this would be great. Thanks again.




Aucun commentaire:

Enregistrer un commentaire