JAVA 1410 Lab Itinerary Instructions: Write a program that does the ofllowing: C
ID: 3664393 • Letter: J
Question
JAVA 1410
Lab Itinerary Instructions: Write a program that does the ofllowing: Create an ArrayList of Strings and call it itinerary Read in destinations from the user until the user enters done (or DONE, or dOnE,..) As you read in the destinations add them to the itinerary Next You need to create a String called travelRoute that includes the travel route as shown in the output Use a String Builder called sb to create the String travelRoute. Loop through all the elements of the ArrayList itinerary Change the destinations (e.g. Rome) to all uppercase letters (e.g. ROME) beofre adding them to sb Add the word to (lowercase) between the destinations, but not at the end) When you are done create the string travelRoute based on the inofrmation stored in sb. Print the String travelRoute.Explanation / Answer
/**Java program to create the string travel route**/
Program:
package sto;
import java.util.ArrayList;
public class PathFinding {
private static ArrayList<ArrayList<String>> routes = new ArrayList<ArrayList<String>>();
private static String[] sourceArray = {"Seattle", "LA", "LA", "Florida", "Seattle"};
private static String[] destinationArray = {"LA", "Florida", "Maine", "Seattle", "Florida"};
public static void main(String rgs[])
{
find_routes("Seattle", "Maine", new ArrayList<String>());
for(ArrayList<String> route : routes) {
for(String node : route) {
System.out.print(node + ", ");
}
System.out.println();
}
}
private static void find_routes(String source, String destination, ArrayList<String> currentRoute) {
ArrayList<String> newRoute = new ArrayList<String>();
newRoute.addAll(currentRoute);
newRoute.add(source);
if(source.equals(destination)) {
routes.add(newRoute);
} else {
for(int i = 0; i < sourceArray.length; ++i) {
if(source.equals(sourceArray[i])) {
if(!currentRoute.contains(source)) {
find_routes(destinationArray[i], destination, newRoute);
}
}
}
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.