Skip to content

Commit cdc0675

Browse files
authored
MathToolkit.java
1 parent dfd8d69 commit cdc0675

File tree

1 file changed

+313
-0
lines changed

1 file changed

+313
-0
lines changed
Lines changed: 313 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,313 @@
1+
/*
2+
* MathToolkit.java
3+
* A simple single-file Java project that provides a command-line math toolkit.
4+
* All user-facing text is in English as requested.
5+
*
6+
* Features:
7+
* - Basic arithmetic (addition, subtraction, multiplication, division)
8+
* - GCD and LCM
9+
* - Factorial (uses BigInteger for large results)
10+
* - Fibonacci (n-th, uses BigInteger)
11+
* - Prime check (BigInteger.isProbablePrime)
12+
* - Quadratic equation solver (real and complex roots)
13+
*
14+
* Usage:
15+
* Compile: javac MathToolkit.java
16+
* Run: java MathToolkit
17+
*
18+
* Author: ChatGPT
19+
*/
20+
21+
import java.math.BigInteger;
22+
import java.util.InputMismatchException;
23+
import java.util.Scanner;
24+
25+
public class MathToolkit {
26+
27+
private static final Scanner scanner = new Scanner(System.in);
28+
29+
public static void main(String[] args) {
30+
System.out.println("Welcome to MathToolkit — a simple Java math project.");
31+
boolean running = true;
32+
while (running) {
33+
printMenu();
34+
System.out.print("Choose an option (1-9) or 0 to exit: ");
35+
int choice = readInt();
36+
System.out.println();
37+
switch (choice) {
38+
case 0:
39+
running = false;
40+
System.out.println("Goodbye!");
41+
break;
42+
case 1:
43+
basicArithmetic();
44+
break;
45+
case 2:
46+
gcdLcm();
47+
break;
48+
case 3:
49+
factorial();
50+
break;
51+
case 4:
52+
fibonacci();
53+
break;
54+
case 5:
55+
primeCheck();
56+
break;
57+
case 6:
58+
quadraticSolver();
59+
break;
60+
case 7:
61+
integerOperationsDemo();
62+
break;
63+
case 8:
64+
about();
65+
break;
66+
case 9:
67+
help();
68+
break;
69+
default:
70+
System.out.println("Invalid option — please try again.");
71+
}
72+
System.out.println();
73+
}
74+
scanner.close();
75+
}
76+
77+
private static void printMenu() {
78+
System.out.println("=== MathToolkit Menu ===");
79+
System.out.println("1) Basic arithmetic ( +, -, *, / )");
80+
System.out.println("2) GCD and LCM");
81+
System.out.println("3) Factorial (BigInteger)");
82+
System.out.println("4) Fibonacci (n-th term, BigInteger)");
83+
System.out.println("5) Prime check");
84+
System.out.println("6) Quadratic equation solver");
85+
System.out.println("7) Integer operations demo (safe long handling)");
86+
System.out.println("8) About this project");
87+
System.out.println("9) Help (short usage)");
88+
System.out.println("0) Exit");
89+
}
90+
91+
private static void basicArithmetic() {
92+
System.out.println("-- Basic arithmetic --");
93+
System.out.print("Enter first number: ");
94+
double a = readDouble();
95+
System.out.print("Enter second number: ");
96+
double b = readDouble();
97+
System.out.println("Choose operator: + - * /");
98+
System.out.print("Operator: ");
99+
String op = scanner.next();
100+
double result;
101+
switch (op) {
102+
case "+":
103+
result = a + b;
104+
System.out.printf("Result: %.10g\n", result);
105+
break;
106+
case "-":
107+
result = a - b;
108+
System.out.printf("Result: %.10g\n", result);
109+
break;
110+
case "*":
111+
result = a * b;
112+
System.out.printf("Result: %.10g\n", result);
113+
break;
114+
case "/":
115+
if (b == 0) {
116+
System.out.println("Error: Division by zero is not allowed.");
117+
} else {
118+
result = a / b;
119+
System.out.printf("Result: %.10g\n", result);
120+
}
121+
break;
122+
default:
123+
System.out.println("Unknown operator.");
124+
}
125+
}
126+
127+
private static void gcdLcm() {
128+
System.out.println("-- GCD and LCM --");
129+
System.out.print("Enter first integer (long): ");
130+
long a = readLong();
131+
System.out.print("Enter second integer (long): ");
132+
long b = readLong();
133+
long g = gcd(a, b);
134+
long l = lcm(a, b);
135+
System.out.println("GCD(" + a + ", " + b + ") = " + g);
136+
System.out.println("LCM(" + a + ", " + b + ") = " + l);
137+
}
138+
139+
private static void factorial() {
140+
System.out.println("-- Factorial --");
141+
System.out.print("Enter a non-negative integer (n): ");
142+
int n = readNonNegativeInt();
143+
BigInteger fact = factorialBig(n);
144+
System.out.println(n + "! = " + fact.toString());
145+
}
146+
147+
private static void fibonacci() {
148+
System.out.println("-- Fibonacci (n-th) --");
149+
System.out.print("Enter n (0-based index, non-negative): ");
150+
int n = readNonNegativeInt();
151+
BigInteger fib = fibonacciBig(n);
152+
System.out.println("Fibonacci(" + n + ") = " + fib.toString());
153+
}
154+
155+
private static void primeCheck() {
156+
System.out.println("-- Prime check --");
157+
System.out.print("Enter integer to check (positive): ");
158+
long n = readLongPositive();
159+
BigInteger big = BigInteger.valueOf(n);
160+
boolean probablePrime = big.isProbablePrime(12); // 12 -> sufficiently small error probability
161+
if (probablePrime) {
162+
System.out.println(n + " is probably prime.");
163+
} else {
164+
System.out.println(n + " is composite (not prime).");
165+
}
166+
}
167+
168+
private static void quadraticSolver() {
169+
System.out.println("-- Quadratic equation solver: ax^2 + bx + c = 0 --");
170+
System.out.print("Enter a (non-zero): ");
171+
double a = readDoubleNonZero();
172+
System.out.print("Enter b: ");
173+
double b = readDouble();
174+
System.out.print("Enter c: ");
175+
double c = readDouble();
176+
double disc = b * b - 4 * a * c;
177+
if (disc > 0) {
178+
double r1 = (-b + Math.sqrt(disc)) / (2 * a);
179+
double r2 = (-b - Math.sqrt(disc)) / (2 * a);
180+
System.out.printf("Two real roots: x1 = %.10g, x2 = %.10g\n", r1, r2);
181+
} else if (disc == 0) {
182+
double r = -b / (2 * a);
183+
System.out.printf("One real root: x = %.10g\n", r);
184+
} else {
185+
double real = -b / (2 * a);
186+
double imag = Math.sqrt(-disc) / (2 * a);
187+
System.out.printf("Two complex roots: x1 = %.10g + %.10gi, x2 = %.10g - %.10gi\n", real, imag, real, imag);
188+
}
189+
}
190+
191+
private static void integerOperationsDemo() {
192+
System.out.println("-- Integer operations demo --");
193+
System.out.println("This demonstrates safe handling with long and BigInteger when needed.");
194+
System.out.print("Enter a long integer: ");
195+
long a = readLong();
196+
System.out.print("Enter another long integer: ");
197+
long b = readLong();
198+
System.out.println("Sum as long: " + (a + b));
199+
BigInteger bigSum = BigInteger.valueOf(a).add(BigInteger.valueOf(b));
200+
System.out.println("Sum as BigInteger: " + bigSum.toString());
201+
}
202+
203+
private static void about() {
204+
System.out.println("MathToolkit — single-file Java project. All text is English.");
205+
System.out.println("Feel free to request additional features or a multi-file project structure.");
206+
}
207+
208+
private static void help() {
209+
System.out.println("Help: Use options from the menu, input numbers when asked.");
210+
System.out.println("For large factorial or Fibonacci values, the program uses BigInteger.");
211+
}
212+
213+
// --- Utility math functions ---
214+
215+
private static long gcd(long a, long b) {
216+
a = Math.abs(a);
217+
b = Math.abs(b);
218+
if (a == 0) return b;
219+
if (b == 0) return a;
220+
while (b != 0) {
221+
long t = a % b;
222+
a = b;
223+
b = t;
224+
}
225+
return Math.abs(a);
226+
}
227+
228+
private static long lcm(long a, long b) {
229+
a = Math.abs(a);
230+
b = Math.abs(b);
231+
if (a == 0 || b == 0) return 0;
232+
return Math.abs(a / gcd(a, b) * b);
233+
}
234+
235+
private static BigInteger factorialBig(int n) {
236+
BigInteger result = BigInteger.ONE;
237+
for (int i = 2; i <= n; i++) {
238+
result = result.multiply(BigInteger.valueOf(i));
239+
}
240+
return result;
241+
}
242+
243+
private static BigInteger fibonacciBig(int n) {
244+
if (n == 0) return BigInteger.ZERO;
245+
if (n == 1) return BigInteger.ONE;
246+
BigInteger prev = BigInteger.ZERO, curr = BigInteger.ONE;
247+
for (int i = 2; i <= n; i++) {
248+
BigInteger next = prev.add(curr);
249+
prev = curr;
250+
curr = next;
251+
}
252+
return curr;
253+
}
254+
255+
// --- Robust input helpers ---
256+
257+
private static int readInt() {
258+
while (true) {
259+
try {
260+
return scanner.nextInt();
261+
} catch (InputMismatchException ex) {
262+
System.out.print("Invalid integer — please enter again: ");
263+
scanner.next();
264+
}
265+
}
266+
}
267+
268+
private static int readNonNegativeInt() {
269+
while (true) {
270+
int v = readInt();
271+
if (v < 0) System.out.print("Please enter a non-negative integer: ");
272+
else return v;
273+
}
274+
}
275+
276+
private static long readLong() {
277+
while (true) {
278+
try {
279+
return scanner.nextLong();
280+
} catch (InputMismatchException ex) {
281+
System.out.print("Invalid long — please enter again: ");
282+
scanner.next();
283+
}
284+
}
285+
}
286+
287+
private static long readLongPositive() {
288+
while (true) {
289+
long v = readLong();
290+
if (v <= 0) System.out.print("Please enter a positive integer: ");
291+
else return v;
292+
}
293+
}
294+
295+
private static double readDouble() {
296+
while (true) {
297+
try {
298+
return scanner.nextDouble();
299+
} catch (InputMismatchException ex) {
300+
System.out.print("Invalid number — please enter again: ");
301+
scanner.next();
302+
}
303+
}
304+
}
305+
306+
private static double readDoubleNonZero() {
307+
while (true) {
308+
double v = readDouble();
309+
if (v == 0.0) System.out.print("Value cannot be zero — please enter again: ");
310+
else return v;
311+
}
312+
}
313+
}

0 commit comments

Comments
 (0)