##### Question

In: Computer Science

# write a program to make scientific calculator in java

Problem Statement

Write a program that uses the java Math library and implement the functionality of a scientific calculator. Your program should have the following components:
1. A main menu listing all the functionality of the calculator.
2. Your program should use the switch structure to switch between various options of the calculator. Your Program should also have the provision to handle invalidoption selection by the user.
3. Your calculator SHOULD HAVE the following functionalities. You can add any other additional features. PLEASE MAKE SURE YOU USE APPROPRIATE DATA TYPES.
a. Basic Functionality:
ii. Subtract two numbers.
iii. Divide a number by the second number (Don’t forget to check for the divide by zero error).
iv. Multiply two numbers.
v. Remainder when a number is divided by the second.
b. Scientific Calculations:
i. Ceiling and Floor of a number.
ii. Sine, Cosine and Tangent of an angle given in radians.
iii. x2, x3 and xy (You can display exponents in your program as x^y).
iv. Square root of a non-negative number (add appropriate checks in your program).
v. Logarithm of x base 10, natural logarithm of x base e and exponential function ex
vi. Absolute value of a given number x.
c. Additional Features: a can be done by using simple arithmetic operators. b can be done using the Math library of java. You have to write code to add the followingfeatures to your calculator:
i. Calculate average of arbitrary positive numbers input by the user (Use a loop and an appropriate sentinel value to achieve this task).
ii. Determine if a number is even or odd.
iii. Determine if one number is a multiple of the other.
iv. Factorial of a non-negative integer.

## Solutions

##### Expert Solution

`System.out.println(``"MAIN MENU: "``);`
`System.out.println(``"1. Add two numbers"``);`
`System.out.println(``"2. Subtract two numbers"``);`
`System.out.println(``"3. Divide a number by the second number"``);`
`System.out.println(``"4. Multiply two numbers"``);`
`System.out.println(``"5. Remainder when a number is divided by the second"``);`
`System.out.println(``"6. Ceiling and Floor of a number"``);`
`System.out.println(``"7. Sine, Cosine and Tangent of an angle given in radians"``);`
`System.out.println(``"8. x^2, x^3 and x^y"``);`
`System.out.println(``"9. Square root of a non-negative number"``);`
`System.out.println(``"10. Logarithm of x base 10, natural logarithm of x base e and exponential function e^x"``);`
`System.out.println(``"11. Absolute value of a given number x"``);`
`System.out.println(``"12. Average of arbitrary positive numbers"``);`
`System.out.println(``"13. Determine if a number is even or odd"``);`
`System.out.println(``"14. Factorial of a non-negative integer"``);`
`System.out.println(``"15. Determine if one number is a multiple of the other"``);`
`System.out.println(``"16. Exit"``);`
`System.out.print(``"Enter your choice: "``);`
`int` `choice = in.nextInt();`

`switch``(choice)`
`{`
`case` `1``: add(in); ``break``;`
`case` `2``: subtract(in); ``break``;`
`case` `3``: divide(in); ``break``;`
`case` `4``: multiply(in); ``break``;`
`case` `5``: remainder(in); ``break``;`
`case` `6``: fc(in); ``break``;`
`case` `7``: sct(in); ``break``;`
`case` `8``: power(in); ``break``;`
`case` `9``: sqroot(in); ``break``;`
`case` `10``: log(in); ``break``;`
`case` `11``: absolute(in); ``break``;`
`case` `12``: average(in); ``break``;`
`case` `13``: modtwo(in); ``break``;`
`case` `14``: factorial(in); ``break``;`
`case` `15``: multiple(in); ``break``;`
`case` `16``: System.out.println(``"Exiting!! Bye"``);`
`return``;`
`default``: System.out.println(``"Invalid option. Exiting!!"``);`
`return``;`
`}`
`}`
`}`

`public` `static` `void` `add(Scanner in)`
`{`
`System.out.print(``"Enter the 1st number: "``);`
`double` `n1 = in.nextDouble();`
`System.out.print(``"Enter the 2nd number: "``);`
`double` `n2 = in.nextDouble();`
`System.out.println(``"Addition of the 2 numbers is: "` `+ (n1+n2));`
`}`

`public` `static` `void` `subtract(Scanner in)`
`{`
`System.out.print(``"Enter the 1st number: "``);`
`double` `n1 = in.nextDouble();`
`System.out.print(``"Enter the 2nd number: "``);`
`double` `n2 = in.nextDouble();`
`System.out.println(``"Difference of the 2 numbers is: "` `+ (n1-n2));`
`}`

