-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFractionDriver.java
More file actions
89 lines (77 loc) · 2.46 KB
/
FractionDriver.java
File metadata and controls
89 lines (77 loc) · 2.46 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import java.util.ArrayList;
import java.util.Scanner;
import java.io.File;
public class FractionDriver {
/**
* main
* ----
* This method is the main method of the fractionsv2 class
* PRE:
* POST: prints the unique fractions in "fractions.txt" and their count
*/
public static void main(String[] args) {
ArrayList<FractionCounter> filledList = fillList("fractions.txt");
printList(filledList);
}
/**
* getNextFraction
* ---------------
* This method convert the next line of the file into a fraction
* and return it.
* PRE: reader is not null.
* Each line of txt file must be a fraction ("int / int")
* POST: return Fraction object containing numerator and denominator
*/
private static Fraction getNextFraction(Scanner reader) {
String[] curFraction = reader.nextLine().split("/");
int numerator = Integer.parseInt(curFraction[0]);
int denominator = Integer.parseInt(curFraction[1]);
return new Fraction(numerator, denominator);
}
/**
* printList
* ---------
* This method will print out toString form of each FractionCounter in
* the ArrayList
* PRE: counterList does not contain null values
* POST: prints each fraction and their count
*/
private static void printList(ArrayList<FractionCounter> counterList) {
for (int i = 0; i < counterList.size(); i++)
System.out.println(counterList.get(i).toString());
}
/**
* fillList
* --------
* This method fills a FractionCounter ArrayList from a file
* PRE: fileName is a txt file with a fraction in each line
* POST: returns ArrayList of FractionCounters for fileName
*/
private static ArrayList<FractionCounter> fillList(String fileName) {
Scanner reader;
try {
reader = new Scanner(new File(fileName));
} catch (Exception e) {
System.out.println("FILE NOT FOUND!!!");
return null;
}
ArrayList<FractionCounter> counterList = new ArrayList<>();
// adding first fraction in the ArrayList
counterList.add(new FractionCounter(getNextFraction(reader)));
while (reader.hasNextLine()) {
Fraction curFraction = getNextFraction(reader);
for (int i = 0; i < counterList.size(); i++) {
// if curFraction is already counted, break. (count is increased)
if (counterList.get(i).compareAndIncrement(curFraction)) {
break;
}
// if reached to the end of list, add new counter to list
else if (i == counterList.size() - 1) {
counterList.add(new FractionCounter(curFraction));
break;
}
}
}
return counterList;
}
}