# Travel Bargains

Exercise 2.zip - (100.4 KB)
[
Exercise 2/.classpath
Exercise 2/.project
Exercise 2/Node.class
Exercise 2/Node.java
Exercise 2/Question.txt
Exercise 2/Screenshot_1.png
Exercise 2/Test_Class.class
Exercise 2/Test_Class.java
Exercise 2/TravelBargains.class
Exercise 2/TravelBargains.java
Exercise 2/WeightedDirectedGraph.class
Exercise 2/WeightedDirectedGraph.java
Exercise 2/WeightedGraph.class
Exercise 2/WeightedGraph.java
]

Ex 2: Travel Bargains
Create a new project and class in Eclipse called TravelBargains, with the following structure:

public class TravelBargains {
/**
* Construct a new TravelBargains object with the given graph as the flight plan
*/
public TravelBargains(WeightedGraph g);

/**
* Given a country’s name, find the name of the cheapest country you can fly directly to from here
*/
public String getCheapestDestination(String currentCountry);
}
getCheapestDestination() should return the directly connected country with the lowest flight price.

If there is a tie, return any country with this minimum price. If the country has no destinations, return null.

For example, consider the following flight plan:

Australia ? USA for \$1128
Australia ? UK for \$2239
UK ? France for \$102
UK ? Australia for \$2102
USA ? France for \$930
USA ? Germany for \$930
Here, the cheapest country you can reach from Australia is the USA, which is only \$1128. So getCheapestDestination(“Australia”) should return “USA”.

You can represent these flight using a directed, weighted graph, with each node representing a country and each edge representing a flight. The direction of the edge shows which country is the source (the tail of the arrow) and which is the destination (the head of the arrow), and the weight of the edge represents the price.

Implement this class, then check your travel system is working with the following tests:

Ensure that getCheapestDestination() returns null for any country in a system with an empty graph as the flight plan
Ensure that getCheapestDestination() returns the correct country for each of the 5 countries (Australia, USA, UK, France & Germany) in the flight plan above

### Write a Review

Get a fresh solution of this question. Ask it now to our experts.