jon carver
jon carver

Reputation: 35

JAVA, method to return the distance between various Points

I have a bit complex app to make bus routes and a lot of methods that help me to do that. This is the relevant code about my question:

My class Station:

public class Station {

    private int id;
    private Point position;
    private int capacity;
    private int currentPpl;

public Point getStationPosition() {
        return position;
    }
}   

My class Stop:

public class Stop {

    private Station station;
    private int nLiters;

public Station getStation() {
        return station;
    }
}

My class route:

public class Route {

    private int start;
    private Vector <Stop>stops=new Vector <Stop>();


 public Vector <Stop> getStops(){
    return stops;
}

Now in the class Bus (shown below) i have a method that calculates the distance between two points, the "getTimeDistance" method and now i want a method that returns all the time taken by the bus to travel trough all the stops using the "getTimeDistance" method by accessing the stops position and calculating one by one.

I will show what i developed so far to get this done in the method "getAllTime":

public class Bus {

    private int id;
    private int capacity;
    private int speed;

public double getTimeDistance(Point ini, Point end){

    double dist;
    double time;
    dist = Math.sqrt( (fim.x-ini.x)*(fim.x-ini.x) + (fim.y-ini.y)*(fim.y-ini.y) );
    time=dist/speed;

    return time;

    }

public getAllTime(Route r){

    int res = 0;
    Point pos=null;
    Stop s;

    for (int i=0; a<r.getStops().size();i++){
        s=i.getStops().get(i);
        pos=r.getStation().getStationPosition();

        res+=(int)getTimeDistance(pos, pos);

    }

    return res;

}

    }

This method "getAllTime" is obviously not working as i want, it returns "0" because it calculates two equal positions through the loop for.

I'm missing something important in my code in order to do that, trough the loop "for" i already have the positions of the stops.

What i want is to calculate the time between the 1st and the 2nd position, the 2nd and the 3rd, the 3rd and the fourth and so on and then returns the sum of all the times calculated.

Upvotes: 1

Views: 435

Answers (2)

Andreas
Andreas

Reputation: 159086

Since you are using Vector (don't, use ArrayList), you can iterate index positions like you do, and compare to next (or previous) stop.

However, just in case you change your mind, and use a LinkedList, or some other implementation with bad get(index) performance, use an iterator, and simply remember the previous stop.

public int getAllTime(Route route) {
    double totalTime = 0;
    Point prevPos = null;
    for (Stop stop : route.getStops()) {
        Point pos = stop.getStation().getStationPosition();
        if (prevPos != null)
            totalTime += getTimeDistance(prevStop, pos);
        prevPos = pos;
    }
    return (int)totalTime;
}

Code changes to also delay double to int rounding to the end, to previous cumulative rounding issues.


For a more real-world implementation, you need to account for the amount of time the bus is stopped at each intermediate bus stop.

Since not every bus stop is created equal, that might be a value of the stop (not the station, since different routes may stop for different amount of time).

You will of course not want to count the time from the first and last stops.

Upvotes: 2

MS Srikkanth
MS Srikkanth

Reputation: 4241

Assuming you took care of the comments above like using ArrayList instead of Vector, change your method to something like this:

public int getAllTime(Route r){
    int res = 0;
    //Am assuming you will have logic to check the route is valid and all 
    //like ensuring that there are altleast two stops. so leaving out all that
    Stop initial = r.getStops().get(0);
    for (int i=1; i<r.getStops().size();i++){
        Stop dest=r.getStops().get(i);
        Point iniP = initial.getStation().getStationPosition();
        Point destP = dest.getStation().getStationPosition();
        res+=(int)getTimeDistance(iniP, destP);
        initial = dest
    }
    return res;  
}

Upvotes: 1

Related Questions