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.
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...
Write a program in java processing. Write a program that does the following: · Assume the...
Write a program in java processing. Write a program that does the following: · Assume the canvas size of 500X500. · The program asks the user to enter a 3 digit number. · The program then checks the value of the first and last digit of the number. · If the first and last digits are even, it makes the background green and displays the three digit number at the mouse pointer. · If the two digits are odd, it...
How to write IO program in java?
How to write IO program in java?
in Java, write a program for methods (Secant, False-Position and Modified Secant) for locating roots. Make...
in Java, write a program for methods (Secant, False-Position and Modified Secant) for locating roots. Make sure that you have clever checks in your program to be warned and stop if you have a divergent solution or stop if the solution is very slowly convergent after a maximum number of iterations. (a) f(x) = 2x3 – 11.7x2 + 17.7x – 5 This function has 3 +ve roots, all of which lie between 0 and 4. Find the roots. Implement the...
Write a Java Program using the concept of objects and classes. Make sure you have two...
Write a Java Program using the concept of objects and classes. Make sure you have two files Employee.java and EmployeeRunner.java. Use the template below for ease. (Please provide detailed explanation) Class Employee Class Variables: Name Work hour per week Hourly payment Class Methods: - Get/Sets - generateAnnualSalary(int:Duration of employment)               annual salary = Hourly payment * Work hours per week * 50 If the employee is working for more than 5 years, 10% added bonus             -void displayAnnualSalary ( )...
Matrix Calculator Goals Write, compile and test a matrix calculator program Review the concept of pointers,...
Matrix Calculator Goals Write, compile and test a matrix calculator program Review the concept of pointers, and multi-dimensional dynamic arrays Create a simple makefile Write a robust input validation. The purpose of this lab is to get familiar with the software environment we will be using this term, and review some C++ concepts to get prepared for future projects and labs. This lab might be very time-consuming since there are a lot of concepts you need to pick up. For...
write a Java program Write a program to assign a letter grade according to the following...
write a Java program Write a program to assign a letter grade according to the following scheme: A: total score >= 90 B: 80 <= total score < 90 C: 70 <= total score < 80 D: 60 <= total score < 70 F: total score < 60 Output - the student's total score (float, 2 decimals) and letter grade Testing - test your program with the following input data: test1 = 95; test2 = 80; final = 90; assignments...
Write the program in java Write a program that does basic encrypting of text. You will...
Write the program in java Write a program that does basic encrypting of text. You will ask the user the filename of a text file which contains a few sentences of text. You will read this text file one character at a time, and change each letter to another one and write out to an output file. The conversion should be done a -> b, b->c , … z->a, A->B, B->C, …. Z->A. This means just shift each letter by...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT