0

Im fairly new to Java , and OOP in general hence many concepts in Java dont make complete sense even though the API is thorough. I have made a small calculator code , however I want to learn how to achieve the same product using arguments within method, samples would be prime.

import java.io.IOException;
import java.util.Scanner;

public class Ga {

static Scanner input = new Scanner(System.in);
public static void main(String[] args) throws IOException {

    System.out.println("First number:");        
    float i = input.nextInt();

    System.out.println("Choose operator +, -, *, /");
    char s = input.next().charAt(0);

    System.out.println("Second number:");
    float z = input.nextInt();

    switch (s) {
        case '+':
            System.out.println("Result= "+(i+z));
            System.in.read();
            break;
        case '-':
            System.out.println("Result= "+(i-z));
            System.in.read();
            break;
        case '*':
            System.out.println("Result= "+(i*z));
            System.in.read();
            break;
        case '/':
            System.out.print("Result= "+(i/z));
            System.in.read();
            break;
    }

}
}
  • 2
    You're at the wrong place for discussions like that. I would recommend you have a look at the command pattern or the strategy pattern to get rid of your switch and see the power of OOP. – schlingel Apr 29 '14 at 11:02
  • A hashmab between a String (the `+`, `-` etc) and an object that does the operation (although it might be overkill in this case) – Richard Tingle Apr 29 '14 at 11:10

1 Answers1

1

To start with OOP, you could write an abstract class representing an operation:

public abstract class Operation {
    public abstract float getResult(float a, float b);
}

Then, try to write concrete operation like Addition, Division:

public class Addition extends Operation {
    @Override
    public float getResult(float a, float b) {
        return a + b;
    }
}
public class Division extends Operation {
    @Override
    public float getResult(float a, float b) {
        return a / b;
    }
}

Then, rewrite your main method like that:

public static void main(String[] args) throws IOException {

    System.out.println("First number:");
    float i = input.nextInt();

    System.out.println("Choose operator +, -, *, /");
    char s = input.next().charAt(0);

    System.out.println("Second number:");
    float z = input.nextInt();

    Operation op = null;
    switch (s) {
    case '+':
        op = new Addition();
        break;
    case '-':
        op = new Subtraction();
        break;
            ...
    }

    System.out.println("Result= " + op.getResult(i, z));
    System.in.read();
}

As Richard mentions it, you could also rewrite the switch with an HashMap:

public static void main(String[] args) throws IOException {

    System.out.println("First number:");
    float i = input.nextInt();

    System.out.println("Choose operator +, -, *, /");
    char s = input.next().charAt(0);

    System.out.println("Second number:");
    float z = input.nextInt();


    Map<String, Operation> operationMap = new HashMap<String, Operation>();
    operationMap.put("+", new Addition());
    operationMap.put("-", new Substraction());
    ...

    Operation op = operationMap.get(s);

    System.out.println("Result= " + op.getResult(i, z));
    System.in.read();

}
fluminis
  • 3,575
  • 4
  • 34
  • 47
  • If we're going to go down this route then a `HashMap` might be nice to avoid all those cases – Richard Tingle Apr 29 '14 at 11:12
  • Thats exactly what I ment, +1, initially this doesn't seem to add much over the switch but it becomes increasingly nice as the size of the program increases. Especially that the list of options can change (or be created) during runtime – Richard Tingle Apr 29 '14 at 11:20