Challenge

Project

Create a Scientific Calculator with the following operations-

  • "+"-plus or addition
  • "-" -minus or subtraction
  • "*" -multiply by
  • "/" -divided by
  • "^"-raised to the power of- x raised to the y
  • "Sqrt"- square root
  • "Nrt"- nth root
  • "log"- Logarithmic value
  • "logn"- Natural log
  • "sin"- Sine
  • "isin" -Inverse sine
  • "cos"- Cosine
  • "icos"- Inverse cosine
  • "tan"- Tangent
  • "itan"- Inverse tan
  • "stor" -place a number in memory for later use
  • "recall"-recover the number from memory for immediate use
  • "d2b" -Decimal to Binary number
  • "b2d" -Binary to Decimal number
  • "sip"- Calculations of monthly, quarterly and yearly SIP calculations for compounding investments.

It should run on an infinite loop and take inputs from the user for any operation they select based on the operation number on the list displayed to them. Save all the inputs, calculations and results in a file named “CalculationsHistory.txt”. Your code should follow all principles of Object-Oriented Programming. Create a report on how your software uses all the OOPs principles with examples from your code.

Answer= 


import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;
import java.util.Stack;

public class Calculator{
private static int choice;
private static Object opeartor;

public static void main(String[] args) throws IOException {

System.out.println("Which operation you want to perform in calculator ? 1.Square 2.SquareRoot 3.Log and Natural log 4. SIP 5.Trignometric and inverse Trignometric ratios 6.Decimal to Binary 7.Storing numbers 8.Arithmetic Operations 9.OFF " );
Scanner sc3=new Scanner(System.in);
int choice =sc3.nextInt();
String typeofoperation;
if(choice==1) {
typeofoperation="square";

Double num ;
Scanner sc= new Scanner(System.in);
System.out.println("Enter a number:");
num=sc.nextDouble();
Double square= num*num;
System.out.println("Square of " + num +" is " + square);
}
//Now getting square root of number

else if(choice==2) {
Double num1;
Scanner sc1 = new Scanner(System.in);
System.out.println("Enter a number for which you want to find root of it ?");
num1=sc1.nextDouble();
double squareroot =Math.pow(num1, 0.5);
System.out.println("The square root of a given number "+ num1 + " =" + squareroot);
}
//Now getting value for log and natural log
else if(choice==3) {
Scanner sc2=new Scanner(System.in);
System.out.println("enter a number");
double n=sc2.nextDouble();
System.out.println("Common logarithm value of n is "+(Math.log10(n)));
System.out.println("Natural logarithm of" + n + " = " + Math.log(n));
}
//Now calculating the sip value
else if(choice==4) {
Scanner sc5=new Scanner(System.in);
System.out.println("Enter present value:");
double p= sc5.nextInt();
System.out.println("Enter the interest rate:");
double r=sc5.nextInt();
System.out.println("Enter the time period in years:");
double y= sc5.nextInt();
double f=p*Math.pow((1+r/100), y);
System.out.println("Value is :"+f);
}

else if(choice==5) {
//Now getting the value of Trignometric ratios and inverse trignometric ratios

// Create a Scanner object which will read
// values from the console which user enters
Scanner scanner = new Scanner(System.in);

// Getting input from user from the console
System.out.println("Enter value of angle in degrees ");
System.out.println("For getting inverse trignometric ratios put the value of angle in radian like =0.40 or other value after decimal point");

// Calling nextDouble method of scanner for
// taking a double value from user and storing
// it in degrees variable
double degrees = scanner.nextDouble();

System.out.println("Lets calculate the sine, cosine and tan of angle ...");
// In order to calculate sine , cosine and tan of angle we
// use the Math class three static methods by name as :
// 1. Math.sin(a) -- Sine of a
// 2. Math.cos(a) -- Cosine of a
// 3. Math.tan(a) -- Tangent of a

double sineOfAngle = Math.sin(degrees);
double cosOfAngle = Math.cos(degrees);
double tanOfAngle = Math.tan(degrees);

System.out.println();
System.out.println("The Sine of " + degrees + " degrees is : "
+ sineOfAngle);
System.out.println("The Cosine of " + degrees + " degrees is : "
+ cosOfAngle);
System.out.println("The Tangent of " + degrees + " degrees is : "
+ tanOfAngle);

System.out.println();
System.out.println("Lets calculate the sec, cosec and cot of angle ...");
// In order to calculate sec, cosec and cot of angle we
// just inverse the value of sin , cos and tan calculated above :
// 4. Sec of a -- 1 / Sine of a
// 5. Cosec of a -- 1/ Cosine of a
// 6. Cot of a -- 1 / Tangent of a

double secOfAngle = 1 / Math.sin(degrees);
double cosecOfAngle = 1 / Math.cos(degrees);
double cotOfAngle = 1 / Math.tan(degrees);


//Enter the value in radian for inverse trignometric ratios
double asineOfAngle = Math.asin(degrees);
System.out.println("The value of inverse trignometric are:");
System.out.println("The Sine of " + degrees + " degrees is : "
+ asineOfAngle);

double acosOfAngle = Math.acos(degrees);
System.out.println("The Cosine of " + degrees + " degrees is : "
+ acosOfAngle);

double atanOfAngle = Math.atan(degrees);
System.out.println("The Tangent of " + degrees + " degrees is : "
+ atanOfAngle);


double asecOfAngle = 1 / Math.asin(degrees);
double acosecOfAngle = 1 / Math.acos(degrees);
double acotOfAngle = 1 / Math.atan(degrees);


System.out.println("\nThe Sec of " + degrees + " degrees is : "
+ asecOfAngle);
System.out.println("The Cosec of " + degrees + " degrees is : "
+ acosecOfAngle);
System.out.println("The Cotangent of " + degrees + " degrees is : "
+ acotOfAngle);
}

else if(choice==6) {
//Now convert decimal to binary
Scanner sc6= new Scanner(System.in);
//Creating stack object
Stackstack= new Stack();
//user input
System.out.println("Enter decimal number:");
int num=sc6.nextInt();

while(num!=0)
{
int d =num % 2;
stack.push(d);
num /= 2;
}
System.out.println("\nBinary representation is :");
while(!(stack.isEmpty()))
{
System.out.println(stack.pop());
}
System.out.println();

}


else if(choice==7) {
//storing a number in array for later use
Scanner sc7=new Scanner(System.in);
int arr[]=new int[10];
System.out.println("Enter 10 numbers");
for(int i=0;i arr[i]=sc7.nextInt();
}
for(int i=0;i if(arr[i]<0)
System.out.println(arr[i]+",");
}
for(int i=0 ;i if(arr[i]>=0)
System.out.println(arr[i]+",");
}
}
// Now performing arithmetic operations
else if(choice==8) {
char operator;
double num1,num2;
Scanner input=new Scanner (System.in);

System.out.println("Enter the Operator (+ , - , * , /)");
operator= input.next().charAt(0);

System.out.println("Enter the two numbers one by one");
num1=input.nextDouble();
num2=input.nextDouble();

input.close();

switch(operator) {
case '+':
System.out.printf(" %.2f + %.2f = %.2f ", num1,num2,(num1+num2));
break;

case '-':
System.out.printf(" %.2f - %.2f = %.2f ", num1,num2,(num1-num2));
break;

case '*':
System.out.printf(" %.2f * %.2f = %.2f ", num1,num2,(num1*num2));
break;

case '/':
if( num2 !=0)
System.out.printf(" %.2f / %.2f = %.2f ", num1,num2,(num1/num2));
else
System.out.printf("Divide by zero situation");
break;

default:
System.out.printf(" %c is an invalid operator",opeartor);




}

}


}
}

  = In the above program we have created public class as calculator  which include main method which               include sysout method that  prints the message for user to choose which type of operation user want to         perform and we created a new scanner class for the user to input the choice of the operation user want         to  perform  

     1. If user choose 1 then user want to perform operation of square of given number operation in this we             have created created new scanner for getting the input of number from the user the double keyword             is used to represent the num we are multiplying the entered input of user by itself so we get the                   square of  the input number by user and the square of given number is printed

      2.  If user choose 2 then user want to perform operation of square root of given number we created an              scanner for getting input as number from the user and we are using num1=sc1.nextDouble(); which              scans the token of the input as a double the Math.pow (double num) method accepts one argument               of the double data type and returns a value which is square root of argument and thus we get the                 square root of number user inputs.

      3. If user choose 3 then user want to perform operation of Getting value of log and natural log we have              created scanner so the user can input the number for which user want the value of log and natural                log  we have used double n=sc2.nextDouble(); method which will throw inputMismatchException if                the next token cannot be translated into a valid double value now for getting log value we used                    Math.log10(n)  which   will find out the logarithimic of input number when the base is 10 this method          will return the base 10 logarithim of a double value and print the value of input number by user in                output and for Natural log we use Math.pow which is inbuilt method in java so this method takes the              same input which was use to find log value and returns the natural log value and print the natural log             value in  output

     4.If user choose 4 then user want to perform operation of SIP we allow the user to put the factors in                input that are  present value interest rate and time period  double f=p*Math.pow((1+r/100), y); here             the (1+i)*n  is the accumulation function which is inbuilt method which will update a particular                     LongAccumulator object with the given value input by the user also the that is the parameter method             will accept a single parameter  as user input x that is the value by which the current LongAccumulator           object will get updated and the method will able to return the updated value .

     5.If user choose 5 then user want to perform operation of Trignometric function and inverse trignometric          functions so we created new scanner to get the value in degree from user we use the double angle                formula and Math.sin(degrees) returns the trignometric sine of an angle between 0.0 and pi .if the                 argument is NaN or infinity then the result is NaN if the argument is zero then the result is also  zero            with the same sign  of argument so when we put an angle in degree we get the sine ratio of that value           with the use of Math.pow method which returns the value  For cos ratio we use Math.pow which is                  inbuilt method it will return the numeric value between -1 and 1 so when we take input from the user           te Math.cos calculates and returns the value of cos in output now for Tan we use Math.tan(degrees)              which will take the input and this method will retun the tangent value Now for getting value of sec                cosec cot we do the reciprocal of sin cos and tan that is 1 / Math.sin(degrees) same is followed for                other angles 

         Now for inverse of sin cos tan sec cosec cot we use Math.asin(degrees) which is predefined method in           java which will print the inverse value of trignometric ratios but we want to put the value of input in               radian if we put the input in degree so it will return and NaN value so when we put value in radian like          0.40   it will return the inverse value of this input       

    6.   If user choose 6 then user want to perform operation of converting  decimal number to                                binary number  we created new scanner for getting user input  also we created stack new            object  and also stack.push  so if the condition that num%2 became true then the push operation                  insert and element into the stack  and num/ =2; converts the decimal into binary form number so in              output we get binary number we also used stack.empty  method in while loop if which will get                      execute if the stack is empty but in our program stack is not empty so it does not get executed

       7.  If user choose 7 then user want to perform operation of Storing a  number in array for later use                    so we created an array  which contain 10 elements or 10 numbers which we will be taking from user              we created for loop  and ask te user to input the 10 numbers so in second for loop we provided a                  condition in which user can input any positive number and in third for loop we created a condition in              which the user can input any negative value so if the condition within the for loop satisfies with the                 user value so  the numbers get stored and printed 

      8.  If user choose 8 then user want to perform Arithmetic operation we provided the user  to choose the              type of operation  so we created scanner for choosing the operator and to take two numbers from                 user so we are using switch case for all operations so in 1st if user choose + operator and we and we             are using %2f syntax which will return our variable with 2 decimal places  so in sysout () method we              are adding two numbers from user input as %2f+%2f which will give us output by returning the                   sum of variable with 2 decimal places so when our operation is beeen performed we use break                     statement so that the calculator performs a single operation at one time                                                      Similaraly for - substraction method and multiplication *method the calculator will follow the same                 procedure only the type of operation is changed  the code for substraction and multiplication                      executes in same manner as for addition now for the division opertaion we are using if statement if                 the value of num2 is not equal to zero so the %2f+%2f =%2f will be true and will get execute but if             it is false then their will execute divide by zero situation  and also we input any other type of                         operation which is not included in the calculator so the calculator will show  invalid operator in ouput            due to default case and when one operation is done the program gets terminated

Your portfolio, next

Ship work like this
in your own course.

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.