Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

package 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

image text in transcribedimage text in transcribed

package 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(); } }); // File file=new File("\\Users\\ismah\\eclipse-workspace\\prog340\\src"); // Scanner sc=new Scanner(file); // while(sc.hasNextLine()) { //System.out.println(sc.hasNextLine()); } //} /** 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); } }

}

image text in transcribedimage text in transcribedimage text in transcribed

txt.

~ val AAA BB C DDD E Alfa S ~ > ~ 99 fig Bravo 67 999 -42 3 x == Charlie ~ ~ 4 ~ yz 9 Delta 4e 3 22 x ~ !=2 Echo yes ~ ~ ~ d>e 33

use Java

ICS 340 Spring 2021 Spring 2021 val MA Programming Project There will be one multi-deliverable Java programming project this semester, which will be divided into three separately-graded program deliverables (DelivA through DelivC). A specification for each deliverable will be uploaded to D2L. Each deliverable will build on past deliverables > fig 67 999 Bravo Charlie Delta Echo CDDD 99 3 3 - yz x d> 3 4 22 de yes 9 !-2 ! 33 . 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 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. The first row is a header row. The first "" is merely a placeholder. All files will have this at the beginning of the header row. o val" is a column label for the column of node values. a The rest of the strings are memonics for the nodes of the graph. They are alphanumeric and do not contain spaces. Each subsequent row describes one node and its outgoing edges. The first column contains the name of the node (e.g., "Alfa") The string in the "val" column is the value of the node, which may be numeric, but which will certainly always consist of a string of printable characters. The character "_" by itself stands for "no value". (eg., node "Charlie" has no value.) For the rest of the columns, if there is no edge going from the node to another node, there is a "_" in the appropriate column. (c.g., there is no edge from node "Charlie" to node "Alfa") . If there is anything else in that column, that is the label of the edge. For some deliverables, this will be numeric, for others it may not be. Note that self-edges are allowed. If you like a picture of what this is, here's what the above table looks like in graphical form: Grading Deliverable A is worth 25 points. It is pretty simple if you understand O programming in Java. For each of Deliverables B (50 pts) and C (55 pts): Correct functioning of the program is worth 40 and points (DelivB and DelivC). You will receive a set of test files which will be used to evaluate the program. Some other test files may also be used that are not given to you (in real life, you design to the specification, you can't test everything). You will receive the desired output for at least one test file. Part of learning the material in the class is figuring out the correct answer to the test files yourself. The correctness points are divided among the tests. For instance, if there are 8 tests, cach test is worth 10 points. Normally, a test is either all or nothing. Oor max points, but you can get substantial partial credit for output whose content is correct, but whose formatting is off. For Deliverable C, there will be a report worth 5 points. Ethe M Ats DOC D Program Architecture The initial program reads a text file and interprets it as a graph. Input File All the input files in this course will look something like this, file ABO.ext. Note that all columns of data are separated by 1 or more spaces (not tabs): Windows Ink Workspace Program Classes and Methods There are 4 classes in the program given to you in addition to a sub class for each deliverable, A-C): Prog 340 is the main class, which handles the IO. Graph is a class whose objects represent graphs Node is a class whose objects represent nodes (a.k.a., vertices) in the graph. Edge is a class whose objects represent edges (a.k.a., arcs) between nodes of the graph. Note that each edge has a direction. It goes from one specific node to another specific node. Here is a basie UML class diagram for these four classes. All methods and attributes are listed explicitly rather than using composition connectors, to be easily understood by people with little UML background. Prog340 Graph File inputFile ArrayList nodeList File outputFile ArrayListget Nodellist String() comboBoxList ArrayList void add Edge Edge Prag 3404) void main() String toString) void createAndShowGUI) void actionPerformed Actino Event) void read Graphinfo) Rules These rules will apply to all the program deliverables for this course 1. You may use any code you can find on the publicly accessible internet (or any other published source) as part of your program. However, if you take code from the internet or published source, you must acknowledge your source. You may not use code from your classmates or other unpublished sources. Two programs or program fragments that are unduly similar and are not acknowledged as having come from a published source will be considered as being incidences of plagiarism, with a 0 for the assignment and a report sent to the Academic Integrity committee. A "published source" is nor somebody else's program unless it's on a publicly accessible, free site (thus, for instance, stackoverflow.com is fine, but Chegg or Course Hero (which require signup) are not). 2. Do not publish any of your solutions to GitHub or any other public place before the end of the semester. If you do, and if someone uses your code, this will be considered academic misconduct and result in your getting a 0 for that deliverable. 3. Your program must run on the university's Windows computers "as is" - without having to import any packages that are not part of the Oracle Java SE installed on the university systems. If it does so, it should run on the instructors' system. Two common problems in the past have been: a) People who write their programs on Macs sometimes have trouble, especially with 1/0. This should not be a problem because you are given working I/O code. If your Deliverable A runs properly on the grading computers, it's very likely all the other deliverables will, too, as long as you don't violate rule (b) below. b) People who import other packages, most commonly import from cclipse.org or netbeans.org Please do not import anything outside of Java SE to do problems in this class, and if you do so, this may make your program not compile and/or run properly on the university system. This will severely impact your grade, since 80-90% of the program grade is based on test results from a running program. There is no reason for you to make the mistake of importing any package not in the Java SE. If you want to use code from the internet that uses such packages, you'll need to strip out those imports and rewrite the equivalent functionality yourself. (Remember to reference your source, please.) Edge String label Node tail Node head Deliverable Submissions Note that most deliverables are due at 2 pm on the due date on D2L. Node ArrayList nodelist; ArrayList edgelist; public Graph() { nodelist = new ArrayList(); edgelist = new ArrayList(); } 1/A node of a graph for the Spring 2018 ICS 340 program 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; outgoing Edges = 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 incoming Edges; } public void setAbbrev( String theAbbrev ) { abbrev = theAbbrev; 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 ); } public void setName( String theName) { name = theName; 3 public void setVal( String theVal ) { val = theVal; } public void addOutgoingEdge Edge e) { outgoingEdges.add( e ); } public void add Incoming Edge( Edge e ) { incoming Edges.add( e ); } } ICS 340 Program Deliverable A Spring 2021 ICS 340 Programming Project Deliverable A Specification: Start with the given Java program "prog 340", 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: L 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 yraph. val AAA CODD x 99 g Bravo 52 99942 3 Charlie 4 ya 9 Delta 4 3 Echo 33 * The output for this file should be: Edge Eron Alpha to Brave labeled > Edge from Alpha to Delta labeled 99. Baga from Alpha to Echa labeled fig. Edge Pran Bravo to Alpha labeled 999 Edge Eron Bravo to Bravo labeled -42 Edge Pran Bravo to Charlie labeled 3. Edge Eron Bravo to Delta labeled X. Edge from Bravo to Echo labeled Edge Eron Charlle o Bravo Labeled 4. Bage from Charlle to Delta labeled ye. Page Exam 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. Baga from Delta to Echo labeled 1-2.. Rage From Scho ta Delta labeled de. Edge Eron Echo to Loho labeled 33. The order in which the Edges print out is not important. See the prog340_pring 2020.docx" file for details ICS 340 Spring 2021 Spring 2021 val MA Programming Project There will be one multi-deliverable Java programming project this semester, which will be divided into three separately-graded program deliverables (DelivA through DelivC). A specification for each deliverable will be uploaded to D2L. Each deliverable will build on past deliverables > fig 67 999 Bravo Charlie Delta Echo CDDD 99 3 3 - yz x d> 3 4 22 de yes 9 !-2 ! 33 . 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 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. The first row is a header row. The first "" is merely a placeholder. All files will have this at the beginning of the header row. o val" is a column label for the column of node values. a The rest of the strings are memonics for the nodes of the graph. They are alphanumeric and do not contain spaces. Each subsequent row describes one node and its outgoing edges. The first column contains the name of the node (e.g., "Alfa") The string in the "val" column is the value of the node, which may be numeric, but which will certainly always consist of a string of printable characters. The character "_" by itself stands for "no value". (eg., node "Charlie" has no value.) For the rest of the columns, if there is no edge going from the node to another node, there is a "_" in the appropriate column. (c.g., there is no edge from node "Charlie" to node "Alfa") . If there is anything else in that column, that is the label of the edge. For some deliverables, this will be numeric, for others it may not be. Note that self-edges are allowed. If you like a picture of what this is, here's what the above table looks like in graphical form: Grading Deliverable A is worth 25 points. It is pretty simple if you understand O programming in Java. For each of Deliverables B (50 pts) and C (55 pts): Correct functioning of the program is worth 40 and points (DelivB and DelivC). You will receive a set of test files which will be used to evaluate the program. Some other test files may also be used that are not given to you (in real life, you design to the specification, you can't test everything). You will receive the desired output for at least one test file. Part of learning the material in the class is figuring out the correct answer to the test files yourself. The correctness points are divided among the tests. For instance, if there are 8 tests, cach test is worth 10 points. Normally, a test is either all or nothing. Oor max points, but you can get substantial partial credit for output whose content is correct, but whose formatting is off. For Deliverable C, there will be a report worth 5 points. Ethe M Ats DOC D Program Architecture The initial program reads a text file and interprets it as a graph. Input File All the input files in this course will look something like this, file ABO.ext. Note that all columns of data are separated by 1 or more spaces (not tabs): Windows Ink Workspace Program Classes and Methods There are 4 classes in the program given to you in addition to a sub class for each deliverable, A-C): Prog 340 is the main class, which handles the IO. Graph is a class whose objects represent graphs Node is a class whose objects represent nodes (a.k.a., vertices) in the graph. Edge is a class whose objects represent edges (a.k.a., arcs) between nodes of the graph. Note that each edge has a direction. It goes from one specific node to another specific node. Here is a basie UML class diagram for these four classes. All methods and attributes are listed explicitly rather than using composition connectors, to be easily understood by people with little UML background. Prog340 Graph File inputFile ArrayList nodeList File outputFile ArrayListget Nodellist String() comboBoxList ArrayList void add Edge Edge Prag 3404) void main() String toString) void createAndShowGUI) void actionPerformed Actino Event) void read Graphinfo) Rules These rules will apply to all the program deliverables for this course 1. You may use any code you can find on the publicly accessible internet (or any other published source) as part of your program. However, if you take code from the internet or published source, you must acknowledge your source. You may not use code from your classmates or other unpublished sources. Two programs or program fragments that are unduly similar and are not acknowledged as having come from a published source will be considered as being incidences of plagiarism, with a 0 for the assignment and a report sent to the Academic Integrity committee. A "published source" is nor somebody else's program unless it's on a publicly accessible, free site (thus, for instance, stackoverflow.com is fine, but Chegg or Course Hero (which require signup) are not). 2. Do not publish any of your solutions to GitHub or any other public place before the end of the semester. If you do, and if someone uses your code, this will be considered academic misconduct and result in your getting a 0 for that deliverable. 3. Your program must run on the university's Windows computers "as is" - without having to import any packages that are not part of the Oracle Java SE installed on the university systems. If it does so, it should run on the instructors' system. Two common problems in the past have been: a) People who write their programs on Macs sometimes have trouble, especially with 1/0. This should not be a problem because you are given working I/O code. If your Deliverable A runs properly on the grading computers, it's very likely all the other deliverables will, too, as long as you don't violate rule (b) below. b) People who import other packages, most commonly import from cclipse.org or netbeans.org Please do not import anything outside of Java SE to do problems in this class, and if you do so, this may make your program not compile and/or run properly on the university system. This will severely impact your grade, since 80-90% of the program grade is based on test results from a running program. There is no reason for you to make the mistake of importing any package not in the Java SE. If you want to use code from the internet that uses such packages, you'll need to strip out those imports and rewrite the equivalent functionality yourself. (Remember to reference your source, please.) Edge String label Node tail Node head Deliverable Submissions Note that most deliverables are due at 2 pm on the due date on D2L. Node ArrayList nodelist; ArrayList edgelist; public Graph() { nodelist = new ArrayList(); edgelist = new ArrayList(); } 1/A node of a graph for the Spring 2018 ICS 340 program 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; outgoing Edges = 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 incoming Edges; } public void setAbbrev( String theAbbrev ) { abbrev = theAbbrev; 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 ); } public void setName( String theName) { name = theName; 3 public void setVal( String theVal ) { val = theVal; } public void addOutgoingEdge Edge e) { outgoingEdges.add( e ); } public void add Incoming Edge( Edge e ) { incoming Edges.add( e ); } } ICS 340 Program Deliverable A Spring 2021 ICS 340 Programming Project Deliverable A Specification: Start with the given Java program "prog 340", 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: L 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 yraph. val AAA CODD x 99 g Bravo 52 99942 3 Charlie 4 ya 9 Delta 4 3 Echo 33 * The output for this file should be: Edge Eron Alpha to Brave labeled > Edge from Alpha to Delta labeled 99. Baga from Alpha to Echa labeled fig. Edge Pran Bravo to Alpha labeled 999 Edge Eron Bravo to Bravo labeled -42 Edge Pran Bravo to Charlie labeled 3. Edge Eron Bravo to Delta labeled X. Edge from Bravo to Echo labeled Edge Eron Charlle o Bravo Labeled 4. Bage from Charlle to Delta labeled ye. Page Exam 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. Baga from Delta to Echo labeled 1-2.. Rage From Scho ta Delta labeled de. Edge Eron Echo to Loho labeled 33. The order in which the Edges print out is not important. See the prog340_pring 2020.docx" file for details

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

Step: 3

blur-text-image

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

Students also viewed these Databases questions

Question

Are BWS and RTS necessary evils for the legal system?

Answered: 1 week ago