-
Notifications
You must be signed in to change notification settings - Fork 0
/
DiffQuestions.java
206 lines (172 loc) · 6.59 KB
/
DiffQuestions.java
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
/**
* Names: Ishan Garg, Krish Patel, Pranav Mahabal
* Course: MCV4U0-1
* Date: January 18, 2023
* Teacher: Ms Iulia Gugoiu
*
* Generates the regular differentiation questions.
*/
import java.util.Random;
public class DiffQuestions {
private double correctAnswer;
private String displayFunction;
private int x;
private int y;
private String question;
private Random rand;
private String[] displayFunctions;
private String[] displayExps;
private Function[] funcs;
private Function[] xFuncs;
private Function func;
private double temp;
/**
* Class constructor.
*/
public DiffQuestions() {
correctAnswer = 0.0;
displayFunctions = new String[]{
"sin(x)",
"cos(x)",
"tan(x)",
"log(x)",
"log10(x)",
"sqrt(x)",
"x^(1/3)",
"x^3",
"2^x"
};
funcs = new Function[] {
(x) -> Math.sin((double) x),
(x) -> Math.cos((double) x),
(x) -> Math.tan((double) x),
(x) -> Math.log((double) x),
(x) -> Math.log10((double) x),
(x) -> Math.sqrt((double) x),
(x) -> Math.pow(x, 1.0/3.0),
(x) -> Math.pow(x, 3),
(x) -> Math.pow(2, x),
};
xFuncs = new Function[]{
(x) -> Math.pow(x,2),
(x) -> Math.pow(x,3),
(x) -> Math.sqrt(x),
(x) -> y*x,
};
displayExps = new String[]{
"x^2",
"x^3",
"sqrt(x)",
"y*x"
};
x = 0;
y = 0;
question = "";
rand = new Random();
}
/**
* Generates the summation rule question.
*/
private void generateQuestion1() {
x = rand.nextInt(20) + 1;
int i = rand.nextInt(6) + 0;
int j = rand.nextInt(6) + 0;
while(i == j) {
j = rand.nextInt(6) + 0;
}
displayFunction = displayFunctions[i] + "+" + displayFunctions[j];
correctAnswer = diff(funcs[i], (double)x) + diff(funcs[j], (double)x);
displayFunction = displayFunction.replaceAll("log10", "temp").replaceAll("log", "ln").replaceAll("temp", "log10").replaceAll("Math.", "");
question = "What is the slope of the tangent of y = " + displayFunction + " at x = " + x + "?";
}
/**
* Generates the product rule question.
*/
private void generateQuestion2() {
x = rand.nextInt(20) + 1;
int i = rand.nextInt(9) + 0;
int j = rand.nextInt(9) + 0;
while(i == j) {
j = rand.nextInt(9) + 0;
}
displayFunction = displayFunctions[i] + " * " + displayFunctions[j];
correctAnswer = diff(funcs[i], (double)x)*funcs[j].eval((double)x) + diff(funcs[j], (double)x)*funcs[i].eval((double)x);
displayFunction = displayFunction.replaceAll("log10", "temp").replaceAll("log", "ln").replaceAll("temp", "log10").replaceAll("Math.", "");
question = "What is the slope of the tangent of y = " + displayFunction + " at x = " + x + "?";
}
/**
* Generates the quotient rule question.
*/
private void generateQuestion3() {
x = rand.nextInt(8) + 2;
y = rand.nextInt(10) + 2;
int z = rand.nextInt(14) + 2;
int i = rand.nextInt(4) + 0;
displayFunction = ("(" + displayExps[i] + ")/(x+z)").replaceAll("y", String.valueOf(y)).replaceAll("z", String.valueOf(z));
correctAnswer = (diff(xFuncs[i], x)*(double)(x+z)-xFuncs[i].eval(x)*1.0)/Math.pow(x+z, 2);
question = "What is the slope of the tangent of y = " + displayFunction + " at x = " + x + "?";
}
/**
* Generates the normal to the graph, chain rule question.
*/
private void generateQuestion4() {
x = rand.nextInt(6) + 1;
y = rand.nextInt(100) + 2;
int i = rand.nextInt(5) + 0;
int j = rand.nextInt(4) + 0;
displayFunction = displayFunctions[i].replaceFirst("x", displayExps[j]).replaceAll("y", String.valueOf(y));
correctAnswer = -1.0/(diff(xFuncs[j], x)*diff(funcs[i], xFuncs[j].eval(x)));
displayFunction = displayFunction.replaceAll("log10", "temp").replaceAll("log", "ln").replaceAll("temp", "log10").replaceAll("Math.", "");
question = "What is the normal of y = " + displayFunction + " at x = " + x + "?\n\nNote: read the question carefully.";
}
/**
* Generates the logarithmic differentiation question.
*/
private void generateQuestion5() {
double g = 0.25*(double)(rand.nextInt(7) + 1);
y = rand.nextInt(3) + 1;
int z = rand.nextInt(3) + 1;
displayFunction = "(" + y + "*x)^(" + z + "*x)";
Function f = (x) -> Math.pow(y*x, z*x);
correctAnswer = diff(f, g);
question = "What is the slope of the tangent of y = " + displayFunction + " at x = " + g + "?";
}
/**
* Calculates the derivative of a function at a point.
* @param f the function.
* @param x the x-value to calculate at.
* @return slope of the graph at that x-value.
*/
private double diff(Function f, double x) {
double h = 0.000000001;
return (f.eval(x+h)-f.eval(x))/h;
}
/**
* Creates interface to store and evaluate functions.
*/
public interface Function {
public double eval(double x);
}
/**
* Sends correct answer to the main program.
* @return Correct answer
*/
public double getCorrectAns() {
return correctAnswer;
}
/**
* Retrieves which question to generate, then generates and sends it to the main program.
* @param num Question number.
* @return question to be asked.
*/
public String getQuestion(int num) {
switch(num) {
case 100 -> generateQuestion1();
case 200 -> generateQuestion2();
case 300 -> generateQuestion3();
case 400 -> generateQuestion4();
case 500 -> generateQuestion5();
}
return question;
}
}