Skip to main content
Commonmark migration
Source Link

Definitely follow h.j.k's guidance here, especially concerning the variable names, they stand out as unconventional and looked like classes, at a glance.

Since this is meant to be a console calculator, you may want to consider is optionally reading and running from the arguments.

Something along the lines of:

public static void main(String[] args) {
        if (args.length == 3) {
            parse(args);
        } else {
            // run your current version
        }
    }

    public static void parse(String[] args) {
        double first = Double.parseDouble(args[0]);
        char operator = args[1].charAt(0);
        double second = Double.parseDouble(args[2]);

        System.out.println(compute(first, operator, second));
    }

    public static double compute(double num1, char operator, double num2) {
        double result = 0;

        switch(operator) {
            case '+':
                result = num1 + num2;
            break;
            case '-':
                result = num1 - num2;
            break;
            case 'x':
                result = num1 * num2;
            break;
            case '/':
                result = num1 / num2;
            break;
            case '%':
                result = num1 % num2;
            break;
        }

        return result;
    }

So you can optionally do it the input -response - output method, or simply call it with what you want to be computed:

Sample output:

Java Algorithm1 3 * 5
15.0

 

Java Algorithm1 120 % 14
8.0

Of course whether you want the operator to come first, want to add support for unlimited arguments, checks for division by 0 or more is up to you, and would make for a good exercise.

Definitely follow h.j.k's guidance here, especially concerning the variable names, they stand out as unconventional and looked like classes, at a glance.

Since this is meant to be a console calculator, you may want to consider is optionally reading and running from the arguments.

Something along the lines of:

public static void main(String[] args) {
        if (args.length == 3) {
            parse(args);
        } else {
            // run your current version
        }
    }

    public static void parse(String[] args) {
        double first = Double.parseDouble(args[0]);
        char operator = args[1].charAt(0);
        double second = Double.parseDouble(args[2]);

        System.out.println(compute(first, operator, second));
    }

    public static double compute(double num1, char operator, double num2) {
        double result = 0;

        switch(operator) {
            case '+':
                result = num1 + num2;
            break;
            case '-':
                result = num1 - num2;
            break;
            case 'x':
                result = num1 * num2;
            break;
            case '/':
                result = num1 / num2;
            break;
            case '%':
                result = num1 % num2;
            break;
        }

        return result;
    }

So you can optionally do it the input -response - output method, or simply call it with what you want to be computed:

Sample output:

Java Algorithm1 3 * 5
15.0

 

Java Algorithm1 120 % 14
8.0

Of course whether you want the operator to come first, want to add support for unlimited arguments, checks for division by 0 or more is up to you, and would make for a good exercise.

Definitely follow h.j.k's guidance here, especially concerning the variable names, they stand out as unconventional and looked like classes, at a glance.

Since this is meant to be a console calculator, you may want to consider is optionally reading and running from the arguments.

Something along the lines of:

public static void main(String[] args) {
        if (args.length == 3) {
            parse(args);
        } else {
            // run your current version
        }
    }

    public static void parse(String[] args) {
        double first = Double.parseDouble(args[0]);
        char operator = args[1].charAt(0);
        double second = Double.parseDouble(args[2]);

        System.out.println(compute(first, operator, second));
    }

    public static double compute(double num1, char operator, double num2) {
        double result = 0;

        switch(operator) {
            case '+':
                result = num1 + num2;
            break;
            case '-':
                result = num1 - num2;
            break;
            case 'x':
                result = num1 * num2;
            break;
            case '/':
                result = num1 / num2;
            break;
            case '%':
                result = num1 % num2;
            break;
        }

        return result;
    }

So you can optionally do it the input -response - output method, or simply call it with what you want to be computed:

Sample output:

Java Algorithm1 3 * 5
15.0

Java Algorithm1 120 % 14
8.0

Of course whether you want the operator to come first, want to add support for unlimited arguments, checks for division by 0 or more is up to you, and would make for a good exercise.

Source Link
Legato
  • 9.9k
  • 4
  • 50
  • 118

Definitely follow h.j.k's guidance here, especially concerning the variable names, they stand out as unconventional and looked like classes, at a glance.

Since this is meant to be a console calculator, you may want to consider is optionally reading and running from the arguments.

Something along the lines of:

public static void main(String[] args) {
        if (args.length == 3) {
            parse(args);
        } else {
            // run your current version
        }
    }

    public static void parse(String[] args) {
        double first = Double.parseDouble(args[0]);
        char operator = args[1].charAt(0);
        double second = Double.parseDouble(args[2]);

        System.out.println(compute(first, operator, second));
    }

    public static double compute(double num1, char operator, double num2) {
        double result = 0;

        switch(operator) {
            case '+':
                result = num1 + num2;
            break;
            case '-':
                result = num1 - num2;
            break;
            case 'x':
                result = num1 * num2;
            break;
            case '/':
                result = num1 / num2;
            break;
            case '%':
                result = num1 % num2;
            break;
        }

        return result;
    }

So you can optionally do it the input -response - output method, or simply call it with what you want to be computed:

Sample output:

Java Algorithm1 3 * 5
15.0

Java Algorithm1 120 % 14
8.0

Of course whether you want the operator to come first, want to add support for unlimited arguments, checks for division by 0 or more is up to you, and would make for a good exercise.