Make a Calculator

remnikalija

Well-known member
Member
Joined
11 yrs. 6 mth. 29 days
Messages
485
Reaction score
0
Wallet
0$
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;
}
}
}
 
Paid adv. expire in 2 months
CLICK to buy Advertisement !
westernunion carding Verified & Trusted WesternUnion | MoneyGram | Bank - Transferring [299$ BTC for 2000$ WU]
electronics carding Verified & Trusted Electronics Carding, Carding iPhone, Samsung Carding, MacBook Carding, Laptops Carding

Tornado

[ Final Boss ]
Staff member
Escrow
Moderator
Administrator
V.I.P
Joined
12 yrs. 10 mth. 17 days
Messages
8,339
Reaction score
27,857
Wallet
8,991$
Please, Log in or Register to view quote content!
Hello! I see that you are trying to create a calculator in Java. Here are some suggestions to improve your code:

1. In your constructor, you are creating new Scanner objects for the operators and operands. Instead, you can pass them as arguments when creating a new Calculator object. For example:

```
public Calculator(Scanner operators, Scanner operands) {
this.operators = operators;
this.x = operands.nextInt();
this.y = operands.nextInt();
}
```

2. Your main method should be static. Also, you should create the Scanner objects for operators and operands in the main method and pass them to the Calculator constructor.

```
public static void main (String[] args) {
Scanner scanner = new Scanner(System.in);
Scanner operands = new Scanner(System.in);
System.out.println("What operation? ('+', '-', '*', '/')");
char operator = scanner.nextLine().charAt(0);
System.out.println("Insert 2 numbers to be operated on");
System.out.println("operand 1: ");
int x = operands.nextInt();
System.out.println("operand 2: ");
int y = operands.nextInt();
Calculator calculator = new Calculator(scanner, operands);
switch(operator) {
case('+'):
calculator.addition(x, y);
break;
case('-'):
calculator.subtraction(x, y);
break;
case('*'):
calculator.multiplication(x, y);
break;
case('/'):
calculator.division(x, y);
break;
}
System.out.println(calculator.solution);
}
```

3. In your switch statement, you are not passing the operands to the methods correctly. Instead, you should pass the values of x and y that you already stored in the Calculator instance variables. Also, you should store the result of the operation in the solution variable. For example:

```
public int addition(int x, int y) {
solution = x + y;
return solution;
}
```

4. Your operators variable should be a char, not a Scanner object. Change it to:

```
private char operators;
```

I hope these suggestions help you improve your calculator program!
 
Top Bottom