Challenge

Project

// Calculator.java

package myCalculator;

import java.io.IOException;

import java.util.InputMismatchException;

import java.util.Scanner;

public class Calculator {

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

Scanner s = new Scanner(System.in);

while (true) {

System.out.println("which operation do you want to perform? input:");

System.out.println("1. for addition");

System.out.println("2. for Subtraction");

System.out.println("3. for multiplication");

System.out.println("4. for division");

System.out.println("5. for getting power");

System.out.println("6. for Square root");

System.out.println("7. for nth root");

System.out.println("8. for common logarithmic value");

System.out.println("9. for natural logarithmic value");

System.out.println("10. for sine vale");

System.out.println("11. for Inverse sine");

System.out.println("12. for cosine values");

System.out.println("13. for inverse Cosine");

System.out.println("14. for Tangent values");

System.out.println("15. for Inverse tangent");

System.out.println("16. for storing a number");

System.out.println("17. for recovering the stored value");

System.out.println("18. for converting a Decimal into binary number");

System.out.println("19. for converting Binary number into Decimal");

System.out.println("20. for Calculating SIP");

int input = s.nextInt(); // user choice

if (input == 1) { //according to user input

try {

Operation opt = new Operation(); //creating obj of Operation Class

opt.add(); //and calling that particular method

} catch (InputMismatchException e) { //surrounding it with try catch block for exception handling

System.out.println("Input type incorrect");

}

}

else if (input == 2) {

try {

Operation opt = new Operation();

opt.sub();

} catch (InputMismatchException e) {

System.out.println("Input type incorrect");

}

}

else if (input == 3) {

try {

Operation opt = new Operation();

opt.mul();

} catch (InputMismatchException e) {

System.out.println("Input type incorrect");

}

}else if (input == 4) {

try{

Operation opt = new Operation();

opt.div();

}

catch (InputMismatchException e){

System.out.println("cannot divide by zero");

}

}

else if (input == 5) {

try {

Operation opt = new Operation();

opt.power();

} catch (InputMismatchException e) {

System.out.println("input type is incorrect ");

}

}

else if (input == 6) {

try {

Operation opt = new Operation();

opt.square();

} catch (InputMismatchException e) {

System.out.println("Input type incorrect");

}

}

else if (input == 7) {

try {

Operation opt = new Operation();

opt.nRoot();

} catch (InputMismatchException e) {

System.out.println("Input type incorrect");

}

}

else if (input == 8) {

try {

Operation opt = new Operation();

opt.log();

} catch (InputMismatchException e) {

System.out.println("Input type incorrect");

}

}

else if (input == 9) {

try {

Operation opt = new Operation();

opt.nLog();

} catch (InputMismatchException e) {

System.out.println("Input type incorrect");

}

}

else if (input == 10) {

try {

Operation opt = new Operation();

opt.sin();

} catch (InputMismatchException e) {

System.out.println("Input type incorrect");

}

}

else if (input == 11) {

try {

Operation opt = new Operation();

opt.invSine();

} catch (InputMismatchException e) {

System.out.println("Input type incorrect");

}

}

else if (input == 12) {

try {

Operation opt = new Operation();

opt.Cosine();

} catch (InputMismatchException e) {

System.out.println("Input type incorrect");

}

}

else if (input == 13) {

try {

Operation opt = new Operation();

opt.invCos();

} catch (InputMismatchException e) {

System.out.println("Input type incorrect");

}

}

else if (input == 14) {

try {

Operation opt = new Operation();

opt.tan();

} catch (InputMismatchException e ){

System.out.println("Input type incorrect");

}

}

else if (input == 15) {

try {

Operation opt = new Operation();

opt.invTan();

} catch (InputMismatchException e) {

System.out.println("Input type incorrect");

}

}

else if (input == 16) {

try {

Operation opt = new Operation();

opt.store();

} catch (InputMismatchException e) {

System.out.println("Input type incorrect");

}

}

else if (input == 17) {

try {

Operation opt = new Operation();

opt.recall();

} catch (InputMismatchException e) {

System.out.println("Input type incorrect");

}

}

else if (input == 18) {

try {

Operation opt = new Operation();

opt.d2b();

} catch (InputMismatchException e) {

System.out.println("Input type incorrect");

}

}

else if (input == 19) {

try {

Operation opt = new Operation();

opt.b2d();

} catch (InputMismatchException e) {

System.out.println("Input type incorrect");

}

}

else if (input == 20) {

try {

Operation opt = new Operation();

opt.sip();

} catch (InputMismatchException e) {

System.out.println("Input type incorrect");

}

}else

System.out.println("invalid input! choose correct Option");

continue;

}

}

}

//Operation.java

package myCalculator;

import java.io.*;

import java.util.Scanner;

