package anewpackage;
/* 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.*/
import java.io.File;
import java.io.IOException;
import java.util.Scanner;
public class ScientificCalculator {
public static void main(String[] args) {
// Creating a file for save all the inputs, calculations and results
File file = new File("CalculationsHistory.txt");
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
// For taking input from user we have Scanner object
Scanner sc = new Scanner(System.in);
double a,b,c;
int selection;
while(true) {
// print the list of operation to user
System.out.println("Make your selection: 1. Addition 2. Subtraction 3. Multiplication 4. Division 5. Power 6.Square Root"
+ " 7. Nth root 8. log 9. logn 10. d2b 11. b2d 12. SIP 13. AC (all clear)");
selection = sc.nextInt();
// Object of the class Calculation
Calculation cal = new Calculation();
// Condition for Addition
if(selection==1) {
System.out.println("Please enter any two digits: ");
a = sc.nextDouble();
b = sc.nextDouble();
// Addition method calling from calculation class
c = cal.add(a,b);
System.out.println("Your ans is: " +c);
break;
}
// Condition for Subtraction
else if(selection==2) {
System.out.println("Please enter any two digits: ");
a = sc.nextDouble();
b = sc.nextDouble();
// Subtraction method calling from calculation class
c = cal.sub(a,b);
System.out.println("Your ans is: " +c);
break;
}
// Condition for Multiplication
else if(selection==3) {
System.out.println("Please enter any two digits: ");
a = sc.nextDouble();
b = sc.nextDouble();
// Multiplication method calling from calculation class
c = cal.multiply(a,b);
System.out.println("Your ans is: " +c);
break;
}
// Condition for Division
else if(selection==4) {
System.out.println("Please enter any two digits: ");
a = sc.nextDouble();
b = sc.nextDouble();
// Division method calling from calculation class
c = cal.div(a,b);
System.out.println("Your ans is: " +c);
break;
}
// Condition for Power
else if(selection==5) {
System.out.println("Enter the number which you want to find power of the number: ");
a = sc.nextDouble();
System.out.println("Enter the power of the number: ");
b = sc.nextDouble();
System.out.println(Math.pow(a, b));
break;
}
// Condition for Square root
else if(selection==6) {
System.out.println("Enter the number which you want to find square root:");
a = sc.nextDouble();
System.out.println(Math.sqrt(a));
break;
}
// Condition for Nth root
else if(selection==7) {
System.out.println("Enter the number which you want to find root of the number: ");
a = sc.nextDouble();
System.out.println("Enter the root of the number: ");
b = sc.nextDouble();
b= 1/b;
System.out.println(Math.pow(a, b));
break;
}
// Condition for Logarithmic value
else if(selection==8) {
System.out.println("Enter the number which you want to find Logarithmic value: ");
a = sc.nextDouble();
System.out.println(Math.log10(a));
break;
}
// Condition for Natural log
else if(selection==9) {
System.out.println("Enter the number which you want to find natural log: ");
a = sc.nextDouble();
System.out.println(Math.log(a));
break;
}
// Condition for converting decimal number to binary number.
else if(selection==10) {
System.out.println("Enter the decimal number: ");
int d2b = sc.nextInt();
System.out.println("Binary equivalent of "+d2b+" is: ");
System.out.println(Integer.toBinaryString(d2b));
break;
}
// Condition for converting binary number to decimal number.
else if(selection==11) {
System.out.println("Enter the binary number: ");
String b2d = sc.next();
System.out.println("Decimal equivalent of "+b2d+" is: ");
System.out.println(Integer.parseInt(b2d,2));
break;
}
// Condition for SIP compound interest.
else if(selection==12) {
System.out.println("Enter principal amount: ");
double principal = sc.nextDouble();
System.out.println("Enter the Rate : ");
double rate = sc.nextDouble();
System.out.println("Enter the time : ");
double time = sc.nextDouble();
double amount = principal * Math.pow((1+rate/100), time);
System.out.println("the compound interest is: "+amount);
break;
}
else if(selection==13) {
break;
}
else {
System.out.println("Please enter the correct selection: ");
continue;
}
}
System.out.println("Do you want to find trigonometric function? Y/N");
String trigonometricFunction = sc.nextLine();
sc.nextLine();
if(trigonometricFunction.equals("Y"))
sc.nextLine();
System.out.println("Radian or Degree mode?: ");
String radianOrDegree = sc.nextLine();
if(radianOrDegree.equalsIgnoreCase("degree")) {
System.out.println("Which operation ? (sin, cos, tan, sin-1, cos-1, tan-1): ");
String operation = sc.nextLine();
switch (operation) {
case "sin" :
System.out.println("What number do you want in the sin: ");
System.out.println(Math.sin(Math.toRadians(sc.nextInt())));
break;
case "cos" :
System.out.println("What number do you want in the cos: ");
System.out.println(Math.cos(Math.toRadians(sc.nextInt())));
break;
case "tan" :
System.out.println("What number do you want in the tan: ");
System.out.println(Math.tan(Math.toRadians(sc.nextInt())));
break;
case "sin-1" :
System.out.println("What number do you want in the sin-1: ");
System.out.println(Math.toDegrees(Math.asin(sc.nextDouble())));
break;
case "cos-1" :
System.out.println("What number do you want in the cos-1: ");
System.out.println(Math.toDegrees(Math.acos(sc.nextDouble())));
break;
case "tan-1" :
System.out.println("What number do you want in the tan-1: ");
System.out.println(Math.toDegrees(Math.atan(sc.nextDouble())));
break;
default :
break;
}
}
else {
System.out.println("Which operation ? (sin, cos, tan, sin-1, cos-1, tan-1): ");
String operation = sc.nextLine();
switch (operation) {
case "sin" :
System.out.println("What number do you want in the sin: ");
System.out.println(Math.sin(sc.nextInt()));
break;
case "cos" :
System.out.println("What number do you want in the cos: ");
System.out.println(Math.cos(sc.nextInt()));
break;
case "tan" :
System.out.println("What number do you want in the tan: ");
System.out.println(Math.tan(sc.nextInt()));
break;
case "sin-1" :
System.out.println("What number do you want in the sin-1: ");
System.out.println(Math.asin(sc.nextDouble()));
break;
case "cos-1" :
System.out.println("What number do you want in the cos-1: ");
System.out.println(Math.acos(sc.nextDouble()));
break;
case "tan-1" :
System.out.println("What number do you want in the tan-1: ");
System.out.println(Math.atan(sc.nextDouble()));
break;
default :
break;
}
}
}
}
package anewpackage;
public class Calculation {
public double add (double x, double y) {
return x+y;
}
public double sub (double x, double y) {
return x-y;
}
public double multiply (double x, double y) {
return x*y;
}
public double div (double x, double y) {
return x/y;
}
}