`public` `static` `void` `divide(Scanner in)`
`{`
`System.out.print(``"Enter the 1st number: "``);`
`double` `n1 = in.nextDouble();`
`System.out.print(``"Enter the 2nd number: "``);`
`double` `n2 = in.nextDouble();`
`if``(n2 == ``0``){System.out.println(``"Division by 0 not possible"``); ``return``;}`
`System.out.println(``"Division of the 2 numbers is: "` `+ (n1/n2));`
`}`

`public` `static` `void` `multiply(Scanner in)`
`{`
`System.out.print(``"Enter the 1st number: "``);`
`double` `n1 = in.nextDouble();`
`System.out.print(``"Enter the 2nd number: "``);`
`double` `n2 = in.nextDouble();`
`System.out.println(``"Multiplication of the 2 numbers is: "` `+ (n1*n2));`
`}`

`public` `static` `void` `remainder(Scanner in)`
`{`
`System.out.print(``"Enter the 1st integer: "``);`
`int` `n1 = in.nextInt();`
`System.out.print(``"Enter the 2nd integer: "``);`
`int` `n2 = in.nextInt();`
`if``(n2 == ``0``){System.out.println(``"Division by 0 not possible"``); ``return``;}`
`System.out.println(``"Remainder of division is: "` `+ (n1%n2));`
`}`

`public` `static` `void` `fc(Scanner in)`
`{`
`System.out.print(``"Enter the number: "``);`
`double` `n1 = in.nextDouble();`
`System.out.println(``"Floor of the number is: "` `+ Math.floor(n1));`
`System.out.println(``"Ceiling of the number is: "` `+ Math.ceil(n1));`
`}`

`public` `static` `void` `sct(Scanner in)`
`{`
`System.out.print(``"Enter theangle (in radians): "``);`
`double` `n1 = in.nextDouble();`
`System.out.println(``"Sine of the angle is: "` `+ Math.sin(n1));`
`System.out.println(``"Cos of the angle is: "` `+ Math.cos(n1));`
`System.out.println(``"Tan of the angle is: "` `+ Math.tan(n1));`
`}`

`public` `static` `void` `power(Scanner in)`
`{`
`System.out.print(``"Enter the number x: "``);`
`double` `n1 = in.nextDouble();`
`System.out.print(``"Enter the number y: "``);`
`double` `n2 = in.nextDouble();`
`System.out.println(``"x^2 is: "` `+ Math.pow(n1,``2``));`
`System.out.println(``"x^3 is: "` `+ Math.pow(n1,``3``));`
`System.out.println(``"x^y is: "` `+ Math.pow(n1,n2));`
`}`

`public` `static` `void` `sqroot(Scanner in)`
`{`
`System.out.print(``"Enter the +ve number x: "``);`
`double` `n1 = in.nextDouble();`
`if``(n1 < ``0``) { System.out.println(``"Invalid Input!"``); ``return``; }`
`System.out.println(``"Square root of x is: "` `+ Math.sqrt(n1));`
`}`

`public` `static` `void` `log(Scanner in)`
`{`
`System.out.print(``"Enter the +ve number x: "``);`
`double` `n1 = in.nextDouble();`
`System.out.println(``"Log base e is: "` `+ Math.log(n1));`
`System.out.println(``"Log base 10 is: "` `+ Math.log(n1)/Math.log(``10``));`
`System.out.println(``"e^x is: "` `+ Math.exp(n1));`
`}`

`public` `static` `void` `absolute(Scanner in)`
`{`
`System.out.print(``"Enter the +ve number x: "``);`
`double` `n1 = in.nextDouble();`

`System.out.println(``"Absolute Value of x is: "` `+ Math.abs(n1));`
`}`

`public` `static` `void` `average(Scanner in)`
`{`
`System.out.print(``"Enter the +ve numbers, terminate by -1: "``);`
`double` `n=``0``, tot=``0``;`
`int` `count=``0``;`
`while``(``true``)`
`{`
`n = in.nextDouble();`
`if` `(n < ``0``) ``break``;`
`tot+=n;`
`count++;`
`}`

`System.out.println(``"Average of the numbers is: "` `+ tot/count);`
`}`

`public` `static` `void` `modtwo(Scanner in)`
`{`
`System.out.print(``"Enter the integer: "``);`
`int` `n1 = in.nextInt();`

`if``(n1%``2` `== ``1``) System.out.println(``"Number is odd"``);`
`else` `System.out.println(``"Number is even"``);`
`}`

`public` `static` `void` `factorial(Scanner in)`
`{`
`System.out.print(``"Enter the integer: "``);`
`int` `n1 = in.nextInt();`

`int` `prod=``1``;`
`for``(``int` `i=`