public class Operation {

Scanner s= new Scanner(System.in); //scanner for taking input from user

public void add() throws IOException { // method for addition , as we have created object of FileWriter class it should throw IOException

System.out.println("Enter the first value"); // asking for first value

double a= s.nextInt();

System.out.println("Enter the Second value"); // asking for second value

double b= s.nextInt();

double c=a+b; // adding those two values and assign to c

System.out.println(a+"+"+b+"="+ c); //printing answer

FileWriter f= new FileWriter("CalculationsHistory.txt", true); // object of FileWriter class for bufferWrite

BufferedWriter bw= new BufferedWriter(f); // object of bufferedWriter

bw.write(String.format("%f+%f= %f",a,b,c)); //storing the values in above mentioned txt file

bw.newLine(); // moving on next line

bw.close(); // closing bufferedWriter

}

public void sub() throws IOException { //method for subtraction

System.out.println("Enter the first value");

double a= s.nextInt();

System.out.println("Enter the Second value");

double b= s.nextInt();

double c=a-b;

System.out.println(a+"-"+b+"="+ c);

FileWriter f= new FileWriter("CalculationsHistory.txt", true);

BufferedWriter bw= new BufferedWriter(f);

bw.write(String.format("%f-%f= %f",a,b,c));

bw.newLine();

bw.close();

}

public void mul() throws IOException { //method for multiplication

System.out.println("Enter the first value");

double a= s.nextInt();

System.out.println("Enter the Second value");

double b= s.nextInt();

double c=a*b;

System.out.println(a+"X"+b+"="+ c);

FileWriter f= new FileWriter("CalculationsHistory.txt", true);

BufferedWriter bw= new BufferedWriter(f);

bw.write(String.format("%f*%f= %f",a,b,c));

bw.newLine();

bw.close();

}

public void div() throws IOException { //method for division

System.out.println("Enter the first value");

double a= s.nextInt();

System.out.println("Enter the Second value");

double b= s.nextInt();

double c=a/b;

System.out.println(a+"/"+b+"="+ c);

FileWriter f= new FileWriter("CalculationsHistory.txt", true);

BufferedWriter bw= new BufferedWriter(f);

bw.write(String.format("%f/%f= %f",a,b,c));

bw.newLine();

bw.close();

}

public void power() throws IOException { // method for power

System.out.println("Enter the first value");

double a= s.nextInt();

System.out.println("Enter the number of power");

double b= s.nextInt();

double c=Math.pow(a,b); // for power using power method from java.math library

System.out.println(a+"^"+b+"= "+ c);

FileWriter f= new FileWriter("CalculationsHistory.txt", true);

BufferedWriter bw= new BufferedWriter(f);

bw.write(String.format("%f^%f= %f",a,b,c));

bw.newLine();

bw.close();

}

public void square() throws IOException { // method for squaring

System.out.println("Enter the value");

double a= s.nextInt();

double b= Math.sqrt(a); // using sqrt method from java.math library for square root

System.out.println("Square root of"+ a + "=" +b);

FileWriter f= new FileWriter("CalculationsHistory.txt", true);

BufferedWriter bw= new BufferedWriter(f);

bw.write(String.format("Square root of %f= %f",a,b));

bw.newLine();

bw.close();

}

public void nRoot() throws IOException {

System.out.println("Enter the value");

double a= s.nextInt();

System.out.println("Enter the number of root");

double b= s.nextInt();

double c=Math.pow(a,1/b); // for nth root using power method as 1/nth power gives us nth root

System.out.println(b+"th root of "+a +"is "+ c);

FileWriter f= new FileWriter("CalculationsHistory.txt", true);

BufferedWriter bw= new BufferedWriter(f);

bw.write(String.format("%fth root of %f is %f",b,a,c));

bw.newLine();

bw.close();

}

public void log() throws IOException { // method for calculating common log i.e log to the base 10

System.out.println("Enter the value");

double a= s.nextInt();

double c= Math.log10(a); // using log10 method from java.math library for common log

System.out.println("logarithmic value of "+a+" =" + c);

FileWriter f= new FileWriter("CalculationsHistory.txt", true);

BufferedWriter bw= new BufferedWriter(f);

bw.write(String.format("logarithmic value of %f = %f",a,c));

bw.newLine();

bw.close();

}

public void nLog() throws IOException { // method for natural log i.e log to the base e

System.out.println("Enter the value");

double a= s.nextInt();

double c= Math.log(a); // using log method for natural log from java.math library

System.out.println("Natural logarithmic value of "+a+" =" + c);

FileWriter f= new FileWriter("CalculationsHistory.txt", true);

BufferedWriter bw= new BufferedWriter(f);

bw.write(String.format("Natural logarithmic value of %f = %f",a,c));

bw.newLine();

bw.close();

}

public void sin() throws IOException { // method for calculating sin value

System.out.println("Enter the value of theta "); // user input of theta in radian

double a = s.nextInt();

double c = Math.sin(a); // using sin() method of java.Math library for calculating Sine value

System.out.println("Sine of " + a + " =" + c);

FileWriter f= new FileWriter("CalculationsHistory.txt", true);

BufferedWriter bw= new BufferedWriter(f);

bw.write(String.format("Sine of %f = %f",a,c));

bw.newLine();

bw.close();

}

public void invSine() throws IOException { //method for inverse sin

System.out.println("Enter the value");

double a = s.nextInt();

double c = Math.asin(a); // for Sine inverse using asin() method of java.Math library and the answer will be in radian

System.out.println(" Sine inverse of " + a + " =" + c);

FileWriter f= new FileWriter("CalculationsHistory.txt", true);

BufferedWriter bw= new BufferedWriter(f);

bw.write(String.format("Sine inverse of %f = %f",a,c));

bw.newLine();

bw.close();

}

public void Cosine() throws IOException { // method for Cosine value

System.out.println("Enter the value");

double a = s.nextInt();

double c = Math.cos(a); // using cos() method of java.Math for Cosine Calculation

System.out.println(" Cosine of " + a + " =" + c);

FileWriter f= new FileWriter("CalculationsHistory.txt", true);

BufferedWriter bw= new BufferedWriter(f);

bw.write(String.format("Cosine of %f = %f",a,c));

bw.newLine();

bw.close();

}

public void invCos() throws IOException { //method for calculating inverse Cosine

System.out.println("Enter the value");

double a = s.nextInt();

double c = Math.acos(a); //using acos() method of java.Math for inverse Cosine Calculation

System.out.println(" Cos inverse of " + a + " =" + c);

FileWriter f= new FileWriter("CalculationsHistory.txt", true);

BufferedWriter bw= new BufferedWriter(f);

bw.write(String.format("Cos inverse of %f = %f",a,c));

bw.newLine();

bw.close();

}

public void tan() throws IOException { // method for Tangent Calculation

System.out.println("Enter the value");

double a = s.nextInt();

double c = Math.tan(a); // using tan() method of java.Math for Tangent Calculation

System.out.println(" Tangent of " + a + " =" + c);

FileWriter f= new FileWriter("CalculationsHistory.txt", true);

BufferedWriter bw= new BufferedWriter(f);

bw.write(String.format("Tangent of %f = %f",a,c));

bw.newLine();

bw.close();

}

public void invTan() throws IOException { // method for inverse Tangent calculation

System.out.println("Enter the value");

double a = s.nextInt();

double c = Math.atan(a); // using atan() method of java.Math for inverse Tangent Calculation

System.out.println(" tan inverse of " + a + " =" + c);

FileWriter f= new FileWriter("CalculationsHistory.txt", true);

BufferedWriter bw= new BufferedWriter(f);

bw.write(String.format("tan inverse of %f = %f",a,c));

bw.newLine();

bw.close();

}

public void store() throws IOException { // method for Storing the value

System.out.println("Enter th value to store");

double a = s.nextInt(); //input of value to be stored

FileWriter fw= new FileWriter("Myfile.txt", true); // this fw of FileWriter store the value in a file Myfile.txt

BufferedWriter b= new BufferedWriter(fw);

b.write(String.format("%f",a));

b.close();

System.out.println(" your value stored successfully");

// f object of FileWriter will store our operation in CalculationHistory.txt file

FileWriter f= new FileWriter("CalculationsHistory.txt", true);

BufferedWriter bw= new BufferedWriter(f);

bw.write(String.format("Your stored value is %f",a));

bw.newLine();

bw.close();

}

public void recall() throws IOException { // method for recalling stored value

String a;

FileReader fr= new FileReader("Myfile.txt"); // fr object of FileReader

BufferedReader br= new BufferedReader(fr); // creating br object of file reader and passing fr into it

a=br.readLine(); // thus will read the stored value from MyFile.txt

System.out.println(a);

br.close();

FileWriter f= new FileWriter("CalculationsHistory.txt", true);

BufferedWriter bw= new BufferedWriter(f);

bw.write(String.format("Your recalled value is %s",a));

bw.newLine();

bw.close();

}

public void d2b() throws IOException { // method for decimal to binary number calculation

System.out.println("Enter the Decimal number");

int a= s.nextInt(); // user decimal input

int b= a; // assigning a to b

String bin=""; // declaring binary value initially be empty

while (b>0){ //

int rem= b%2; // getting remainder by dividing b by 2

//initially value of b will be inputted value but later on it will change below operation tales place in loop

b= b/2; // getting quotient of b/2 and assign to b for above operation

bin= rem+bin; // collecting remainder and binary value which is initially empty later it will change as this operation

}

System.out.println("Binary value of "+ a+ " is "+ bin);

FileWriter f= new FileWriter("CalculationsHistory.txt", true);

BufferedWriter bw= new BufferedWriter(f);

bw.write(String.format("Binary value of %d is %s",a,bin));

bw.newLine();

bw.close();

}

public void b2d() throws IOException { //binary to decimal calculation method

System.out.println("Enter the binary number");

int a= s.nextInt(); // user input of binary value

int dec=0; // initially decimal value be 0

int b= a; // assigning a to b because we will need original inputted value further

int i= 0; // i is power's number initially be 0

while (b>0){

int rem1= b%10; // getting remainder of b/10

b= b/10; // assigning quotient of b/10 to b

dec= dec+rem1*(int) Math.pow(2,i++); // adding (remainder*2^i) and decimal which is in initially 0

// and assigning it to decimal

}

System.out.println("Decimal number of "+a+ " is "+dec );

FileWriter f= new FileWriter("CalculationsHistory.txt", true);

BufferedWriter bw= new BufferedWriter(f);

bw.write(String.format("Decimal number of %d is %d",a,dec));

bw.newLine();

bw.close();

}

public void sip() throws IOException { // method for Systematic Investment Plan Calculation

System.out.println("input 1. for monthly SIP 2. for quarterly SIP 3. for annually SIP");

int sipChoice = s.nextInt(); // choice of user for monthly quarterly and annually SIP

if (sipChoice == 1) { //for monthly

System.out.println("Enter investment amount per month");

double invAm = s.nextInt(); // investment amount to be payed per month

System.out.println("Enter monthly interest rate");

double mIntRate = s.nextInt(); // rate of interest per month i.e annual interest/12

System.out.println("Enter number of months for which SIP required ");

int numM = s.nextInt(); // number of months which SIP required

double rAmt = 0; // initially return amount will be 0

for (int i = 0; i < numM; i++) { // loop for getting compound interest

// interest per month = investment per month (returned amount of prevised month + investment to be done on that month)* monthly rate

double intPm = (rAmt + invAm) * mIntRate / 100;

rAmt = invAm + rAmt + intPm; //return value for that a month = investment amount + prevised month returned value + interest of that month

System.out.println("month-" + (i + 1) + " investment amount:" + invAm + "" +

" Monthly interest rate:" + mIntRate + " return Value:" + rAmt); // printing SIP

FileWriter f = new FileWriter("CalculationsHistory.txt", true);

BufferedWriter b = new BufferedWriter(f);

b.write(String.format("month-%d investment amount:%f Monthly interest rate:%f return Value:%f", (i + 1), invAm, mIntRate, rAmt));

b.newLine();

b.close();

}

}

if (sipChoice == 2) { // for Quarterly SIP

System.out.println("Enter investment amount per Quarter");

double invAq = s.nextInt();

System.out.println("Enter quarterly interest rate");

double qIntRate = s.nextInt(); //interest Rate per Quarter i.e annual interest rate /4

System.out.println("Enter number of quarters ");

int numQ = s.nextInt(); //number of Quarters

double reAmt = 0; // initially return amount

for (int i = 0; i < numQ; i++) {

double intPq = (reAmt + invAq) * qIntRate / 100; //interest per quarter

reAmt = invAq + reAmt + intPq; // return amount after each month

System.out.println("month-" + (i + 1) + " investment amount:" + invAq + "" +

" Monthly interest rate:" + qIntRate + " return Value:" + reAmt);

FileWriter f = new FileWriter("CalculationsHistory.txt", true);

BufferedWriter b = new BufferedWriter(f);

b.write(String.format("month-%d investment amount:%f Monthly interest rate:%f return Value:%f", (i + 1), invAq, qIntRate, reAmt));

b.newLine();

b.close();

}

}

if (sipChoice == 3) {

System.out.println("Enter investment amount per Anum");

double invAa = s.nextInt(); // yearly investment amount

System.out.println("Enter annually interest rate");

double aIntRate = s.nextInt(); //annual interest rate

System.out.println("Enter number of years");

int numY = s.nextInt(); //number of Years

double retAmt = 0; // return amount initially 0

for (int i = 0; i < numY; i++) {

double intPa = (retAmt + invAa) * aIntRate / 100; //interest per year

retAmt = invAa + retAmt + intPa; //return amount after each year

System.out.println("month-" + (i + 1) + " investment amount:" + invAa + "" +

" Monthly interest rate:" + aIntRate + " return Value:" + retAmt);

FileWriter f = new FileWriter("CalculationsHistory.txt", true);

BufferedWriter b = new BufferedWriter(f);

b.write(String.format("month-%d investment amount:%f Monthly interest rate:%f return Value:%f", (i + 1), invAa, aIntRate, retAmt));

b.newLine();

b.close();

}

}

}

}

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.