Thursday, 12 February 2015

Simple Math calculator in Java

So I gave my students a task the following question:





Write a java function that take three parameters, the first two are integers and the last one is a string. The value of the last parameter will always be an operator (e.g. “+”, “-“, “%” etc. Your program should check the value of the operator and perform such action on the two integers. For example, if the integer parameters are 5 and 10, and the operator parameter is “+”, then your program will add the two integers together and print the results to the screen.



  •  Use the If-else control structure
  • Use the Switch control structure 


This was just a very simple program to get a salary and subtract the medical aid contribution, income tax and pension fund. This is my solution here and i will explain how it works.

 
public class Maths {

    public static void main(String[] args) {
        compute(5, 2, "-");
    }

    public static void compute(int x, int y, String st) {

        String str = st;
        char c = str.charAt(0);

        if (c == '+') {
            System.out.println(x + " + " + y + " = " + (x + y));
        } else if (c == '-') {
            System.out.println(x + " - " + y + " = " + (x - y));
        } else if (c == '/') {
            System.out.println(x + " / " + y + " = " + (x / y));
        } else if (c == '*') {
            System.out.println(x + " * " + y + " = " + (x * y));
        } else if (c == '%') {
            System.out.println(x + " % " + y + " = " + (x % y));
        } else {
            System.out.println("Please Check Operand!");
        }

    }

}







Explanation of the code



There is a method called compute which takes 3 parameters, int x is an integer and int y is also an integer. Tha last parameter is the symbol which represents the operand to be used on both x and y, for example, if the parameters are 10,2,"*" then the arithmetic performed will be 10 multiplied by 2 and will print out the answer to the screen. 

The if else statements are just responsible for checking the kind of operation to perform by checking whether the operator is a legal one. if you parse anything not recognisable the error message is printed to the screen.

The above code can also be done using the switch statements as shown below.


public class MathsSwich {

    public static void main(String[] args) {
        compute(8, 3, "%");
    }

    public static void compute(int x, int y, String st) {

        String str = st;
        char c = str.charAt(0);

        switch (c) {
            case '+':
                System.out.println(x + " + " + y + " = " + (x + y));
                break;
            case '-':
                System.out.println(x + " - " + y + " = " + (x - y));
                break;
            case '/':
                System.out.println(x + " / " + y + " = " + (x / y));
                break;
            case '*':
                System.out.println(x + " * " + y + " = " + (x * y));
                break;
            case '%':
                System.out.println(x + " % " + y + " = " + (x % y));
                break;
            default:
                System.out.println("Please check operand!");

        }

    }

}


The output of the above programs will always be the same. Cheers and see you next time on another simple and fun java code 

0 comments:

Post a Comment