Skip to Main Content

New to Java

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Distance Between cities

843789May 12 2010 — edited May 13 2010
This is my first Java class. Honestly for this being only my second week and my second project I think there asking a lot for someone just learning. It took me 5 hours to put together what I have not even knowing if its what's being asked of me exactly. Here are the instructions.

Create a Java program that prompts the user for a list of cities, where each city has a name and x and y coordinates. After all cities have been entered, the program should use a recursive algorithm to print the length of all possible routes that start at the first city entered and end at the last city entered, and visit every city in the list. For each route, the program should print the name of each city visited, followed by the length of the route. (Note: See the Distance equation button in the resource bar above.)

1. Create a program to prompt the user for cities along with the (x, y) coordinate
2. Program computes recursively the length (distance) of all possible routes
3. Print the name of each city visited along with the length of the route

Here is what I have and I'm stuck beyond this point how to finish it. I've been on many forums and no one really has any answers. Hopefully someone here can help out.
package Citydistance;

import java.util.*;


public class Cityroute {

public static void main(String[] args)
          {
                 Scanner scanner = new Scanner (System.in);
                 LinkedList citiesList = new LinkedList();
                 LinkedList yList = new LinkedList();
                 LinkedList xList = new LinkedList();

                 String city = "";
                 double x = 0;
                 double y = 0;
                 int choice = 0;

                  // First run to enter the starting location.
                 System.out.println("Enter the name of the city:");
                 city = scanner.next();
                 citiesList.addFirst(city);
                 System.out.println("Enter the X coordinate");
                 x = scanner.nextDouble();
                 xList.addFirst(x);
                 System.out.println("Enter the Y coordinate");
                 y = scanner.nextDouble();
                 yList.addFirst(y);

                 System.out.println("City: " + city + " X " + x + " Y " + y);
                 System.out.println("CityList: " + citiesList + " X " + xList + " Y " + yList);

                 System.out.println();
                 System.out.println("Pick one of the following choices:");
                 System.out.println(" 1. Enter another city");
                 System.out.println(" 2. Enter destination");
                 System.out.println(" 3. Exit");

                 System.out.println();
                 choice = scanner.nextInt();

                 
                 while(choice == 1)
                 {
                        switch(choice)
                        {
                           // enter additional cities
                           case 1:
                                  System.out.println("Enter the name of the city:");
                                  city = scanner.next();
                                  citiesList.add(city);
                                  System.out.println("Enter the X coordinate");
                                  x = scanner.nextDouble();
                                  xList.add(x);
                                  System.out.println("Enter the Y coordinate");
                                  y = scanner.nextDouble();
                                  yList.add(y);

                                  System.out.println("City: " + city + " X " + x + " Y " + y);
                                  System.out.println("CityList: " + citiesList + " X " + xList + " Y " + yList);

                                                // ask for next choice
                                  System.out.println();
                                  System.out.println("Pick one of the following choices:");
                                  System.out.println(" 1. Enter another city");
                                  System.out.println(" 2. Enter destination");
                                  System.out.println(" 3. Exit");

                                  System.out.println();
                                  choice = scanner.nextInt();

                                  break;

                           case 3:
                                        // Exit
                                  System.out.println("Good bye");
                                  System.exit(0);
                                  break;

                        }// end switch
                 }// end loop

                        // Enter final destination
                 System.out.println("Enter your final destination:");
                 city = scanner.next();
                 citiesList.addLast(city);
                 System.out.println("Enter the X coordinate");
                 x = scanner.nextDouble();
                 xList.addLast(x);
                 System.out.println("Enter the Y coordinate");
                 y = scanner.nextDouble();
                 yList.addLast(y);

                 System.out.println("City: " + city + " X " + x + " Y " + y);
                 System.out.println("CityList: " + citiesList + " X " + xList + " Y " + yList);


                 // Compute the distance between all the cities recursively, print all cities, and the total distance
                 // between all cities. Not Sure How....?????

                 double distance = 0;
                 double endX = 0;
                 double startX = 0;
                 double endY = 0;
                 double startY = 0;
                 distance = Math.sqrt((endX-startX)*(endX-startX) + (endY-startY)*(endY-startY));

          }// end main
   }//end class
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jun 10 2010
Added on May 12 2010
9 comments
356 views