Route planning system in java

Posted on 2006-04-14
Last Modified: 2012-05-05
I need to write a short java programme that could form the core of  a route planning system. Users will enter names of towns and the programme will calculate distances and likely times.
The programme should:
1 Calculate speed between towns
2 Calculate time taken to travel btwn them
3 Use Kilometers
4 Should enable users to enter an itinerary showing distance travelled and time at which each town is reached.
5 The itinerary should include the arrival and departure time at each stop.
6 The programme will recognise journeys for which the total travelling time(excluding stops) exceeds a statutory maximum.
7 The programme will provide some means by which the user can select a particular town when there's more than one.


i've got a town.txt file  which has all town information (3310line).
It consists of a list of place names, each line (or record) consists of a name (all upper case letters), a two letter county code, the latitude and longitude of the place expressed in seconds with east latitudes being positive and the grid coordinates (east and north) expressed in kilometres. Each item of a record is separated by a colon.

For example


This means that Brisbane is in Bromley (BR), has a latitude of 191520 seconds north (53° 12’) and a longitude of 10440 seconds west (2° 54’), a grid east coordinate of 339 km and a grid north coordinate of 367 km.

The distance between place 1 and place 2 (in kilometres) can be calculated using the formula

Distance =(Math.pow((grid east 1 – grid east 2),2) + Math.pow((grid north 1 – grid north 2),2)) ;

How do i write a prgramme that accesses the information in the file and manipulate the information in the file to enable users to interact with the programme.
town.txt has got all the towns in the above format.

Please contact me if you need to ask any questions, re- my question

thanking you in advance

<changed by CS request by GranMod>
Question by:RamsTheDon
    LVL 1

    Accepted Solution

    Create a class to represent a Town and have its constructor accept a string formatted like one of the entries in the list of towns. In the constructor, split the string by colons to extract the individual values. Also write methods for accessing a Town's values and performing calculations. In the main class, read the towns file line-by-line using a BufferedReader. Create an instance of Town for each line and store it in a vector in the main class. When you receive user input, find the Towns you need in the vector and call the appropriate methods to perform operations on them.

    This is a very rough outline of what I think you need. It's by no means complete, and I haven't tried it out:
    // Main class, in
    class TownsApp {
      private Vector towns;
      public static void main(String[] args) {
         try {
            BufferedReader in = new BufferedReader(new FileReader("towns.txt"));
            String str;
            while ((str = in.readLine()) != null) {
                towns.add(new Town(str));
        } catch (IOException e) {

    // Town class, in
    class Town {
      // Members
      private String name;
      private float lat;
      private float lon;
      private float e_coord;
      private float n_coord;
      private String county;

      // Constructors
      Town(String info) {
        String[] f = info.split(":");
        // Put in code for converting types.
      // Define accessors and mutators
      String getName() { return name; }
      String getCounty() { return county; }
      float getLat() { return lat; }
      float getLon() { return lon; }
      float getECoord() { return e_coord; }
      float getnNCoord() { return n_coord; }

      // Methods
      float getDistance(Town t) {
        // Computes distance to another town
        // Put implementation here

      // These two methods override the Object class's equals method. I think you need them for using the Vector class's search methods.
      boolean equals(Town t) {
       // Compare towns

      boolean equals(String n) {
        // Compare towns by name
      // Define additional methods
    LVL 23

    Assisted Solution

    great job abalonia you are very clear in your approach. The program given above is very rough and simple implmentation.

    And i have only one suggestion to make the program even lighter & effecient.

    just change

    >> private Vector towns;


     private ArrayList towns;

    ArrayList is faster than Vector,but not thread safe and for other things check

    Author Comment

    Thank you very much abolonia , i'll try what you suggested after the hols, I have tried the programme and still coming with a few errors but will have a proper look as soon as i get enough time. Shivaspk i'll try to use the  private Arraylist towns; in place of >>private Vector towns. What do you think of the formula  Distance = &#8730;((grid east 1 – grid east 2)2 + (grid north 1 – grid north 2)2) to get distance between 2 towns, how do i incorporate it?

    Thanks guys
    LVL 23

    Expert Comment

     Distance =((grid east 1 – grid east 2)2 + (grid north 1 – grid north 2)2)

    i think this must be the formula as it the distance formula, used to find distance between two potnts.

      Distance =(Math.pow((grid east 1 – grid east 2),2) + Math.pow((grid north 1 – grid north 2),2)) ;

    just replace the variables in the above equation suitably.
    LVL 1

    Expert Comment

    Hmmm, well, I guess I could have gotten at least an assisted answer if I had replied sooner. Oh well, sorry for the delay in replying. I think you need a square root in the distance formula:
     Distance =Math.sqrt((Math.pow((grid east 1 – grid east 2),2) + Math.pow((grid north 1 – grid north 2),2))) ;
    LVL 23

    Expert Comment

    thanks abalonia I was just about to correct that equation.

    Thanks for correcting.


    Author Comment

    abalonia thank you for the correction. The both of you(abolonia and shivaspk)have been very helpful  and i appreciate it.I'm still a novice and still finding my way around java and am easily confused about these codes.
    I'm still cracking my head as i'm still getting numerous errors but i'm sure i'm on the right path with your assistance.

    Thank you
    LVL 23

    Expert Comment

    I think you need to close the question again.

    And please split the points.

    Thank You.

    Author Comment

    Thanks shivaspk , i had requested community support to re-open the question because i realised i had not awarded any points to abalonia. This was a mistake on my part.
    Once again thank you.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Suggested Solutions

    Title # Comments Views Activity
    either24  challenge 19 73
    Fix45 challenge 15 65
    triangle challenge 4 35
    wordcount challenge 11 46
    Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
    Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
    In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

    759 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    10 Experts available now in Live!

    Get 1:1 Help Now