Question: //Codes given //Prog340 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, **

 //Codes given //Prog340 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

//Codes given

//Prog340

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 Prog340 extends JPanel implements ActionListener { private static final long serialVersionUID = 1L; // Keep Eclipse happy. File inputFile; File outputFile; PrintWriter output; JFileChooser fileChooser; Graph g; String[] comboBoxList; // For putting names in Combo Box

/** The main method instantiates a Prog340 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() { createAndShowGUI(); } }); } /** Create and show the GUI. ** For thread safety, this method should be invoked from the event-dispatching thread. **/ private static void createAndShowGUI() { // Create and set up the window JFrame frame = new JFrame("Prog340"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // Create and set up the content pane. JComponent newContentPane = new Prog340(); newContentPane.setOpaque(true);; // content panes must be opaque frame.setContentPane(newContentPane);; // Display the window. frame.pack(); frame.setVisible(true); } /** The constructor creates a new ProgramA object, and sets up the input and output files. **/ public Prog340() { super( new BorderLayout() );

try { // I like the colorful FileChooser. UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); System.out.println( "Look and feel set."); } catch (Exception e) { // exit on exception. System.err.format("Exception: %s%n", e); System.exit(0); }

fileChooser = new JFileChooser(); fileChooser.setDialogTitle("Choose a file"); // Start looking for files at the currect directory, not home. fileChooser.setCurrentDirectory(new File(".")); inputFile = null; g = new Graph(); // Select the action comboBoxList = new String[5]; comboBoxList[0] = new String("prog340: Select file and read graph"); comboBoxList[1] = new String("Deliv A"); comboBoxList[2] = new String("Deliv B"); comboBoxList[3] = new String("Deliv C"); comboBoxList[4] = new String("exit");

JComboBox actionList = new JComboBox( comboBoxList ); actionList.setName("Action List"); actionList.setSelectedIndex(0); actionList.addActionListener( this ); add( actionList, BorderLayout.PAGE_START ); } // Listen to the Combo Box public void actionPerformed( ActionEvent e ) { JComboBox cb = (JComboBox)e.getSource(); int actionIndex = cb.getSelectedIndex(); switch( actionIndex ) { case 0: g = new Graph(); readGraphInfo( g ); break; case 1: DelivA dA = new DelivA( inputFile, g ); break; case 2: DelivB dB = new DelivB( inputFile, g ); break; case 3: DelivC dC = new DelivC( inputFile, g ); break; case 4: System.out.println( "Goodbye"); System.exit(0); default: System.out.println( "Invalid choice" ); System.exit(0); } } /** Read the file containing the Strings, line by line, then process each line as it is read. **/ public void readGraphInfo( Graph g ) {

try { if (fileChooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {

// Instantiate the selected input and output files. inputFile = fileChooser.getSelectedFile(); System.out.println( "Chosen file = " + inputFile + " "); } // read text file Scanner sc = new Scanner( inputFile ); // First line special: It contains "~", and "val", and the nodes with the edges. String firstLine = sc.nextLine(); String[] splitString = firstLine.split( " +" ); // Ignore first two fields of first line, Every other field is a node. for ( int i = 2; i

Node n = nodeList.get( nodeIndex ); n.setName( splitString[0] ); n.setVal( splitString[1] );

for ( int i = 2; i

} sc.close();

} catch (Exception x) { System.err.format("ExceptionOuter: %s%n", x); } }

}

//DelivA that needs to be edit.

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( "DelivA: To be implemented"); output.println( "DelivA: To be implemented"); output.flush(); } }

Strings, ** your assignment is to print out those strings interpreted as

