From 3248b428124ec9c7853291b71384be12e78cef03 Mon Sep 17 00:00:00 2001 From: Zach S-B Date: Sat, 28 Aug 2021 16:45:06 +1000 Subject: [PATCH] Filereader changed to take user input --- A1.java | 202 +++++++++++++++++++++++++++++-------------------- Algorithm.java | 11 +++ 2 files changed, 130 insertions(+), 83 deletions(-) diff --git a/A1.java b/A1.java index f8a952c..a8dfbf5 100644 --- a/A1.java +++ b/A1.java @@ -24,19 +24,29 @@ public class A1 { */ public void run() { - readFile(fileName); //Read the file + System.out.println("COMP2240 - Assignment 1 \n Author: Zach S-B \n Student# :c3262201"); + + + double[] fcfsResults = new double[2]; + double[] srtResults = new double[2]; + double[] ltrResults = new double[2]; + + readFile(); //Read the file //printFile(); Algorithm fcfs = new Algorithm(disp, processList, randomNum, "FCFS"); fcfs.runDispatch(); + fcfsResults[0] = fcfs.averageTurnTime(); + fcfsResults[1] = fcfs.averageWaitTime(); printResults(); - System.out.println(fcfs.averageTurnTime()); reset(); System.out.println(); Algorithm srt = new Algorithm(disp, processList, randomNum,"SRT"); srt.runDispatch(); + srtResults[0] = srt.averageTurnTime(); + srtResults[1] = srt.averageWaitTime(); printResults(); reset(); @@ -44,8 +54,12 @@ public class A1 { Algorithm ltr = new Algorithm(disp, processList, randomNum,"LTR"); ltr.runDispatch(); + ltrResults[0] = ltr.averageTurnTime(); + ltrResults[1] = ltr.averageWaitTime(); printResults(); - reset(); + reset(); + + printAverages(fcfsResults, srtResults, ltrResults); } @@ -57,6 +71,17 @@ public class A1 { } } + public void printAverages(double[] fcfs, double[] srt, double[] ltr) + { + System.out.println("\nAlgorithm \tAverage Turnaround Time \tAverage Waiting Time"); + + System.out.printf("FCFS\t\t%.2f\t\t\t\t%.2f\n",fcfs[0],fcfs[1]); + System.out.printf("SRT\t\t%.2f\t\t\t\t%.2f\n",srt[0],srt[1]); + System.out.printf("FBV\t\t-\t\t\t\t-\n"); + System.out.printf("LTR\t\t%.2f\t\t\t\t%.2f\n",ltr[0],ltr[1]); + + + } public void printResults() { System.out.println("\nProcess \tTurnaround Time \tWaiting Time"); @@ -71,105 +96,116 @@ public class A1 { * readFile. Reads the file given, creating processes as needed to save details. * @param fileNameInput address of the file to be read in. */ - public void readFile(String fileNameInput) + public void readFile() { + boolean fileRead = false; + Scanner sc = new Scanner(System.in); + Scanner fileReader; //Scanner to read the file. - File dataSet = new File(fileNameInput); - - - //Try and open file. If it works, procede. - try { - fileReader = new Scanner(dataSet); + File dataSet;// = new File(fileNameInput); + while(!fileRead) + { + System.out.print("Enter filename: "); + String fileNameInput = sc.nextLine(); + + dataSet = new File(fileNameInput); + //Try and open file. If it works, procede. + try { + fileReader = new Scanner(dataSet); + fileRead = true; - //Progress through each line in the text file - while(fileReader.hasNextLine()){ - String line = fileReader.nextLine(); - - //If the line has "EOF" written, stop reading lines. - if(line.equals("EOF")) - { - - break; - } - - else if(line.startsWith("DISP")) - { - disp =Integer.parseInt(line.substring(6)); - } - - //If the Line starts with "ID:", create a new process - else if(line.startsWith("ID:")) - { - //Create new proccess - Process temp = new Process(); - - - temp.setId(line.substring(4)); //Set the id of the proccess to whatever remains of the line after the "ID: " - - //As we've read in the ID, we shall assume the following lines (until "END") are all part of the process definition. - while(fileReader.hasNextLine()) + //Progress through each line in the text file + while(fileReader.hasNextLine()){ + String line = fileReader.nextLine(); + + //If the line has "EOF" written, stop reading lines. + if(line.equals("EOF")) { - line = fileReader.nextLine(); - if(line.equals("END")) + break; + } + + else if(line.startsWith("DISP")) + { + disp =Integer.parseInt(line.substring(6)); + } + + //If the Line starts with "ID:", create a new process + else if(line.startsWith("ID:")) + { + //Create new proccess + Process temp = new Process(); + + + temp.setId(line.substring(4)); //Set the id of the proccess to whatever remains of the line after the "ID: " + + //As we've read in the ID, we shall assume the following lines (until "END") are all part of the process definition. + while(fileReader.hasNextLine()) { - break; - } - - else if(line.startsWith("Arrive:")) - { - temp.setArrive(Integer.parseInt(line.substring(8))); - } + line = fileReader.nextLine(); - else if(line.startsWith("ExecSize:")) - { + if(line.equals("END")) + { + break; + } - temp.setSize(Integer.parseInt(line.substring(10))); - } - - else if(line.startsWith("Tickets:")) - { - temp.setTickets(Integer.parseInt(line.substring(9))); - } + else if(line.startsWith("Arrive:")) + { + temp.setArrive(Integer.parseInt(line.substring(8))); + } - else - { - //Delete process? + else if(line.startsWith("ExecSize:")) + { + + temp.setSize(Integer.parseInt(line.substring(10))); + } + + else if(line.startsWith("Tickets:")) + { + temp.setTickets(Integer.parseInt(line.substring(9))); + } + + else + { + //Delete process? + } } - } - processList.add(temp); - } - - //If we read the line "BEGINRANDOM", we've started the random numbers section. Until the "ENDRANDOM" line, - //we'll just keep reading in each number (and it is assumed each line is a single number) and adding it - //to the random number array. - else if(line.equals("BEGINRANDOM")) - { - while(fileReader.hasNextLine()) + processList.add(temp); + } + + //If we read the line "BEGINRANDOM", we've started the random numbers section. Until the "ENDRANDOM" line, + //we'll just keep reading in each number (and it is assumed each line is a single number) and adding it + //to the random number array. + else if(line.equals("BEGINRANDOM")) { - line = fileReader.nextLine(); + while(fileReader.hasNextLine()) + { + line = fileReader.nextLine(); - if(line.equals("ENDRANDOM")) - { - break; - } - else - { - randomNum.add(Integer.parseInt(line)); + if(line.equals("ENDRANDOM")) + { + break; + } + else + { + randomNum.add(Integer.parseInt(line)); + } } } } + fileReader.close(); + + } + + //If the file isn't found, throw a hissy fit. + catch (FileNotFoundException e) { + System.out.println("File not found."); + //e.printStackTrace(); //Optional, shows more data. } - fileReader.close(); - } - - //If the file isn't found, throw a hissy fit. - catch (FileNotFoundException e) { - System.out.println("File not found."); - //e.printStackTrace(); //Optional, shows more data. } + sc.close(); } public void printFile() diff --git a/Algorithm.java b/Algorithm.java index 4617e45..7fef7eb 100644 --- a/Algorithm.java +++ b/Algorithm.java @@ -220,4 +220,15 @@ public class Algorithm { return turnTime/processListFromFile.size(); } + + public double averageWaitTime() + { + double waitTime = 0; + for(int i = 0; i