Write a Java program that 1. Selects a text file from your computer using either
ID: 3878881 • Letter: W
Question
Write a Java program that
1. Selects a text file from your computer using either a Java Swing JFileChooser or a JavaFX FileChooser, which starts out showing the files in the same directory from which the program was executed.[1]
2. Reads the selected text file. The text file will form an adjacency matrix representation of a graph.
3. Selects the action to perform from a list of seven actions, plus “exit”. (Five of those actions will be “stubs” at this time.)
4. The Action: Writes a description of the graph, determined from the text file, to an output file in the same directory as the input file. If the input file is abc.txt, the output file is abc_out.txt.
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. To ensure this I strongly recommend that you:
1. Use only Oracle Java 8 SE and earlier constructs, and
2. Test it on the University systems before submission if you have any doubts about its ability to run on the University Windows.
Submit the Java source code to the open Deliverable io submission folder. (ioA, ioB, or ioC). Just submit the source code, not a full Eclipse package.
Description of the File:
All the input files in this course will look something like this. Note that all columns of data are separated by 1 or more spaces:
~ val AAA BB C DDD E
Alfa x ~ > ~ 99 fig
Bravo 67 999 -42 3 x ==
Charlie ~ ~ 4 ~ yz 9
Delta 4e 3 22 x ~ !=2
Echo yes ~ ~ ~ d>e 33
· 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.
“val” is a column label for values
The rest of the strings are mnemonics for the nodes of the graph. They are alphanumeric and do not contain spaces.
· Each other row describes one node and its outgoing edges.
The first column contains the name of the node (e.g., “Alfa”)
[1] If you use Eclipse, this could be either the “src” directory or the directory just above it.
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”
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.
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.
Output:
The output should describe the graph in words. The best way to explain this is in regard to the sample graph above. The output for this file should be:
Node Alfa, mnemonic AAA, value x
Alfa has edge to Bravo labeled >
Alfa has edge to Delta labeled 99
Alfa has edge to Echo labeled fig
Alfa has edge from Bravo labeled 999
Alfa has edge from Delta labeled 3
Node Bravo, mnemonic BB, value 67
Bravo has edge to Alfa labeled 999
Bravo has edge to Bravo labeled -42
Bravo has edge to Charlie labeled 3
Bravo has edge to Delta labeled x
Bravo has edge to Echo labeled ==
Bravo has edge from Alfa labeled >
Bravo has edge from Charlie labeled b
Bravo has edge from Delta labeled 22
Node Charlie, mnemonic C, no value
Charlie has edge to Bravo labeled 4
Charlie has edge to Delta labeled yz
Charlie has edge to Echo labeled 9
Charlie has edge from Bravo labeled 3
Charlie has edge from Delta labeled x
Node Delta, mnemonic DDD, value 4e
Delta has edge to Alfa labeled 3
Delta has edge to Bravo labeled 22
Delta has edge to Charlie labeled x
Delta has edge to Echo labeled !=2
Delta has edge from Alfa labeled 99
Delta has edge from Bravo labeled x
Delta has edge from Charlie labeled yz
Delta has edge from Echo labeled d>e
Node Echo, mnemonic E, value yes
Echo has edge to Delta labeled d>e
Echo has edge to Echo labeled 33
Echo has edge from Alfa labeled fig
Echo has edge from Bravo labeled ==
Echo has edge from Charlie labeled 9
Echo has edge from Delta labeled !=2
Echo has edge from Echo labeled 33
The order in which the nodes, and edges incident on a node, print out is not important.
Explanation / Answer
Given below is the code for the question.
Please do rate the answer if it was helpful. Thank you
To indent code in eclipse , select code by pressing ctrl+a and then indent using ctrl+i
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Scanner;
import javax.swing.JFileChooser;
public class Graph {
class Node
{
String name;
String mnemonic;
String value;
ArrayList<Edge> edges; //a list of edges starting from this node
Node(String mnemonic)
{
this.mnemonic = mnemonic;
edges = new ArrayList<Edge>();
}
}
class Edge
{
int fromId;
int toId;
String weight;
Edge(int from, int to, String w)
{
fromId = from;
toId = to;
weight = w;
}
}
private void addEdge(int from, int to, String w)
{
Node n1 = nodes.get(from);
Node n2 = nodes.get(to);
n1.edges.add(new Edge(from, to, w));
if(from != to)
n2.edges.add(new Edge(from, to, w));
}
ArrayList<Node> nodes = new ArrayList<Node>();
public Graph(Scanner file)
{
nodes = new ArrayList<Node>();
process(file);
}
private void process(Scanner file)
{
String line = file.nextLine();
String[] tokens = line.split("\s+");
//process all mnemonics names, starts from 3rd token i.e index 2
for(int i = 2; i < tokens.length; i++)
{
Node n = new Node(tokens[i]);
nodes.add(n);
}
for(int i = 0; i < nodes.size(); i++)
{
Node n = nodes.get(i);
n.name = file.next(); //get the nodes name
n.value = file.next(); // get the node's value
//process the edges weight
for(int j = 0; j < nodes.size(); j++)
{
String weight = file.next();
if(!weight.equals("~"))
{
addEdge(i, j, weight);
}
}
}
}
public void display()
{
for(int i = 0; i < nodes.size(); i++)
{
Node n = nodes.get(i);
System.out.println("Node " + n.name + ", mnemonic " + n.mnemonic +", value " + n.value);
String name1 = n.name;
for(int j = 0; j < n.edges.size(); j++)
{
Edge e = n.edges.get(j);
if(e.fromId == i)
System.out.printf("%s has edge to %s labelled %s ", name1, nodes.get(e.toId).name, e.weight);
else
System.out.printf("%s has edge from %s labelled %s ", name1, nodes.get(e.fromId).name, e.weight);
}
System.out.println(" ");
}
}
public static void main(String[] args) {
JFileChooser fc = new JFileChooser();
fc.setCurrentDirectory(new File("."));
System.out.println("Choose the file containing graph adjacency matrix");
fc.showOpenDialog(null);
File f = fc.getSelectedFile();
if(f == null)
System.out.println("No file selected");
else
{
try {
Scanner inFile = new Scanner(f);
Graph g = new Graph(inFile);
inFile.close();
g.display();
} catch (FileNotFoundException e) {
System.out.println(e.getMessage());
}
}
}
}
input file: graph.txt
===============
~ val AAA BB C DDD E
Alfa x ~ > ~ 99 fig
Bravo 67 999 -42 3 x ==
Charlie ~ ~ 4 ~ yz 9
Delta 4e 3 22 x ~ !=2
Echo yes ~ ~ ~ d>e 33
output
=====
Choose the file containing graph adjacency matrix
Node Alfa, mnemonic AAA, value x
Alfa has edge to Bravo labelled >
Alfa has edge to Delta labelled 99
Alfa has edge to Echo labelled fig
Alfa has edge from Bravo labelled 999
Alfa has edge from Delta labelled 3
Node Bravo, mnemonic BB, value 67
Bravo has edge from Alfa labelled >
Bravo has edge to Alfa labelled 999
Bravo has edge to Bravo labelled -42
Bravo has edge to Charlie labelled 3
Bravo has edge to Delta labelled x
Bravo has edge to Echo labelled ==
Bravo has edge from Charlie labelled 4
Bravo has edge from Delta labelled 22
Node Charlie, mnemonic C, value ~
Charlie has edge from Bravo labelled 3
Charlie has edge to Bravo labelled 4
Charlie has edge to Delta labelled yz
Charlie has edge to Echo labelled 9
Charlie has edge from Delta labelled x
Node Delta, mnemonic DDD, value 4e
Delta has edge from Alfa labelled 99
Delta has edge from Bravo labelled x
Delta has edge from Charlie labelled yz
Delta has edge to Alfa labelled 3
Delta has edge to Bravo labelled 22
Delta has edge to Charlie labelled x
Delta has edge to Echo labelled !=2
Delta has edge from Echo labelled d>e
Node Echo, mnemonic E, value yes
Echo has edge from Alfa labelled fig
Echo has edge from Bravo labelled ==
Echo has edge from Charlie labelled 9
Echo has edge from Delta labelled !=2
Echo has edge to Delta labelled d>e
Echo has edge to Echo labelled 33
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.