-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTest.java
More file actions
163 lines (147 loc) · 4.07 KB
/
Copy pathTest.java
File metadata and controls
163 lines (147 loc) · 4.07 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
// Test.java
package integrals;
import java.util.Scanner;
/**
* Program that tests class <code>Function</code> by showing examples of
* valid functions versus invalid functions.
*
* @author Ayesha Ilyas
* @since 4/24/22
* <p>Updated 4/25/22</p>
* @version 1.0
*
*/
public class Test {
/**
* Tests Function class.
* @param args NA
*/
public static void main(String[] args) {
test1();
test2();
}
/**
* Test class Function by allowing the user to enter a function, displaying its tokenization,
* allowing the user to evaluate the function at a specified value, and displaying the result
* if the value is in the domain. Type QUIT to exit.
*/
public static void test1() {
System.out.println("Type QUIT to exit");
Scanner input = new Scanner(System.in);
System.out.print("Enter a function: ");
String f = input.nextLine();
while (!f.equals("QUIT")) {
try {
Function function = new Function(f);
System.out.println(function.getTokens());
System.out.print("Eval at: ");
System.out.println(function.evaluateAt(input.nextLine()));
System.out.println("LEGAL");
} catch (IllegalArgumentException e) {
e.printStackTrace();
System.out.println("Could not tokenize");
System.out.println("ILLEGAL");
}
catch (ArithmeticException e) {
e.printStackTrace();
System.out.println("Domain error");
}
System.out.println("\n");
System.out.print("Enter a function: ");
f = input.nextLine();
}
input.close();
}
/**
* Prints to the console the result of creating <code>Function</code> objects and evaluating them at 5.
* NOTE: 5 may or may not be in the domain of the tested functions.
*/
public static void test2() {
// valid functions
String[] functions = {
"(x^2)^2",
"(x^2)-(10/2)",
"x/5*8",
"x+5-1+5",
"(2 * (x + 5))",
"((-10 * x + 10) / (x + 1)^3)",
"(-1 / (x ^ 2)) / (1 / 4)",
"(-(x^4) + 10 * x^4 - 24 * x^2)",
"e^(-2 * x + 14)",
"e^(-x + 3)",
"(sin(x + 1) - 1) / cos(x * 5)",
"sin(sin(x)) / (x^3 + 4 * x^2 + x + 4)",
"sin(pi)",
"sin(e)",
"((x^2) + (sec(x * pi))) / ((e^x) - x)",
"(x+3)(x-2)",
"(-x^4)",
"-x^4",
"-x + (-9) - (-10)",
"-x^3(-1)(-x)",
"(-x)^4",
"x / pi",
"x / e",
"1 / x",
"(x^2-1)(x+1)",
"(x^2-1)x",
"x(x^2-1)",
"pi(x-1)",
"x^3sinx",
"xsinx",
"sinx(cosx)",
"sinx(cosx)sinx",
"sin(x)x",
"x^2+2x",
"pisinx",
"3.25cosx",
"(xpi)3.5",
"10/(-5)+1",
};
// invalid functions
String[] illegalFunctions = {
"sin^-1(x)", // use arcsin
"ln+x", // dont use leading plus signs
"cos^4x", // use cos(x) ^ 4
"sqrt(cos^2(x)+1)", // use cos(x) ^ 2
"sinx + cos^4(x)", // use cos(x) ^ 4
"1--x", // Consecutive operators, use 1-x
"10/-5+1", // put parens around -5
"10*-5", // put parens around -5
"x - (+x)", // get rid of leading plus sign
"x++9" // remove extra plus sign
};
// these should work (unless the function cant be evaluated at 5 (ie 5 is not in the domain)
for (String f: functions) {
try {
System.out.println("--------------------------------------------------");
Function func = new Function(f);
System.out.println(func.getTokens());
System.out.println(func.evaluateAt(5));
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("--------------------------------------------------");
System.out.println();
}
// these should all throw exceptions
int counter = 0;
for (String f: illegalFunctions) {
try {
System.out.println("--------------------------------------------------");
Function func = new Function(f);
System.out.println(func.getTokens()); // should always throw an exception
System.out.println(func.evaluateAt(5)); // should never run
} catch (Exception e) {
counter++;
e.printStackTrace();
}
System.out.println("--------------------------------------------------");
System.out.println();
}
if (counter == illegalFunctions.length)
System.out.println("SUCCESS");
else
System.out.println("FAILED");
}
}