CS111_Homework - A Simple Calculator Program
CS111_Homework - A Simple Calculator Program
1
Q1. Write a Java program for a simple calculator of the operations: Addition, Subtraction,
Multiplication, Division, and Exponent values (i.e. power-of). The program must satisfy the
following requirements:
(1) Declare an integer variable for the operation choice (opChoice) and three double variables
for first and second numbers (num1, num2) and the result of operation (result);
(2) Ask the user to enter the operation (opChoice) he/she wants to perform: 1 for Addition, 2 for
Subtraction, 3 for Multiplication, 4 for Division, and 5 for Exponent value;
(3) Create a method for each operation: add(), subtract(), multiply(), divide(), power(); Each
method returns a double value and takes num1, num2 as arguments; and get the numbers for
num1 and num2 randomly (0 to 10 inclusive); In subtract() and divide() methods, swap num1
and num2 if num1 < num2; In the power() method, calculate num1 to the power of num2;
(4) Use a while block so that the user can see the results of multiple operations of Addition,
Subtraction, Multiplication, Division and/or Exponent. This loop keeps asking the user to
enter his option for the next operation, and only stops if opChoice is less than 1 or greater
than 5;
(5) Inside the while block, use a switch-case block to do the calculations calling the right
corresponding method, such that case 1 calls add(), case 2 calls subtract(), case 3 calls
multiply(), case 4 calls divide(), and case 5 calls power();
The program must show the inputs and outputs according to the given sample below:
2
The Program Source Code:
3
The Program Outputs Snapshot Copied from NetBeans: