Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Please can someone tell me how to incorporate/add this graph.txt into this program. So that I can get the result when I click class dependence

Please can someone tell me how to incorporate/add this graph.txt into this program. So that I can get the result when I click class dependence or directed graph button. Please.

Here is the graph.txt file I created on notepad:

ClassA ClassC ClassE ClassJ

ClassB ClassD ClassG

ClassC ClassA

ClassE ClassB ClassF ClassH

ClassJ ClassB

ClassI ClassC

Here are the codes:

import java.awt.Color;

import java.awt.FlowLayout;

import java.awt.GridLayout;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.awt.event.WindowAdapter;

import java.awt.event.WindowEvent;

import java.io.FileNotFoundException;

import java.util.ArrayList;

import javax.swing.BorderFactory;

import javax.swing.JButton;

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.JOptionPane;

import javax.swing.JPanel;

import javax.swing.JTextField;

public class GUI extends JFrame {

DirectedGraph graph = new DirectedGraph();

public GUI() {

JPanel rootPanel = new JPanel(new GridLayout(2, 1));

JPanel filePanel = new JPanel(new FlowLayout());

filePanel.add(new JLabel("Input file name: "));

JTextField filename = new JTextField(15);

filePanel.add(filename);

JButton btnBuild = new JButton("Directed Graph");

filePanel.add(btnBuild);

// rootPanel.add(filePanel);

filePanel.add(new JLabel("Class to recompile:"));

JTextField classname = new JTextField(15);

filePanel.add(classname);

JButton topoOrder = new JButton("Class Dependencies");

filePanel.add(topoOrder);

rootPanel.add(filePanel);

JPanel resultPanel = new JPanel();

JLabel compilationOrder = new JLabel("");

resultPanel.setBorder(BorderFactory.createTitledBorder("Dependency Relationship"));

resultPanel.setBackground(Color.WHITE);

resultPanel.add(compilationOrder);

rootPanel.add(resultPanel);

btnBuild.addActionListener(new ActionListener() {

@Override

public void actionPerformed(ActionEvent e) {

String file = filename.getText();

if (file.equals("")) {

return;

}

try {

graph.buildGraphFromFile(file);

JOptionPane.showMessageDialog(null, "Cycle detected", "Message",

JOptionPane.INFORMATION_MESSAGE);

} catch (FileNotFoundException ex) {

JOptionPane.showMessageDialog(null, "Vertex action", "Message",

JOptionPane.INFORMATION_MESSAGE);

}

}

});

topoOrder.addActionListener(new ActionListener() {

@Override

public void actionPerformed(ActionEvent e) {

String className = classname.getText();

if (className.equals("")) {

return;

}

try {

ArrayList dependencyOrder = graph.getDependency(className);

if (null == dependencyOrder) {

JOptionPane.showMessageDialog(null, "Descend action", "Message",

JOptionPane.INFORMATION_MESSAGE);

} else {

String order = "";

for (String o : dependencyOrder) {

order += o + " ";

}

compilationOrder.setText(order);

}

} catch (Exception ex) {

JOptionPane.showMessageDialog(null, "Ascend action", "Message", JOptionPane.INFORMATION_MESSAGE);

}

}

});

add(rootPanel);

// add(resultPanel);

addWindowListener(new GuiWindowAdapter());

setTitle("Class Dependency Graph");

setVisible(true);

setSize(500, 300);

setResizable(false);

}

private class GuiWindowAdapter extends WindowAdapter {

@Override

public void windowClosing(WindowEvent e) {

System.exit(0);

}

}

}

import java.io.File;

import java.io.FileNotFoundException;

import java.util.ArrayList;

import java.util.HashMap;

import java.util.Scanner;

import java.io.FileNotFoundException;

import java.util.ArrayList;

import java.util.HashMap;

import java.util.Scanner;

public class DirectedGraph {

private ArrayList> adjacencyList = new ArrayList<>();

private HashMap mapping = new HashMap<>();

private ArrayList stack = new ArrayList<>();

private boolean[] discoverd;

private boolean[] finished;

public void buildGraphFromFile(String filename) throws FileNotFoundException {

Scanner file = new Scanner(new File(filename));

int index = 0;

while (file.hasNextLine()) {

String line = file.nextLine();

String[] classNames = line.split(" ");

for (String key : classNames) {

if (!mapping.containsKey(key)) {

mapping.put(key, index);

index++;

}

}

}

file.close();

// Initialize adjacency list

discoverd = new boolean[mapping.size()];

finished = new boolean[mapping.size()];

for (int i = 0; i < mapping.size(); i++) {

adjacencyList.add(new ArrayList<>());

discoverd[i] = false;

finished[i] = false;

}

// reopen again to build the adjacency list

file = new Scanner(new File(filename));

while (file.hasNextLine()) {

String line = file.nextLine();

String[] classNames = line.split(" ");

int nodeIndex = mapping.get(classNames[0]);

for (int i = 1; i < classNames.length; i++) {

adjacencyList.get(nodeIndex).add(mapping.get(classNames[i]));

}

}

}

public void depthFisrtSearch(int classIndex) throws Exception {

if (discoverd[classIndex]) {

throw new Exception("Cycle detected");

}

if (finished[classIndex]) {

return;

}

discoverd[classIndex] = true;

ArrayList adjacentVertices = adjacencyList.get(classIndex);

for (Integer k : adjacentVertices) {

depthFisrtSearch(k);

}

finished[classIndex] = true;

stack.add(classIndex);

}

public ArrayList getDependency(String className) throws Exception {

ArrayList dependecyOrder = new ArrayList<>();

if (null == mapping.get(className)) {

return null;

}

depthFisrtSearch(mapping.get(className));

for (int index : stack) {

dependecyOrder.add(0, getKey(index));

}

stack.clear();

for (int i = 0; i < mapping.size(); i++) {

discoverd[i] = finished[i] = false;

}

return dependecyOrder;

}

private String getKey(int value) {

Object[] keys = mapping.keySet().toArray();

int index = 0;

String className = null;

for (Object key : keys) {

if (mapping.get(key.toString()) == value) {

className = key.toString();

}

index++;

}

return className;

}

}

import java.io.FileNotFoundException;

public class Main {

public static void main(String[] args) throws FileNotFoundException, Exception {

new GUI();

}

}

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

Financial management theory and practice

Authors: Eugene F. Brigham and Michael C. Ehrhardt

12th Edition

978-0030243998, 30243998, 324422695, 978-0324422696

Students also viewed these Programming questions