Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

yea public class Node String name; String val; // The value of the Node String abbrev; // The abbreviation for the Node ArrayList outgoingEdges; ArrayList

image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
yea
image text in transcribed
public class Node String name; String val; // The value of the Node String abbrev; // The abbreviation for the Node ArrayList outgoingEdges; ArrayList incomingEdges; public Node ( String theAbbrev ) { setAbbrev theAbbrev); val = null; name = null; outgoingEdges = new ArrayList ; incomingEdges = new ArrayList(); } public String getAbbrev () { return abbrev; > public String getName() { return name; > public String getVal() return val; > public ArrayList getOutgoingEdges() { return outgoingEdges; } public ArrayList get IncomingEdges() { return incomingEdges; } public void setAbbrev ( String theAbbrev ) { abbrev = the Abbrev; } public void setName String theName) { name = theName; > public void setval( String theVal ) { val - theVal; } public void addoutgoingEdge Edge e) { outgoingEdges.adde); } public void add IncomingEdge Edge e ) { incomingEdges.add( e ); } //import java.util.*; // Edge between two nodes public class Edge { String label; int dist; Node tail; Node head; public Edge ( Node tailNode, Node headNode, String theLabel ) { setLabel( theLabel); setTail( tailNode ); setHead ( headNode); ) public String getLabel() { return label; > public Node getrail() { return tail; } public Node getHead() { return head; } public int getDist() { return dist; } public void setLabel( Strings) { label - S; > public void setTail Node n ) { tail = n; > public void setHead ( Node n ) { head = n; > public void set Dist( Strings) { try { dist - Integer.parseInt(s); } catch NumberFormatException nfe ) { dist = Integer.MAX_VALUE; > } import java.util.*; public class Graph { ArrayList nodeList; ArrayList edgeList; public Graph() { nodeList = new ArrayList(); edgeList = new ArrayList(); } public ArrayList getNodeList() { return nodeList; } public ArrayList getEdgeList() { return edgeList; } public void addNode( Node n ) { nodeList.add(n); } public void addEdge ( Edge e) { edgeList.add( e ); } } import java.io.*; import java.util.*; // Class DelivA does the work for deliverable DelivA of the Prog340 public class DelivA { File inputFile; File outputFile; PrintWriter output; Graph g; public Deliva File in, Graph gr ) { inputFile = in; g = gr; // Get output file name. String inputFileName = inputFile.toString(); String baseFileName = inputFileName.substring 0, inputFileName. length() -4 ); // Strip off ".txt" String outputFileName = baseFileName.concat( out.txt" ); outputFile = new File( outputFileName); if (outputFile.exists()) { // For retests outputFile.delete(); ) try { output = new Printwriter (outputFile); catch (Exception x ) { System.err.format("Exception: $s$n", x); System.exit(0); System.out.println("Deliv: To be implemented"); output.println("Deliva: To be implemented"); output.flush(); > ) Specification: Start with the given Java program "prog340", which lets you select a file to read from your computer, reads the file, and interprets that file as the specification of a graph.' Then enhance it to do the following: 1. Write code necessary to print out, for each Edge, the name of the Node from which the Edge emanates, the name of the Node at which the Edge terminates, and the label of the Edge, in the format listed below. The "prog340" handout describes the format of the input file for this and all program deliverables. As will always be the case in this class, the program must be written in Java and must run on the University Windows computer systems. Output: Here is sample output for one graph. BB E val X 67 fig 999 -42 3 DDD 99 X y2 Alfa Bravo Charlie Delta Echo 9 3 22 yes de 33 The output for this file should be: Edge from Alpha to Bravo labeled >. Edge from Alpha to Delta labeled 99. Edge from Alpha to Echo labeled fig. Edge from Bravo to Alpha labeled 999. Edge from Bravo to Bravo labeled -42. Edge from Bravo to Charlie labeled 3. Edge from Bravo to Delta labeled x. Edge from Bravo to Echo labeled ==. Edge from Charlie to Bravo labeled 4. Edge from Charlie to Delta labeled yz. Edge from Charlie to Echo labeled 9. Edge from Delta to Alpha labeled 3. Edge from Delta to Bravo labeled 22. Edge from Delta to Charlie labeled x. Edge from Delta to Echo labeled !-2. Edge from Echo to Delta labeled de. Edge from Echo to Echo labeled 33. The order in which the Edges print out is not important. Progs40 File Edit Format View Help package prog348; import javax.swing.; import java.io.*; import java.util. import java.awt. import java.awt.event."; * Programa simply reads a file containing rows of space-separated Strings, ** your assignment is to print out those strings interpreted as a graph. public class Prog34e extends JPanel implements ActionListener { private static final long serialversionUID = 11; // Keep Eclipse happy. File inputFile; File outputFile; Printwriter output; JFileChooser fileChooser; Graph B; String[] comboBoxList; // For putting names in Combo Box /** The main method instantiates a Prog34e class, which includes giving you a choice of things to do. The only one active now will be reading the graph file and having you parse it. ** @param args Not used **@throws FileNotFoundException Thrown if the file selected is not found. Shouldn't happen with a FileChooser. **/ public static void main(String[] args) throws FileNotFoundException { Javax.swing.Swingutilities.invokelater(new Runnable() { public void run() { createAndShowGUT(); > }); // File file-new File("\\users\\ismahVeclipse-workspacel prog340\\src"); 11 Scanner sc-new Scanner (file); 1/ while(sc.hasNextLine()) { 1/System.out.printin(sc. hasnextLine()); 1. Create and show the Gul. ** for thread safety, this method should be invoked from the event-dispatching thread. **/ private static void createAnd ShowcUL) I create and set up the window Frame frame = new Frame{"Pro 140"); Prog340 - Notepad File Edit Format View Help /* Create and show the GUI. ** For thread safety, this method should be invoked fron the event-dispatching thread. / private static void createAndShowGUI) 1/ Create and set up the window Frame frame = new Frane("Prog 340"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 1/ Create and set up the content pane. Component necontentPane = new Prog340(); new contentPane.setOpaque(true); // content panes must be opaque frame.setContentPane(new contentPane); // Display the window. frame.pack(); frame.setvisible(true) true)in * The constructor creates a new Programa object, and sets up the input and output files. public Prog340() { supert new torderlayout()); try 11 I like the colorful FileChooser. UlManager.setLookAndFeel(tranger.getSystemLookAndree classe(); System.out.printin "Look and feel set."); catch (Exception e) { 1 exit on exception System.err.format("Exception, e); System.exit(0); > filechooser = new FileChoose(); lechooser.setoialogtitle" choose a file); 1/ start looking for files at the currect directory, not home Flechooser.setCurrent rectory(new File("")); Inutile : new Graph 11 Select the action bist - String cost or stringi prog40 Select file and read rah City - String); Type here to search o Prog540 - Notepad File Edit Format View Help new Graph Select the action combobox Ist new string(5) comboboxList[new String"prog 340 Select File and read graph) comboBoxList a new string"Deliv A) comboBoxtist(2) new string("DIV 8-) comboBoxlist - ne string "Deliv Cy! comboboxList 141w String("exit); ComboBoxeString> actionListe o boboxcstring> comboBoxList actionist.setName("Action List": actionist.setSelectedIndex(); actionList.addActionListener this); add actionList, BorderLayout.PAGE_START): W Listen to the comboBox public void actionPerformedt ActionEvente) Combobox ch. ("Combobox).getsource) int action Index .co.getSelectedIndex(); switche action Index ) { case 2: new Graph() readGraphInfo : break case 1: DeLIVA A new Delta Inutile, break; case 2: Delive new Delive inputlle, ): case 3: Deliv oC new Delive input file): case 4: System.out.printing "Goodbye"); Systent) default: System.out.println("Invalid chec): System.exit(); */ the file containing the string by the process each line as it is read public traGrant Graph try 1 tlehomero) - Weser APPROVE_OPTION) 11 Tattiate the selected input files. Inpullie erotectie Sysprintin "Cheese.. " 1 read til Type here to search O fi E Prog340 - Notepad File Edit Format View Help System.out.println("Invalid choice" System.exit() read the file containing the strings, line by line, the process each lines it is read. putili void renegraph Infof Graph) It (FileChooser showOperbialog(null) - FileChooser. APPROVE_OPTION) // Instantiate the selected input and output files inputFile Fechooser.getSelectedFile System.out.printla "Chosen File. + Input file + ) > 17 read text file Scanner se new Scanner Inputrile); 11 First line special! it contains and "val", and the nodes with the edges String firstline sextLine(): Stringsplatstring - Firstline.split(""); 1 Trore first two fields of first line, Every other field is a node for (int 12; splitString.length; i++) Moden Node solsestring] ) 8. addNodef): 1 11 Every other line gives the same and value of the Node, and any edges. int madeIndex. ArrayListcode nodelist..odelist(): while s.haseline) String Linesc.) splittringextLinesplit("") Noendeliat.get(nodeIndex > s splittringej > valt siisti): for (int 12; 1 splittring length; in) 1 tsplittring).equals()) Node het modelis (2) Logee new Edgehead, splittring); Baddogel): Baddoutgoing) headcoingget) Rodeinde 1 starr forwaltschutart.) Type here to search O BI Programming Project Thaar with=-itte ka pay-- ---- IS Start from Working Prearam , - ----- at attemmaterial anthe y Delete Gradina Daterate A 13 tyrs ie For asiatinade0 got the center the fiel. The com , , Program Architecture Mastated AS | - The Best Mamiyarai - Ile resistantiate - Thanta teen . public class Node String name; String val; // The value of the Node String abbrev; // The abbreviation for the Node ArrayList outgoingEdges; ArrayList incomingEdges; public Node ( String theAbbrev ) { setAbbrev theAbbrev); val = null; name = null; outgoingEdges = new ArrayList ; incomingEdges = new ArrayList(); } public String getAbbrev () { return abbrev; > public String getName() { return name; > public String getVal() return val; > public ArrayList getOutgoingEdges() { return outgoingEdges; } public ArrayList get IncomingEdges() { return incomingEdges; } public void setAbbrev ( String theAbbrev ) { abbrev = the Abbrev; } public void setName String theName) { name = theName; > public void setval( String theVal ) { val - theVal; } public void addoutgoingEdge Edge e) { outgoingEdges.adde); } public void add IncomingEdge Edge e ) { incomingEdges.add( e ); } //import java.util.*; // Edge between two nodes public class Edge { String label; int dist; Node tail; Node head; public Edge ( Node tailNode, Node headNode, String theLabel ) { setLabel( theLabel); setTail( tailNode ); setHead ( headNode); ) public String getLabel() { return label; > public Node getrail() { return tail; } public Node getHead() { return head; } public int getDist() { return dist; } public void setLabel( Strings) { label - S; > public void setTail Node n ) { tail = n; > public void setHead ( Node n ) { head = n; > public void set Dist( Strings) { try { dist - Integer.parseInt(s); } catch NumberFormatException nfe ) { dist = Integer.MAX_VALUE; > } import java.util.*; public class Graph { ArrayList nodeList; ArrayList edgeList; public Graph() { nodeList = new ArrayList(); edgeList = new ArrayList(); } public ArrayList getNodeList() { return nodeList; } public ArrayList getEdgeList() { return edgeList; } public void addNode( Node n ) { nodeList.add(n); } public void addEdge ( Edge e) { edgeList.add( e ); } } import java.io.*; import java.util.*; // Class DelivA does the work for deliverable DelivA of the Prog340 public class DelivA { File inputFile; File outputFile; PrintWriter output; Graph g; public Deliva File in, Graph gr ) { inputFile = in; g = gr; // Get output file name. String inputFileName = inputFile.toString(); String baseFileName = inputFileName.substring 0, inputFileName. length() -4 ); // Strip off ".txt" String outputFileName = baseFileName.concat( out.txt" ); outputFile = new File( outputFileName); if (outputFile.exists()) { // For retests outputFile.delete(); ) try { output = new Printwriter (outputFile); catch (Exception x ) { System.err.format("Exception: $s$n", x); System.exit(0); System.out.println("Deliv: To be implemented"); output.println("Deliva: To be implemented"); output.flush(); > ) Specification: Start with the given Java program "prog340", which lets you select a file to read from your computer, reads the file, and interprets that file as the specification of a graph.' Then enhance it to do the following: 1. Write code necessary to print out, for each Edge, the name of the Node from which the Edge emanates, the name of the Node at which the Edge terminates, and the label of the Edge, in the format listed below. The "prog340" handout describes the format of the input file for this and all program deliverables. As will always be the case in this class, the program must be written in Java and must run on the University Windows computer systems. Output: Here is sample output for one graph. BB E val X 67 fig 999 -42 3 DDD 99 X y2 Alfa Bravo Charlie Delta Echo 9 3 22 yes de 33 The output for this file should be: Edge from Alpha to Bravo labeled >. Edge from Alpha to Delta labeled 99. Edge from Alpha to Echo labeled fig. Edge from Bravo to Alpha labeled 999. Edge from Bravo to Bravo labeled -42. Edge from Bravo to Charlie labeled 3. Edge from Bravo to Delta labeled x. Edge from Bravo to Echo labeled ==. Edge from Charlie to Bravo labeled 4. Edge from Charlie to Delta labeled yz. Edge from Charlie to Echo labeled 9. Edge from Delta to Alpha labeled 3. Edge from Delta to Bravo labeled 22. Edge from Delta to Charlie labeled x. Edge from Delta to Echo labeled !-2. Edge from Echo to Delta labeled de. Edge from Echo to Echo labeled 33. The order in which the Edges print out is not important. Progs40 File Edit Format View Help package prog348; import javax.swing.; import java.io.*; import java.util. import java.awt. import java.awt.event."; * Programa simply reads a file containing rows of space-separated Strings, ** your assignment is to print out those strings interpreted as a graph. public class Prog34e extends JPanel implements ActionListener { private static final long serialversionUID = 11; // Keep Eclipse happy. File inputFile; File outputFile; Printwriter output; JFileChooser fileChooser; Graph B; String[] comboBoxList; // For putting names in Combo Box /** The main method instantiates a Prog34e class, which includes giving you a choice of things to do. The only one active now will be reading the graph file and having you parse it. ** @param args Not used **@throws FileNotFoundException Thrown if the file selected is not found. Shouldn't happen with a FileChooser. **/ public static void main(String[] args) throws FileNotFoundException { Javax.swing.Swingutilities.invokelater(new Runnable() { public void run() { createAndShowGUT(); > }); // File file-new File("\\users\\ismahVeclipse-workspacel prog340\\src"); 11 Scanner sc-new Scanner (file); 1/ while(sc.hasNextLine()) { 1/System.out.printin(sc. hasnextLine()); 1. Create and show the Gul. ** for thread safety, this method should be invoked from the event-dispatching thread. **/ private static void createAnd ShowcUL) I create and set up the window Frame frame = new Frame{"Pro 140"); Prog340 - Notepad File Edit Format View Help /* Create and show the GUI. ** For thread safety, this method should be invoked fron the event-dispatching thread. / private static void createAndShowGUI) 1/ Create and set up the window Frame frame = new Frane("Prog 340"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 1/ Create and set up the content pane. Component necontentPane = new Prog340(); new contentPane.setOpaque(true); // content panes must be opaque frame.setContentPane(new contentPane); // Display the window. frame.pack(); frame.setvisible(true) true)in * The constructor creates a new Programa object, and sets up the input and output files. public Prog340() { supert new torderlayout()); try 11 I like the colorful FileChooser. UlManager.setLookAndFeel(tranger.getSystemLookAndree classe(); System.out.printin "Look and feel set."); catch (Exception e) { 1 exit on exception System.err.format("Exception, e); System.exit(0); > filechooser = new FileChoose(); lechooser.setoialogtitle" choose a file); 1/ start looking for files at the currect directory, not home Flechooser.setCurrent rectory(new File("")); Inutile : new Graph 11 Select the action bist - String cost or stringi prog40 Select file and read rah City - String); Type here to search o Prog540 - Notepad File Edit Format View Help new Graph Select the action combobox Ist new string(5) comboboxList[new String"prog 340 Select File and read graph) comboBoxList a new string"Deliv A) comboBoxtist(2) new string("DIV 8-) comboBoxlist - ne string "Deliv Cy! comboboxList 141w String("exit); ComboBoxeString> actionListe o boboxcstring> comboBoxList actionist.setName("Action List": actionist.setSelectedIndex(); actionList.addActionListener this); add actionList, BorderLayout.PAGE_START): W Listen to the comboBox public void actionPerformedt ActionEvente) Combobox ch. ("Combobox).getsource) int action Index .co.getSelectedIndex(); switche action Index ) { case 2: new Graph() readGraphInfo : break case 1: DeLIVA A new Delta Inutile, break; case 2: Delive new Delive inputlle, ): case 3: Deliv oC new Delive input file): case 4: System.out.printing "Goodbye"); Systent) default: System.out.println("Invalid chec): System.exit(); */ the file containing the string by the process each line as it is read public traGrant Graph try 1 tlehomero) - Weser APPROVE_OPTION) 11 Tattiate the selected input files. Inpullie erotectie Sysprintin "Cheese.. " 1 read til Type here to search O fi E Prog340 - Notepad File Edit Format View Help System.out.println("Invalid choice" System.exit() read the file containing the strings, line by line, the process each lines it is read. putili void renegraph Infof Graph) It (FileChooser showOperbialog(null) - FileChooser. APPROVE_OPTION) // Instantiate the selected input and output files inputFile Fechooser.getSelectedFile System.out.printla "Chosen File. + Input file + ) > 17 read text file Scanner se new Scanner Inputrile); 11 First line special! it contains and "val", and the nodes with the edges String firstline sextLine(): Stringsplatstring - Firstline.split(""); 1 Trore first two fields of first line, Every other field is a node for (int 12; splitString.length; i++) Moden Node solsestring] ) 8. addNodef): 1 11 Every other line gives the same and value of the Node, and any edges. int madeIndex. ArrayListcode nodelist..odelist(): while s.haseline) String Linesc.) splittringextLinesplit("") Noendeliat.get(nodeIndex > s splittringej > valt siisti): for (int 12; 1 splittring length; in) 1 tsplittring).equals()) Node het modelis (2) Logee new Edgehead, splittring); Baddogel): Baddoutgoing) headcoingget) Rodeinde 1 starr forwaltschutart.) Type here to search O BI Programming Project Thaar with=-itte ka pay-- ---- IS Start from Working Prearam , - ----- at attemmaterial anthe y Delete Gradina Daterate A 13 tyrs ie For asiatinade0 got the center the fiel. The com , , Program Architecture Mastated AS | - The Best Mamiyarai - Ile resistantiate - Thanta teen

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

50 Tips And Tricks For MongoDB Developers Get The Most Out Of Your Database

Authors: Kristina Chodorow

1st Edition

1449304613, 978-1449304614

More Books

Students also viewed these Databases questions