Implement a subject scheduler using the topological sorting algorithm. The program shall prompt for an
input file containing the subjects in the set and the partial ordering of subjects. A subject in the input
file shall be of the form (number, subject) where number is an integer assigned to the subject and
subject is the course identifier [e.g. (1, CS 1)]. Every (number, subject) pair must be placed on a
separate line in the input file. To terminate the input, use (0, 0). Prerequisites of subjects shall be
retrieved also from the same file. A prerequisite definition must be of the form (i, j), one line per pair,
where i is the number assigned to the prerequisite of subject numbered j. Also terminate the input
using (0, 0).
The output is also a file where the name will be asked from the user. The output shall bear the semester
number (just an auto-number from 1 to n) along with the subjects for the semester in table form.
For simplicity, we won’t consider here year level requisites and seasonal subjects.

please help me...

Read this...

PS: PMs are not welcomed, also one of forum rules - Keep It On the Site

Do not post asking for an answer to be sent to you via email or PM. Problems and their responses assist others who read them. Please do not email or PM forum staff with your support questions.

First you need to learn how to read and write to files. There are plenty code examples here and tutorials.
You will have a separate class for that with methods like:

public void write(SomeObject [] elements){
}
public SomeObject read() {
}

You will have set,get methods for the file.

The SomeObject will be the object with attributes the number and the subject described in the file.

You better use the split method of the String class in order to split each line and get the number and subject when reading the file

Also search the API for the Comparable interface if you are free to use any method in order to do the sorting. I don't know what algorithm is this: topological sorting algorithm

i already have my codes... but there is a problem in reading and writing files... could you check it? plz...

package objects.sorters.sortProper;

import objects.sorters.preSort.SorterNode;

public class TopologicalSorter {
    private SorterNode sort[];
    private int output[],topOut;

    public TopologicalSorter(SorterNode unsorted[]) {
        init(unsorted.length);
        sort = unsorted;
    }

    protected void init(int length) {
        sort = new SorterNode[length];
        output = new int[length];
        topOut = 0;
    }

    public void sortList() {
        setZero();
        for(int i=0;i<sort.length;i++) {
            if(canVisit(i) && sort[i].isZero) {
                visit(i);
            }
            sort[i].isZero = false;
        }
        if(!visitedAll()) {
            sortList();
        }
    }

    public int[] getList() {
        return(output);
    }

    public int getSize() {
        return(output.length);
    }

    private void setZero() {
        for(int i=0;i<sort.length;i++) {
            if(sort[i].count==0) {
                sort[i].isZero = true;
            }
        }
    }

    private boolean canVisit(int n) {
        return(!sort[n].wasVisited && sort[n].count<=0);
    }

    private void visit(int n) {
        sort[n].setVisited();
        output[topOut] = sort[n].num;
        topOut++;
        while(sort[n].hasQ()) {
            track(search(sort[n].remQ()));
        }
    }

    private void track(int n) {
        sort[n].subCount();
    }

    private int search(int n) {
        for(int i=0;i<sort.length;i++) {
            if(n==sort[i].num) {
                return(i);
            }
        }
        return(0);
    }

    private boolean visitedAll() {
        for(int i=0;i<sort.length;i++) {
            if(!sort[i].wasVisited) {
                return(false);
            }
        }
        return(true);
    }
}



package process;

import javax.swing.JOptionPane;

import objects.sorters.preSort.NodeBuilder;
import objects.sorters.sortProper.TopologicalSorter;

public class TopologicalSortingApplication {

    public static void main(String[] args) {
        process();
    }

    private static void process() {
        JOptionPane.showMessageDialog(null,"Welcome to my Sorter!");
        String arr1[] = inputStrArray();
        NodeBuilder b = new NodeBuilder(arr1);
        TopologicalSorter t = new TopologicalSorter(b.getNodes());
        t.sortList();
        show(t.getList());
        process();
    }

    private static String[] inputStrArray() {
        String temp[] = new String[100];
        int i = 0;
        do {
            temp[i] = new String(inputString());
            i++;
        } while(!temp[i-1].equalsIgnoreCase("x,x"));
        String real[] = new String[i-1];
        for(int x=0;x<real.length;x++) {
            real[x] = temp[x];
        }
        return(real);
    }

    private static String inputString() {
        return(JOptionPane.showInputDialog(null,"Input Ordered Pair:"));
    }

    private static void show(int arr[]) {
        String temp = new String("Sorted List: ");
        for(int i=0;i<arr.length;i++) {
            temp = temp + arr[i] + " ";
        }
        JOptionPane.showMessageDialog(null,temp);
    }

}



package objects.fileIO;

import java.io.File;
import java.io.FileWriter;
import java.io.Writer;
import java.io.FileNotFoundException;

public class FileOutput {
    String subjects[];

    public FileOutput(String subjects[]) {
        this.subjects = new String[subjects.length];
        this.subjects = subjects;
    }

    public void write(File filename) throws FileNotFoundException {
        try {
            Writer writer = new FileWriter(filename);
            for(int i=0;i<subjects.length;i++) {
                writer.write("Semester "+(i+1)+":"+"\n"+subjects[i]+"\n");
            }
            writer.flush();
            writer.close();
        } catch(Exception e) {
            throw new FileNotFoundException();
        }
    }
}



package objects.fileIO;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.LineNumberReader;

public class FileInput {
    private String lineData[];  
    private int lines = 0;

    public FileInput(File fileName) {
        LineNumberReader counter = null;
        LineNumberReader reader = null; 
        int i;
        int countLine;

        try {
            counter = new LineNumberReader(new BufferedReader(new FileReader(fileName)));
            reader = new LineNumberReader(new BufferedReader(new FileReader(fileName)));
            String line;
            i=0;
            countLine=0;

            for(line=counter.readLine(); line!=null; line=counter.readLine()) {
                countLine++;
            }

            this.lines = countLine;
            lineData = new String[countLine];

            for(line=reader.readLine(); line!=null; line=reader.readLine()) {
                this.lineData[i] = line;
                i++;
            }

            reader.close();
            counter.close();
        } catch (FileNotFoundException e) {
            GUI.popMess("ERROR: FILE NOT FOUND!");
            e.printStackTrace();
            System.exit(0);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public String[] getArray() {
        return(lineData); 
    }

    public int getLines() {
        return(lines); 
    }
}

Edited 3 Years Ago by Reverend Jim: Fixed formatting

You reading from file. Can you provide copy of it for testing?
(Use advanced posting option when replying "Go Advanced". Bellow post window are other post options one of them is "Manage Attachments", use it to upload and attach file)

PS: When posting code next time please use code tags [code] CODE HERE [/code] or [code=Java]CODE HERE [/code]

You need to use BufferedWriter to write to files. And use the newLine() method for changing lines:

BufferedWriter writer = new BufferedWriter(new FileWriter("file"));
writer.write("ABCD");
writer.newLine();


writer.close();

Also for reading this is better:

BufferedReader reader = new BufferedReader(new FileReader(fileName));

String line = reader.readLine();
while(line!=null) {
//do whatever you want with the line



//read the next line
line = reader.readLine();
//continue with the loop
}


reader.close();

Don't forger to close the reader, writer at the end of the reading writing

uhm... i have another question... where will i put the codes? i mean what is the class that needs 'repair'? hehe...

This article has been dead for over six months. Start a new discussion instead.