-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCalc.java
More file actions
118 lines (95 loc) · 3.89 KB
/
Calc.java
File metadata and controls
118 lines (95 loc) · 3.89 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
import java.util.Scanner;
//necessary for the exponentiate function, as Java does not have a natural exponent operator
public class Calc {
// instance fields
double num1;
double num2;
// constructor method
public Calc(double firstNum, double secondNum) {
num1 = firstNum;
num2 = secondNum;
}
//class methods
public double add(double firstNum, double secondNum){
return firstNum + secondNum;
}
public double subtract(double firstNum, double secondNum){
return firstNum - secondNum;
}
public double multiply(double firstNum, double secondNum){
return firstNum * secondNum;
}
public long expontentiate(double firstNum, double secondNum){
// Math.pow returns a double, so use typecasting to account for large numbers
long exponentProd = (long) Math.pow(firstNum, secondNum);
return exponentProd;
}
public double divide(double firstNum,double secondNum){
return firstNum / secondNum;
}
public static void backToMainMenu(){
System.out.println("\nReturning to main menu...\n");
wholeThing();
}
public static void wholeThing(){
try{
Thread.sleep(500);
}
catch(InterruptedException ex) {
Thread.currentThread().interrupt();
}
System.out.println("Welcome to the Java CLI Calculator! \n This calculator performs operations on two numbers. \n Please type your first number \n Type 'exit' to exit at anytime! \n");
Scanner sc1 = new Scanner(System.in);
String firstInput = sc1.next();
double firstNum = 0.0;
if(firstInput.equals("exit")){
System.exit(0);
} else{
firstNum = Double.parseDouble(firstInput);
}
// double firstNum = sc1.nextInt();
System.out.println("\nGreat! Now input your second number.\n");
String secondInput = sc1.next();
double secondNum = 0.0;
if(secondInput.equals("exit")){
System.exit(0);
} else{
secondNum = Double.parseDouble(secondInput);
}
Calc calc1 = new Calc(firstNum, secondNum);
System.out.println("\nAwesome! Now, choose the operation you want.\n Type + for addition\n Type - for subtraction\n Type * for multiplication\n Type ** to raise the first number to the power of the second\n Type / to divide");
String operator = sc1.next();
switch (operator){
case "+":
System.out.println(calc1.num1 + " plus " + calc1.num2 + " equals "+ calc1.num1 + " plus " + calc1.num2 + " equals "+calc1.add(firstNum, secondNum));
backToMainMenu();
break;
case "-":
System.out.println(calc1.num1 + " minus " + calc1.num2 + " equals "+calc1.subtract(firstNum, secondNum));
backToMainMenu();
break;
case "*":
System.out.println(calc1.num1 + " multiplied by " + calc1.num2 + " equals "+calc1.multiply(firstNum, secondNum));
backToMainMenu();
break;
case "**":
System.out.println(calc1.num1 + " raised to the power of " + calc1.num2 + " equals "+calc1.expontentiate(firstNum, secondNum));
backToMainMenu();
break;
case "/":
System.out.println(calc1.num1 + " divided by " + calc1.num2 + " equals "+calc1.divide(firstNum, secondNum));
backToMainMenu();
break;
case "exit":
System.exit(0);
break;
default:
System.out.println("Input not recognized.");
backToMainMenu();
break;
}
}
public static void main(String[] args) {
wholeThing();
}
}