Skip to main content
added 598 characters in body
Source Link
Legato
  • 9.8k
  • 4
  • 49
  • 116
  1. ImplementingImplement something that works on simple binary operations.
  2. Extend itImplement something to parse out those operations from string form.
  3. Add in dialogue to interface with the user.
  4. Take in command line arguments.

ExceedinglyA Simple example that illustrates all 4 of the above:

For now, you can think of an enumeration as a useful way to store related series of values that won't change. A calculator is a good opportunity to look into them rather than referencing the values directly, and if you're curious there's an interesting way we can apply them to the purpose of a calculator.

Then the calculator program itself.

Notice that I optionally directly accept argument which is something desirable for a CLI. This example has a lot you can still improve on, including input validation and a more direct utilization of the enumeration among other things.

If you're unfamiliar with the split function I'm simply converting whatever the user gives us to a String array, with the indexes separated by spaces (what the regex "\s++" means). so I may treat it as if it was invoked from the command line.

This example has a lot If you can still improve on, including input validation and awant to learn more direct utilization of the enumeration amongabout Regex, you can check out other things.

Notice that I optionally directly accept argument which is something desirable for a CLIpatterns or use HackerRank's decent introduction/practice platform here.

  1. Implementing something that works on simple binary operations.
  2. Extend it to parse out those operations from string form.
  3. Add in dialogue to interface with the user.
  4. Take in command line arguments.

Exceedingly Simple example that illustrates all 4 of the above:

Then the calculator program itself.

If you're unfamiliar with the split function I'm simply converting whatever the user gives us to a String array, with the indexes separated by spaces (what the regex "\s++" means). so I may treat it as if it was invoked from the command line.

This example has a lot you can still improve on, including input validation and a more direct utilization of the enumeration among other things.

Notice that I optionally directly accept argument which is something desirable for a CLI.

  1. Implement something that works on simple binary operations.
  2. Implement something to parse out those operations from string form.
  3. Add in dialogue to interface with the user.
  4. Take in command line arguments.

A Simple example that illustrates all 4 of the above:

For now, you can think of an enumeration as a useful way to store related series of values that won't change. A calculator is a good opportunity to look into them rather than referencing the values directly, and if you're curious there's an interesting way we can apply them to the purpose of a calculator.

Then the calculator program itself.

Notice that I optionally directly accept argument which is something desirable for a CLI. This example has a lot you can still improve on, including input validation and a more direct utilization of the enumeration among other things.

If you're unfamiliar with the split function I'm simply converting whatever the user gives us to a String array, with the indexes separated by spaces (what the regex "\s++" means). so I may treat it as if it was invoked from the command line. If you want to learn more about Regex, you can check out other patterns or use HackerRank's decent introduction/practice platform here.

Source Link
Legato
  • 9.8k
  • 4
  • 49
  • 116

Scanner as an argument is not necessary here.

I would suggest, in order, that you

  1. Implementing something that works on simple binary operations.
  2. Extend it to parse out those operations from string form.
  3. Add in dialogue to interface with the user.
  4. Take in command line arguments.

Exceedingly Simple example that illustrates all 4 of the above:

First an operator enumeration.

public enum Operator {
    ADD('+'),
    SUBTRACT('-'),
    MULTIPLY('*'),
    DIVIDE('/'),
    MOD('%'),
    EXPONENTIATE('^');

    private final char symbol;

    Operator(char symbol) {
        this.symbol = symbol;
    }

    public char symbol() {
        return symbol;
    }

    @Override
    public String toString() {
        return name().charAt(0) + name().substring(1).toLowerCase();
    }
}

Then the calculator program itself.

import java.util.Scanner;

public class Calculator{
    public static void main(String[] args) {
        if (args.length == 3) {
            System.out.println(calculate(args));
        } else {
            Scanner input = new Scanner(System.in);
            System.out.print("Enter a binary calculation: ");
            System.out.println(calculate(input.nextLine().split("\\s+")));
        }
    }

    private static double calculate(String[] args) {
        if (args.length != 3) {
            throw new IllegalArgumentException("Can only calculate binary operations");
        }
        
        double input1 = Double.parseDouble(args[0]);
        char operator = args[1].charAt(0);
        double input2 = Double.parseDouble(args[2]);
        
        if (input2 == 0 && (operator == Operator.DIVIDE.symbol() || operator == Operator.MOD.symbol())) {
            throw new IllegalArgumentException("Cannot divide by 0");
        }

        return calculation(input1, operator, input2);
    }

    private static double calculation(double input1, char operator, double input2) {
        double result = 0;
        switch(operator) {
            case '+':
                result = input1 + input2;
            break;
            case '-':
                result = input1 - input2;
            break;
            case '*':
                result = input1 * input2;
            break;
            case '/':
                result = input1 / input2;
            break;
            case '%':
                result = input1 % input2;
            break;
            case '^':
                result = Math.pow(input1, input2);
            break;
        }
        return result;
    }
}

If you're unfamiliar with the split function I'm simply converting whatever the user gives us to a String array, with the indexes separated by spaces (what the regex "\s++" means). so I may treat it as if it was invoked from the command line.

This example has a lot you can still improve on, including input validation and a more direct utilization of the enumeration among other things.

Notice that I optionally directly accept argument which is something desirable for a CLI.