Tell me where is the error? Cannot handle division by zero.

public class Calculator_OOP { public static void main(String[] args) { CalculatorLogic calclogic = new CalculatorLogic(); Calculator calc = new Calculator(calclogic); calc.exec(); } } interface Operation { double exec(double first_value, double second_value); } class Addition implements Operation { public double exec(double first_value, double second_value) { return (first_value + second_value); } } class Subtraction implements Operation { public double exec(double first_value, double second_value) { return (first_value - second_value); } } class Multiply implements Operation { public double exec(double first_value, double second_value) { return (first_value * second_value); } } class Division implements Operation { public double exec(double first_value, double second_value) { if (second_value==0) { throw new DivisionByZero(); } return (first_value / second_value); } } interface Operations { Operation getOper(char op); } class CalculatorLogic implements Operations { char resOperation; public Operation operation; public Operation getOper(char op) { this.resOperation = op; switch (resOperation) { case '+': { operation = new Addition(); break; } case '-': { operation = new Subtraction(); break; } case '*': { operation = new Multiply(); break; } case '/': { operation = new Division(); break; } default: } return operation; } } class Calculator { CalculatorLogic resultOperation; public Calculator(CalculatorLogic resultOperation) { this.resultOperation = resultOperation; } public void exec() { Scanner scanner = new Scanner(System.in); try { System.out.println("Введите первое число и нажмите Enter"); double num1 = scanner.nextDouble(); System.out.println("Выберите операцию: +, -, *, / и нажмите Enter"); char operation = scanner.next().trim().charAt(0); System.out.println("Введите второе число и нажмите Enter"); double num2 = scanner.nextDouble(); Operation op = resultOperation.getOper(operation); if (op != null) System.out.println("Ответ: " + op.exec(num1, num2)); else System.out.println("Error: Не верная операция!"); } catch (Exception e) { System.out.println("Ошибка: Не верное значение!"); } } public class DivisionByZero extends Exception {} 
  • Give a trace of the error that you have, as well as input data. Judging by your code, you generate an exception, when the divisor = 0 and this is correct, so what exactly needs to be done? - Ladence
  • When dividing by zero, I want to display the message "It is impossible to divide by zero" Error: (51, 16) java: cannot find symbol symbol: class DivisionByZero location: class Project_Calculator_OOP.Division - Alex Lip
  • Where is your DivisionByZero class code? - Ladence

1 answer 1

Try this. The problem is that you didn’t handle the generated DivisionByZero exception in your old code (by the way, it’s better to call the DivisionByZeroException class of exceptions so as not to deviate from the named conventions). And what you wrote in the comment means that the compiler does not see this DivisionByZero class, where is its code? You can not write your class, and use ArithmeticException, then

  class Division implements Operation { public double exec(double first_value, double second_value) { if (second_value==0) { throw new ArithmeticException("На ноль делить нельзя"); } return (first_value / second_value); } } try { System.out.println("Введите первое число и нажмите Enter"); double num1 = scanner.nextDouble(); System.out.println("Выберите операцию: +, -, *, / и нажмите Enter"); char operation = scanner.next().trim().charAt(0); System.out.println("Введите второе число и нажмите Enter"); double num2 = scanner.nextDouble(); Operation op = resultOperation.getOper(operation); try { if (op != null) System.out.println("Ответ: " + op.exec(num1, num2)); else System.out.println("Error: Не верная операция!"); } catch (ArithmeticException ex) { ex.printStackTrace(); } } catch (Exception e) { System.out.println("Ошибка: Не верное значение!"); } 
  • That is, if it produces such an error, is it normal? java.lang.ArithmeticException: It is impossible to divide by zero at Project_Calculator_OOP.Division.exec (Calculator_OOP.java:51) at Project_Calculator_OOP.Calculator.exec (Calculator_OOP.java:118) at Project_Calculator_OOP.Calculator_OOP.Calculator_OOP.Calculator.exec at Project_Calculator_OOP.Calculator_OOP.Calculator.exec (Calculator_OOP.java:118) Alex Lip
  • @AlexLip yes, of course, if you are intimidated by the red color of the exception logger, then you can ex.printStackTrace () replace with System.out.println (ex.getMessage ()) and then it will simply display "Do not divide by zero." - Ladence
  • Thank! Yes, I figured it out already) - Alex Lip