-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuestions.java
More file actions
63 lines (56 loc) · 2.31 KB
/
Questions.java
File metadata and controls
63 lines (56 loc) · 2.31 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
package officialInternalAssessment;
import java.util.Random;
public class Questions {
private static Random rand = new Random();
public static String generateQuestion(String selectedTopic) {
if ("Algebra".equals(selectedTopic)) {
return generateAlgebraQuestion();
} else if ("Areas & Perimeters".equals(selectedTopic)) {
return generateAreaPerimeterQuestion();
} else {
return "No question available";
}
}
private static String generateAlgebraQuestion() {
int a = rand.nextInt(5) + 1; // 1–5
int b = rand.nextInt(10) + 1; // 1–10
int x = rand.nextInt(10) + 1; // solution
int c = a * x + b;
return a + "x + " + b + " = " + c;
}
private static String generateAreaPerimeterQuestion() {
int length = rand.nextInt(10) + 1;
int width = rand.nextInt(10) + 1;
if (rand.nextBoolean()) {
return "Find area of rectangle with length " + length + " and width " + width;
} else {
return "Find perimeter of rectangle with length " + length + " and width " + width;
}
}
public static String getAnswer(String question) {
try {
if (question.contains("x")) {
// Algebra: ax + b = c
String[] parts = question.split("x \\+ ");
int a = Integer.parseInt(parts[0].trim());
String[] rightSide = parts[1].split("=");
int b = Integer.parseInt(rightSide[0].trim());
int c = Integer.parseInt(rightSide[1].trim());
return String.valueOf((c - b) / a);
} else if (question.contains("area")) {
String[] nums = question.replaceAll("[^0-9 ]", "").trim().split("\\s+");
int l = Integer.parseInt(nums[0]);
int w = Integer.parseInt(nums[1]);
return String.valueOf(l * w);
} else if (question.contains("perimeter")) {
String[] nums = question.replaceAll("[^0-9 ]", "").trim().split("\\s+");
int l = Integer.parseInt(nums[0]);
int w = Integer.parseInt(nums[1]);
return String.valueOf(2 * (l + w));
}
} catch (Exception e) {
return "?";
}
return "?";
}
}