0

I made a program that receives a (int) number of courses as well as their (arr) prerequisites and puts the correct order they should be taken. However, whenever I try to run this code, this error message appears: Error: Could not find or load main class assignment5.Assignment5. I cannot figure out how to fix it and would really appreciate some assistance in doing so. Thanks in advance

package Assignment5;

import java.util.*;

/**
 *
 * @author anon
 */
public class findOrder {

    private Stack<Vertex> vertexStack;

    public findOrder() {
        vertexStack = new Stack<>();
    }

    public static void main(String args[]) {
        int[][] arr = {{1, 0}, {2, 0}, {3, 1}, {3, 2}}; //already sent the code last night during the due date, but changed it slightly so it works a little better
        int rows1 = arr.length;

        int[][] brr = {{1, 0}};
        int rows2 = brr.length;

        int[][] crr = {{1, 0}, {2, 1}, {3, 2}, {4, 3}, {5, 4}};
        int rows3 = crr.length;

        int[][] drr = {{1, 0}, {2, 0}, {3, 1}, {2, 1}, {4, 3}, {5, 3}, {6, 4}, {6, 5}};
        int rows4 = drr.length;

        findOrder dfs1 = new findOrder();
        System.out.println(Arrays.toString(dfs1.findOrder(4, arr)));

        findOrder dfs2 = new findOrder();
        System.out.println(Arrays.toString(dfs2.findOrder(2, brr)));

        findOrder dfs3 = new findOrder();
        System.out.println(Arrays.toString(dfs3.findOrder(6, crr)));

        findOrder dfs4 = new findOrder();
        System.out.println(Arrays.toString(dfs4.findOrder(7, drr)));

    }

    private class Vertex {

        int number;
        boolean visited;
        List<Vertex> neighbours;

        public Vertex(int number) {
            this.number = number;
            this.neighbours = new ArrayList<>();
        }

        public int getNumber() {
            return number;
        }

        public void setVisited(boolean visited) {
            this.visited = visited;
        }

        public void addneighbours(Vertex neighbourVertex) {
            this.neighbours.add(neighbourVertex);
        }

        public List<Vertex> getNeighbours() {
            return neighbours;
        }

        public void setNeighbours(List<Vertex> neighbours) {
            this.neighbours = neighbours;
        }

    }

    public void toplogicalSort(Vertex vertex) {
        List<Vertex> neighbours = vertex.getNeighbours();
        for (int i = 0; i < neighbours.size(); i++) {
            Vertex vertexToGo = neighbours.get(i);
            if (vertexToGo != null && !vertexToGo.visited) {
                toplogicalSort(vertexToGo);
                vertexToGo.setVisited(true);
            }
        }
        vertexStack.push(vertex);
    }

    public int[] findOrder(int numCourses, int[][] prerequisites) {
        int[][] adjacentyMatrix = new int[numCourses][numCourses];

        Vertex[] list = new Vertex[numCourses];

        for (int i = 0; i < numCourses; i++) {
            list[i] = new Vertex(i);
        }
        for (int i = 0; i < prerequisites.length; i++) {
            list[prerequisites[i][1]].addneighbours(list[prerequisites[i][0]]);
        }

        toplogicalSort(list[0]);

        int[] answer = new int[numCourses];

        int j = 0;
        for (int i = answer.length - 1; i >= 0; i--) {
            answer[j++] = vertexStack.get(i).getNumber();
        }
        return answer;

    }
}
  • Two things: A) only use relevant tags. Your editor is *not* relevant to the question and B) in java class names go UpperCase, always. – GhostCat May 09 '19 at 05:29

2 Answers2

0

Change your java file name Assignment5.java to findOrder.java file. If you want to keep file name as Assignment5.java then replace it with below code change.

import java.util.*;

/**
 *
 * @author anon
 */
public class Assignment5 {

    private Stack<Vertex> vertexStack;

    public Assignment5() {
        vertexStack = new Stack<>();
    }

