introduction to object oriented programming 7 – exclusivewritings.com

In this assignment, you will add functionality to the Graph abstract class and UndirectedGraph subclass from Homework Assignment 9. I have provided you with the following files:

  • NodeEdge.jar: Java archive containing compiled Node and Edge classes. (Same as in Homework Assignment 9)
  • Graph.java: implementation of an abstract class representing general graphs. Since it is an abstract class, it cannot be used to create objects. (Same as in Homework Assignment 9)
  • UndirectedGraph.java: implementation of a class representing undirected graphs. (Solution to Homework Assignment 9)Figure 1. UML diagram for graph classes. New and modified components in this homework assignment are enclosed with red boxes.

page1image8944
page1image9104
page1image9272
page1image9432
page1image9592

EECS 1510 Spring 2020 Sections 009/011/093 Instructor: Dr. Kevin S. Xu Introduction to Object-Oriented Programming

CLASS SPECIFICATION

The UML diagram for the graph classes is shown in Figure 1. Most of the UML diagram is the same as in Homework Assignment 9. This homework assignment will require you to add or modify the following components:

  • Modify the Graph abstract class to implement the Comparable interface. To do this, you will have to implement the compareTo() method, which should return 1 if the current graph has more nodes than g, -1 if the current graph has less nodes than g, and 0 if they both have the same number of nodes.
  • Modify the implementation of the addEdge() method in the UndirectedGraph subclass to throwIllegalArgumentException for the following cases:o Iftheedgeeisdirected,use”Cannotadddirectededgetoundirectedgraph”as the exception string.o Ifthesourcenodeisnotinthegraph,use”Node{source}inedgeneedstofirst be added to graph” where {source} denotes the ID of the source node.o Ifthetargetnodeisnotinthegraph,use”Node{target}inedgeneedstofirstbe added to graph” where {target} denotes the ID of the target node.
  • Add a constructor UndirectedGraph() that reads in a File object for a comma-separated values (CSV) file in the following format:
                 Alice, Bob_x000D_
                 Alice, Zulu_x000D_
                 Alice, Charlie_x000D_
                 Bob, Charlie_x000D_
                 Charlie, Zulu_x000D_
    

    where each line denotes an edge in the format source, target. The constructor should add all of the nodes and edges present in the file.

  • Add a void method writeAdjacencyList() that writes the string representation of the graph adjacency list to the specified File object in the following format:Alice: [(Alice, Bob), (Alice, Zulu), (Alice, Charlie)]
    Bob: [(Alice, Bob), (Bob, Charlie)]
    Charlie: [(Bob, Charlie), (Charlie, Zulu), (Alice, Charlie)] Zulu: [(Alice, Zulu), (Charlie, Zulu)]SUBMISSION INSTRUCTIONSA portion of your grade will be assigned based on adherence to OOP practices. Submit only your modified Graph.java and UndirectedGraph.java source files! Do not include a main() method in your submission!Sample output for the following code block:
    // Create nodes_x000D_
    

    ArrayList<Node> allNodes = new ArrayList<>();allNodes.add(new Node(“Alice”));allNodes.add(new Node(“Bob”));allNodes.add(new Node(“Charlie”));// Create graph and add nodes to themUndirectedGraph graph = new UndirectedGraph();

EECS 1510 Spring 2020 Sections 009/011/093 Instructor: Dr. Kevin S. Xu Introduction to Object-Oriented Programming

graph.addNode(allNodes.get(0));graph.addNode(allNodes.get(1));graph.addNode(allNodes.get(2));

// Create edges and add them to the graph

ArrayList<Edge> allEdges = new ArrayList<>();
allEdges.add(new Edge(graph.getNode(“Alice”), graph.getNode(“Bob”)));allEdges.add(new Edge(graph.getNode(“Bob”), graph.getNode(“Charlie”)));graph.addEdge(allEdges.get(0));
graph.addEdge(allEdges.get(1));

// Testing reading and writing files

File inFile2 = new File(“edgelist2.txt”);
UndirectedGraph graph2 = new UndirectedGraph(inFile2);
File outFile2 = new File(“graph2.txt”);graph2.writeAdjacencyList(outFile2);
File inFile = new File(“edgelist1.txt”);
UndirectedGraph graph3 = new UndirectedGraph(inFile); System.out.println(graph3.toString().equals(graph.toString())); System.out.println(graph3.toString().equals(graph2.toString())); System.out.println(graph3.compareTo(graph2)); System.out.println(graph3.compareTo(graph));

// Testing exception handling

Node zulu = new Node(“Zulu”);
Edge aliceZulu = new Edge(graph.getNode(“Alice”), zulu); Edge zuluAlice = new Edge(zulu, graph.getNode(“Alice”)); Edge aliceCharlie = new Edge(graph.getNode(“Alice”),

graph.getNode(“Charlie”), true);graph.addEdge(aliceZulu);

}
catch (IllegalArgumentException ex) {

System.out.println(ex.toString());try {

graph.addEdge(zuluAlice);catch (IllegalArgumentException ex) {

System.out.println(ex.toString());try {

graph.addEdge(aliceCharlie);catch (IllegalArgumentException ex) {

System.out.println(ex.toString());Output:

true
false
-1
0
java.lang.IllegalArgumentException: Node Zulu in edge needs to first be added to graph

try {

}

}

}

} }

EECS 1510 Spring 2020 Sections 009/011/093 Instructor: Dr. Kevin S. Xu Introduction to Object-Oriented Programming

java.lang.IllegalArgumentException: Node Zulu in edge needs to first be added to graph java.lang.IllegalArgumentException: Cannot add directed edge to undirected graph

ORDER NOW