SophiaPretty

(5)

$14/per page/Negotiable

About SophiaPretty

Levels Tought:
Elementary,Middle School,High School,College,University,PHD

Expertise:
Accounting,Algebra See all
Accounting,Algebra,Applied Sciences,Architecture and Design,Art & Design,Biology,Business & Finance,Calculus,Chemistry,Communications,Computer Science,Economics,Engineering,English,Environmental science,Essay writing Hide all
Teaching Since: Jul 2017
Last Sign in: 304 Weeks Ago, 5 Days Ago
Questions Answered: 15833
Tutorials Posted: 15827

Education

  • MBA,PHD, Juris Doctor
    Strayer,Devery,Harvard University
    Mar-1995 - Mar-2002

Experience

  • Manager Planning
    WalMart
    Mar-2001 - Feb-2009

Category > Computer Science Posted 13 Dec 2017 My Price 10.00

GUI, a second class is needed to define the directed graph.

Hello again.  Just posted this on one of our closed threads.  Looking to change the Graph Class to a generic class.  Type and parameter description below.  And classes attached.

 

In addition to the main class that defines the GUI, a second class is needed to define the directed graph. It should be a generic class allowing for a generic type for the vertex names. In this application those names will be strings. The graph should be represented as an array list of vertices that contain a linked list of their associated adjacency lists. The adjacency lists should be lists of integers that represent the index rather than vertex name itself. A hash map should be used to associate vertex names with their index in the list of vertices.

  • package p4gui;

    import java.io.File;
    import java.io.FileNotFoundException;
    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.LinkedList;
    import java.util.Scanner;


    import javax.swing.JOptionPane;

    public class Graph {
        private HashMap<String, Integer> classMapping;
        private HashMap<Integer, String> vertexMapping;
        private ArrayList<LinkedList<Integer>> adjacency;
        private boolean cycleFound = false;

        void loadGraph(String fileName) {
            try {
                cycleFound = false;
                Scanner file = new Scanner(new File(fileName));
                classMapping = new HashMap<>();
                vertexMapping =new HashMap<>();
                adjacency = new ArrayList<>();
                int vertex = 0;
                while (file.hasNextLine()) {
                    LinkedList<Integer> vertices = new LinkedList<>();
                    String nodes[] = file.nextLine().split(" ");
                    for (String node : nodes) {
                        if (classMapping.get(node) == null) {
                            classMapping.put(node, vertex);
                            vertexMapping.put(vertex, node);
                            vertices.add(vertex);
                            vertex++;
                        } else {
                            vertices.add(classMapping.get(node));
                        }
                    }
                    adjacency.add(vertices);
                }
                JOptionPane.showMessageDialog(null, "Graph Built Sucessfully");
                return;
            } catch (FileNotFoundException e) {

                JOptionPane.showMessageDialog(null, "File Did Not Open");
                return;
            }
        }

        public String displayGraph(String className) {
            if(classMapping == null){
                JOptionPane.showMessageDialog(null, "Please generate the graph first!!");
                return null;
            }
            Integer vertex = classMapping.get(className);
            String result="";
            if(vertex==null){
                JOptionPane.showMessageDialog(null, "Class Name is not valid!!");
                return null;
            }
            result=vertex+" ";
            result =displayIterative(vertex,result);
            className="";
            for(String text: result.split(" ")){
                className+=vertexMapping.get(Integer.valueOf(text))+" ";
            }

            return className;
        }

        private String displayIterative(Integer vertex, String result) {
            for(LinkedList<Integer> vertices : adjacency){
                if(vertices.get(0)!= vertex)
                    continue;
                for(int i=vertices.size()-1;i>=1;i--){
                     int j =vertices.get(i);
                     if(result.contains(String.valueOf(j))){
                         JOptionPane.showMessageDialog(null, "Cycle exist in the graph!!");
                          System.exit(0);
                     }
                     result = result+j+" ";
                     result =displayIterative(j, result);
                    
                }
               
            }
           
            return result;
           
        }
    }
    package p4gui;

    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;

    import javax.swing.*;

    /**
     *
     * @author waylo
     */
    public class P4GUI extends JFrame implements ActionListener {

        private JLabel inputLabel = new JLabel("Input file name: ");
        private JTextField inputText = new JTextField(10);
        private JButton buildBtn = new JButton("Build Directed Graph");
        private JLabel classLabel = new JLabel("Class to recompile: ");
        private JTextField classText = new JTextField(10);
        private JButton topoBtn = new JButton("Topological Order");
        private JPanel topPanel = new JPanel();
        private JTextArea order = new JTextArea(10, 40);
        private JPanel bottomPanel = new JPanel();
        private JPanel panel = new JPanel();
        private Graph graph;

        public P4GUI() {

            super("Class Dependency Graph");
            graph = new Graph();
            setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
            topPanel.setLayout(new GridLayout(2, 3, 10, 10));
            topPanel.add(inputLabel);
            topPanel.add(inputText);
            topPanel.add(buildBtn);
            topPanel.add(classLabel);
            topPanel.add(classText);
            topPanel.add(topoBtn);
            panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
            panel.add(topPanel);
            bottomPanel.setLayout(new BoxLayout(bottomPanel, BoxLayout.Y_AXIS));
            bottomPanel.add(order);
            bottomPanel.setBorder(BorderFactory
                    .createTitledBorder("Recompilation Order"));
            order.setEditable(false);
            add(panel, BorderLayout.NORTH);
            add(bottomPanel, BorderLayout.SOUTH);
            buildBtn.addActionListener(this);
            topoBtn.addActionListener(this);
            pack();
        }

        public static void main(String[] args) {
            EventQueue.invokeLater(new Runnable() {
                public void run() {
                    new P4GUI().setVisible(true);
                }
            });
        }

        @Override
        public void actionPerformed(ActionEvent e) {
            if (e.getSource() == buildBtn) {
                String fileName = inputText.getText();
                if (fileName.equalsIgnoreCase("")) {
                    JOptionPane.showMessageDialog(null,
                            "Please enter the file name");
                    return;
                }
                graph.loadGraph(fileName);
            }
            else if (e.getSource() == topoBtn) {
                String className = classText.getText();
                if (className.equalsIgnoreCase("")) {
                    JOptionPane.showMessageDialog(null,
                            "Please enter the class name");
                    return;
                }
                String graphCycle =graph.displayGraph(className);
                order.setText(graphCycle);
            }

        }
    }

Answers

(5)
Status NEW Posted 13 Dec 2017 07:12 AM My Price 10.00

-----------  ----------- H-----------ell-----------o S-----------ir/-----------Mad-----------am ----------- Th-----------ank----------- yo-----------u f-----------or -----------you-----------r i-----------nte-----------res-----------t a-----------nd -----------buy-----------ing----------- my----------- po-----------ste-----------d s-----------olu-----------tio-----------n. -----------Ple-----------ase----------- pi-----------ng -----------me -----------on -----------cha-----------t I----------- am----------- on-----------lin-----------e o-----------r i-----------nbo-----------x m-----------e a----------- me-----------ssa-----------ge -----------I w-----------ill----------- be----------- qu-----------ick-----------ly

Not Rated(0)