New York 10, 15
Las Vegas 33, 55
Denver 22, 44
I need to calculate the distance from New York to Las Vegas and from Las Vegas to Denver, then add it all up and display it.
Here are the actual 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.
As for now I am just trying to get it to calculate ONE route. I will worry about the all possible routes later.
I know the code is very elementary but I am just trying to get it to work before I make it all fancy.
Can anyone point me in the right direction? If you have any suggestions on how to solve this a different way please let me know. My brain is smoking right now.
Here is what I have so far:
import java.io.*;
import java.util.*;
class findRoutesMain
{
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
PS: Someone had posted a question about this project back in June which I found when I did a search, but it did not help me any so I thought I post it again. Thanks in advance.

New Topic/Question
Reply




MultiQuote





|