Challenge

Project

Calculator Application Report

Objective: The development process of the Calculator application will help to :

Create application which request input from users, Validate, process the input received and provide desired output.

Use features of java like type conversion, inheriting, looping and branching, packages and I/O classes.

The Calculator application performs both basic and scientific operations. The application provides user an option to choose the opration. Based on the option selected by the user, the application calls the corresponding class and the user can perform various mathematical operations provided in the class. There is a seperate classes for group of similar operations in the application which contains all the methods for calculation, basic as well as scientific. The application validates the user input also and provides appropriate messages when wrong input is given by the user.

To create Calculator application, 7 classes are created. First class ArethmeticOperations.java having +, -, *, / operation methods. Second class BasicOperations.java having power, squareroot, nth root methods. These two classes have same basic operations so for these classes created seperate package basicoperations.

third class LogarithemicOperations.java can do log and natural log operations. this class not have same as another so for it created package logarithmicoperations.

fourth class TrigonametricOperations.java having sin, inverse sin, cos, inverse cos, tan, inverse tan operation methods. This class have all the trigonametric operations so for this class created package name trigonametricOperations.

fifth class have set and get method to store and recall.

sixth class Conversion.java having conversions of Decimal to Binary and Binary to Decimal operaton methods. This class have conversions related operations so fir this class created package name conversions.

seventh class Calculationfiles.java it can store all the calculations in "CalculationsHistory.txt" file and read the history of all the calculations. so for this created package Calculationfiles.

eigtth class Calculator.java have main method it can show all the operations list and ask to user for operation. As per the user input it will call the method according to input option and provide output.

Code for the Calculator application
 
Calculator.java
 
package scientificcalculator;

import java.io.*;
import java.util.Scanner;

import basicOperations.*;
import calculationfiles.CalculationFile;
import conversions.Conversions;
import logarithmicOperations.LogarithemicOperations;
import trigonametricOmerations.TrigonametricOperations;

public class Calculator extends SetGet {

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

        Scanner sc = new Scanner(System.in);
        File file = new File("CalculationsHistory.txt");
        FileWriter fw = new FileWriter(file, true);
        try (PrintWriter pw = new PrintWriter(fw, true)) {
            int x = 0, y = 0;
            double val = 0;
            double c = 0;
            try {
                while (true) {
                    System.out.println("Calculate Operations : ");
                    System.out.println("1. Addition     2. Subtraction      3. Multiplication       4. Devidation");
                    System.out.println("5. Power            6. Squareroot       7. Nth Root         8. log");
                    System.out.println("9. Nlog         10. sin         11. asin                12. cos");
                    System.out.println("13. acos            14. tan         15. atan                16. store+");
                    System.out.println("17. recall          18. D2B         19. B2D             20. store file\n");

                    System.out.println("Enter your choice What you want?");
                    int choice = sc.nextInt();
                   
                    ArethmeticOperations ao = new ArethmeticOperations();
                    BasicOperations bo = new BasicOperations();
                    LogarithemicOperations lo = new LogarithemicOperations();
                    TrigonametricOperations to = new TrigonametricOperations();
                    Conversions co = new Conversions();
                    CalculationFile cf = new CalculationFile();
                    SetGet sg = new SetGet();

                    switch (choice) {

                    case 1:
                        System.out.print("Enter first value     : ");
                        x = sc.nextInt();
                        System.out.print("Enter second value : ");
                        y = sc.nextInt();

                        c = ao.addition(x, y);
                        sg.setX(c);
                        System.out.println("Answer : " + c);
                        pw.println(x + " + " + y);
                        pw.println("Answer : " + c);
                        pw.println();
                        System.out.println();
                        break;
                    case 2:
                        System.out.print("Enter first value  : ");
                        x = sc.nextInt();
                        System.out.print("Enter second value : ");
                        y = sc.nextInt();

                        c = ao.subtraction(x, y);
                        System.out.println("Answer : " + c);
                        pw.println(x + " - " + y);
                        pw.println("Answer : " + c);
                        pw.println();
                        System.out.println();
                        break;
                    case 3:
                        System.out.print("Enter first value  : ");
                        x = sc.nextInt();
                        System.out.print("Enter second value : ");
                        y = sc.nextInt();

                        c = ao.multiplication(x, y);
                        System.out.println("Answer : " + c);
                        pw.println(x + " * " + y);
                        pw.println("Answer : " + c);
                        pw.println();
                        System.out.println();
                        break;
                    case 4:
                        System.out.print("Enter first value  : ");
                        x = sc.nextInt();
                        System.out.print("Enter second value : ");
                        y = sc.nextInt();

                        c = ao.devidation(x, y);
                        System.out.println("Answer : " + c);
                        pw.println(x + " / " + y);
                        pw.println("Answer : " + c);
                        pw.println();
                        System.out.println();
                        break;
                    case 5:
                        System.out.print("Enter first value  : ");
                        x = sc.nextInt();
                        System.out.print("Enter second value : ");
                        y = sc.nextInt();

                        c = bo.power(x, y);
                        System.out.println("Answer : " + c);
                        pw.println(x + " pow " + y);
                        pw.println("Answer : " + c);
                        pw.println();
                        System.out.println();
                        break;
                    case 6:
                        System.out.print("Enter Number for Square root  : ");
                        x = sc.nextInt();

                        c = bo.squareroot(x);
                        System.out.println("Answer : " + c);
                        pw.println(x + " squareroot ");
                        pw.println("Answer : " + c);
                        pw.println();
                        System.out.println();
                        break;
                    case 7:
                        System.out.print("Enter Base Number for nth Root : ");
                        int base = sc.nextInt();
                        System.out.print("Enter Exponant number : ");
                        double exp = sc.nextDouble();

                        double nthroot = bo.nthRoot(base, exp);

                        System.out.println("Answer : " + Math.round(nthroot * 1000.0) / 1000.0);
                        pw.println(base + " nthroot " + exp);
                        pw.println("Answer : " + Math.round(nthroot * 1000.0) / 1000.0);
                        pw.println();
                        System.out.println();
                        break;
                    case 8:
                        System.out.print("Enter number to find Log value  : ");
                        val = sc.nextInt();

                        c = lo.log(val);
                        System.out.println("Answer : " + c);
                        pw.println(val + " log ");
                        pw.println("Answer : " + c);
                        pw.println();
                        System.out.println();
                        break;
                    case 9:
                        System.out.print("Enter number to find natural Log value  : ");
                        val = sc.nextInt();

                        c = lo.log(val);
                        System.out.println("Answer : " + c);
                        pw.println(val + " nlog ");
                        pw.println("Answer : " + c);
                        pw.println();
                        System.out.println();
                        break;
                    case 10:
                        System.out.print("Enter angle to find Sine value  : ");
                        x = sc.nextInt();

                        c = to.sin(x);
                        System.out.println("Answer : " + c);
                        pw.println(x + " sin ");
                        pw.println("Answer : " + c);
                        pw.println();
                        System.out.println();
                        break;
                    case 11:
                        System.out.print("Enter angle to find Inverse Sine value  : ");
                        x = sc.nextInt();

                        c = to.inverssin(x);
                        System.out.println("Answer : " + c);
                        pw.println(x + " isin ");
                        pw.println("Answer : " + c);
                        pw.println();
                        System.out.println();
                        break;
                    case 12:
                        System.out.print("Enter angle to find Cosine value  : ");
                        x = sc.nextInt();

                        c = to.cos(x);
                        System.out.println("Answer : " + c);
                        pw.println(x + " cos ");
                        pw.println("Answer : " + c);
                        pw.println();
                        System.out.println();
                        break;
                    case 13:
                        System.out.print("Enter angle to find Inverse Cosine value  : ");
                        x = sc.nextInt();

                        c = to.inverscos(x);
                        System.out.println("Answer : " + c);
                        pw.println(x + " icos ");
                        pw.println("Answer : " + c);
                        pw.println();
                        System.out.println();
                        break;
                    case 14:
                        System.out.print("Enter angle to find Sine value  : ");
                        x = sc.nextInt();

                        c = to.tan(x);
                        System.out.println("Answer : " + c);
                        pw.println(x + " tan ");
                        pw.println("Answer : " + c);
                        pw.println();
                        System.out.println();
                        break;
                    case 15:
                        System.out.print("Enter angle to find Sine value  : ");
                        x = sc.nextInt();

                        c = to.inverstan(x);
                        System.out.println("Answer : " + c);
                        pw.println(x + " itan ");
                        pw.println("Answer : " + c);
                        pw.println();
                        System.out.println();
                        break;
                       
                    case 16:
                        sg.setX(c);
                       
                        break;
                    case 17:
                        c = sg.getAns();
                        System.out.println(c);
                        System.out.println();
                        break;
                       
                    case 18:
                        System.out.print("Enter Decimal number to binary conversion  : ");
                        x = sc.nextInt();

                        c = co.decimalToBinary(x);
                        System.out.println("Answer : " + c);
                        pw.println(x + " D2B ");
                        pw.println("Answer : " + c);
                        pw.println();
                        System.out.println();
                        break;
                    case 19:
                        System.out.print("Enter Binary number to Decimal conversion  : ");
                        x = sc.nextInt();

                        c = co.binaryToDecimal(x);
                        System.out.println("Answer : " + c);
                        pw.println(x + " B2D ");
                        pw.println("Answer : " + c);
                        pw.println();
                        System.out.println();
                        break;
                       
                    case 20:
                        System.out.println("Calculation History : \n");
                        cf.history();
                        break;
                       
                    default:
                        System.out.println("Choose correct option... Try again ....!");

                    }

                }
            } catch (Exception e) {
                e.printStackTrace();
                System.out.println("Do an Operations only in Number Format ");
            }
        }
        sc.close();
    }
}
 
ArethmeticOperations.java
 
package basicOperations;

import scientificcalculator.SetGet;

public class ArethmeticOperations {

    SetGet sg = new SetGet();
    public int addition(int x, int y) {

        int c = x + y;
        sg.setX(x);
        sg.setY(y);
        sg.setAns(c);
        return c;
    }

    public int subtraction(int x, int y) {

        int c = x - y;
        sg.setX(x);
        sg.setY(y);
        sg.setAns(c);
        return c;

    }

    public int multiplication(int x, int y) {

        int c = x * y;
        sg.setX(x);
        sg.setY(y);
        sg.setAns(c);
        return c;

    }

    public int devidation(int x, int y) {

        int c = x / y;
        sg.setX(x);
        sg.setY(y);
        sg.setAns(c);
        return c;

    }
   
}
 
BasicOperations.java
 
package basicOperations;

public class BasicOperations {

    public double power(int val1, int val2) {
       
        double pow = Math.pow(val1, val2);
        return pow;
    }
   
    public double squareroot(int val) {
       
        double sqrt = Math.sqrt(val);
       
        return sqrt;
       
    }
   
    public double nthRoot(int base, int exponant) {
                 
                // intially guessing a random number between
                // 0 and 9
                double xPre = Math.random() % 10;
             
                // smaller eps, denotes more accuracy
                double eps = 0.001;
             
                // initializing difference between two
                // roots by INT_MAX
                double delX = 2147483647;
             
                // xK denotes current value of x
                double xK = 0.0;
             
                // loop untill we reach desired accuracy
                while (delX > eps)
                {
                    // calculating current value from previous
                    // value by newton's method
                    xK= ((base-1.0) *xPre+
                    (double)exponant / Math.pow(xPre, base - 1)) / (double)base;
                    delX = Math.abs(xK - xPre);
                    xPre = xK;
                }
             
                return xK;
            }
     public double nthRoot(int base, double exponant)
        {
            return nthroot(base, exponant, .0001);
        }
        public double nthroot(int n, double x, double p)
        {
            if(x < 0)
            {
                System.err.println("Negative!");
                return -1;
            }
            if(x == 0)
                return 0;
            double x1 = x;
            double x2 = x / n;  
            while (Math.abs(x1 - x2) > p)
            {
                x1 = x2;
                x2 = ((n - 1.0) * x2 + x / Math.pow(x2, n - 1.0)) / n;
            }
            return x2;
        }
   
}
LogarithemicOperations.java
 
package logarithmicOperations;

public class LogarithemicOperations {

    public double log(double log) {
       
        double logval = Math.log(log);
        return logval;
    }
   
    public double inversrlog(double log) {
       
        double inverselog = Math.log(log);
       
        return inverselog;
    }
}
 
TrigonametricOperations.java
 
package trigonametricOmerations;

public class TrigonametricOperations {

    public double sin(int angle) {
       
        double sin = Math.sin(angle);
        return sin;
    }
   
    public double inverssin(int angle) {
       
        double asin = Math.asin(angle);
        return asin;
    }
   
    public double cos(int angle) {
       
        double cos = Math.cos(angle);
        return cos;
    }
   
    public double inverscos(int angle) {
       
        double acos = Math.acos(angle);
        return acos;
    }
   
    public double tan(int angle) {
       
        double tan = Math.tan(angle);
        return tan;
    }
   
    public double inverstan(int angle) {
       
        double atan = Math.atan(angle);
        return atan;
    }
}
SetGet.java
 
package scientificcalculator;

public class SetGet {

    private double x;
    private double y;
    private double ans;
   
    public double getAns() {
        return ans;
    }
    public void setAns(double ans) {
        this.ans = ans;
    }
   
    public double getX() {
        return x;
    }
    public void setX(double x) {
        this.x = x;
    }
    public double getY() {
        return y;
    }
    public void setY(double y) {
        this.y = y;
    }
   
}
Conversions.java
 
package conversions;

public class Conversions {

    public int decimalToBinary(int decimal) {
       
        int binary = 0, temp = 1, mod = 0;
        while(decimal > 0) {
            mod = decimal % 2;
            temp = temp * 10+mod;
            decimal /= 2;
        }

        while(temp>1) {
            mod = temp % 10;
            binary = binary * 10+mod;
            temp = temp / 10;
        }
        return binary;
    }
   
    public double binaryToDecimal(int binary) {
       
        int pow = 0, temp = 0;
        double decimal = 0;
        while(true) {
            if(binary == 0)
                break;
            else {
                temp = binary%10;
                decimal = decimal+ temp* Math.pow(2, pow);
                binary = binary / 10;
                pow++;
            }
        }
        return decimal;
    }
}
 
CalculationFiles.java
 
package calculationfiles;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

public class CalculationFile {

    public String history() throws IOException {
       
        FileReader fr = new FileReader("CalculationsHistory.txt");
        BufferedReader br = new BufferedReader(fr);
       
        String line = br.readLine();
        while(line != null) {
            System.out.println(line);
            line = br.readLine();
        }
        br.close();
   
    return line;    
    }
}
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.