Hack Forums
[Java] Java calculator source code - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444)
+--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151)
+---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118)
+---- Thread: [Java] Java calculator source code (/showthread.php?tid=4998460)



Java calculator source code - xlosmuertosx - Sep 23, 2015

Heres a calculator

math.java
Code
package Calc;

/**
*
* @author xlosmuertosx
*/
public class Maths {

    double add(double a, double b) {
        double answer = a+b;
        return answer;          
    }

    double subtract(double a, double b) {
        double answer = a-b;
        return answer;          
    }

    double multiply(double a, double b) {
        double answer = a*b;
        return answer;          
    }

    double divide(double a, double b) {
        double answer = a/b;
        return answer;          
    }

    double power(double a, double b){
        double answer =a;

        for (int x=2; x<=b; x++){
            answer *= a;
        }

        return answer;
    }

}

Calculator.java
Code
package calc;

/**
*
* @author xlosmuertosx
*/
import java.util.Scanner;

public class Calculator {

public static void main(String[] args) {

    Scanner input = new Scanner(System.in);                                              

    Maths Maths = new Maths();

    double answer = 0;
    double inputA, inputB;
    char operator;
    boolean done = false;

     while (done == false) {
        System.out.print("Please enter what you want to calculate: ");

        inputA = input.nextDouble();
        operator = input.next().charAt(0);
        inputB = input.nextDouble();        

        switch (operator) {
            case '+': answer = Maths.add(inputA, inputB);
                      break;
            case '-': answer = Maths.subtract(inputA, inputB);
                      break;
            case '*': answer = Maths.multiply(inputA, inputB);
                      break;
            case '/': answer = Maths.divide(inputA, inputB);
                      break;
            case '^': answer = Maths.power(inputA, inputB);
                      break;
        }

            System.out.println(answer);            
    }      

    input.close();

  }

}



* - clrlslwi - Sep 23, 2015

Why do you have an instanced class full of pure functions? And a better question is why you have methods wrapping simple arithmic operations.

... insert rant about closing System.in and predefining your variables without assigning anything to them ...


* - xlosmuertosx - Sep 24, 2015

(Sep 23, 2015 09:59 PM)Demmonic Wrote:  Why do you have an instanced class full of pure functions? And a better question is why you have methods wrapping simple arithmic operations.

... insert rant about closing System.in and predefining your variables without assigning anything to them ...

I cant understand, could you please write the source code ?


* - csrk - Sep 25, 2015

(Sep 24, 2015 07:44 PM)xlosmuertosx Wrote:  I cant understand, could you please write the source code ?

You made something way more complex than it could be. I.e you made a function for adding two numbers, while a+b would have been just fine.


* - gua - Sep 25, 2015

Will u in future add more functions to this ? :)


* - xlosmuertosx - Sep 25, 2015

(Sep 25, 2015 06:48 AM)Rewbert Wrote:  You made something way more complex than it could be. I.e you made a function for adding two numbers, while a+b would have been just fine.

Oh okay thanks, now I get it.


* - the wolf of blockchain - Sep 25, 2015

Went the extra mile but practice doesn't hurt :)


* - xlosmuertosx - Sep 25, 2015

(Sep 25, 2015 08:59 AM)gua Wrote:  Will u in future add more functions to this ? :)

What more function do you want ? you mean like a scientific calculator or a graphical display calculator ?