A program is required which will act as a calculator. The program should prompt
ID: 3704020 • Letter: A
Question
A program is required which will act as a calculator. The program should prompt the user to enter 2 integers and a code. The program should perform the following actions based on code entered.
- A code of 1 indicates that the two numbers should be added.
- A code of 2 indicates that the two numbers should be multiplied.
- A code of 3 indicates that the two numbers should be divided by the second number.
- A code of 4 indicates that the two numbers should be subtracted by the second number.
The program should then print both numbers, with an appropriate label indicating what is being printed. This process should be repeated until 2 zeros are entered. An appropriate message should then be printed to notify the user of the end of the program.
Sample Output
Please enter a number 3
Please enter another number 6
Please enter the code 1
The first number is 3 and second number s 6.
The sum of three numbers is 9
Please enter a number 4
Please enter another number 5
Please enter the code 2
The first number is 3 and second number is 6.
The sum of three numbers is 9
Please enter a number 0
Please enter another number 0
Good Bye !
Your program MUST use at least 2 value return functions (excluding the main functions) and must use a loop.
Explanation / Answer
CalcTester.java
import java.util.Scanner;
public class CalcTester {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("Please enter a number ");
int a = scan.nextInt();
System.out.println("Please enter another number ");
int b = scan.nextInt();
while(a!=0 && b!=0) {
System.out.println("Please enter the code ");
int code = scan.nextInt();
System.out.println("The first number is "+a+" and second number s "+b+".");
if(code==1) {
System.out.println("The sum of two numbers is "+getSum(a, b));
} else if(code == 2) {
System.out.println("The multiplecation of two numbers is "+getMultiply(a, b));
} else if(code == 3) {
System.out.println("The division of two numbers is "+getDivision(a, b));
} else {
System.out.println("The substraction of two numbers is "+getSubstraction(a, b));
}
System.out.println("Please enter a number ");
a = scan.nextInt();
System.out.println("Please enter another number ");
b = scan.nextInt();
}
System.out.println("Good Bye !");
}
public static int getSum(int a, int b) {
return a + b;
}
public static double getDivision(int a, int b) {
return a / (double)b;
}
public static int getMultiply(int a, int b) {
return a * b;
}
public static int getSubstraction(int a, int b) {
return a - b;
}
}
Output:
Please enter a number
3
Please enter another number
6
Please enter the code
1
The first number is 3 and second number s 6.
The sum of two numbers is 9
Please enter a number
4
Please enter another number
5
Please enter the code
2
The first number is 4 and second number s 5.
The multiplecation of two numbers is 20
Please enter a number
0
Please enter another number
0
Good Bye !
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.