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:
i. Add two numbers.
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 < 0break;
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=2; i<=n1; i++) prod*=i;
 
System.out.println("Factorial is: " + prod);
 
}
 
public static void multiple(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;}
 
if(n1%n2 == 0 || n2%n1 == 0) System.out.println("Yes. One number is a multiple of other");
else System.out.println("No. One number is not a multiple of other");
 
}
 
}



Related Solutions

Time Calculator – Intro To Programming - JAVA Write a program that asks the user to...
Time Calculator – Intro To Programming - JAVA Write a program that asks the user to enter a number of seconds. • There are 60 seconds in a minute. If the number of seconds entered by the user is greater than or equal to 60, the program should display the number of minutes in that many seconds. • There are 3,600 seconds in an hour. If the number of seconds entered by the user is greater than or equal to...
create scientific calculator using java language with OOP rule and interfaces.
create scientific calculator using java language with OOP rule and interfaces.
Integer value calculator: Write a program using C++ that acts as a calculator. The program should...
Integer value calculator: Write a program using C++ that acts as a calculator. The program should ask the user for two numbers and an operator. Then the program must print the result using the two input values and the operator. Prompts and input requirements. Ask the user for two integer values and a value that indicates the operation. Likely you will want to choose the normal operators: + - * / %. Output Requirements: Make your output easy to understand,...
I need to make this into a Java Code: Write a program that prompts the user...
I need to make this into a Java Code: Write a program that prompts the user for a double value representing a radius. You will use the radius to calculate: circleCircumference = 2πr circleArea = πr2 sphereArea = 4πr2 sphereVolume = 43πr3 You must use π as defined in the java.lang.Math class. Your prompt to the user to enter the number of days must be: Enter radius: Your output must be of the format: Circle Circumference = circleCircumference Circle Area...
TO BE DONE IN JAvA Complete the calculator program so that it prompts the user to...
TO BE DONE IN JAvA Complete the calculator program so that it prompts the user to enter two integers and an operation and then outputs the answer. Prompt the user three times, don't expect all the inputs on one line. Possible operations are + - * / No other words or symbols should be output, just the answer. Requested files import java.util.Scanner; /** * This is a simple calculator Java program. * It can be used to calculate some simple...
Program Specifications The built-in Java Math methods make some calculations much easier. Write a program called...
Program Specifications The built-in Java Math methods make some calculations much easier. Write a program called "DoTheMath" that accepts as input three floating-point numbers x, y, and z (define them as double) and outputs several calculations: x to the power of y x to the power of (y to the power of z) The absolute value of x The square root of (x*y to the power of z). Sample Run: Enter the values for x, y, z: -3.7 -3 5...
Write a java program: Write a program that creates a text file. Write to the file...
Write a java program: Write a program that creates a text file. Write to the file three lines each line having a person's name. In the same program Append to the file one line of  'Kean University'.  In the same program then Read the file and print the four lines without lines between.
Write program#2 upload .java file. 2A) Write a java program that uses the Random class to...
Write program#2 upload .java file. 2A) Write a java program that uses the Random class to generate a number in the range 21 to 64.  Print the generated number. 2B) Using the Random class and nextInt(6), rolls two die generating two random numbers each in the range 1 through 6. Total by adding the two values together. Print the value of each die, and the total value. 2C) Using the Math class and sqrt(num), calculate the square root of integer twenty-two...
Write program#2 upload .java file. 2A) Write a java program that uses the Random class to...
Write program#2 upload .java file. 2A) Write a java program that uses the Random class to generate a number in the range 21 to 64.  Print the generated number. 2B) Using the Random class and nextInt(6), rolls two die generating two random numbers each in the range 1 through 6. Total by adding the two values together. Print the value of each die, and the total value. 2C) Using the Math class and sqrt(num), calculate the square root of integer twenty-two...
Write program#1 upload .java file. #1 Write a java program that prompts the user for input...
Write program#1 upload .java file. #1 Write a java program that prompts the user for input using the Scanner class. First to enter their first name. Then prompts the user to enter their last name. Then prompts the user to enter their city. Then prompts the user to enter their state. Then prompts the user to enter their zip code. Concatenate first name and last name into full_name. Using String Class is optional. Use the String Class to replace zip...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT