Skip to content

Instantly share code, notes, and snippets.

@aorjoa
Created January 5, 2018 16:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aorjoa/ef8b66625ee3cfbbe34ccade96791612 to your computer and use it in GitHub Desktop.
Save aorjoa/ef8b66625ee3cfbbe34ccade96791612 to your computer and use it in GitHub Desktop.
package oot.lab8;
public class Calculator implements MathCalculator {
private double operand1;
private double operand2;
public Calculator(double operand1, double operand2){
this.operand1 = operand1;
this.operand2 = operand2;
}
public double add() {
return operand1 + operand2;
}
public double minus() {
return operand1 - operand2;
}
public void compare() {
if(operand1 > operand2) {
System.out.println(operand1 + " > " + operand2 + " : " + (operand1-operand2));
} else {
System.out.println(operand1 + " <= " + operand2 + " : " + (operand2-operand1));
}
}
}
@aorjoa
Copy link
Author

aorjoa commented Jan 5, 2018

package oot.lab8;

/**

  • Created by Bhuridech Sudsee.
    */
    public interface MathCalculator {
    public double add();
    public double minus();
    }

@aorjoa
Copy link
Author

aorjoa commented Jan 5, 2018

package oot.lab8;

/**
 * Created by Bhuridech Sudsee.
 */
public interface MathCalculator {
    public double add();
    public double minus();
}

@aorjoa
Copy link
Author

aorjoa commented Jan 5, 2018

package oot.lab8;

import java.util.Scanner;

/**
 * Created by Bhuridech Sudsee.
 */
public class Lab8Sec1 {
    public static void main(String args[]){
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter number with operator : ");
        String calText = scanner.nextLine();
        String[] calTextSplit = calText.split(" ");
        double firstOperand = Double.valueOf(calTextSplit[0]);
        String operator = calTextSplit[1];
        double secondOperand = Double.valueOf(calTextSplit[2]);
        Calculator c = new Calculator(firstOperand, secondOperand);
        switch (operator){
            case "ADD":
                System.out.println(calText + " = "+ c.add());
                break;
            case "MINUS":
                System.out.println(calText + " = "+ c.minus());
                break;
        }
        c.compare();
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment