Java Help Please ASAP 1. Represent this map in your program. I will describe how
ID: 3682729 • Letter: J
Question
Java Help Please ASAP
1. Represent this map in your program. I will describe how below, and you are required to do this as described. You will also need an array of city names. It should be exactly this for order, spelling, and capitalization: public final String cities[] = { "Reykjavik", "Oslo", "Helsinki", "London", "Copenhagen", "Moscow", "Paris", "Berlin", "Prague", "Bern", "Vienna", "Kiev, "Madrid", "Monaco", "Rome", "Athens" }; (you may make it static if you choose)
2. Write a program that has a loop that reads in either a city name, or a city name followed by a ^, or the word quit.
2 a. If a city name is read your program will report the cities directly connected – those reachable in “one hop”. For example if the city read was Moscow, your program would respond Helsinki, Prague. (Order does not matter)
b. If the city name is followed (with no space) by the ^ then your program would report the 2-hop routes from the city. If the input was Moscow^ your program would report Moscow->Helsinki->Copenhagen, Moscow->Prague->Berlin, Moscow->Prague->Bern, Moscow->Prague->Vienna. Do notice neither Moscow->Helsinki->Moscow nor Moscow->Prague->Moscow were reported. Round trips are not interesting. However, if the city was Berlin, your program would report both Berlin->Paris->Bern and Berlin->Prague->Bern, as alternative routes are interesting.
c. If the input read was quit then your program would exit.
d. a-c imply a loop I will show you how to write below
3. In order to implement 2a and 2b you are required to implement and use an instance method String[ ] hop( String origin ), to be discussed below
Explanation / Answer
import java.io.*;
import java.util.*;
class cities
{
public static void main(String args[])
{
//graph array has indices as the indices of the string array.
//example: cities[0] is reyjavik. therefore 0 represents reyjavik.
int map[16][16]=//intitialize array with graph connection values
String cities[]={"Reykjavik","Oslo","Helsinki","London","Copenhagen","Moscow","Paris","Berlin","Prague","Bern","Vienna","Kiev","Madrid","Monaco","Rome","Athens"};
Scanner s = new Scanner(System.in);
System.out.println("enter city name for direct connections cityname^ for 2 hop neighbours and quit for exit");
String in = s.next();
int n=cities.length;
int len=in.length();
if(in.equals("quit"))
{
System.exit(0);
}
else if(in.charAt(len-1)!='^')
{
//to find direct connections
int ind = Arrays.asList(cities).indexOf(in);
System.out.println("one hop");
for(int i=0;i<n;i++)
{
//if there is direct connection between given city and others
if(map[ind][i]!=0)
System.out.print(map[ind][i]+",");
}
System.out.println();
}
else
{
//to find two - hop connections
int ind = Arrays.asList(cities).indexOf(in);
System.out.println("two hops");
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++){
//if there is tw hop connection between given city and others
if(map[ind][i]!=0&&map[i][j]!=0)
{
System.out.println(cities[ind]+"->"+map[ind][i]+"->"+map[i][j]);
}
}
}
}
}
}
Provide the map values and the code will work. In case of any implementation doubts, leave a comment.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.