Simple Java Calculator

Firstly, this is not a matter of homework. I practice my knowledge of java. I decided that a good way to do this is to write a simple program without any help. Unfortunately, my compiler tells me about errors, I don’t know how to fix it. Without changing a lot of logic and code, can someone point out where some of my errors are? Thanks

import java.lang.*; import java.util.*; public class Calculator { private int solution; private int x; private int y; private char operators; public Calculator() { solution = 0; Scanner operators = new Scanner(System.in); Scanner operands = new Scanner(System.in); } public int addition(int x, int y) { return x + y; } public int subtraction(int x, int y) { return x - y; } public int multiplication(int x, int y) { return x * y; } public int division(int x, int y) { solution = x / y; return solution; } public void main (String[] args) { System.out.println("What operation? ('+', '-', '*', '/')"); System.out.println("Insert 2 numbers to be subtracted"); System.out.println("operand 1: "); x = operands; System.out.println("operand 2: "); y = operands.next(); switch(operators) { case('+'): addition(operands); operands.next(); break; case('-'): subtraction(operands); operands.next(); break; case('*'): multiplication(operands); operands.next(); break; case('/'): division(operands); operands.next(); break; } } } 
+1
source share
10 answers
 package org.com; import java.lang.*; import java.util.*; public class Calculator { private int solution; private static int x; private static int y; private char operators; public Calculator() { solution = 0; Scanner operators = new Scanner(System.in); Scanner operands = new Scanner(System.in); } public int addition(int x, int y) { return x + y; } public int subtraction(int x, int y) { return x - y; } public int multiplication(int x, int y) { return x * y; } public int division(int x, int y) { solution = x / y; return solution; } public void calc(int ops){ x = 4; System.out.println("operand 2: "); y = 5; switch(ops) { case(1): System.out.println(addition(x, y)); // operands.next(); break; case(2): System.out.println(subtraction(x, y)); // operands.next(); break; case(3): System.out.println(multiplication(x, y)); // operands.next(); break; case(4): System.out.println(division(x, y)); // operands.next(); break; } } public static void main (String[] args) { System.out.println("What operation? ('+', '-', '*', '/')"); System.out.println(" Enter 1 for Addition"); System.out.println(" Enter 2 for Subtraction"); System.out.println(" Enter 3 for Multiplication"); System.out.println(" Enter 4 for Division"); Calculator calc = new Calculator(); calc.calc(1); } } 

It will work

+3
source

operands and operators not available for the main one. You declare local variables in the constructor, so when you exit ctor, they are entitled to the GC and leave.

You have compilation errors - 10 of them.

+4
source

Another problem is that the line

 y = operands.next(); 

tries to put the String returned from Scanner.next() into a variable y that is declared as an int type.

The Scanner.nextInt() method can be used to try to return an int .

+2
source
 package com.abc; import java.util.Scanner; public class Calculator { private static final String pos = "+"; private static final String neg = "-"; private static final String mult = "*"; private static final String div = "/"; private enum operation { pos, neg, mult, div }; private int solution; private int x; public int getX() { return x; } public void setX(int x) { this.x = x; } public int getY() { return y; } public void setY(int y) { this.y = y; } private int y; static Scanner operators; public Calculator() { solution = 0; operators = new Scanner(System.in); } public int addition(int x, int y) { return x + y; } public int subtraction(int x, int y) { return x - y; } public int multiplication(int x, int y) { return x * y; } public int division(int x, int y) { solution = x / y; return solution; } public static void main(String[] args) { Calculator calc = new Calculator(); System.out.println("Insert 2 numbers"); System.out.println("operand 1: "); calc.setX(Integer.parseInt(operators.next())); System.out.println("operand 2: "); calc.setY(Integer.parseInt(operators.next())); System.out.println("What operation? ('pos', 'neg', 'mult', 'div')"); operation ttt = operation.valueOf(operators.next()); int output = 0 ; switch(ttt){ case pos: output = calc.addition(calc.getX(), calc.getY()); break; case neg: output = calc.subtraction(calc.getX(), calc.getY()); break; case mult: output = calc.multiplication(calc.getX(), calc.getY()); break; case div: output = calc.division(calc.getX(), calc.getY()); break; } System.out.println("output ="+output); } } 
+2
source

In addition to the other answers, your main () method must be static in order to be the entry point to the program. In main (), you will need to create your own Calculator object and call methods to do this.

+1
source

This is all great, but what program do you use to write java? Perhaps you should consider using an IDE, such as Eclipse, as it can automatically detect errors, as well as add imports. (I'm not sure yours does this). He also informs you that the problem is with your program in English. Also, consider this class as an easier and less complicated way to make a calculator:

 public class Calculator { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.print("Enter an Operator: "); String in = sc.next(); char oper = in.charAt(0); System.out.print("Enter a number: "); in = sc.next(); double num1 = Double.parseDouble(in); System.out.print("Enter another number: "); in = sc.next(); double num2 = Double.parseDouble(in); if(oper == '+') { double result = num1 + num2; System.out.println(result); } else if(oper == '-') { double result = num1 - num2; System.out.println(result); } else if(oper == 'x') { double result = num1 * num2; System.out.println(result); } else if(oper == '/') { double result = num1 / num2; System.out.println(result); } else { double result = num1 % num2; System.out.println(result); } System.out.println("Hope this helped your mathmatical troubles!"); } 

}
And as usual, instead of doing:

 import java.util.*; 

better to do:

 import java.util.Scanner; 

This probably doesn't matter much here, but if you use a much larger program importing all java.util, it will slow down your program significantly.

Hope this helps!

+1
source

Your main method should be declared as follows:

 public static void main(String[] args) {..} 

In addition, it seems that you only supply one argument to all your arithmetic methods (addition, subtraction, etc.), although they require two.

 public int addition(int x, int y); 

It is impossible to call using addition(operands) , that is, only one argument, and the argument is of the wrong type (this method requires two int , you give it a Scanner ). The same goes for all of these methods. You need to extract int from Scanner . You can do this with Scanner.nextInt() .

0
source
 import java.lang.*; import java.util.*; public class Calculator { private int solution; private int x; private int y; private char operators; public Calculator() { solution = 0; Scanner operators = new Scanner(System.in); Scanner operands = new Scanner(System.in); } public int addition(int x, int y) { return x + y; } public int subtraction(int x, int y) { return x - y; } public int multiplication(int x, int y) { return x * y; } public int division(int x, int y) { solution = x / y; return solution; } public void main (String[] args) { System.out.println("What operation? ('+', '-', '*', '/')"); System.out.println("Insert 2 numbers to be subtracted"); System.out.println("operand 1: "); x = operands; System.out.println("operand 2: "); y = operands.next(); switch(operators) { case('+'): addition(operands); operands.next(); break; case('-'): subtraction(operands); operands.next(); break; case('*'): multiplication(operands); operands.next(); break; case('/'): division(operands); operands.next(); break; } } } 
0
source

You ask the user to enter integers, but as input you put the operands.next(); operator operands.next(); . Try to maintain compatibility with your variables and user input, so change it to operands.nextInt() .

-1
source

As a hint, it's usually not recommended to start throwing

  import java.util. *; 
into your program because it makes the program unnecessarily large and slow. All you need for this is
  import java.util.Scanner; 
If I am right, most, if not everything in java.lang has already been imported for you.
-2
source

Source: https://habr.com/ru/post/1235039/


All Articles