-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPA3.java
More file actions
70 lines (60 loc) · 1.5 KB
/
Copy pathPA3.java
File metadata and controls
70 lines (60 loc) · 1.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
// Hannah Provenza
// Data Structures
// 12 December 2015
// Programming Assignment #3
// This file contains the main method for PA3.
import java.util.*;
import java.io.*;
public class PA3{
/*
* Main method! Tada!
* Runs in O(n) time.
*/
public static void main (String[] args){
File f = getFile();
processFile(f);
}
/*
* Gets the file from the user!
* O(1) - self-evident
* @return File f that the user asked to open.
*/
public static File getFile(){
Scanner input = new Scanner(System.in);
System.out.println("Please enter the name of the file: ");
String filename = input.next();
File f = new File(filename);
return f;
}
/*
* Processes all the lines of text in the file!
* Runs in O(n) time where n is the number of lines.
* @param File f the file containing the lines.
*/
public static void processFile(File f){
try {
Scanner file = new Scanner(f);
HashTable Hash = new HashTable(2029);
minPriorityQueue Q = new minPriorityQueue(62887);
while (file.hasNextLine()){
int id = file.nextInt();
int priority = file.nextInt();
String word = file.next();
Process p = new Process(id, priority, word);
if (Hash.addRecord(p)){
Q.insert(p);
}
}
while (Q.size > 21){
Q.extractMin();
}
String output = "";
while (Q.size > 0){
output = Hash.delete(Q.extractMin()).getName() + " " + output;
}
System.out.println(output);
} catch(FileNotFoundException ex){
System.out.println("error: file not found");
}
}
}