#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    2
    Rep Power
    0

    [Homework] Not displaying my output


    Hi all,

    I'm stuck with my code here, I want the user to be able to select the type of algorithm they want and the algorithm will compute the input txt file to give an answer. Currently I'm stuck on not knowing how to correctly output according to a selected algorithm.

    public class Question1 {


    private ArrayList<JobData> data = new ArrayList<JobData>();

    public Question1(String file, String sort, String timeQuantum) throws IOException{


    loadFile(file);

    outPut();


    if(sort.equals("FCFS")){
    FCFS(data);

    }
    else if(sort.equals("SRT")){
    SRT(data);
    }
    else if(sort.equals("RR")){
    int timeQ = Integer.parseInt(timeQuantum);
    RR(data, timeQ);
    }

    }

    public static void main(String[] args) throws IOException {

    Question1 q = new Question1(args[0], args[1], args[2]);


    }

    public void loadFile(String fName) throws IOException{


    BufferedReader in = new BufferedReader(new FileReader(fName));

    String line = in.readLine();

    while(line != null){
    JobData j = new JobData(line.split(","));
    // System.out.println("Arrival Time" + j.getArrivalTime());
    data.add(j);
    line = in.readLine();
    }


    }

    public void FCFS(ArrayList<JobData> data){

    Collections.sort(data, new FCFSComparator());

    int completionTime = 0;
    for(int i=0; i<data.size(); i++){
    completionTime += data.get(i).getCpuTime();
    data.get(i).setCompletionTime(completionTime);

    }
    }

    public void SRT(ArrayList<JobData> data){

    Collections.sort(data, new SRTComparator());

    int completionTime = 0;
    for(int i=0; i<data.size(); i++){
    completionTime += data.get(i).getCpuTime();
    data.get(i).setCompletionTime(completionTime);
    }

    }

    public void RR(ArrayList<JobData> data, int timeQuantum){
    ArrayList<JobData> finJobs = new ArrayList<JobData>();
    int completionTime = 0;
    while(!data.isEmpty()){

    for(int i=0; i<data.size(); i++){
    JobData j = data.get(i);

    completionTime += j.getCpuTime();
    j.setCompletionTime(completionTime);

    //completed the job
    if(j.getTimeRemaining() >= timeQuantum){

    completionTime += j.getTimeRemaining();
    j.setCompletionTime(completionTime);

    finJobs.add(data.remove(i));
    i--; //with each interation of the loop, it goes back one job as to not miss any in the queue
    }
    //spend x time on job
    else{
    j.spendTime(timeQuantum);

    }


    }

    }

    data = finJobs;
    }

    public void outPut(){

    System.out.println("======================================================");
    System.out.println("Process ID" + "\t" + "|" + "Turnaround time" + "\t" + "|" + "Waiting time");
    System.out.println("======================================================");

    for(JobData job : data){
    System.out.println(job.getProcessID() + "\t" + "\t" + "|" + job.getTurnAroundTime() + "\t" + "\t" + "\t" + "|" + job.getWaitingTime() + "");


    }
    int sumAverageTR = 0;
    for(JobData job : data){
    sumAverageTR += job.getTurnAroundTime();

    }
    int sumAverageWT = 0;
    for(JobData job : data){
    sumAverageWT += job.getWaitingTime();
    }
    System.out.println("======================================================");

    System.out.println("Avg." + "\t" + "\t" + "|" + sumAverageTR/data.size() + "\t" + "\t" + "\t" + sumAverageWT/data.size());

    System.out.println("======================================================");

    }


    }
  2. #2
  3. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,711
    Rep Power
    347
    not knowing how to correctly output
    Can you explain? Post what the program currently outputs and add some comments showing what the output should look like.

    Please edit the post and wrap the code in code tags.

IMN logo majestic logo threadwatch logo seochat tools logo