Project
package main.java.io.mahesh;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.util.Scanner;
import main.java.io.mahesh.Commands.Command;
public class MainCalci {
private static boolean condition = true;
static Scanner sc = new Scanner(System.in);
public static void main(String[] args) {
Calculator calculator = new Calculator();
System.out.println("#CALCULATOR#");
while (condition) {
System.out.println("What you want to do!");
System.out.println(
"1.Addition \n2.Subtraction \n3.Division \n4.second number raised to First \n5.Square root \n6.(Second number)th root of first number \n7.Log \n8.Natural log \n9.Sine \n10.Sine inverse \n11.Cosine \n12.Cosine inverse \n13.Tangent \n14.Tangent inverse \n15.Decimal to Binary conversion \n16.Binary to decimal conversion \n17.Place a number in memory for later use \n18.Recall number from memory for immediate use \n19.SIP calculation \n20.Exite");
System.out.println("Enter your choice: ");
int choice = sc.nextInt();
if (choice == 1) {
var a = Command.ADDITION;
calculator.letsCalculate(a);
} else if (choice == 2) {
var a = Command.SUBTRACT;
calculator.letsCalculate(a);
} else if (choice == 3) {
var a = Command.DIVIDATION;
calculator.letsCalculate(a);
} else if (choice == 4) {
var a = Command.XRAISETOY;
calculator.letsCalculate(a);
} else if (choice == 5) {
var a = Command.SQUAREROOT;
calculator.letsCalculate(a);
} else if (choice == 6) {
var a = Command.NTHROOT;
calculator.letsCalculate(a);
} else if (choice == 7) {
var a = Command.LOG;
calculator.letsCalculate(a);
} else if (choice == 8) {
var a = Command.NATURALLOG;
calculator.letsCalculate(a);
} else if (choice == 9) {
var a = Command.SINE;
calculator.letsCalculate(a);
} else if (choice == 10) {
var a = Command.INVERSESINE;
calculator.letsCalculate(a);
} else if (choice == 11) {
var a = Command.COSINE;
calculator.letsCalculate(a);
} else if (choice == 12) {
var a = Command.INVERSECOSINE;
calculator.letsCalculate(a);
} else if (choice == 13) {
var a = Command.TANGENT;
calculator.letsCalculate(a);
} else if (choice == 14) {
var a = Command.INVERSETANGENT;
calculator.letsCalculate(a);
} else if (choice == 15) {
var a = Command.D2B;
calculator.letsCalculate(a);
} else if (choice == 16) {
var a = Command.B2D;
calculator.letsCalculate(a);
} else if (choice == 17) {
var a = Command.STOREMEMORY;
calculator.letsCalculate(a);
} else if (choice == 18) {
var a = Command.RECALLMEMORY;
calculator.letsCalculate(a);
} else if (choice == 19) {
var a = Command.SIPCALCULATION;
calculator.letsCalculate(a);
} else if (choice == 20) {
condition = false;
break;
} else {
System.out.println("Invalid choice,please try again!");
}
}
}
}
package main.java.io.mahesh;
import java.lang.Math;
import main.java.io.mahesh.Commands.Command;
public class Calculator {
public double addition(double value1, double value2) {
/**
* This method should take two values (double)
* and adds them together, returning the new value.
*/
return value1 + value2;
}
public double subtraction(double value1, double value2) {
/**
* This method should take two values (double)
* and subtracts the first from the second.
*/
return value1 - value2;
}
public double dividation(double value1, double value2) {
/**
* This method should take two values (double)
* and divides the first by the second.
*/
if(value2 == 0) {
displayError();
}
return value1 / value2;
}
private void displayError() {
System.out.println("can not divide by xero");
}
public double multiplication(double value1, double value2) {
/**
* This method should take two values (double)
* and multiply the first with the second.
*/
return value1 * value2;
}
public double square(double value1) {
/**
* This method should square a value (double) provided by the user.
*/
return value1 * value1;
}
public double squareRoot(double value1) {
/**
* This method should provide the square root of a value (double).
*/
return Math.sqrt(value1);
}
public double nthRoot(double value1, double value2) {
/**
* This method should evaluate (value2)th root of value1 (double)
*/
return Math.pow(value1, 1/value2);
}
public double xRaisedTOy(double value1, double value2) {
/**
* This method should evaluate value1 (double) to the power of value2 (double).
*/
return Math.pow(value1,value2);
}
public double sine(double value1) {
/**
* This method will evaluate to an answer that is equal to the ratio
* of the side opposite a given angle (in a right triangle) to the hypotenuse. */
return Math.sin(value1);
}
public double cosine(double value1) {
/**
* This method will evaluate to an answer that is equal to the ratio
* of the side adjacent to an acute angle (in a right-angled triangle) to the hypotenuse.
*/
return Math.cos(value1);
}
public double tangent(double value1) {
/**
* This method will evaluate a tangent line
*/
return Math.tan(value1);
}
public double inverseSine(double value1) {
/**
* This will provide the inverse of the sine of the value provided.
*/
return Math.asin(value1);
}
public double inverseCosine(double value1) {
/**
* This will provide the inverse of the cosine of the value provided.
*/
return Math.acos(value1);
}
public double inverseTangent(double value1) {
/**
* This will provide the inverse of the tangent of the value provided.
*/
return Math.atan(value1);
}
public double sipCalculation(double investment,double annualRate, double timePeriod) {
double monthlyRate = (annualRate/100)/12; //Rate of interest
double months =timePeriod; //Time period
double futureValue;
return futureValue = (investment * (Math.pow(1 + monthlyRate, months) - 1) * (1+monthlyRate) )/ monthlyRate; //Final Value
}
public double log(double value1) {
/**
* This will evaluate to the base 10 value of the number provided.
*/
return Math.log10(value1);
}
public double naturalLog(double value1) {
/**
* TThis will evaluate to the base e value of the number provided.
*/
return Math.log(value1);
}
// private void displayError() {
// /**
// * Checks if dividing by Zero
// */
// System.out.println("ERROR - You broke the universe");
// }
public void letsCalculate(Command commandname) {
Commands commands = new Commands();
commands.giveCommand(commandname);
commands.doTheThing();
}
}
package main.java.io.mahesh;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.util.Scanner;
public class User {
Scanner sc = new Scanner(System.in);
double memory = 0; // ....................This is the Memory that can be Set, Recalled, and Cleared.
private String text;
public double getMemory() {
try {
InputStreamReader isr = new InputStreamReader(System.in);
OutputStreamWriter osw = new OutputStreamWriter(System.out);
BufferedReader br = new BufferedReader(isr);
BufferedWriter bw = new BufferedWriter(osw);
bw.flush();
String line = br.readLine();
text = line;
FileWriter fw = new FileWriter("CalculationsHistory.txt");
PrintWriter pw = new PrintWriter(fw);
pw.println(text);
pw.close();
}
catch(IOException e1) {
System.out.println("Error during reading/writing");
}
double d =Double.parseDouble(text);
return d;
}
public void setMemory(double memory) {
this.memory = memory;
}
public void clearMemory() {
this.memory = 0;
}
// From here the inputs are taken from the user....
public double getInput() { // .................................this method gets first number from user.
System.out.println("Please,enter first number: ");
return sc.nextDouble();
}
public double getAnothrInput() { // .................................this method gets second number from user.
System.out.println("Please,enter second number: ");
return sc.nextDouble();
}
public int getDecimal() { // .................................this method gets decimal number from user.
System.out.println("Please,enter the decimal number: ");
return sc.nextInt();
}
public String getBinary() { // .................................this method gets binary number from user.
System.out.println("Please,enter the binary number: ");
return sc.nextLine();
}
public double getInvestedAmt() { // .................................this method gets monthly invested amount from user for SIP calculation.
System.out.println("Please,enter monthly investment amount: ");
return sc.nextDouble();
}
public double getAnnualRate() { // .................................this method gets annual rate of return from user for SIP calculation.
System.out.println("Please,enter Annual rate of return in %: ");
return sc.nextDouble();
}
public double getTimePeriod() { // .................................this method gets time period of SIP investment from user.
System.out.println("Please,enter time period of investment in months: ");
return sc.nextDouble();
}
public double getAngle() { // .................................this method takes angle from user and converts angle from degrees into radians.
System.out.println("Please,enter the angle in degrees: ");
double degrees = sc.nextDouble();
// convert degrees to radians
double radians = Math.toRadians(degrees);
return radians;
}
}