Below is a java class called OuterClass that has a private inner class called In
ID: 3771981 • Letter: B
Question
Below is a java class called OuterClass that has a private inner class called InnerClass. Both the inner and outer classes have a method with same name called “Arithmetic” but with different definitions. The inner class Arithmetic can perform addition or subtraction of two integer numbers, while the Arithmetic in the outer class can perform multiplication of two integer numbers. Complete the method defintion of ArithmeticTutor in the OuterClass according to the comments (inside /* */) provided inside the method. import java.util.Scanner; public class OuterClass { public int Arithmetic(int x, int y, char operator) { int res =0; switch(operator) { case '*': res= (x*y);break; //multiply x and y } return res; } public void ArithmeticTutor() { int x, y; //asks the user to enter 2 numbers System.out.println("Enter two numbers"); Scanner s = new Scanner(System.in); x = s.nextInt(); y = s.nextInt(); //asks the user to choose an operation System.out.println("What operation you wish to perform"); System.out.println("Enter + for addition Enter - for subtraction Enter * for multiplication"); char op = s.next().charAt(0); //asks the user to guess the actual result System.out.println("Guess the result of " + x + " " + op + " " + y + " ?"); double guess = s.nextInt(); /*Here, perform the actual arithmetic operation of user’s choice between x and y. You can only use either the "Arithmetic method" from the inner or outer class to do so Then, you need to check if user's guess is equal to the actual result If the user's guess is correct, display that the guess was correct otherwise display that the guess was incorrect along with the correct answer. */ } //inner class private class InnerClass { public int Arithmetic(int x, int y, char operator) { int res =0; switch(operator) { case '+': res= (x+y);break; //add x and y case '-': res= (x-y);break; //subtract x and y } return res; } } public static void main(String[] args) { // TODO Auto-generated method stub OuterClass oc = new OuterClass(); oc.ArithmeticTutor(); } }
Explanation / Answer
import java.util.Scanner;
public class OuterClass {
public int Arithmetic(int x, int y, char operator) {
int res =0; switch(operator) { case '*': res= (x*y);
break;
//multiply x and y
} return res;
}
public void ArithmeticTutor() {
int x, y;
//asks the user to enter 2 numbers
System.out.println("Enter two numbers");
Scanner s = new Scanner(System.in);
x = s.nextInt();
y = s.nextInt();
//asks the user to choose an operation
System.out.println("What operation you wish to perform");
System.out.println("Enter + for addition Enter - for subtraction Enter * for multiplication");
char op = s.next().charAt(0);
//asks the user to guess the actual result
System.out.println("Guess the result of " + x + " " + op + " " + y + " ?");
double guess = s.nextInt();
/*Here, perform the actual arithmetic operation of user’s choice between x and y. You can only use either the "Arithmetic method" from the inner or outer class to do so Then, you need to check if user's guess is equal to the actual result If the user's guess is correct, display that the guess was correct otherwise display that the guess was incorrect along with the correct answer. */
} //inner class
private class InnerClass {
public int Arithmetic(int x, int y, char operator) {
int res =0; switch(operator) {
case '+': res= (x+y);break;
//add x and y
case '-':
res= (x-y);break;
//subtract x and y
} return res;
}
}
public static void main(String[] args) {
OuterClass oo=new OuterClass();
oo.Arithmetic(2, 3, '+');
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.