diff --git "a/6\354\243\274\354\260\250/\354\236\220\353\260\224 \352\270\260\354\264\210.md" "b/6\354\243\274\354\260\250/\354\236\220\353\260\224 \352\270\260\354\264\210.md" new file mode 100644 index 00000000..444a0aeb --- /dev/null +++ "b/6\354\243\274\354\260\250/\354\236\220\353\260\224 \352\270\260\354\264\210.md" @@ -0,0 +1,141 @@ +# [Java] 자바 기초(5) +## 1. 클래스 +### 1-1. 클래스란 +클래스란 **객체를 정의해놓은 것** 또는 **객체의 설계도, 틀** 이라고 정의할 수 있다. +클래스는 **객체를 생성하는 데 사용**되며, 객체는 **클래스에 정의된 대로 생성**된다. + +클래스는 객체의 상태를 나타내는 필드(field)와 객체의 행동을 나타내는 메소드(method)로 구성된다. +필드란 클래스에 포함된 변수(variable)를 의미하며, 메소드란 어떠한 특정 작업을 수행하기 위한 명령문의 집합이라고 할 수 있다. + +> ### 클래스 작성 규칙 +> 1. 하나 이상의 문자로 이루어져야 한다. +> 2. 첫 번째 글자에는 숫자가 올 수 없다. +> 3. ```$```, ```,```, ```_``` 외에는 특수문자를 사용할 수 없다. +> 4. 자바 명령어, 키워드는 사용할 수 없다. (while, for, int 등 사용불가) +> 5. 이름은 대소문자를 구분한다. (보통 첫번째 글자는 대문자로 작성) + +### 1-2. 클래스 정의하는 방법 +클래스를 정의하기 위해서는 우선 **"클래스명.java"**로 소스 파일을 생성해야 한다. + +소스 파일 생성 후 아래처럼 클래스를 선언할 수 있다. +```java +public class 클래스명 { + // 내용 +} +``` +이때 파일 이름과 동일한 이름의 클래스에만 public 접근 지정자를 붙일 수 있다. +하나의 소스 파일에 2개 이상의 클래스가 있을 경우 컴파일 시 바이트 코드 파일(.class)은 선언한 클래스의 개수만큼 생긴다. 가급적이면 소스 파일 하나당 하나의 클래스를 선언하는 것이 좋다. + +## 2. 객체 +### 2-1. 객체란 +객체란 사전적인 정의로 실제 존재하는 것이다. +객체지향 이론에서는 사물과 같은 유형적인 것뿐만 아니라, 개념이나 논리와 같은 무형적인 것들도 객체로 간주한다. 프로그래밍에서의 객체는 클래스에 정의된 내용대로 메모리에 생성된 것을 뜻한다. + +클래스로부터 객체를 만드는 과정을 **클래스의 인스턴스화(instantiate)** 라고 하며, 어떤 클래스로부터 만들어진 객체를 그 **클래스의 인스턴스(instance)** 라고 한다. +인스턴스는 객체와 같은 의미지만 **객체는 모든 인스턴스를 대표하는 포괄적인 의미를 갖고 있으며, 인스턴스는 어떤 클래스로부터 만들어진 것인지를 강조하며, 보다 구체적인 의미** 를 갖고 있다. + +### 2-2. 객체 만드는 방법 +객체를 생성하기 위해서는 **new 키워드** 를 사용한다. ```new```는 클래스로부터 객체를 생성시키는 연산자이다. + +```new``` 연산자로 생성된 객체는 메모리 **힙(heap) 영역에 생성** 된다. +```new``` 연산자는 객체를 생성시킨 후, **객체의 주소를 리턴** 하도록 되어 있다. +이 주소를 참조 타입인 클래스 변수에 저장해 두면, 변수를 통해 객체를 사용할 수 있다. + +```java +클래스명 변수명 = new 클래스명(); +``` +클래스의 객체를 참조하기 위한 참조 변수를 선언한 후 클래스의 객체를 생성해 객체의 주소를 참조 변수에 저장한다. + +인스턴스는 참조 변수를 통해 다룰 수 있으며, 참조 변수의 타입은 인스턴스의 타입과 일치해야 한다. + +> 클래스는 객체를 생성하는 데 사용될 뿐, 객체 그 자체는 아니다. +클래스는 한 번만 잘 만들어 놓으면 매번 객체를 생성할 때마다 어떻게 객체를 만들어야 할 지를 고민하지 않아도 된다. 그냥 클래스로부터 객체를 생성해서 사용하기만 하면 되기 때문이다. + +## 3. 메소드 +### 3-1. 메소드란 +자바에서 클래스는 멤버(member)로 속성을 표현하는 필드(field)와 기능을 표현하는 메소드(method)를 가진다. +그 중에서 메소드란 어떠한 특정 작업을 수행하기 위한 명령문의 집합이라고 할 수 있다. + +### 3-2. 메소드 정의하는 방법 +```java +접근지정자 반환타입 메소드이름(매개변수목록) { + // 내용 +} +``` +자바에서는 하나의 클래스에 같은 이름의 메소드를 둘 이상 정의할 수 없다. 하지만 메소드 오버로딩(overloading)을 이용하면, 같은 이름의 메소드를 중복하여 정의할 수 있다. + +> **메소드 오버로딩**이란 매개변수의 개수나 타입을 다르게 하여 같은 이름의 또 다른 메소드를 작성하는 것이다. + +이러한 메소드 오버로딩을 사용함으로써 메소드에 사용되는 이름을 절약할 수 있다. 또한, 메소드를 호출할 때 전달해야 할 매개변수의 타입이나 개수에 대해 크게 신경을 쓰지 않고 호출할 수 있게 된다. + +## 4. 생성자 +### 4-1. 생성자란 +생성자는 ```new``` 연산자를 통해 인스턴스화되어 객체를 생성할 때 반드시 호출이 되고 제일 먼저 실행되는 일종의 메소드라고 생각할 수 있다. (메소드와 비슷하지만 그 의미가 같은 것은 아니다.) + +생성자는 멤버 변수를 초기화하는 역할을 한다. +생성자를 선언하지 않으면 컴파일러에서 자동으로 기본 생성자(Default Constructor)를 생성해준다. + +생성자는 다음과 같은 특징을 가진다. + +1. 생성자는 반환값이 없지만, 반환 타입을 void형으로 선언하지 않는다. +2. 생성자는 초기화를 위한 데이터를 인수로 전달받을 수 있다. +3. 객체를 초기화하는 방법이 여러 개 존재할 경우에는 하나의 클래스가 여러 개의 생성자를 가질 수 있다. + +### 4-2. 생성자 정의하는 방법 +객체를 선언할 때 보통 아래와 같이 선언한다. +```java +클래스명 객체명 = new 클래스명(); +``` +여기서 ```클래스명()``` 부분이 생성자다. 즉, 우리는 객체를 만들면서 무의식적으로 생성자를 사용하고 있었던 것이다. + +이때 클래스명 뒤의 소괄호에는 아무것도 적지 않는 게 대부분이지만, 이 안에 어떤 값을 넣어줄 수 있다. + +생성자에 값을 넣는 것은 클래스의 객체를 만들 때 내가 입력해준 값을 갖는 객체를 만든다는 뜻이다. + +
+ +객체 선언과 생성자에 대해 정리하면 아래와 같다. + +![객체 선언 정리](https://images.velog.io/images/minide/post/e5dea634-6ee1-4cd4-9d5a-a969fb78ed4a/%EC%8A%A4%ED%81%AC%EB%A6%B0%EC%83%B7%202021-02-23%20%EC%98%A4%ED%9B%84%204.47.24.png) + +## 5. this 키워드 +### 5-1. this 키워드란 +this 키워드란 현재 객체(자기 자신)을 참조하는 키워드이다. +아래 this 키워드 사용 방법을 통해 이 말이 무슨 의미인지 이해해보자. + +### 5-2. this 키워드 사용하는 방법 +```java +public class Car { + String name; + + public Car(String n) { + name = n; + } +} +``` +위와 같이 Car라는 클래스가 있다고 하자. Car 클래스는 String 변수와 String 변수를 받는 생성자를 갖고 있다. + +그런데 매개변수 명이 n이라 이 변수가 무엇을 의미하는지 감이 안온다. 따라서 **n**이라는 매개변수를 **name**으로 바꿨다. + +```java +public class Car { + String name; + + public Car(String name) { + name = name; + } +} +``` +그런데 이렇게 쓰면 name이라는 변수명이 두 번 나오기 때문에 뭐가 다른지 정확히 알 수 없다. +이를 구분하기 위해 사용되는 것이 ```this```이다. + +```java +public class Car { + String name; + + public Car(String name) { + this.name = name; + } +} +``` +이렇게 적으면 왼쪽의 this.name은 클래스의 name 변수이고, 오른쪽의 name은 매개변수라는 것을 알 수 있다. \ No newline at end of file diff --git "a/6\354\243\274\354\260\250/\355\224\204\353\241\234\352\267\270\353\236\230\353\260\215 \352\263\274\354\240\234/build.gradle" "b/6\354\243\274\354\260\250/\355\224\204\353\241\234\352\267\270\353\236\230\353\260\215 \352\263\274\354\240\234/build.gradle" new file mode 100644 index 00000000..353aec80 --- /dev/null +++ "b/6\354\243\274\354\260\250/\355\224\204\353\241\234\352\267\270\353\236\230\353\260\215 \352\263\274\354\240\234/build.gradle" @@ -0,0 +1,19 @@ +plugins { + id 'java' +} + +group 'org.javastudy' +version '1.0-SNAPSHOT' + +repositories { + mavenCentral() +} + +dependencies { + testImplementation 'org.junit.jupiter:junit-jupiter-api:5.6.0' + testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine' +} + +test { + useJUnitPlatform() +} \ No newline at end of file diff --git "a/6\354\243\274\354\260\250/\355\224\204\353\241\234\352\267\270\353\236\230\353\260\215 \352\263\274\354\240\234/docs/Calculator.md" "b/6\354\243\274\354\260\250/\355\224\204\353\241\234\352\267\270\353\236\230\353\260\215 \352\263\274\354\240\234/docs/Calculator.md" new file mode 100644 index 00000000..998860f1 --- /dev/null +++ "b/6\354\243\274\354\260\250/\355\224\204\353\241\234\352\267\270\353\236\230\353\260\215 \352\263\274\354\240\234/docs/Calculator.md" @@ -0,0 +1,59 @@ +# 프로그래밍 과제1 - 문자열 계산기 +## 객체 책임(아는 것과 하는 것) +### 입력 +- 아는 것 +- 하는 것 + - 사용자에게 입력을 요구 + +### 출력 +- 아는 것 +- 하는 것 + - 입력받은 것을 화면에 출력 + +### 계산 시작 +- 아는 것 +- 하는 것 + - 계산기 실행 + +### 계산기 +- 아는 것 + - 첫번째 원소, 다음 연산자, 다음 원소 + - 공식 절단자, 연산자, 숫자 객체 +- 하는 것 + - 출력을 요구 + +### 숫자 +- 아는 것 + - 보장된 숫자(정수) +- 하는 것 + - 객체 생성 + +### 연산자 +- 아는 것 + - 보장된 연산자(+, -, *, /) +- 하는 것 + - 객체 생성 + +### 연산자(들) +- 아는 것 + - 숫자, 연산자 +- 하는 것 + - 저장된 결과 값 반환 + +### 공식 절단자 +- 아는 것 + - 문자 +- 하는 것 + - 문자를 검증하여 반환 + +--- +## 구현할 기능 + +- [x] 객체 모델링 +- [x] 메인 화면 출력 +- [x] 보장된 숫자 입력 + - 보장된 숫자(정수) +- [x] 보장된 숫자가 아닐 시 에러 발생 +- [x] 보장된 연산자 입력 + - 보장된 연산자(+, -, *, /) +- [x] 보장된 연산자가 아닐 시 에러 발생 \ No newline at end of file diff --git "a/6\354\243\274\354\260\250/\355\224\204\353\241\234\352\267\270\353\236\230\353\260\215 \352\263\274\354\240\234/gradle/wrapper/gradle-wrapper.jar" "b/6\354\243\274\354\260\250/\355\224\204\353\241\234\352\267\270\353\236\230\353\260\215 \352\263\274\354\240\234/gradle/wrapper/gradle-wrapper.jar" new file mode 100644 index 00000000..e708b1c0 Binary files /dev/null and "b/6\354\243\274\354\260\250/\355\224\204\353\241\234\352\267\270\353\236\230\353\260\215 \352\263\274\354\240\234/gradle/wrapper/gradle-wrapper.jar" differ diff --git "a/6\354\243\274\354\260\250/\355\224\204\353\241\234\352\267\270\353\236\230\353\260\215 \352\263\274\354\240\234/gradle/wrapper/gradle-wrapper.properties" "b/6\354\243\274\354\260\250/\355\224\204\353\241\234\352\267\270\353\236\230\353\260\215 \352\263\274\354\240\234/gradle/wrapper/gradle-wrapper.properties" new file mode 100644 index 00000000..be52383e --- /dev/null +++ "b/6\354\243\274\354\260\250/\355\224\204\353\241\234\352\267\270\353\236\230\353\260\215 \352\263\274\354\240\234/gradle/wrapper/gradle-wrapper.properties" @@ -0,0 +1,5 @@ +distributionBase=GRADLE_USER_HOME +distributionPath=wrapper/dists +distributionUrl=https\://services.gradle.org/distributions/gradle-6.7-bin.zip +zipStoreBase=GRADLE_USER_HOME +zipStorePath=wrapper/dists diff --git "a/6\354\243\274\354\260\250/\355\224\204\353\241\234\352\267\270\353\236\230\353\260\215 \352\263\274\354\240\234/gradlew" "b/6\354\243\274\354\260\250/\355\224\204\353\241\234\352\267\270\353\236\230\353\260\215 \352\263\274\354\240\234/gradlew" new file mode 100755 index 00000000..4f906e0c --- /dev/null +++ "b/6\354\243\274\354\260\250/\355\224\204\353\241\234\352\267\270\353\236\230\353\260\215 \352\263\274\354\240\234/gradlew" @@ -0,0 +1,185 @@ +#!/usr/bin/env sh + +# +# Copyright 2015 the original author or authors. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# https://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +############################################################################## +## +## Gradle start up script for UN*X +## +############################################################################## + +# Attempt to set APP_HOME +# Resolve links: $0 may be a link +PRG="$0" +# Need this for relative symlinks. +while [ -h "$PRG" ] ; do + ls=`ls -ld "$PRG"` + link=`expr "$ls" : '.*-> \(.*\)$'` + if expr "$link" : '/.*' > /dev/null; then + PRG="$link" + else + PRG=`dirname "$PRG"`"/$link" + fi +done +SAVED="`pwd`" +cd "`dirname \"$PRG\"`/" >/dev/null +APP_HOME="`pwd -P`" +cd "$SAVED" >/dev/null + +APP_NAME="Gradle" +APP_BASE_NAME=`basename "$0"` + +# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"' + +# Use the maximum available, or set MAX_FD != -1 to use that value. +MAX_FD="maximum" + +warn () { + echo "$*" +} + +die () { + echo + echo "$*" + echo + exit 1 +} + +# OS specific support (must be 'true' or 'false'). +cygwin=false +msys=false +darwin=false +nonstop=false +case "`uname`" in + CYGWIN* ) + cygwin=true + ;; + Darwin* ) + darwin=true + ;; + MINGW* ) + msys=true + ;; + NONSTOP* ) + nonstop=true + ;; +esac + +CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar + + +# Determine the Java command to use to start the JVM. +if [ -n "$JAVA_HOME" ] ; then + if [ -x "$JAVA_HOME/jre/sh/java" ] ; then + # IBM's JDK on AIX uses strange locations for the executables + JAVACMD="$JAVA_HOME/jre/sh/java" + else + JAVACMD="$JAVA_HOME/bin/java" + fi + if [ ! -x "$JAVACMD" ] ; then + die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." + fi +else + JAVACMD="java" + which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." +fi + +# Increase the maximum file descriptors if we can. +if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then + MAX_FD_LIMIT=`ulimit -H -n` + if [ $? -eq 0 ] ; then + if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then + MAX_FD="$MAX_FD_LIMIT" + fi + ulimit -n $MAX_FD + if [ $? -ne 0 ] ; then + warn "Could not set maximum file descriptor limit: $MAX_FD" + fi + else + warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" + fi +fi + +# For Darwin, add options to specify how the application appears in the dock +if $darwin; then + GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" +fi + +# For Cygwin or MSYS, switch paths to Windows format before running java +if [ "$cygwin" = "true" -o "$msys" = "true" ] ; then + APP_HOME=`cygpath --path --mixed "$APP_HOME"` + CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` + + JAVACMD=`cygpath --unix "$JAVACMD"` + + # We build the pattern for arguments to be converted via cygpath + ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` + SEP="" + for dir in $ROOTDIRSRAW ; do + ROOTDIRS="$ROOTDIRS$SEP$dir" + SEP="|" + done + OURCYGPATTERN="(^($ROOTDIRS))" + # Add a user-defined pattern to the cygpath arguments + if [ "$GRADLE_CYGPATTERN" != "" ] ; then + OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" + fi + # Now convert the arguments - kludge to limit ourselves to /bin/sh + i=0 + for arg in "$@" ; do + CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` + CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option + + if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition + eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` + else + eval `echo args$i`="\"$arg\"" + fi + i=`expr $i + 1` + done + case $i in + 0) set -- ;; + 1) set -- "$args0" ;; + 2) set -- "$args0" "$args1" ;; + 3) set -- "$args0" "$args1" "$args2" ;; + 4) set -- "$args0" "$args1" "$args2" "$args3" ;; + 5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; + 6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; + 7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; + 8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; + 9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; + esac +fi + +# Escape application args +save () { + for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done + echo " " +} +APP_ARGS=`save "$@"` + +# Collect all arguments for the java command, following the shell quoting and substitution rules +eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS" + +exec "$JAVACMD" "$@" diff --git "a/6\354\243\274\354\260\250/\355\224\204\353\241\234\352\267\270\353\236\230\353\260\215 \352\263\274\354\240\234/gradlew.bat" "b/6\354\243\274\354\260\250/\355\224\204\353\241\234\352\267\270\353\236\230\353\260\215 \352\263\274\354\240\234/gradlew.bat" new file mode 100644 index 00000000..ac1b06f9 --- /dev/null +++ "b/6\354\243\274\354\260\250/\355\224\204\353\241\234\352\267\270\353\236\230\353\260\215 \352\263\274\354\240\234/gradlew.bat" @@ -0,0 +1,89 @@ +@rem +@rem Copyright 2015 the original author or authors. +@rem +@rem Licensed under the Apache License, Version 2.0 (the "License"); +@rem you may not use this file except in compliance with the License. +@rem You may obtain a copy of the License at +@rem +@rem https://www.apache.org/licenses/LICENSE-2.0 +@rem +@rem Unless required by applicable law or agreed to in writing, software +@rem distributed under the License is distributed on an "AS IS" BASIS, +@rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +@rem See the License for the specific language governing permissions and +@rem limitations under the License. +@rem + +@if "%DEBUG%" == "" @echo off +@rem ########################################################################## +@rem +@rem Gradle startup script for Windows +@rem +@rem ########################################################################## + +@rem Set local scope for the variables with windows NT shell +if "%OS%"=="Windows_NT" setlocal + +set DIRNAME=%~dp0 +if "%DIRNAME%" == "" set DIRNAME=. +set APP_BASE_NAME=%~n0 +set APP_HOME=%DIRNAME% + +@rem Resolve any "." and ".." in APP_HOME to make it shorter. +for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi + +@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m" + +@rem Find java.exe +if defined JAVA_HOME goto findJavaFromJavaHome + +set JAVA_EXE=java.exe +%JAVA_EXE% -version >NUL 2>&1 +if "%ERRORLEVEL%" == "0" goto execute + +echo. +echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. +echo. +echo Please set the JAVA_HOME variable in your environment to match the +echo location of your Java installation. + +goto fail + +:findJavaFromJavaHome +set JAVA_HOME=%JAVA_HOME:"=% +set JAVA_EXE=%JAVA_HOME%/bin/java.exe + +if exist "%JAVA_EXE%" goto execute + +echo. +echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% +echo. +echo Please set the JAVA_HOME variable in your environment to match the +echo location of your Java installation. + +goto fail + +:execute +@rem Setup the command line + +set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar + + +@rem Execute Gradle +"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %* + +:end +@rem End local scope for the variables with windows NT shell +if "%ERRORLEVEL%"=="0" goto mainEnd + +:fail +rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of +rem the _cmd.exe /c_ return code! +if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 +exit /b 1 + +:mainEnd +if "%OS%"=="Windows_NT" endlocal + +:omega diff --git "a/6\354\243\274\354\260\250/\355\224\204\353\241\234\352\267\270\353\236\230\353\260\215 \352\263\274\354\240\234/settings.gradle" "b/6\354\243\274\354\260\250/\355\224\204\353\241\234\352\267\270\353\236\230\353\260\215 \352\263\274\354\240\234/settings.gradle" new file mode 100644 index 00000000..b52ead54 --- /dev/null +++ "b/6\354\243\274\354\260\250/\355\224\204\353\241\234\352\267\270\353\236\230\353\260\215 \352\263\274\354\240\234/settings.gradle" @@ -0,0 +1,3 @@ +rootProject.name = 'javastudy' +include 'main' + diff --git "a/6\354\243\274\354\260\250/\355\224\204\353\241\234\352\267\270\353\236\230\353\260\215 \352\263\274\354\240\234/src/main/java/Application.java" "b/6\354\243\274\354\260\250/\355\224\204\353\241\234\352\267\270\353\236\230\353\260\215 \352\263\274\354\240\234/src/main/java/Application.java" new file mode 100644 index 00000000..3ac890ee --- /dev/null +++ "b/6\354\243\274\354\260\250/\355\224\204\353\241\234\352\267\270\353\236\230\353\260\215 \352\263\274\354\240\234/src/main/java/Application.java" @@ -0,0 +1,8 @@ +import service.CalculatorStarter; + +public class Application { + public static void main(String[] args) throws Exception { + CalculatorStarter calculator = new CalculatorStarter(); + calculator.start(); + } +} \ No newline at end of file diff --git "a/6\354\243\274\354\260\250/\355\224\204\353\241\234\352\267\270\353\236\230\353\260\215 \352\263\274\354\240\234/src/main/java/domain/Number.java" "b/6\354\243\274\354\260\250/\355\224\204\353\241\234\352\267\270\353\236\230\353\260\215 \352\263\274\354\240\234/src/main/java/domain/Number.java" new file mode 100644 index 00000000..eada9c69 --- /dev/null +++ "b/6\354\243\274\354\260\250/\355\224\204\353\241\234\352\267\270\353\236\230\353\260\215 \352\263\274\354\240\234/src/main/java/domain/Number.java" @@ -0,0 +1,22 @@ +package domain; + +public class Number { + + private final int value; + + public Number(int value) { + this.value = value; + } + + public boolean validate() { + final int IMPOSSIBLE_DIVISOR = 0; + if (value == IMPOSSIBLE_DIVISOR) { + return true; + } + return false; + } + + public int getValue() { + return value; + } +} diff --git "a/6\354\243\274\354\260\250/\355\224\204\353\241\234\352\267\270\353\236\230\353\260\215 \352\263\274\354\240\234/src/main/java/domain/Operator.java" "b/6\354\243\274\354\260\250/\355\224\204\353\241\234\352\267\270\353\236\230\353\260\215 \352\263\274\354\240\234/src/main/java/domain/Operator.java" new file mode 100644 index 00000000..f0c2ace7 --- /dev/null +++ "b/6\354\243\274\354\260\250/\355\224\204\353\241\234\352\267\270\353\236\230\353\260\215 \352\263\274\354\240\234/src/main/java/domain/Operator.java" @@ -0,0 +1,10 @@ +package domain; + +public class Operator { + + final String value; + + public Operator(String value) { + this.value = value; + } +} diff --git "a/6\354\243\274\354\260\250/\355\224\204\353\241\234\352\267\270\353\236\230\353\260\215 \352\263\274\354\240\234/src/main/java/domain/Operators.java" "b/6\354\243\274\354\260\250/\355\224\204\353\241\234\352\267\270\353\236\230\353\260\215 \352\263\274\354\240\234/src/main/java/domain/Operators.java" new file mode 100644 index 00000000..91420635 --- /dev/null +++ "b/6\354\243\274\354\260\250/\355\224\204\353\241\234\352\267\270\353\236\230\353\260\215 \352\263\274\354\240\234/src/main/java/domain/Operators.java" @@ -0,0 +1,37 @@ +package domain; + +import java.util.Arrays; +import java.util.function.BiFunction; + +public enum Operators { + + ADD("+", (num1, num2) -> num1 + num2), + SUB("-", (num1, num2) -> num1 - num2), + MUL("*", (num1, num2) -> num1 * num2), + DIV("/", (num1, num2) -> num1 / num2); + + private final String operator; + private BiFunction expression; + + Operators(String operator, BiFunction expression) { + this.operator = operator; + this.expression = expression; + } + + public static int operate(String operator, int num1, int num2) { + return getOperator(operator).expression.apply(num1, num2); + } + + private static Operators getOperator(String operator) { + return Arrays.stream(values()) + .filter(operators -> operators.operator.equals(operator)) + .findFirst().orElseThrow(() -> new IllegalArgumentException("잘못된 연산자가 입력되었습니다.")); + } + + public static int operateFormula(Number number1, Operator operator, Number number2) { + if (operator.value.equals("/") && number2.validate()) { + throw new IllegalArgumentException("숫자 0으로 나눌 수 없습니다."); + } + return operate(operator.value, number1.getValue(), number2.getValue()); + } +} diff --git "a/6\354\243\274\354\260\250/\355\224\204\353\241\234\352\267\270\353\236\230\353\260\215 \352\263\274\354\240\234/src/main/java/service/Calculator.java" "b/6\354\243\274\354\260\250/\355\224\204\353\241\234\352\267\270\353\236\230\353\260\215 \352\263\274\354\240\234/src/main/java/service/Calculator.java" new file mode 100644 index 00000000..de7f533c --- /dev/null +++ "b/6\354\243\274\354\260\250/\355\224\204\353\241\234\352\267\270\353\236\230\353\260\215 \352\263\274\354\240\234/src/main/java/service/Calculator.java" @@ -0,0 +1,25 @@ +package service; + +import domain.Number; +import domain.Operator; +import domain.Operators; +import view.PrinterOutputView; + +public class Calculator { + + private static final int FIRST_ARGUMENT = 0; + private static final int NEXT_ARGUMENT = 1; + private static final int NEXT_OPERATOR = 2; + private static int result; + + public static void calculateFormula(String[] formula) { + result = FormulaSplitter.toInt(formula[FIRST_ARGUMENT]); + for (int i = 1; i < formula.length; i += NEXT_OPERATOR) { + Number calculatedResult = new Number(result); + Operator operator = new Operator(formula[i]); + Number nextArgument = new Number(FormulaSplitter.toInt(formula[i + NEXT_ARGUMENT])); + result = Operators.operateFormula(calculatedResult, operator, nextArgument); + } + PrinterOutputView.printOutputView(result); + } +} diff --git "a/6\354\243\274\354\260\250/\355\224\204\353\241\234\352\267\270\353\236\230\353\260\215 \352\263\274\354\240\234/src/main/java/service/CalculatorStarter.java" "b/6\354\243\274\354\260\250/\355\224\204\353\241\234\352\267\270\353\236\230\353\260\215 \352\263\274\354\240\234/src/main/java/service/CalculatorStarter.java" new file mode 100644 index 00000000..cd7d2bab --- /dev/null +++ "b/6\354\243\274\354\260\250/\355\224\204\353\241\234\352\267\270\353\236\230\353\260\215 \352\263\274\354\240\234/src/main/java/service/CalculatorStarter.java" @@ -0,0 +1,12 @@ +package service; + +import view.ScannerInputView; + +import java.io.IOException; + +public class CalculatorStarter { + + public void start() throws IOException { + Calculator.calculateFormula(ScannerInputView.getFormular()); + } +} diff --git "a/6\354\243\274\354\260\250/\355\224\204\353\241\234\352\267\270\353\236\230\353\260\215 \352\263\274\354\240\234/src/main/java/service/FormulaSplitter.java" "b/6\354\243\274\354\260\250/\355\224\204\353\241\234\352\267\270\353\236\230\353\260\215 \352\263\274\354\240\234/src/main/java/service/FormulaSplitter.java" new file mode 100644 index 00000000..570c1eeb --- /dev/null +++ "b/6\354\243\274\354\260\250/\355\224\204\353\241\234\352\267\270\353\236\230\353\260\215 \352\263\274\354\240\234/src/main/java/service/FormulaSplitter.java" @@ -0,0 +1,13 @@ +package service; + +public class FormulaSplitter { + + public static int toInt(String formularArguement) { + try { + int number = Integer.parseInt(formularArguement); + return number; + } catch (IllegalArgumentException e) { + throw new IllegalArgumentException("숫자가 아닌 문자가 입력되었습니다."); + } + } +} diff --git "a/6\354\243\274\354\260\250/\355\224\204\353\241\234\352\267\270\353\236\230\353\260\215 \352\263\274\354\240\234/src/main/java/view/PrinterOutputView.java" "b/6\354\243\274\354\260\250/\355\224\204\353\241\234\352\267\270\353\236\230\353\260\215 \352\263\274\354\240\234/src/main/java/view/PrinterOutputView.java" new file mode 100644 index 00000000..34dc64e3 --- /dev/null +++ "b/6\354\243\274\354\260\250/\355\224\204\353\241\234\352\267\270\353\236\230\353\260\215 \352\263\274\354\240\234/src/main/java/view/PrinterOutputView.java" @@ -0,0 +1,8 @@ +package view; + +public class PrinterOutputView{ + + public static void printOutputView(int result) { + System.out.println("계산 값 : " + result); + } +} diff --git "a/6\354\243\274\354\260\250/\355\224\204\353\241\234\352\267\270\353\236\230\353\260\215 \352\263\274\354\240\234/src/main/java/view/ScannerInputView.java" "b/6\354\243\274\354\260\250/\355\224\204\353\241\234\352\267\270\353\236\230\353\260\215 \352\263\274\354\240\234/src/main/java/view/ScannerInputView.java" new file mode 100644 index 00000000..54138793 --- /dev/null +++ "b/6\354\243\274\354\260\250/\355\224\204\353\241\234\352\267\270\353\236\230\353\260\215 \352\263\274\354\240\234/src/main/java/view/ScannerInputView.java" @@ -0,0 +1,15 @@ +package view; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; + +public class ScannerInputView{ + + static BufferedReader bf = new BufferedReader(new InputStreamReader(System.in)); + + public static String[] getFormular() throws IOException { + String[] formularArguement = bf.readLine().split(" "); + return formularArguement; + } +} diff --git a/calculator/.idea/gradle.xml b/calculator/.idea/gradle.xml index ba1ec5c7..02b3e5bf 100644 --- a/calculator/.idea/gradle.xml +++ b/calculator/.idea/gradle.xml @@ -1,8 +1,11 @@ +