    public static void main(String args[]) {
        int[][] arr = {{1, 0}, {2, 0}, {3, 1}, {3, 2}}; //already sent the code last night during the due date, but changed it slightly so it works a little better
        int rows1 = arr.length;

        int[][] brr = {{1, 0}};
        int rows2 = brr.length;

        int[][] crr = {{1, 0}, {2, 1}, {3, 2}, {4, 3}, {5, 4}};
        int rows3 = crr.length;

        int[][] drr = {{1, 0}, {2, 0}, {3, 1}, {2, 1}, {4, 3}, {5, 3}, {6, 4}, {6, 5}};
        int rows4 = drr.length;

        Assignment5 dfs1 = new Assignment5();
        System.out.println(Arrays.toString(dfs1.findOrder(4, arr)));

        Assignment5 dfs2 = new Assignment5();
        System.out.println(Arrays.toString(dfs2.findOrder(2, brr)));

        Assignment5 dfs3 = new Assignment5();
        System.out.println(Arrays.toString(dfs3.findOrder(6, crr)));

        Assignment5 dfs4 = new Assignment5();
        System.out.println(Arrays.toString(dfs4.findOrder(7, drr)));

    }

    private class Vertex {

        int number;
        boolean visited;
        List<Vertex> neighbours;

        public Vertex(int number) {
            this.number = number;
            this.neighbours = new ArrayList<>();
        }

        public int getNumber() {
            return number;
        }

        public void setVisited(boolean visited) {
            this.visited = visited;
        }

        public void addneighbours(Vertex neighbourVertex) {
            this.neighbours.add(neighbourVertex);
        }

        public List<Vertex> getNeighbours() {
            return neighbours;
        }

        public void setNeighbours(List<Vertex> neighbours) {
            this.neighbours = neighbours;
        }

    }

    public void toplogicalSort(Vertex vertex) {
        List<Vertex> neighbours = vertex.getNeighbours();
        for (int i = 0; i < neighbours.size(); i++) {
            Vertex vertexToGo = neighbours.get(i);
            if (vertexToGo != null && !vertexToGo.visited) {
                toplogicalSort(vertexToGo);
                vertexToGo.setVisited(true);
            }
        }
        vertexStack.push(vertex);
    }

    public int[] findOrder(int numCourses, int[][] prerequisites) {
        int[][] adjacentyMatrix = new int[numCourses][numCourses];

        Vertex[] list = new Vertex[numCourses];

        for (int i = 0; i < numCourses; i++) {
            list[i] = new Vertex(i);
        }
        for (int i = 0; i < prerequisites.length; i++) {
            list[prerequisites[i][1]].addneighbours(list[prerequisites[i][0]]);
        }

        toplogicalSort(list[0]);

        int[] answer = new int[numCourses];

        int j = 0;
        for (int i = answer.length - 1; i >= 0; i--) {
            answer[j++] = vertexStack.get(i).getNumber();
        }
        return answer;

    }
}
Dhaval Goti
  • 447
  • 2
  • 10
  • 25
0

If Dhaval's answer does not work for you, try selecting the main class in the project properties. How you do that will depend upon the IDE.

For example:

  • In Intellij, Right click on the class file in the project explorer and select "run findorder.main()".
  • In NetBeans, right click on the project and choose properties, then from the "Run" tab select the name of your class (Assignment5.findorder) as the "Main Class:" - you can use the browse button to help enter the correct class and package name.
  • In Eclipse, you can select anything in the source file of the class and click the run sign. Another way would be to click the arrow next to the run sign and then click on Run Configurations. After this you can create a new run configuration with your specified main class.
  • Other tools will no doubt have other methods.

Here is the NetBeans project properties dialog showing the run tab.

Main class selection in NetBeans

In my example, the package is "hexdump" and the class containing the main method is "HexDumpWithFilter".

dan1st
  • 12,568
  • 8
  • 34
  • 67
GMc
  • 1,764
  • 1
  • 8
  • 26