Question: 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

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); } }

}

 import javax.swing.*; import java.io.*; import java.util.*; import java.awt.*; import java.awt.event.*; /**

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(); }

}

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. val AAA BB DDD 99 E fig 67 999 3 Alfa Bravo Charlie Delta Echo -42 4 22 yz 3 4e yes 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 de. Edge from Echo to Echo labeled 33. The order in which the Edges print out is not important 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. val AAA BB DDD 99 E fig 67 999 3 Alfa Bravo Charlie Delta Echo -42 4 22 yz 3 4e yes 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 de. Edge from Echo to Echo labeled 33. The order in which the Edges print out is not important

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!