Everyday Dev System

3주차 과제 - 계산기 구현 본문

내배캠 초기 학습/Java 문법 종합반

3주차 과제 - 계산기 구현

chaeyoung- 2023. 5. 25. 11:32

[week3] - [calculateSample] package안에 구현

 

1. Calculator.java

2. AbstractOperation.java

3. MultiplyOperation.java

4. AddOperation.java

5. DivideOperation.java

6. SubstractOperation.java

7. ModOperation.java

8. Main.java

    Result 

 

File Share : (아래 링크에서 다운로드 가능합니다)

https://drive.google.com/file/d/17rTTeo6KmJ8cZ03N2XgiDsxHCKdsU1FR/view?usp=sharing 

 

calculateSample.zip

 

drive.google.com

 

1.  Calculator.java

package org.example.week3.calculateSample;

public class Calculator {
    private final String operator;
    private AbstractOperation operation;

    public Calculator(String operator) {
        this.operator = operator;
        this.setOperation();
    }

    public void setOperation() {
        if (operator.equals("+")) {
            this.operation = new AddOperation();
        } else if (operator.equals("-")) {
            this.operation = new SubstractOperation();
        } else if (operator.equals("*")) {
            this.operation = new MultiplyOperation();
        } else if (operator.equals("/")) {
            this.operation = new DivideOperation();
        } else {
            System.out.println("올바른 연산 기호를 입력해주세요 ( +:덧셈 , -:뺄셈 ,  *:곱셈 , /:나눗셈) ");
        }
    }

    double calculate(int firstNum, int secondNum) {
        double result = this.operation.operate(firstNum, secondNum);
        return result;
    }
}

2. AbstractOperation.java

package org.example.week3.calculateSample;

public abstract class AbstractOperation {
    public abstract double operate(int firstNumber, int secondNumber);
}

3. MultiplyOperation.java

package org.example.week3.calculateSample;

public class MultiplyOperation extends AbstractOperation{
    @Override
    public double operate(int firstNumber, int secondNumber) {
        return firstNumber * secondNumber;
    }
}

4. AddOperation.java

package org.example.week3.calculateSample;

public class AddOperation extends AbstractOperation{
    @Override
    public double operate(int firstNumber, int secondNumber) {
        return firstNumber + secondNumber;
    }
}

5. DivideOperation.java

package org.example.week3.calculateSample;

public class DivideOperation extends AbstractOperation{
    @Override
    public double operate(int firstNumber, int secondNumber) {
        return firstNumber/secondNumber;
    }
}

6. SubstractOperation.java

package org.example.week3.calculateSample;

public class SubstractOperation extends AbstractOperation{
    @Override
    public double operate(int firstNumber, int secondNumber) {
        return firstNumber-secondNumber;
    }
}

7. ModOperation.java

package org.example.week3.calculateSample;

public class ModOperation extends AbstractOperation{
    @Override
    public double operate(int firstNumber, int secondNumber) {
        return firstNumber%secondNumber;
    }
}

 

8. Main.java

package org.example.week3.calculateSample;

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        System.out.println("연산할 식을 띄어쓰기로 구분하여 기재해주세요 (예: 1 + 2)");
        int fN = sc.nextInt();
        String operateMethod = sc.next();
        int sN = sc.nextInt();
        sc.nextLine();

        Calculator calculator  = new Calculator(operateMethod);
        System.out.println();
        System.out.println(fN + " " + operateMethod + " " + sN + " = " + calculator.calculate(fN,sN));
    }
}

 

 

 

Result :