Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class Calc {
- private double a, b;
- private boolean running = true;
- private final Scanner mScanner;
- public Calc() {
- mScanner = new Scanner(System.in);
- start();
- }
- private void start() {
- while (running) {
- getInput();
- processInput();
- }
- mScanner.close();
- }
- private void processInput() {
- System.out.println("1. Arithmetic");
- System.out.println("2. Boolean");
- System.out.println("3. Bitwise (Integers only!)");
- System.out.println("4. Exit");
- System.out.print("Select an operation: ");
- int op = mScanner.nextInt();
- switch (op) {
- case 1:
- doArithmetic();
- break;
- case 2:
- doBoolean();
- break;
- case 3:
- doBitwise();
- break;
- case 4:
- System.out.println("Exiting...");
- running = false;
- break;
- default:
- System.err.println("Invalid operation");
- processInput();
- break;
- }
- }
- private void doArithmetic() {
- System.out.println("1. Add");
- System.out.println("2. Subtract");
- System.out.println("3. Multiply");
- System.out.println("4. Divide");
- System.out.println("5. Modulo");
- System.out.println("6. Power");
- System.out.println("7. Root");
- System.out.println("8. Natural log");
- System.out.print("Select an operation: ");
- int op = mScanner.nextInt();
- if (op < 9 && op > 0) {
- System.out.print("The result is: ");
- }
- switch (op) {
- case 1:
- System.out.println(a + b);
- break;
- case 2:
- System.out.println(a - b);
- break;
- case 3:
- System.out.println(a * b);
- break;
- case 4:
- System.out.println(a / b);
- break;
- case 5:
- System.out.println(a % b);
- break;
- case 6:
- System.out.println(Math.pow(a, b));
- break;
- case 7:
- System.out.println(Math.pow(a, 1 / b));
- break;
- case 8:
- System.out.println(Math.log(a) + ", " + Math.log(b));
- break;
- default:
- System.err.println("Invalid operation");
- doArithmetic();
- break;
- }
- }
- private void doBoolean() {
- System.out.println("1. Greater");
- System.out.println("2. Greater than or equal");
- System.out.println("3. Smaller");
- System.out.println("4. Smaller than or equal");
- System.out.println("5. Logical equal");
- System.out.print("Select an operation: ");
- int op = mScanner.nextInt();
- if (op < 6 && op > 0) {
- System.out.print("The result is: ");
- }
- switch (op) {
- case 1:
- System.out.println(a > b);
- break;
- case 2:
- System.out.println(a >= b);
- break;
- case 3:
- System.out.println(a < b);
- break;
- case 4:
- System.out.println(a <= b);
- break;
- case 5:
- System.out.println(a == b);
- break;
- default:
- System.err.println("Invalid operation");
- doBoolean();
- break;
- }
- }
- private void doBitwise() {
- System.out.println("1. AND");
- System.out.println("2. OR");
- System.out.println("3. XOR");
- System.out.println("4. NAND");
- System.out.println("5. NOR");
- System.out.println("6. Shift left");
- System.out.println("7. Shift right");
- System.out.print("Select an operation: ");
- int op = mScanner.nextInt();
- if (op < 8 && op > 0) {
- System.out.print("The result is: ");
- }
- int iA = (int) a;
- int iB = (int) b;
- switch (op) {
- case 1:
- System.out.println(iA & iB);
- break;
- case 2:
- System.out.println(iA | iB);
- break;
- case 3:
- System.out.println(iA ^ iB);
- break;
- case 4:
- // TODO: Implement NAND
- break;
- case 5:
- System.out.println(~(iA | iB));
- break;
- case 6:
- System.out.println(iA << iB);
- break;
- case 7:
- System.out.println(iA >> iB);
- break;
- default:
- System.err.println("Invalid operation");
- doBitwise();
- break;
- }
- }
- private void getInput() {
- System.out.print("Enter the first number: ");
- a = mScanner.nextDouble();
- System.out.print("Enter the second number: ");
- b = mScanner.nextDouble();
- }
- public static void main(String[] args) {
- new Calc();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement