728x90
이번 주 목표
JAVA 완강
오늘 목표
JAVA 3주차
JAVA 4주차
JAVA 5주차
오늘 한 것
JAVA 3주차
JAVA 4주차
JAVA 5주차
오늘 스케줄
AM 9:00 JAVA 공부
PM 10:00 TIL 작성
PM 11:00 취침
1. 3주차 숙제 : 추상화를 이용해 계산기 만들기
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("첫 번째 숫자를 입력하세요.");
int firstNumber = sc.nextInt();
System.out.println("두 번째 숫자를 입력하세요.");
int secondNumber = sc.nextInt();
System.out.println("연산자를 입력하세요.");
String operation = sc.next();
Calculator calculator = new Calculator(new AddOperation());
switch (operation){
case "+":
calculator.operation = new AddOperation();
System.out.println(calculator.calculate(firstNumber,secondNumber));
break;
case "-":
calculator.operation = new SubstractOperation();
System.out.println(calculator.calculate(firstNumber,secondNumber));
break;
case "*":
calculator.operation = new MultiplyOperation();
System.out.println(calculator.calculate(firstNumber,secondNumber));
break;
case "/":
calculator.operation = new DivideOperation();
System.out.println(calculator.calculate(firstNumber,secondNumber));
break;
case "%":
System.out.println(firstNumber % secondNumber);
break;
}
}
}
class Calculator {
AbstractOperation operation ;
Calculator (AbstractOperation operation){
this.operation = operation;
}
double calculate (int firstNumber, int secondNumber){
double answer = 0.0;
answer = operation.operate(firstNumber,secondNumber);
return answer;
}
void setOperation (String opeoperationrator){
this.operation = operation;
}
}
abstract class AbstractOperation{
AbstractOperation () {
String operator;
}
abstract double operate(int firstNumber, int secondNumber);
}
class AddOperation extends AbstractOperation{
double operate(int firstNumber, int secondNumber){
return firstNumber + secondNumber;
}
}
class SubstractOperation extends AbstractOperation{
@Override
double operate(int firstNumber, int secondNumber){
return firstNumber - secondNumber;
}
}
class MultiplyOperation extends AbstractOperation{
@Override
double operate(int firstNumber, int secondNumber){
return firstNumber * secondNumber;
}
}
class DivideOperation extends AbstractOperation{
@Override
double operate(int firstNumber, int secondNumber){
return firstNumber / secondNumber;
}
}
2. 4주차 숙제 : 예외처리 한 계산기 만들기
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Main {
public static void main(String[] args) throws Exception {
boolean calculateEnded = false;
// 구현 2.
CalculatorApp calculatorApp = new CalculatorApp();
calculatorApp.start();
}
}
class Calculator {
private int firstNumber;
private int secondNumber;
private AbstractOperation operation;
public Calculator(AbstractOperation operation) {
this.operation = operation;
}
public Calculator() {
}
public void setOperation(AbstractOperation operation) {
this.operation = operation;
}
public void setFirstNumber(int firstNumber) {
this.firstNumber = firstNumber;
}
public void setSecondNumber(int secondNumber) {
this.secondNumber = secondNumber;
}
public double calculate() {
double answer = 0;
answer = operation.operate(this.firstNumber, this.secondNumber);
return answer;
}
}
class BadInputException extends Exception {
public BadInputException(String type) {
super("잘못된 입력입니다! " + type + "을 입력해주세요!");
}
}
class Parser {
static final String OPERATION_REG = "[+\\-*/]";
static final String NUMBER_REG = "^[0-9]*$";
Pattern numberPattern = Pattern.compile(NUMBER_REG);
Pattern operationPattern = Pattern.compile(OPERATION_REG);
final Calculator calculator = new Calculator();
Parser parseFirstNum(String firstInput) throws BadInputException {
// 구현 1.
Matcher matcher = numberPattern.matcher(firstInput);
boolean isMatch = matcher.matches();
if (isMatch == false){
throw new BadInputException(firstInput);
} else {
this.calculator.setFirstNumber(Integer.valueOf(firstInput));
return this;
}
}
Parser parseSecondNum(String secondInput) throws BadInputException {
// 구현 1.
Matcher matcher = numberPattern.matcher(secondInput);
boolean isMatch = matcher.matches();
if (isMatch == false){
throw new BadInputException(secondInput);
} else{
this.calculator.setSecondNumber(Integer.valueOf(secondInput));
return this;
}
}
Parser parseOperator(String operationInput) throws BadInputException {
// 구현 1.
Matcher matcher = operationPattern.matcher(operationInput);
boolean isMatch = matcher.matches();
if (isMatch == false){
throw new BadInputException(operationInput);
} else{
switch (operationInput){
case "+":
this.calculator.setOperation(new AddOperation());
break;
case "-":
this.calculator.setOperation(new SubstractOperation());
break;
case "*":
this.calculator.setOperation(new MultiplyOperation());
break;
case "/":
this.calculator.setOperation(new DivideOperation());
break;
}
return this;
}
}
double executeCalculator() {
return calculator.calculate();
}
}
class CalculatorApp {
public static boolean start() throws Exception{
Parser parser = new Parser();
Scanner scanner = new Scanner(System.in);
System.out.println("첫번째 숫자를 입력해주세요!");
String firstInput = scanner.nextLine();
parser.parseFirstNum(firstInput);
System.out.println("연산자를 입력해주세요!");
String operator = scanner.nextLine();
parser.parseOperator(operator);
System.out.println("두번째 숫자를 입력해주세요!");
String secondInput = scanner.nextLine();
parser.parseSecondNum(secondInput);
System.out.println("연산 결과 : " + parser.executeCalculator());
return true;
}
}
abstract class AbstractOperation {
public abstract double operate(int firstNumber, int secondNumber);
}
class AddOperation extends AbstractOperation{
@Override
public double operate(int firstNumber, int secondNumber){
return firstNumber + secondNumber;
}
}
class SubstractOperation extends AbstractOperation{
@Override
public double operate(int firstNumber, int secondNumber){
return firstNumber - secondNumber;
}
}
class MultiplyOperation extends AbstractOperation{
@Override
public double operate(int firstNumber, int secondNumber){
return firstNumber * secondNumber;
}
}
class DivideOperation extends AbstractOperation{
@Override
public double operate(int firstNumber, int secondNumber){
return firstNumber / secondNumber;
}
}
3. 5주차 숙제 Stream 이용하기
정리
자바를 빠르게 흝었다. 내일은 스프링 강의 듣고 열심히 또 해봐야겠다.
KPT
Keep
열심히 하려고는 하는 것
Problem
조금 더 열심히 집중 못하는 것
Try
주어진 시간에 최선을 다하자.
소감
내 집중력에 내가 실망했다.
가끔 딴짓에 심취해서 몇 시간이 흐르는데 이런 시간을 줄여야겠다.
강의에 집중을 못해서 그나마 중요한 개념을 강의자료로 흝고 숙제 위주로 훅훅 지나쳤는데
스프링 강의를 다 듣고 나면 다시 살펴봐야겠다.
'verdantjuly > Today I Learned' 카테고리의 다른 글
TIL 20240302 (0) | 2024.03.02 |
---|---|
TIL 20240229 (0) | 2024.02.29 |
TIL 20240226 (2) | 2024.02.26 |
TIL 20240222 (0) | 2024.02.22 |
TIL 20240221 (0) | 2024.02.22 |