mirror of
https://github.com/zach-sb/COMP2240-Assignment1.git
synced 2024-07-02 12:04:00 +10:00
Filereader changed to take user input
This commit is contained in:
parent
ae03995d4e
commit
3248b42812
46
A1.java
46
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,9 +54,13 @@ public class A1 {
|
||||
|
||||
Algorithm ltr = new Algorithm(disp, processList, randomNum,"LTR");
|
||||
ltr.runDispatch();
|
||||
ltrResults[0] = ltr.averageTurnTime();
|
||||
ltrResults[1] = ltr.averageWaitTime();
|
||||
printResults();
|
||||
reset();
|
||||
|
||||
printAverages(fcfsResults, srtResults, ltrResults);
|
||||
|
||||
}
|
||||
|
||||
public void reset()
|
||||
@ -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,16 +96,24 @@ 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);
|
||||
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()){
|
||||
@ -163,6 +196,7 @@ public class A1 {
|
||||
}
|
||||
}
|
||||
fileReader.close();
|
||||
|
||||
}
|
||||
|
||||
//If the file isn't found, throw a hissy fit.
|
||||
@ -171,6 +205,8 @@ public class A1 {
|
||||
//e.printStackTrace(); //Optional, shows more data.
|
||||
}
|
||||
}
|
||||
sc.close();
|
||||
}
|
||||
|
||||
public void printFile()
|
||||
{
|
||||
|
||||
@ -220,4 +220,15 @@ public class Algorithm {
|
||||
|
||||
return turnTime/processListFromFile.size();
|
||||
}
|
||||
|
||||
public double averageWaitTime()
|
||||
{
|
||||
double waitTime = 0;
|
||||
for(int i = 0; i<processListFromFile.size(); i++)
|
||||
{
|
||||
waitTime+= processListFromFile.get(i).getWaitTime();
|
||||
}
|
||||
|
||||
return waitTime/processListFromFile.size();
|
||||
}
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user