Question

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;
    }

}
}
Was it helpful?

Solution

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

}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top