Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

Java 

Java calculator source code

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();

  }

}
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 ...
(Sep 23, 2015 09:59 PM)clrlslwi 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 ?
(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.
Will u in future add more functions to this ? :)
Proud to be 100.000 user on HF

[Image: 4tAQ4Gf.png]
(Sep 25, 2015 06:48 AM)csrk 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.
Went the extra mile but practice doesn't hurt :)
(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 ?


Possibly Related Threads...
Thread / Author Replies Views Last Post
[Java] by PELockAug 4, 2026 03:02 AM 28 1,372 Aug 4, 2026 03:02 AM
Last Post: PELock
[Java] by MordiciaNov 22, 2018 05:41 PM 5 274 Nov 22, 2018 05:41 PM
Last Post: Minesweeper
[.NET] by Leroy Jethro GibbsMar 10, 2018 06:28 PM 10 296 Mar 10, 2018 06:28 PM
Last Post: Ninja