Computer scienceProgramming languagesJavaCode organizationObject-oriented programmingClasses and objectsObjects

Convert text input

Using logic to add two integers

Report a typo

The method checkOperator receives three string values — operator, i, j — from the main method. Complete the checkOperator method so that the two integer values are added. The result of the operation is returned to the main method. The operator argument should determine whether to add or subtract.

Sample Input 1:

true
5
2

Sample Output 1:

7
Write a program in Java 17
import java.util.Scanner;

class IfTrueAdd {

public static int checkOperator(String b, String si, String sj) {
// Write your code here
// ...

// Do not change this code
if (operator) {
return i + j;
} else {
return i - j;
}
}

public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);

String operator = scanner.next();
String i = scanner.next();
String j = scanner.next();
System.out.printf("%d", checkOperator(operator, i, j));
}
}
___

Create a free account to access the full topic