Starting from:

$30

Assignment 5 – Finding Least Expensive Routes

Assignment 5 CSCI-C212/A592 – Intro to Software Systems 
Instructions: • Review the requirements given below and complete both parts from below. (Note: There is a Bonus part as well). Please submit all files through Canvas. • The grading scheme is provided on Canvas. Be sure that your code includes everything required in the grading rubric. Part 1 – Finding Least Expensive Routes (50 points) Consider the problem of finding the least expensive routes to all cities in a network from a given starting point. For example, in the network shown on the map below, the least expensive route from Pendleton to Peoria has cost 8 (going through Pierre and Pueblo). The following helper class expresses the distance to another city: public class DistanceTo implements Comparable { private String target; private int distance; public DistanceTo(String city, int dist) { target = city; distance = dist; } public String getTarget() { return target; } public int getDistance() { return distance; } public int compareTo(DistanceTo other) { return distance - other.distance; } } All direct connections between cities are stored in a Map<String, TreeSet. The algorithm now proceeds as follows: Let from be the starting point. Add DistanceTo(from, 0) to a priority queue. Construct a map shortestKnownDistance from city names to distances. While the priority queue is not empty Get its smallest element. If its target is not a key in shortestKnownDistance Let d be the distance to that target. Put (target, d) into shortestKnownDistance. For all cities c that have a direct connection from target Add DistanceTo(c, d + distance from target to c) to the priority queue. When the algorithm has finished, shortestKnownDistance contains the shortest distance from the starting point to all reachable targets. Your task is to write a program that implements this algorithm. Your program should read in lines of the form city1 city2 distance. The starting point is the first city in the first line. Print the shortest distances to all other cities. Finally, add appropriate Junit tests to test the program. Part 2 – Reversing a Linked-List using Stack (50 points) Write a program that creates a Linked-List object of 10 Strings of Ransom sizes, then: • creates a second Linked-List object containing a copy of the first list, but in reverse order. You must use a stack to reverse the Strings. • creates a third Linked-List object containing a copy of the first list, but after sorting it based on the length of strings as well as alphabetically (in ascending order). The program will print all three Linked-Lists. Add appropriate Junit tests to test the program. Part 3 – Implementation (Bonus worth 50 points) Suppose you buy 100 shares of a stock at $12 per share, then another 100 at $10 per share, and then sell 150 shares at $15. You have to pay taxes on the gain, but exactly what is the gain? In the United States, the FIFO rule holds: You first sell all shares of the first batch for a profit of $300, then 50 of the shares from the second batch, for a profit of $250, yielding a total profit of $550. Write a program that can make these calculations for arbitrary purchases and sales of shares in a single company. The user enters commands buy quantity price, sell quantity (which causes the gain to be displayed), and quit. Hint: Keep a queue of objects of a class Block that contains the quantity and price of a block of shares. Then write a program that can handle shares of multiple companies. The user enters commands buy symbol quantity price and sell symbol quantity. Finally, add appropriate Junit tests to test the program. Hint: Keep a Map<String, Queue that manages a separate queue for each stock symbol.

More products