Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | |||
5 | 6 | 7 | 8 | 9 | 10 | 11 |
12 | 13 | 14 | 15 | 16 | 17 | 18 |
19 | 20 | 21 | 22 | 23 | 24 | 25 |
26 | 27 | 28 | 29 | 30 | 31 |
Tags
- REST API 규칙
- JPA주의사항
- Unsatisfied dependency
- 빈생성안됨
- 스프링부트오류
- Error creating bean with name
- @IdClass
- 복합키
- JPA
- jwt메서드
- 1차캐시
- 스프링 부트 기능
- spring서버
- jpa에러
- jpa회원가입
- Q 클래스
- ERD 작성
- json gson 차이
- github
- Filter
- 최종 프로젝트
- uncheck Exception
- json
- 인텔리제이
- Spring Spring boot 차이
- git
- 스프링 부트 공식 문서
- queryDSL
- JoinColumn
- REST란
Archives
- Today
- Total
Everyday Dev System
3주차 과제 - 계산기 구현 본문
[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
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 :
'내배캠 초기 학습 > Java 문법 종합반' 카테고리의 다른 글
4주차 과제 : 계산기 예외 처리 (0) | 2023.05.26 |
---|---|
4주차 공부 - List 타입 (0) | 2023.05.24 |
4주차 공부 - Generic (0) | 2023.05.24 |
4주차 공부 - 예외처리 (0) | 2023.05.24 |
Java 문법 종합반 - 1,2주차 (2) | 2023.05.22 |