a graph. **/ public class Prog340 extends JPanel implements ActionListener { private

static final long serialVersionUID = 1L; // Keep Eclipse happy. File inputFile;

Sample txt file (all whitespaces are spaced, not tabed)

A1.txt

File outputFile; PrintWriter output; JFileChooser fileChooser; Graph g; String[] comboBoxList; // For

A4.txt

putting names in Combo Box /** The main method instantiates a Prog340

AB0.txt

class, which includes giving you a choice of things to do. **

ABD6.txt

The only one active now will be reading the graph file and

Start from a Working Program Some Java programs work on student computers but not on University lab (Windows) computers or grading computer. So this semester, you are given an initial codebase, Prog340, that seems to compile and run on all platforms. It opens a menu for you to select an action. It has a number of predefined menu items describing the various actions (which you will implement during the semester). When you start the program the first action you will want to take will be to read a file. The input files for all deliverables have the same, specified format. It allows you to continuously select actions as long as you wish o Including read another file. It allows you to exit the program. Each Deliverable will involve your modifying your existing program (DelivA involves modifying the initial program given to you). This minimizes any duplicate effort. But it means that if you screw up a deliverable, you may have screwed up another deliverable later in the course. 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: E fig Here is sample output for one graph. val AAA BB Alfa X > Bravo 67 999 -42 3 Charlie 4 Delta 4e 3 22 Echo yes DDD 99 X yz == 9 != 2 33 d>e 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 d>e. Edge from Echo to Echo labeled 33. The order in which the Edges print out is not important. NA 19 1 //import java.util.*; 2 3 // Edge between two nodes 4 public class Edge { 5 6 String label; 7 int dist; 8 Node tail; 9 Node head; 10 110 public Edge Node tailNode, Node headNode, String theLabel ) { 12 setLabel( theLabel ); 13 setTail tailNode ); 14 setHead ( headNode ); 15 } 16 170 public String getLabel() { 18 return label; } 20 210 public Node getTail() { 22 return tail; 23 } 24 250 public Node getHead() { 26 return head; 27 } 28 290 public int getDist() { 30 return dist; 31 } 32 330 public void setLabel( String s ) { 34 label = s; 35 } 36 370 public void setTail( Node n ) { 38 tail = n; 39 } 40 410 public void setHead( Node n ) { 42 head = n; 43 } 44 450 public void setDist( String s ) { 46 try { 47 dist = Integer.parseInt(s); 48 } 49 catch (Number FormatException nfe ) { 50 dist = Integer.MAX_VALUE; 51 52 53 } 54 1 import java.util.*; 2 3 // A node of a graph for the Spring 2018 ICS 340 program 4 5 public class Node { 6 7 String name; 8 String val; // The value of the Node 9 String abbrev; // The abbreviation for the Node 10 ArrayList outgoingEdges; 11 ArrayList incomingEdges; 12 130 public Node( String theAbbrev ) { 14 setAbbrev( theAbbrev ); 15 val = null; 16 name = null; 17 outgoingEdges = new ArrayList(); 18 incomingEdges = new ArrayList(); 19 } 20 210 public String getAbbrev() { 22 return abbrev; 23 } 24 250 public String getName() { 26 return name; 27 } 28 290 public String getval() { 30 return val; 31 } 32 330 public ArrayList getOutgoingEdges() { 34 return outgoingEdges; 35 } 36 370 public ArrayList getIncomingEdges() { 38 return incoming Edges; 39 } 40 410 public void setAbbrev( String theAbbrev ) { 42 abbrev = theAbbrev; 43 } 44 450 public void setName( String theName ) { 46 name = theName; 47 } 48 490 public void setval( String theval ) { 50 val = theVal; 51 } 52 530 public void addoutgoingEdge( Edge e ) { 54 outgoingEdges.add( e ); 55 } 56 570 public void add Incoming Edge( Edge e ) { 58 incomingEdges.add( e ); 59 } 60 61 } 62 1 import java.util.*; 2 3 public class Graph { 4 5 ArrayList nodeList; 6 ArrayList edgelist; 7 80 public Graph() { 9 nodelist = new ArrayList(); 10 edgelist = new ArrayList(); 11 } 12 130 public ArrayList getNodeList() { 14 return nodeList; 15 } 16 170 public ArrayList getEdgelist() { 18 return edgelist; 19 } 20 210 public void addNode Node n ) { 22 nodeList.add(n); 23 } 24 250 public void addEdge Edge e) { 26 edgeList.add( e ); 27 } 28 29} 30 val M Minneapolis 400 1 2 3 4 5 9 N 2 22 N N N tu A B D E F val 4 6 -3 -6 0 55 N N 22 2 2 BB val AAA S 67 999 2 Alfa Bravo Charlie Delta Echo > -42 4 22 U2 m 2 C DDD E 99 fig 3 X yz 9 !=2 d>e 33 2 m 4e yes N h val Atl Bos Chi Dal Den LA Mia Min NY SF Sea Was Atlanta 0 957 581 725 1220 1942 608 898 752 2155 2201 538 Boston G 957 0 859 1571 1783 2594 1285 1127 207 2719 2505 420 Chicago 581 859 0803 928 1736 1185 354 718 1876 1758 601 Dallas 725 1571 803 0 667 1248 1116 851 1384 1494 1698 1190 Denver 1220 1783 928 667 812 1736 697 1645 957 1033 1507 LosAngeles S 1942 2594 1736 1248 812 0 2357 1500 2451 313 923 2301 Miami 608 1285 1185 1116 1736 2357 0 1505 1097 2611 2755 917 Minneapolis 898 1127 354 851 697 1500 1505 0 1024 1597 1415 940 NewYork 752 207 718 1384 1645 2451 1097 1024 0 2594 2430 213 San Francisco 2155 2719 1876 1494 957 313 2611 1597 2594 0 691 2464 Seattle 2201 2505 1758 1698 1033 923 2755 1415 2430 691 0 2354 Washington 538 420 601 1190 1507 2301 917 940 213 2464 2354 0 Start from a Working Program Some Java programs work on student computers but not on University lab (Windows) computers or grading computer. So this semester, you are given an initial codebase, Prog340, that seems to compile and run on all platforms. It opens a menu for you to select an action. It has a number of predefined menu items describing the various actions (which you will implement during the semester). When you start the program the first action you will want to take will be to read a file. The input files for all deliverables have the same, specified format. It allows you to continuously select actions as long as you wish o Including read another file. It allows you to exit the program. Each Deliverable will involve your modifying your existing program (DelivA involves modifying the initial program given to you). This minimizes any duplicate effort. But it means that if you screw up a deliverable, you may have screwed up another deliverable later in the course. 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: E fig Here is sample output for one graph. val AAA BB Alfa X > Bravo 67 999 -42 3 Charlie 4 Delta 4e 3 22 Echo yes DDD 99 X yz == 9 != 2 33 d>e 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 d>e. Edge from Echo to Echo labeled 33. The order in which the Edges print out is not important. NA 19 1 //import java.util.*; 2 3 // Edge between two nodes 4 public class Edge { 5 6 String label; 7 int dist; 8 Node tail; 9 Node head; 10 110 public Edge Node tailNode, Node headNode, String theLabel ) { 12 setLabel( theLabel ); 13 setTail tailNode ); 14 setHead ( headNode ); 15 } 16 170 public String getLabel() { 18 return label; } 20 210 public Node getTail() { 22 return tail; 23 } 24 250 public Node getHead() { 26 return head; 27 } 28 290 public int getDist() { 30 return dist; 31 } 32 330 public void setLabel( String s ) { 34 label = s; 35 } 36 370 public void setTail( Node n ) { 38 tail = n; 39 } 40 410 public void setHead( Node n ) { 42 head = n; 43 } 44 450 public void setDist( String s ) { 46 try { 47 dist = Integer.parseInt(s); 48 } 49 catch (Number FormatException nfe ) { 50 dist = Integer.MAX_VALUE; 51 52 53 } 54 1 import java.util.*; 2 3 // A node of a graph for the Spring 2018 ICS 340 program 4 5 public class Node { 6 7 String name; 8 String val; // The value of the Node 9 String abbrev; // The abbreviation for the Node 10 ArrayList outgoingEdges; 11 ArrayList incomingEdges; 12 130 public Node( String theAbbrev ) { 14 setAbbrev( theAbbrev ); 15 val = null; 16 name = null; 17 outgoingEdges = new ArrayList(); 18 incomingEdges = new ArrayList(); 19 } 20 210 public String getAbbrev() { 22 return abbrev; 23 } 24 250 public String getName() { 26 return name; 27 } 28 290 public String getval() { 30 return val; 31 } 32 330 public ArrayList getOutgoingEdges() { 34 return outgoingEdges; 35 } 36 370 public ArrayList getIncomingEdges() { 38 return incoming Edges; 39 } 40 410 public void setAbbrev( String theAbbrev ) { 42 abbrev = theAbbrev; 43 } 44 450 public void setName( String theName ) { 46 name = theName; 47 } 48 490 public void setval( String theval ) { 50 val = theVal; 51 } 52 530 public void addoutgoingEdge( Edge e ) { 54 outgoingEdges.add( e ); 55 } 56 570 public void add Incoming Edge( Edge e ) { 58 incomingEdges.add( e ); 59 } 60 61 } 62 1 import java.util.*; 2 3 public class Graph { 4 5 ArrayList nodeList; 6 ArrayList edgelist; 7 80 public Graph() { 9 nodelist = new ArrayList(); 10 edgelist = new ArrayList(); 11 } 12 130 public ArrayList getNodeList() { 14 return nodeList; 15 } 16 170 public ArrayList getEdgelist() { 18 return edgelist; 19 } 20 210 public void addNode Node n ) { 22 nodeList.add(n); 23 } 24 250 public void addEdge Edge e) { 26 edgeList.add( e ); 27 } 28 29} 30 val M Minneapolis 400 1 2 3 4 5 9 N 2 22 N N N tu A B D E F val 4 6 -3 -6 0 55 N N 22 2 2 BB val AAA S 67 999 2 Alfa Bravo Charlie Delta Echo > -42 4 22 U2 m 2 C DDD E 99 fig 3 X yz 9 !=2 d>e 33 2 m 4e yes N h val Atl Bos Chi Dal Den LA Mia Min NY SF Sea Was Atlanta 0 957 581 725 1220 1942 608 898 752 2155 2201 538 Boston G 957 0 859 1571 1783 2594 1285 1127 207 2719 2505 420 Chicago 581 859 0803 928 1736 1185 354 718 1876 1758 601 Dallas 725 1571 803 0 667 1248 1116 851 1384 1494 1698 1190 Denver 1220 1783 928 667 812 1736 697 1645 957 1033 1507 LosAngeles S 1942 2594 1736 1248 812 0 2357 1500 2451 313 923 2301 Miami 608 1285 1185 1116 1736 2357 0 1505 1097 2611 2755 917 Minneapolis 898 1127 354 851 697 1500 1505 0 1024 1597 1415 940 NewYork 752 207 718 1384 1645 2451 1097 1024 0 2594 2430 213 San Francisco 2155 2719 1876 1494 957 313 2611 1597 2594 0 691 2464 Seattle 2201 2505 1758 1698 1033 923 2755 1415 2430 691 0 2354 Washington 538 420 601 1190 1507 2301 917 940 213 2464 2354 0

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!