-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCalc.java
More file actions
168 lines (142 loc) · 5.56 KB
/
Calc.java
File metadata and controls
168 lines (142 loc) · 5.56 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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
package calculatorprojecteval;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;
import java.lang.*;
/**
*
* @author ParkerC
*/
public class CalculatorProjectEval
{
/**
* @param args the command line arguments
*/
public static void main(String[] args) throws FileNotFoundException
{
// comment out either startCalc() or testCalc() (just the method call below)
// based on how you want to run the project
// use this code to drive your interactive calculator
// add a welcome message here
//startCalc(); // you have to write this method below
// it should ask the user for input and print
// results until the user enters "quit" to stop
// use this to validate your project (the calculator part, anyways)
testCalc(); // testCalc will call a calculate(String s) method that you create
// as part of your overall project. This method will take the user's
// input, and return a String with the appropriate output.
// add a goodbye message here
}
public static void testCalc() throws FileNotFoundException{
ArrayList<String> problems = new ArrayList<>();
ArrayList<String> results = new ArrayList<>();
// load problems from a file
File fProblems = new File("problems.txt");
Scanner sc = new Scanner(fProblems);
int count = 0;
String line = "";
int problemCount = 0;
int resultCount = 0;
while (sc.hasNextLine()){
line = sc.nextLine();
if (!line.startsWith("//") && !line.trim().equals("")){ // ignore comments at the beginning
problems.add(line.substring(3).trim());
problemCount++;
if (sc.hasNextLine()){
line = sc.nextLine();
if (!line.startsWith("//") && !line.trim().equals("")){
results.add(line.substring(3).trim());
resultCount++;
}
}
count++;
}
}
if (problemCount == resultCount){
// now run the tests
for (int i=0; i<problemCount; i++){
String prob = problems.get(i);
String result = calculate(prob);
if (result == null){
System.out.println("FAILED test " + i);
System.out.println("Expression: " + problems.get(i));
System.out.println("Expected result: " + results.get(i));
System.out.println("Actual: null String returned from calculate()");
} else {
if (result.equals(results.get(i))){
System.out.println("PASSED test " + i);
} else {
System.out.println("FAILED test " + i);
System.out.println("Expression: " + problems.get(i));
System.out.println("Expected result: " + results.get(i));
System.out.println("Actual: " + result);
}
}
}
} else {
System.out.println("problem file error");
}
}
public static void startCalc() {
// your code here to get user input, and calculate/print results. You'll call
// the calculate(String s) as part of your code here, which returns a String
// with the result to print.
String input = ReadInput();
if ("quit".equalsIgnoreCase(input)){
System.out.println("E: quit");
return;
}else{
String result = calculate(input);
System.out.println("E: "+ result);
}
}
public static String calculate(String s){
try{
// you add code here to take a String as a parameter, and return a String with the result
String[] tokens = Tokenize(s);
if (tokens == null || tokens.length <= 1){
// Invalid input when there is just one token
throw new Exception("");
}else{
switch (tokens[0]){
case "|":
return ProcessAbsCommand(tokens);
case "v":
return ProcessSqrtCommand(tokens);
}
return "";
}
} catch (Exception e){
return "ERROR";
}
}
public static String ReadInput(){
Scanner scan = new Scanner(System.in);
return scan.next();
}
private static String[] Tokenize(String input){
return input.split(" ");
}
private static String ProcessSqrtCommand(String[] tokens) throws Exception{
if (tokens.length != 2){
throw new Exception("");
}
int num = ParseInt(tokens[1]);
return String.valueOf(Math.sqrt(num));
}
private static String ProcessAbsCommand(String[] tokens) throws Exception{
if (tokens.length != 2){
throw new Exception("");
}
int num = ParseInt(tokens[1]);
return String.valueOf(Math.abs(num));
}
private static int ParseInt(String token) throws Exception{
try{
return Integer.parseInt(token);
}catch (Exception e){
throw new Exception(token);
}
}
}