Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Final Project Part 1: Implementing a Set #12

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 79 additions & 0 deletions src/FlightRoutesGraph.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import edu.greenriver.sdev333.BSTSet;
import edu.greenriver.sdev333.HashSet;
import edu.greenriver.sdev333.MathSet;

public class FlightRoutesGraph {
// two sets needed to model a graph (network)
// 1. a set of vertices (points, nodes) - airports
// 2. a set of edges (connections, lines, relationships) - route between airport

// helper class
private class Edge {
private String node1;
private String node2;

public Edge(String from, String to) {
node1 = from;
node2 = to;
}
}
// fields
private MathSet<String> nodes;
private MathSet<Edge> edges;

public FlightRoutesGraph()
{
nodes = new BSTSet<>(); // BST ok here b/c strings are comparable
edges = new HashSet<>(); // must use HashSet here b/c edges aren't comparable
}

public void addNode(String city) {
nodes.add(city);
}

public void addEdge(String city1, String city2) {
Edge connection = new Edge(city1, city2);
edges.add(connection);
}

MathSet<String> getNeighbors(String city) {
// create an empty set to gold the results
MathSet<String> neighbors = new BSTSet<>();

// loop through the edges and check
// if the city is either in node 1 or node 2
for (Edge e : edges.keys()) {
if (e.node1.equals(city)) {
neighbors.add(e.node2);
}
else if (e.node2.equals(city)) {
neighbors.add(e.node1);
}
}
return neighbors;
}

public static void main(String[] args) {
FlightRoutesGraph g = new FlightRoutesGraph();

// add all the cities first (nodes) - order doesn't matter with sets
g.addNode("JFK");
g.addNode("ORD");
g.addNode("ATL");
g.addNode("MCO");
g.addNode("DEN");

// add connections between cities (edges, routes)
g.addEdge("JFK", "MCO");
g.addEdge("ATL", "MCO");
g.addEdge("DEN", "ORD");
g.addEdge("ORD", "ATL");
// more to go if you want...

// look for direct flights from JFK
MathSet<String> directFromJFK = g.getNeighbors("JFK");
MathSet<String> directFromAtl = g.getNeighbors("ATL");

}

}
72 changes: 71 additions & 1 deletion src/Main.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,75 @@
import edu.greenriver.sdev333.BSTSet;
import edu.greenriver.sdev333.MathSet;
import edu.greenriver.sdev333.HashSet;

/**
* Main method that tests the BSTSet and HashSet classes
* @author: Jasmine David
*/
public class Main {
public static void main(String[] args) {
System.out.println("Hello world!");
// write code that tests methods in set
// Create 2 sets
// add items to each of the sets (some same, some different)

MathSet<String> set1 = new BSTSet<>();
set1.add("Ken");
set1.add("Tina");
set1.add("Jasmine");
set1.add("Ashley");

MathSet<String> set2 = new HashSet<>();
set2.add("Ken");
set2.add("Josh");
set2.add("Tyler");
set2.add("Ashley");
set2.add("Stephanie");

System.out.println("Set 1: ");
for (String key : set1.keys()) {
System.out.println(key);
}

System.out.println("\nSet 2: ");
for (String key : set2.keys()) {
System.out.println(key);
}


// TESTING FOR SET 1 -------
System.out.println("\nIs Set 1 empty? " + set1.isEmpty());
System.out.println("Does Set 1 contain Josh? " + set1.contains("Josh"));
System.out.println("Size of Set 1: " + set1.size());

// TESTING FOR SET 2 -------
System.out.println("\nIs Set 2 empty? " + set2.isEmpty());
System.out.println("Does Set 2 contain Josh? " + set2.contains("Josh"));
System.out.println("Size of Set 2: " + set2.size());


// TESTING UNION ------
System.out.println("\nTEST UNION: ");
// union set 1 and set 2, save into result1
MathSet<String> result1 = set1.union(set2);
// print out keys from result1 - need for each loop to get the keys!
for (String key : result1.keys()) {
System.out.println(key);
}

// TESTING INTERSECTION ------
System.out.println("\nTEST INTERSECTION: ");
MathSet<String> result2 = set1.intersection(set2);
for (String key : result2.keys()) {
System.out.println(key);
}


// TESTING DIFFERENCE ------
System.out.println("\nTEST DIFFERENCE: ");
MathSet<String> result3 = set1.difference(set2);
for (String key : result3.keys()) {
System.out.println(key);
}

}
}
Loading