A
A
Amir Kenesbay2021-10-20 23:07:57
Java
Amir Kenesbay, 2021-10-20 23:07:57

How to write an OOP-style calculator?

You need to write a simple OOP-style calculator. This is a learning problem and I want to ask myself, did I write my code correctly? I would like less procedural code. I made an implementation, but I don't like the format of the code.

import java.util.Scanner;

public class Calculator {
    private final int precision;

    public Calculator(int precision) {
        this.precision = precision;
    }

    public double add(double a, double b) {
        return a + b;
    }

    public double subtract(double a, double b) {
        return a - b;
    }

    public double multiply(double a, double b) {
        double result = a * b;
        double scale = Math.pow(10.0, precision);
        return Math.round(result * scale) / scale;
    }

    public double div(double a, double b) {
        double result = a / b;
        if (b == 0) {
            return result;
        } else {
            double scale = Math.pow(10.0, precision);
            return Math.round(result * scale) / scale;
        }
    }

    public void calculateNumbers(){
        Scanner scanner = new Scanner(System.in);
        Calculator calculator = new Calculator(2);
        System.out.println("Enter the first number: ");
        double inputFirstNumber = scanner.nextDouble();
        System.out.println("Enter the second number: ");
        double inputSecondNumber = scanner.nextDouble();
        System.out.println("Enter operator (+, -, *, /):");
        char operation = scanner.next().charAt(0);
        while (true){
            switch (operation){
                case '+':
                    System.out.println("Result: " + calculator.add(inputFirstNumber, inputSecondNumber));
                    break;
                case '-':
                    System.out.println("Result: " + calculator.subtract(inputFirstNumber, inputSecondNumber));
                    break;
                case '*':
                    System.out.println("Result: " + calculator.multiply(inputFirstNumber, inputSecondNumber));
                    break;
                case '/':
                    System.out.println("Result: " + calculator.div(inputFirstNumber, inputSecondNumber));
                    break;
                default:
                    System.out.println("Not a correct character");
            }
            System.out.println("Do you want to continue? (Y/N)");
        }
    }
}


public class Application {
    public static void main(String[] args) {
        Calculator calculator = new Calculator(2);
        calculator.calculateNumbers();
    }
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexey Cheremisin, 2021-10-20
@Amir1807

Study the builder template - https://habr.com/en/post/244521/
Study the chain of responsibility and command template https://habr.com/en/post/538954/
Put it all together and get a calculator.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question