Skip to content

Commit db76218

Browse files
authored
New Contributor
Merge Pull Request #189-2
2 parents 9ae2b15 + 10aff0a commit db76218

File tree

6 files changed

+347
-0
lines changed

6 files changed

+347
-0
lines changed
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# Scientific Calculator CLI
2+
A command-line utility for computing simple expressions
3+
4+
## Project structure
5+
6+
```
7+
ScientificCalculator-Java-AlexandrAnatoliev
8+
└── src
9+
├── Calculator.java
10+
├── Functions.java
11+
├── MathFunction.java
12+
├── Parser.java
13+
└── ScientificCalculator.java
14+
```
15+
16+
## Compilation
17+
To compile the source classes:
18+
```
19+
javac -d bin src/*.java
20+
```
21+
## Usage
22+
23+
* Navigate to `bin/` directory
24+
```
25+
cd bin/
26+
```
27+
* Run program
28+
```
29+
java ScientificCalculator <arguments>
30+
```
31+
* Run from root directory
32+
```
33+
java -cp bin ScientificCalculator <arguments>
34+
```
35+
36+
#### Examples of use
37+
```
38+
java -cp bin/ ScientificCalculator "sin(3)"
39+
SIN(3.0) = 0.1411200080598672
40+
java -cp bin/ ScientificCalculator cos\(2\)
41+
COS(2.0) = -0.4161468365471424
42+
java -cp bin/ ScientificCalculator "log(5)" + "ln(1)"
43+
LOG(5.0) + LN(1.0) = 0.6989700043360189
44+
```
45+
* Warning!!! To multiplication use 'x' operator instead '*'
46+
```
47+
java -cp bin/ ScientificCalculator 2 x 2
48+
NUMBER(2.0) x NUMBER(2.0) = 4.0
49+
```
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/**
2+
* Class for computing simple mathematical expressions
3+
*
4+
* Support basic arifmetic operations:
5+
* addition, substraction, multiplication, and division
6+
*
7+
* @version 0.1.0
8+
* @since 23.11.2025
9+
* @author AlexandrAnatoliev
10+
*/
11+
public class Calculator {
12+
public double number1;
13+
public double number2;
14+
15+
/**
16+
* Construct a Calculator with two operands
17+
*
18+
* @param number1 First operand
19+
* @param number2 Second operand
20+
*/
21+
public Calculator(double number1, double number2) {
22+
this.number1 = number1;
23+
this.number2 = number2;
24+
}
25+
26+
/**
27+
* Perfoms addition of two numbers
28+
*
29+
* @return Sum of number1 and number2
30+
*/
31+
public double add() {
32+
return number1 + number2;
33+
}
34+
35+
/**
36+
* Perfoms substraction of two numbers
37+
*
38+
* @return Difference between number1 and number2
39+
*/
40+
public double sub() {
41+
return number1 - number2;
42+
}
43+
44+
/**
45+
* Perfoms multiplication of two numbers
46+
*
47+
* @return Product of number1 and number2
48+
*/
49+
public double mult() {
50+
return number1 * number2;
51+
}
52+
53+
/**
54+
* Perfoms division of two numbers
55+
*
56+
* @return Quotient of number1 divided by number2
57+
*/
58+
public double div() {
59+
if(number2 != 0) {
60+
return number1 / number2;
61+
}
62+
else {
63+
System.out.println("You cannot divide by zero");
64+
return 0;
65+
}
66+
}
67+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/**
2+
* Enum representation supported functions.
3+
*
4+
* @version 0.1.0
5+
* @since 23.11.2025
6+
* @author AlexandrAnatoliev
7+
*/
8+
public enum Functions {
9+
SIN,
10+
COS,
11+
TAN,
12+
COT,
13+
LOG,
14+
LN,
15+
EXP,
16+
SQRT,
17+
FACT,
18+
NUMBER
19+
}
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/**
2+
* Class for computing scientific mathematical operations
3+
*
4+
* Support basic scientific mathematical operations:
5+
* Trigonometric functions: sin(x), cos(x), tan(x)
6+
* Logarithms: log(x) for base 10, ln(x) for natural log
7+
* Exponentials and roots: exp(x) and sqrt(x)
8+
* Factorials: fact(x)
9+
*
10+
* @version 0.1.0
11+
* @since 23.11.2025
12+
* @author AlexandrAnatoliev
13+
*/
14+
public class MathFunction {
15+
public Functions function;
16+
public double argument;
17+
18+
/**
19+
* Construct a MathFunction
20+
*
21+
* @param function Function name
22+
* @param argument Function argument
23+
*/
24+
public MathFunction(Functions function, double argument) {
25+
this.function = function;
26+
this.argument = argument;
27+
}
28+
29+
/**
30+
* Performs computation of scientific mathematical function
31+
*
32+
* @return Value of mathematical function
33+
*/
34+
public double getValue() {
35+
switch (function) {
36+
case SIN:
37+
return Math.sin(argument);
38+
case COS:
39+
return Math.cos(argument);
40+
case TAN:
41+
return Math.tan(argument);
42+
case COT:
43+
return 1/Math.tan(argument);
44+
case LOG:
45+
return Math.log10(argument);
46+
case LN:
47+
return Math.log(argument);
48+
case EXP:
49+
return Math.exp(argument);
50+
case SQRT:
51+
return Math.sqrt(argument);
52+
case FACT:
53+
return getFactorial(argument);
54+
default:
55+
return argument;
56+
}
57+
}
58+
59+
/**
60+
* Performs computation of integer factorial
61+
*
62+
* @param num Number to compute factorial for
63+
* @return Factorial value of the number
64+
* @throws IllegalArgumentException if number is less then zero
65+
*/
66+
public double getFactorial(double num) {
67+
if (num < 0) {
68+
throw new IllegalArgumentException(
69+
"Factorial is defined only for non-negative integers");
70+
}
71+
double result = 1;
72+
for (int i = 2; i <= num; i++) {
73+
result *= i;
74+
}
75+
return result;
76+
}
77+
78+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/**
2+
* Class for parsing mathematical functions from string
3+
*
4+
* @version 0.1.0
5+
* @since 23.11.2025
6+
* @author AlexandrAnatoliev
7+
*/
8+
public class Parser {
9+
public String input;
10+
11+
/**
12+
* Construct a Parser
13+
*
14+
* @param input Mathematical function in String
15+
*/
16+
public Parser(String input) {
17+
this.input = input;
18+
}
19+
20+
/**
21+
* Extracts mathematical function name from input string
22+
*
23+
* @return Mathematical function name
24+
*/
25+
public Functions getFunction() {
26+
int openBracket = input.indexOf('(');
27+
if(openBracket == -1) {
28+
return Functions.NUMBER;
29+
}
30+
String functionName = input.substring(0, openBracket).toUpperCase();
31+
return Functions.valueOf(functionName);
32+
}
33+
34+
/**
35+
* Extracts mathematical function argument from input string
36+
*
37+
* @return Mathematical function argument
38+
*/
39+
public double getArgument() {
40+
int openBracket = input.indexOf('(');
41+
int closeBracket = input.indexOf(')');
42+
if(openBracket == -1 || closeBracket == -1) {
43+
return Double.parseDouble(input);
44+
}
45+
String functionArgument = input.substring(openBracket + 1, closeBracket);
46+
return Double.parseDouble(functionArgument);
47+
}
48+
}
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
/**
2+
* A command-line utility for computing simple expressions
3+
*
4+
* Support:
5+
* Trigonometric functions: sin(x), cos(x), tan(x)
6+
* Logarithms: log(x) for base 10, ln(x) for natural log
7+
* Exponentials and roots: exp(x) and sqrt(x)
8+
* Factorials: fact(x)
9+
*
10+
* @version 0.1.0
11+
* @since 23.11.2025
12+
* @author AlexandrAnatoliev
13+
*/
14+
import java.util.Locale;
15+
16+
public class ScientificCalculator {
17+
/**
18+
* Main method that handles command-line arguments and
19+
* computing simple expressions
20+
*
21+
* @param args Command-line arguments in the format:
22+
* [mathFunction1] [operator] [mathFunction2]
23+
* Example: "sin(10)" + "cos(2)"
24+
* [mathFunction]
25+
* Example: "tan(1)"
26+
*/
27+
public static void main(String args[]) {
28+
29+
Locale.setDefault(Locale.US);
30+
31+
if(args.length == 1) {
32+
Parser parser = new Parser(args[0]);
33+
MathFunction mathFunction = new MathFunction(
34+
parser.getFunction(),
35+
parser.getArgument());
36+
System.out.println(mathFunction.function + "(" +
37+
mathFunction.argument + ")" +
38+
" = " + mathFunction.getValue());
39+
40+
} else if (args.length == 3) {
41+
Parser parser1 = new Parser(args[0]);
42+
String operator = args[1];
43+
Parser parser2 = new Parser(args[2]);
44+
45+
MathFunction mathFunction1 = new MathFunction(
46+
parser1.getFunction(),
47+
parser1.getArgument());
48+
MathFunction mathFunction2 = new MathFunction(
49+
parser2.getFunction(),
50+
parser2.getArgument());
51+
52+
Calculator calculator = new Calculator(
53+
mathFunction1.getValue(),
54+
mathFunction2.getValue());
55+
56+
System.out.print(
57+
parser1.getFunction() + "("
58+
+ parser1.getArgument() + ")"
59+
+ " " + operator + " " +
60+
parser2.getFunction() + "("
61+
+ parser2.getArgument() + ")"
62+
+ " = ");
63+
64+
if(operator.equals("+")) {
65+
System.out.println(calculator.add());
66+
} else if(operator.equals("-")) {
67+
System.out.println(calculator.sub());
68+
} else if(operator.equals("x")) {
69+
System.out.println(calculator.mult());
70+
} else if(operator.equals("/")) {
71+
System.out.println(calculator.div());
72+
} else {
73+
System.out.println(
74+
"Use '+', '-', 'x' or '/' operators");
75+
}
76+
} else {
77+
System.out.println("Usage examples:");
78+
System.out.println("java ScientificCalculator \"sin(3)\"");
79+
System.out.println("java ScientificCalculator cos\\(1\\)");
80+
System.out.println("java ScientificCalculator \"sqrt(9)\"");
81+
System.out.println("java ScientificCalculator 2 + 2");
82+
System.out.println("java ScientificCalculator \"tan(4)\" / 2");
83+
System.out.println("java ScientificCalculator \"log(5)\" x \"ln(6)\"");
84+
}
85+
}
86+
}

0 commit comments

Comments
 (0)