diff --git a/src/yuyeongwoo/report1/Report1_1 b/src/yuyeongwoo/report1/Report1_1 new file mode 100644 index 0000000..446a94c --- /dev/null +++ b/src/yuyeongwoo/report1/Report1_1 @@ -0,0 +1,3 @@ +2-4번 문제 + +정답 : a, b, c, d diff --git a/src/yuyeongwoo/report1/Report1_2 b/src/yuyeongwoo/report1/Report1_2 new file mode 100644 index 0000000..a2577fc --- /dev/null +++ b/src/yuyeongwoo/report1/Report1_2 @@ -0,0 +1,3 @@ +2-7번 문제 + +정답 : 12, true, 131, 51, 99, Java, 오류 \ No newline at end of file diff --git a/src/yuyeongwoo/report1/Report1_3.java b/src/yuyeongwoo/report1/Report1_3.java new file mode 100644 index 0000000..930b0ac --- /dev/null +++ b/src/yuyeongwoo/report1/Report1_3.java @@ -0,0 +1,21 @@ +package yuyeongwoo.report1; +// 2-8 번 문제 +public class Report1_3 { + public static void main(String[] args) { + int x = 1; + int y = 2; + int z = 3; + + // 정답 작성 + int a = x; + x = y; + y = z; + z = a; + // + + System.out.println("x = " + x); + System.out.println("y = " + y); + System.out.println("z = " + z); + } +} + diff --git a/src/yuyeongwoo/report2/Report2_1 b/src/yuyeongwoo/report2/Report2_1 new file mode 100644 index 0000000..4984b96 --- /dev/null +++ b/src/yuyeongwoo/report2/Report2_1 @@ -0,0 +1,4 @@ +3-1번 문제 + +정답: d, e + diff --git a/src/yuyeongwoo/report2/Report2_2.java b/src/yuyeongwoo/report2/Report2_2.java new file mode 100644 index 0000000..4ee5d8b --- /dev/null +++ b/src/yuyeongwoo/report2/Report2_2.java @@ -0,0 +1,30 @@ +package yuyeongwoo.report2; +// 3-2번 문제 +public class Report2_2 { + public static void main(String[] args) { + int x = 2; + int y = 5; + char c = 'A'; // 'A'의 문자코드는 65 + + System.out.println(y >= 5 || x < 0 && x > 2); + //결과: true, 이유: true || false && false 가 되고, && 연산자의 우선순위가 더 높기 때문에 true || false 가 된다. 따라서 true가 나온다. + System.out.println(y += 10 - x++); + //결과: 13, 이유: 위 식을 풀어쓰게 되면, y = y + 10 - x++ 가 된다. x는 후위형이기 때문에 값이 증가하지 않은 상태에서 식이 계산된다. 따라서 y = 5 + 10 - 2 -> y = 13 + System.out.println(x += 2); + //결과: 5, 이유: 이전 식에서 x의 값이 1증가했기 때문에 x = 3 + 2가 되어 x는 5가 된다. + System.out.println(!('A' <= c && c <= 'Z')); + //결과: false, 이유: c는 A와 Z사이에 존재하는 대문자이기 때문에 참이 된다. 하지만 논리부전연산자에 의해 false가 된다. + System.out.println('C' - c); + //결과: 2, 이유: 'C' 와 'A'는 정수로 변환되어 각 67, 65가 되고 따라서 67 - 65 = 2가 된다. + System.out.println('5' - '0'); + //결과: 5, 이유: 위와 같은 이유로 53 - 48이 되어 5가 답이다. + System.out.println(c + 1); + //결과: 66, 'A'가 정수 65로 변환되고 65 + 1 = 66이 된다. + System.out.println(++c); + //결과: B, 이유: 'A'의 문자코드 값이 1증가되고, 66이 된다. 문자코드 66에 해당하는 문자 B를 출력한다. + System.out.println(c++); + //결과: B, 이유: 후위형이기 때문에 위에서 증가된 값 B가 출력되고 1증가한다. + System.out.println(c); + //결과: C, 이유: 위에서 1증가했기 때문에 문자코드 67에 해당하는 C가 출력된다. + } +} diff --git a/src/yuyeongwoo/report2/Report2_3.java b/src/yuyeongwoo/report2/Report2_3.java new file mode 100644 index 0000000..fcec592 --- /dev/null +++ b/src/yuyeongwoo/report2/Report2_3.java @@ -0,0 +1,9 @@ +package yuyeongwoo.report2; +// 3-3번 문제 +public class Report2_3 { + public static void main(String[] args) { + int num = 456; + //정답 + System.out.println(num / 100 * 100); + } +} diff --git a/src/yuyeongwoo/report2/Report2_4.java b/src/yuyeongwoo/report2/Report2_4.java new file mode 100644 index 0000000..0639583 --- /dev/null +++ b/src/yuyeongwoo/report2/Report2_4.java @@ -0,0 +1,11 @@ +package yuyeongwoo.report2; +// 3-4번 문제 +public class Report2_4 { + public static void main(String[] args) { + int numOfApples = 123; + int sizeOfBucket = 10; + //정답 + int numOfbucket = numOfApples / sizeOfBucket + (numOfApples % 10 > 0 ? 1 : 0); + System.out.println("필요한 바구니의 수 : " + numOfbucket); + } +} diff --git a/src/yuyeongwoo/report2/Report2_5.java b/src/yuyeongwoo/report2/Report2_5.java new file mode 100644 index 0000000..dc6b02e --- /dev/null +++ b/src/yuyeongwoo/report2/Report2_5.java @@ -0,0 +1,9 @@ +package yuyeongwoo.report2; +// 3-5번 문제 +public class Report2_5 { + public static void main(String[] args) { + int num = 10; + //정답 + System.out.println(num > 0 ? "양수" : num < 0 ? "음수" : "0"); + } +} diff --git a/src/yuyeongwoo/report2/Report2_6.java b/src/yuyeongwoo/report2/Report2_6.java new file mode 100644 index 0000000..c6a58b3 --- /dev/null +++ b/src/yuyeongwoo/report2/Report2_6.java @@ -0,0 +1,12 @@ +package yuyeongwoo.report2; +// 3-6번 문제 +public class Report2_6 { + public static void main(String[] args) { + int fahrenheit = 100; + //정답 + float celcius = (int) (5 / 9f * (fahrenheit - 32) * 100 + 0.5) / 100f ; + + System.out.println("fahrenheit = " + fahrenheit); + System.out.println("celcius = " + celcius); + } +} diff --git a/src/yuyeongwoo/report3/Report3_1 b/src/yuyeongwoo/report3/Report3_1 new file mode 100644 index 0000000..a35a2c3 --- /dev/null +++ b/src/yuyeongwoo/report3/Report3_1 @@ -0,0 +1,26 @@ +4-1번 문제 + +정답 : +//int형 변수 x가 10보다 크고 20보다 작을 때 true인 조건식 +- 10 < x && x < 20 + +//char형 변수 ch가 공백이나 탭이 아닐 때 true인 조건식 +- !(ch == ' ' || ch == \t) + +//char형 변수 ch가 'x' 또는 'X'일 때 true인 조건식 +- ch == 'x' || ch == 'X' + +//char형 변수 ch가 숫자('0'~'9')일 때 true인 조건식 +- '0' <= ch && ch <= '9' + +//char형 변수 ch가 영문자(대문자 또는 소문자)일 때 true인 조건식 +- ('a' <= ch && ch <= 'z') || ('A' <= ch && ch <= 'Z') + +//int형 변수 year가 400으로 나눠떨어지거나 또는 4로 나눠떨어지고 100으로 나눠떨어지지 않을때 true인 조건식 +- year % 400 == 0 || year % 4 == 0 && year % 100 != 0 + +//boolean형 변수 powerOn이 false일 때 true인 조건식 +- !powerOn + +//문자열 참조변수 str이 "yes"일 때 true인 조건식 +- str.equals("yes") diff --git a/src/yuyeongwoo/report3/Report3_10.java b/src/yuyeongwoo/report3/Report3_10.java new file mode 100644 index 0000000..3a0b3f3 --- /dev/null +++ b/src/yuyeongwoo/report3/Report3_10.java @@ -0,0 +1,28 @@ +package yuyeongwoo.report3; + +import java.util.Scanner; + +// 4-10번 문제 +public class Report3_10 { + public static void main(String[] args) { + int answer = (int) (Math.random() * 100) + 1; + int input = 0; + int count = 0; + + Scanner s = new Scanner(System.in); + do { + count++; + System.out.print("1과 100사이의 값을 입력하세요. : "); + input = s.nextInt(); + if (answer > input) { + System.out.println("더 큰 수를 입력하세요."); + } else if (answer < input) { + System.out.println("더 작은 수를 입력하세요."); + } else { + System.out.println("맞혔습니다."); + System.out.println("시도횟수는 " + count + "번 입니다."); + break; + } + } while (true); + } +} diff --git a/src/yuyeongwoo/report3/Report3_2.java b/src/yuyeongwoo/report3/Report3_2.java new file mode 100644 index 0000000..770a11b --- /dev/null +++ b/src/yuyeongwoo/report3/Report3_2.java @@ -0,0 +1,14 @@ +package yuyeongwoo.report3; +// 4-2번 문제 +public class Report3_2 { + public static void main(String[] args) { + int sum = 0; + //정답 + for (int i = 1; i < 20; i++) { + if (!(i % 2 == 0 || i % 3 == 0)) { + sum += i; + } + } + System.out.println("sum = " + sum); + } +} diff --git a/src/yuyeongwoo/report3/Report3_3.java b/src/yuyeongwoo/report3/Report3_3.java new file mode 100644 index 0000000..54c77df --- /dev/null +++ b/src/yuyeongwoo/report3/Report3_3.java @@ -0,0 +1,14 @@ +package yuyeongwoo.report3; +// 4-3번 문제 +public class Report3_3 { + public static void main(String[] args) { + int sum = 0; + int totalSum = 0; + //정답 + for (int i = 1; i <= 10; i++) { + sum += i; + totalSum += sum; + } + System.out.println("totalSum = " + totalSum); + } +} diff --git a/src/yuyeongwoo/report3/Report3_4.java b/src/yuyeongwoo/report3/Report3_4.java new file mode 100644 index 0000000..148acff --- /dev/null +++ b/src/yuyeongwoo/report3/Report3_4.java @@ -0,0 +1,19 @@ +package yuyeongwoo.report3; +// 4-4번 문제 +public class Report3_4 { + public static void main(String[] args) { + int sum = 0; + int s = 1; + int num = 0; + for (int i = 1; true; i++) { + sum += i * s; + if (sum >= 100) { + num = i * s; + break; + } + s = -s; + } + System.out.println("num = " + num); + System.out.println("sum = " + sum); + } +} diff --git a/src/yuyeongwoo/report3/Report3_5.java b/src/yuyeongwoo/report3/Report3_5.java new file mode 100644 index 0000000..7f2293a --- /dev/null +++ b/src/yuyeongwoo/report3/Report3_5.java @@ -0,0 +1,16 @@ +package yuyeongwoo.report3; +// 4-5번 문제 +public class Report3_5 { + public static void main(String[] args) { + int i = 0; + while (i <= 10) { + int j = 0; + while (j <= i) { + System.out.print("*"); + j++; + } + System.out.println(); + i++; + } + } +} diff --git a/src/yuyeongwoo/report3/Report3_6.java b/src/yuyeongwoo/report3/Report3_6.java new file mode 100644 index 0000000..5f186cb --- /dev/null +++ b/src/yuyeongwoo/report3/Report3_6.java @@ -0,0 +1,13 @@ +package yuyeongwoo.report3; +// 4-6번 문제 +public class Report3_6 { + public static void main(String[] args) { + for (int i = 1; i <= 6; i++) { + for (int j = 1; j <= 6; j++) { + if (i + j == 6) { + System.out.println(i + " + " + j + " = " + (i + j)); + } + } + } + } +} diff --git a/src/yuyeongwoo/report3/Report3_7.java b/src/yuyeongwoo/report3/Report3_7.java new file mode 100644 index 0000000..e9669b2 --- /dev/null +++ b/src/yuyeongwoo/report3/Report3_7.java @@ -0,0 +1,14 @@ +package yuyeongwoo.report3; +// 4-7번 문제 +public class Report3_7 { + public static void main(String[] args) { + String str = "12345"; + int sum = 0; + //정답 + for (int i = 0; i < str.length(); i++) { +// sum += Integer.parseInt(str.charAt(i) + ""); + sum += str.charAt(i) - '0'; + } + System.out.println("sum = " + sum); + } +} diff --git a/src/yuyeongwoo/report3/Report3_8.java b/src/yuyeongwoo/report3/Report3_8.java new file mode 100644 index 0000000..4847382 --- /dev/null +++ b/src/yuyeongwoo/report3/Report3_8.java @@ -0,0 +1,8 @@ +package yuyeongwoo.report3; +// 4-8번 문제 +public class Report3_8 { + public static void main(String[] args) { + int value = (int) (Math.random() * 6) + 1; + System.out.println("value = " + value); + } +} diff --git a/src/yuyeongwoo/report3/Report3_9.java b/src/yuyeongwoo/report3/Report3_9.java new file mode 100644 index 0000000..43d71c4 --- /dev/null +++ b/src/yuyeongwoo/report3/Report3_9.java @@ -0,0 +1,13 @@ +package yuyeongwoo.report3; +// 4-9번 문제 +public class Report3_9 { + public static void main(String[] args) { + int num = 12345; + int sum = 0; + while (num != 0) { + sum += num % 10; + num /= 10; + } + System.out.println("sum = " + sum); + } +} diff --git a/src/yuyeongwoo/report3/Untitled Diagram.jpg b/src/yuyeongwoo/report3/Untitled Diagram.jpg new file mode 100644 index 0000000..e632e48 Binary files /dev/null and b/src/yuyeongwoo/report3/Untitled Diagram.jpg differ diff --git a/src/yuyeongwoo/report4/Report4_1 b/src/yuyeongwoo/report4/Report4_1 new file mode 100644 index 0000000..a826cf7 --- /dev/null +++ b/src/yuyeongwoo/report4/Report4_1 @@ -0,0 +1,4 @@ +5-1번 문제 + +정답 : 4번 - 이유: 배열의 개수에 따라 자동으로 크기가 지정되기 때문에 대괄호안에 숫자를 넣을 수 없다. + 5번 - 이유: 배열을 선언하는 단계에서는 크기를 지정할 수 없다. \ No newline at end of file diff --git a/src/yuyeongwoo/report4/Report4_2 b/src/yuyeongwoo/report4/Report4_2 new file mode 100644 index 0000000..8586312 --- /dev/null +++ b/src/yuyeongwoo/report4/Report4_2 @@ -0,0 +1,3 @@ +5-2번 문제 + +정답 : 4번째 배열의 길이 - 2 \ No newline at end of file diff --git a/src/yuyeongwoo/report4/Report4_3.java b/src/yuyeongwoo/report4/Report4_3.java new file mode 100644 index 0000000..e92df55 --- /dev/null +++ b/src/yuyeongwoo/report4/Report4_3.java @@ -0,0 +1,12 @@ +package yuyeongwoo.report4; +// 5-3번 문제 +public class Report4_3 { + public static void main(String[] args) { + int[] arr = {10, 20, 30, 40, 50}; + int sum = 0; + for (int i : arr) { + sum += i; + } + System.out.println("sum = " + sum); + } +} diff --git a/src/yuyeongwoo/report4/Report4_4.java b/src/yuyeongwoo/report4/Report4_4.java new file mode 100644 index 0000000..97c8096 --- /dev/null +++ b/src/yuyeongwoo/report4/Report4_4.java @@ -0,0 +1,23 @@ +package yuyeongwoo.report4; +// 5-4번 문제 +public class Report4_4 { + public static void main(String[] args) { + int[][] arr = { + { 5, 5, 5, 5, 5 }, + { 10, 10, 10, 10, 10 }, + { 20, 20, 20, 20, 20 }, + { 30, 30, 30, 30, 30 } + }; + int total = 0; + float average = 0; + + for (int i = 0; i < arr.length; i++) { + for (int j = 0; j < arr[0].length; j++) { + total += arr[i][j]; + } + } + average = (float) total / (arr.length * arr[0].length); + System.out.println("total = " + total); + System.out.println("average = " + average); + } +} diff --git a/src/yuyeongwoo/report4/Report4_5.java b/src/yuyeongwoo/report4/Report4_5.java new file mode 100644 index 0000000..ac52e7d --- /dev/null +++ b/src/yuyeongwoo/report4/Report4_5.java @@ -0,0 +1,30 @@ +package yuyeongwoo.report4; + +import java.util.Arrays; + +// 5-5번 문제 +public class Report4_5 { + public static void main(String[] args) { + int[] ballArr = { 1, 2, 3, 4, 5, 6, 7, 8, 9 }; + int[] ball3 = new int[3]; + + for (int i = 0; i < ballArr.length; i++) { + int j = (int) (Math.random() * ballArr.length); + int tmp = 0; + + tmp = ballArr[i]; + ballArr[i] = ballArr[j]; + ballArr[j] = tmp; + } + +// for (int i = 0; i < 3; i++) { +// ball3[i] = ballArr[i]; +// } + + ball3 = Arrays.copyOf(ballArr, 3); + + for (int i = 0; i < ball3.length; i++) { + System.out.print(ball3[i]); + } + } +} diff --git a/src/yuyeongwoo/report4/Report4_6.java b/src/yuyeongwoo/report4/Report4_6.java new file mode 100644 index 0000000..bef0615 --- /dev/null +++ b/src/yuyeongwoo/report4/Report4_6.java @@ -0,0 +1,34 @@ +package yuyeongwoo.report4; + +import java.util.Scanner; + +// 5-6번 문제 +public class Report4_6 { + public static void main(String[] args) { + String[] words = { "television", "computer", "mouse", "phone" }; + + Scanner scanner = new Scanner(System.in); + + for (int i = 0; i < words.length; i++) { + char[] question = words[i].toCharArray(); + + for (int j = 0; j < question.length; j++) { + int k = (int) (Math.random() * question.length); + char tmp = 0; + + tmp = question[i]; + question[i] = question[k]; + question[k] = tmp; + } + + + System.out.printf("Q%d. %s의 정답을 입력하세요.> ", i + 1, new String(question)); + String answer = scanner.nextLine(); + + if (words[i].equals(answer.trim())) + System.out.printf("맞았습니다.%n%n"); + else + System.out.printf("틀렸습니다.%n%n"); + } + } +} diff --git a/src/yuyeongwoo/report5/Report5_1 b/src/yuyeongwoo/report5/Report5_1 new file mode 100644 index 0000000..e1e164e --- /dev/null +++ b/src/yuyeongwoo/report5/Report5_1 @@ -0,0 +1,10 @@ +6-1번 문제 + +정답 : class Student { + String name; + int ban; + int no; + int kor; + int eng; + int math; + } \ No newline at end of file diff --git a/src/yuyeongwoo/report5/Report5_2.java b/src/yuyeongwoo/report5/Report5_2.java new file mode 100644 index 0000000..031b797 --- /dev/null +++ b/src/yuyeongwoo/report5/Report5_2.java @@ -0,0 +1,34 @@ +package yuyeongwoo.report5; +//6-2번 문제 +public class Report5_2 { + public static void main(String[] args){ + Student s = new Student("홍길동", 1, 1, 100, 60, 76); + + String str = s.info(); + System.out.println(str); + }// 예상 결과 : 홍길동, 1, 1, 100, 60, 76, 236, 78.7 +} + +class Student { + String name; + int ban; + int no; + int kor; + int eng; + int math; + + public Student(String name, int ban, int no, int kor, int eng, int math) { + this.name = name; + this.ban = ban; + this.no = no; + this.kor = kor; + this.eng = eng; + this.math = math; + } + + + + public String info() { + return name + ", " + ban + ", " + no + ", " + kor + ", " + eng + ", " + math + ", " + (kor + eng + math) + ", " + ((int)((kor + eng + math) / 3f * 10 + 0.5) / 10f ); + } +} \ No newline at end of file diff --git a/src/yuyeongwoo/report5/Report5_3.java b/src/yuyeongwoo/report5/Report5_3.java new file mode 100644 index 0000000..fe94658 --- /dev/null +++ b/src/yuyeongwoo/report5/Report5_3.java @@ -0,0 +1,43 @@ +package yuyeongwoo.report5; +//6-3번 문제 +public class Report5_3 { + public static void main(String[] args){ + Student2 s = new Student2("홍길동", 1, 1, 100, 60, 76); + + s.name = "홍길동"; + s.ban = 1; + s.no = 1; + s.kor = 100; + s.eng = 60; + s.math = 76; + System.out.println("이름 :"+s.name); + System.out.println("총점 :"+s.getTotal()); + System.out.println("평균 :"+s.getAverage()); + }// 예상 결과 : 홍길동, 1, 1, 100, 60, 76, 236, 78.7 +} + +class Student2 { + String name; + int ban; + int no; + int kor; + int eng; + int math; + + public Student2(String name, int ban, int no, int kor, int eng, int math) { + this.name = name; + this.ban = ban; + this.no = no; + this.kor = kor; + this.eng = eng; + this.math = math; + } + + int getTotal() { + return kor + eng + math; + } + + float getAverage() { + return (int) (getTotal() / 3f * 10 + 0.5) / 10f; + } +} \ No newline at end of file diff --git a/src/yuyeongwoo/report5/Report5_4 b/src/yuyeongwoo/report5/Report5_4 new file mode 100644 index 0000000..114ab93 --- /dev/null +++ b/src/yuyeongwoo/report5/Report5_4 @@ -0,0 +1,19 @@ +6-5번 문제 + +정답 : class PlayingCard { + int kind; + int num; + static int width; + static int height; + PlayingCard(int k, int n) { + kind = k; + num = n; + } + public static void main(String args[]) { + PlayingCard card = new PlayingCard(1,1); + } + } + +클래스 변수 : width, height +인스턴스 변수 : kind, num +지역변수 : k, n, card, args \ No newline at end of file diff --git a/src/yuyeongwoo/report5/Report5_5 b/src/yuyeongwoo/report5/Report5_5 new file mode 100644 index 0000000..a2bc4ff --- /dev/null +++ b/src/yuyeongwoo/report5/Report5_5 @@ -0,0 +1,22 @@ +6-7번 문제 + +정답 : class Marine { + int x=0, y=0; //Marine의 위치좌표 (x,y) + int hp = 60; //현재 체력 + static int weapon = 6; //공격력 + static int armor = 0; //방어력 + static void weaponUp() { + weapon++; + } + static void armorUp() { + armor++; + } + void move(int x, int y) { + this.x = x; + this.y = y; + } + } + +weapon, armor : weapon과 armor변수는 모든 인스턴스에 대해 +동일한 값을 가져야 하기 때문에 static 변수여야 한다. +void weaponUp(), void armorUp() : weapon과 armor변수가 static이기 때문에 static메서드여야 한다. \ No newline at end of file diff --git a/src/yuyeongwoo/report6/report6_1 b/src/yuyeongwoo/report6/report6_1 new file mode 100644 index 0000000..d53a8e8 --- /dev/null +++ b/src/yuyeongwoo/report6/report6_1 @@ -0,0 +1,4 @@ +문제 6-8번 + +b : 생성자는 초기화의 목적이 있고 객체를 생성하는 것은 new연산자이다. +e : 생성자는 오버로딩이 가능하다. \ No newline at end of file diff --git a/src/yuyeongwoo/report6/report6_2 b/src/yuyeongwoo/report6/report6_2 new file mode 100644 index 0000000..6670452 --- /dev/null +++ b/src/yuyeongwoo/report6/report6_2 @@ -0,0 +1,3 @@ +문제 6-9번 + +b : 클래스 멤버 안에서는 사용이 불가하다. \ No newline at end of file diff --git a/src/yuyeongwoo/report6/report6_3 b/src/yuyeongwoo/report6/report6_3 new file mode 100644 index 0000000..5aa7c19 --- /dev/null +++ b/src/yuyeongwoo/report6/report6_3 @@ -0,0 +1,4 @@ +문제 6-10번 + +c : 리턴타입은 영향을 주지 않는다. +d : 매개변수의 이름은 상관이 없다. \ No newline at end of file diff --git a/src/yuyeongwoo/report6/report6_4 b/src/yuyeongwoo/report6/report6_4 new file mode 100644 index 0000000..4b97944 --- /dev/null +++ b/src/yuyeongwoo/report6/report6_4 @@ -0,0 +1,3 @@ +문제 6-11번 + +b, c, d \ No newline at end of file diff --git a/src/yuyeongwoo/report6/report6_5 b/src/yuyeongwoo/report6/report6_5 new file mode 100644 index 0000000..d20b982 --- /dev/null +++ b/src/yuyeongwoo/report6/report6_5 @@ -0,0 +1,4 @@ +문제 6-12번 + +c : 초기화 블럭이 먼저 초기화 된다. +e : 클래스 변수가 먼저 초기화 된다. \ No newline at end of file diff --git a/src/yuyeongwoo/report6/report6_6 b/src/yuyeongwoo/report6/report6_6 new file mode 100644 index 0000000..d6618ff --- /dev/null +++ b/src/yuyeongwoo/report6/report6_6 @@ -0,0 +1,3 @@ +문제 6-13번 + +a : 기본값 - 명시적 초기화 - 초기화 블럭 - 생성자 \ No newline at end of file diff --git a/src/yuyeongwoo/report6/report6_7 b/src/yuyeongwoo/report6/report6_7 new file mode 100644 index 0000000..2a65bba --- /dev/null +++ b/src/yuyeongwoo/report6/report6_7 @@ -0,0 +1,4 @@ +문제 6-14번 + +a : 사용전에 수동으로 초기화 해줘야 한다. +e : 지역변수는 호출스택에 생성된다. diff --git a/src/yuyeongwoo/report6/report6_8 b/src/yuyeongwoo/report6/report6_8 new file mode 100644 index 0000000..35ddc4f --- /dev/null +++ b/src/yuyeongwoo/report6/report6_8 @@ -0,0 +1,3 @@ +문제 6-15번 + +b : 종료된게 아니라 대기중이다. diff --git a/src/yuyeongwoo/report6/report6_9 b/src/yuyeongwoo/report6/report6_9 new file mode 100644 index 0000000..05ae4fb --- /dev/null +++ b/src/yuyeongwoo/report6/report6_9 @@ -0,0 +1,18 @@ +문제 6-16번 + +class Exercise6_16 { + public static void change(String str) { + str += "456"; + } + + public static void main(String[] args) { + String str = "ABC123"; + System.out.println(str); + change(str); + System.out.println("After change:" + str); + } +} + +ABC123 +After change: ABC123 +이유: change메서드의 str은 지역변수이다. 따라서 메서드가 종료되면 메모리에서 제거된다. \ No newline at end of file diff --git a/src/yuyeongwoo/report7/report7_1.java b/src/yuyeongwoo/report7/report7_1.java new file mode 100644 index 0000000..5804aa2 --- /dev/null +++ b/src/yuyeongwoo/report7/report7_1.java @@ -0,0 +1,22 @@ +package yuyeongwoo.report7; +//문제 6-17번 +public class report7_1 { + public static void main(String[] args) { + int[] original = { 1, 2, 3, 4, 5, 6, 7, 8, 9 }; + System.out.println(java.util.Arrays.toString(original)); + + int[] result = shuffle(original); + System.out.println(java.util.Arrays.toString(result)); + } + + public static int[] shuffle(int[] arr) { + for (int i = 0; i < arr.length; i++) { + int j = (int) (Math.random() * arr.length); + int tmp = 0; + tmp = arr[i]; + arr[i] = arr[j]; + arr[j] = tmp; + } + return arr; + } +} diff --git a/src/yuyeongwoo/report7/report7_2.java b/src/yuyeongwoo/report7/report7_2.java new file mode 100644 index 0000000..7556d03 --- /dev/null +++ b/src/yuyeongwoo/report7/report7_2.java @@ -0,0 +1,23 @@ +package yuyeongwoo.report7; +//문제 6-18번 +public class report7_2 { + public static void main(String[] args) { + String str = "123"; + System.out.println(str + " 는 숫자입니까? " + isNumber(str)); + str = "1234o"; + System.out.println(str + " 는 숫자입니까? " + isNumber(str)); + } + + public static boolean isNumber(String str) { + if (str == null || str.equals("")) { + return false; + } + for (int i = 0; i < str.length(); i++) { + int a = str.charAt(i) - '0'; + if (0 > a || a > 9) { + return false; + } + } + return true; + } +} \ No newline at end of file diff --git a/src/yuyeongwoo/report7/report7_3.java b/src/yuyeongwoo/report7/report7_3.java new file mode 100644 index 0000000..676ba5f --- /dev/null +++ b/src/yuyeongwoo/report7/report7_3.java @@ -0,0 +1,60 @@ +package yuyeongwoo.report7; +//문제 6-19번 +public class report7_3 { + public static void main(String[] args) { + MyTv t = new MyTv(); + t.channel = 100; + t.volume = 0; + System.out.println("CH:" + t.channel + ", VOL:" + t.volume); + t.channelDown(); + t.volumeDown(); + System.out.println("CH:" + t.channel + ", VOL:" + t.volume); + t.volume = 100; + t.channelUp(); + t.volumeUp(); + System.out.println("CH:" + t.channel + ", VOL:" + t.volume); + } +} +class MyTv { + boolean isPowerOn; + int channel; + int volume; + final int MAX_VOLUME = 100; + final int MIN_VOLUME = 0; + final int MAX_CHANNEL = 100; + final int MIN_CHANNEL = 1; + void turnOnOff() { + // (1) isPowerOn의 값이 true면 false로, false면 true로 바꾼다. + isPowerOn = !isPowerOn; + } + void volumeUp() { + // (2) volume의 값이 MAX_VOLUME보다 작을 때만 값을 1 증가시킨다. + if (volume < MAX_VOLUME) { + volume++; + } + } + void volumeDown() { + // (3) volume의 값이 MIN_VOLUME보다 클 때만 값을 1 감소시킨다. + if (volume > MIN_VOLUME) { + volume--; + } + } + void channelUp() { + // (4) channel의 값을 1 증가시킨다. + // 만일 channel이 MAX_CHANNEL이면 , channel의 값을 MIN_CHANNEL로 바꾼다. + if (channel == MAX_CHANNEL) { + channel = MIN_CHANNEL; + } else { + channel++; + } + } + void channelDown() { + // (5) channel의 값을 1 감소시킨다 . + // 만일 channel이 MIN_CHANNEL이면, channel의 값을 MAX_CHANNEL로 바꾼다. + if (channel == MIN_CHANNEL) { + channel = MAX_CHANNEL; + } else { + channel--; + } + } +} diff --git a/src/yuyeongwoo/report7/report7_4.java b/src/yuyeongwoo/report7/report7_4.java new file mode 100644 index 0000000..2ff9111 --- /dev/null +++ b/src/yuyeongwoo/report7/report7_4.java @@ -0,0 +1,26 @@ +package yuyeongwoo.report7; +//6-20번 +import java.util.Arrays; + +public class report7_4 { + public static void main(String[] args) { + int[] data = {3, 2, 9, 4, 7}; + System.out.println(Arrays.toString(data)); + System.out.println("최대값 : " + max(data)); + System.out.println("최대값 : " + max(null)); + System.out.println("최대값 : " + max(new int[]{})); + } + + public static int max(int[] arr) { + if (arr == null || arr.length == 0) { + return -999999; + } + int max = 0; + for (int i = 0; i < arr.length; i++) { + if (arr[i] > max) { + max = arr[i]; + } + } + return max; + } +} diff --git a/src/yuyeongwoo/report7/report7_5.java b/src/yuyeongwoo/report7/report7_5.java new file mode 100644 index 0000000..1b9cd6c --- /dev/null +++ b/src/yuyeongwoo/report7/report7_5.java @@ -0,0 +1,14 @@ +package yuyeongwoo.report7; +//6-21번 +public class report7_5 { + public static void main(String[] args) { + int value = 5; + System.out.println(value + "의 절대값 :" + abs(value)); + value = -10; + System.out.println(value + "의 절대값 :" + abs(value)); + } + + public static int abs(int value) { + return value < 0 ? -value : value; + } +} diff --git a/src/yuyeongwoo/report8/report8_1.java b/src/yuyeongwoo/report8/report8_1.java new file mode 100644 index 0000000..31705bb --- /dev/null +++ b/src/yuyeongwoo/report8/report8_1.java @@ -0,0 +1,42 @@ +package yuyeongwoo.report8; +//문제 7-1번 +public class report8_1 { + public static void main(String[] args) { + SutdaDeck2 deck = new SutdaDeck2(); + for (int i = 0; i < deck.cards.length; i++) { + System.out.print(deck.cards[i] + ","); + } + } +} + +class SutdaDeck { + final int CARD_NUM = 20; + SutdaCard2[] cards = new SutdaCard2[CARD_NUM]; + + SutdaDeck() { + for (int i = 0; i < cards.length; i++) { + int a = i % 10 + 1; + boolean b = i == 0 || i == 2 || i == 7; + cards[i] = new SutdaCard2(a, b); + } + } +} + +class SutdaCard { + int num; + boolean isKwang; + + public SutdaCard(int num, boolean isKwang) { + this.num = num; + this.isKwang = isKwang; + } + + SutdaCard() { + this(1, true); + } + + @Override + public String toString() { + return num + (isKwang ? "K" : ""); + } +} diff --git a/src/yuyeongwoo/report8/report8_2.java b/src/yuyeongwoo/report8/report8_2.java new file mode 100644 index 0000000..de6d092 --- /dev/null +++ b/src/yuyeongwoo/report8/report8_2.java @@ -0,0 +1,59 @@ +package yuyeongwoo.report8; +//문제 7-2번 +public class report8_2 { + public static void main(String[] args) { + + } +} + +class SutdaDeck2 { + final int CARD_NUM = 20; + SutdaCard2[] cards = new SutdaCard2[CARD_NUM]; + + SutdaDeck2() { + for (int i = 0; i < cards.length; i++) { + int a = i % 10 + 1; + boolean b = i == 0 || i == 2 || i == 7; + cards[i] = new SutdaCard2(a, b); + } + } + + void shuffle() { + for (int i = 0; i < cards.length; i++) { + int a = (int) (Math.random() * cards.length); + + SutdaCard2 j = cards[i]; + cards[i] = cards[a]; + cards[a] = j; + } + } + + SutdaCard2 pick(int index) { + return cards[index]; + } + + SutdaCard2 pick() { + int a = (int) (Math.random() * cards.length); + return cards[a]; + } +} + +class SutdaCard2 { + int num; + boolean isKwang; + + public SutdaCard2(int num, boolean isKwang) { + this.num = num; + this.isKwang = isKwang; + } + + SutdaCard2() { + this(1, true); + } + + @Override + public String toString() { + return num + (isKwang ? "K" : ""); + } +} + diff --git a/src/yuyeongwoo/report8/report8_3 b/src/yuyeongwoo/report8/report8_3 new file mode 100644 index 0000000..a1a1306 --- /dev/null +++ b/src/yuyeongwoo/report8/report8_3 @@ -0,0 +1,33 @@ +//문제 7-3번 + +class Product { + int price; // 제품의 가격 + int bonusPoint; // 제품구매 시 제공하는 보너스점수 + + // (구현) + Product() {} <= 해결1 + + Product(int price) { + this.price = price; + bonusPoint = (int) (price / 10.0); + } +} + +class Tv extends Product { + Tv() {} <= 컴파일 에러 / 이유: Tv() 생성자가 호출되면 Tv()는 조상의 기본 생성자를 호출한다. + 하지만 조상 클래스에는 기본 생성자가 정의되어있지 않다. + (이미 다른 생성자가 정의되어있기 때문에 컴파일러가 기본 생성자를 자동 추가해주지 않는다.) + Tv() { <= 해결2 조상 클래스의 Product(int price) 생성자를 직접 호출해도 된다. + super(200) + } + public String toString() { + return "Tv"; + } +} + +class Exercise7_3 { + public static void main(String[] args) { + Tv t = new Tv(); + } +} + diff --git a/src/yuyeongwoo/report8/report8_4.java b/src/yuyeongwoo/report8/report8_4.java new file mode 100644 index 0000000..64edfb6 --- /dev/null +++ b/src/yuyeongwoo/report8/report8_4.java @@ -0,0 +1,39 @@ +package yuyeongwoo.report8; +//문제 7-4 +public class report8_4 { + public static void main(String[] args) { + MyTv t = new MyTv(); + + t.setChannel(10); + System.out.println("CH: " + t.getChannel()); + t.setVolume(20); + System.out.println("VOL: " + t.getVolume()); + } +} + +class MyTv { + private boolean isPowerOn; + private int channel; + private int volume; + + final int MAX_VOLUME = 100; + final int MIN_VOLUME = 0; + final int MAX_CHANNEL = 100; + final int MIN_CHANNEL = 1; + + public int getChannel() { + return channel; + } + + public void setChannel(int channel) { + this.channel = channel; + } + + public int getVolume() { + return volume; + } + + public void setVolume(int volume) { + this.volume = volume; + } +} diff --git a/src/yuyeongwoo/report8/report8_5.java b/src/yuyeongwoo/report8/report8_5.java new file mode 100644 index 0000000..5a4c207 --- /dev/null +++ b/src/yuyeongwoo/report8/report8_5.java @@ -0,0 +1,55 @@ +package yuyeongwoo.report8; +//문제 7-5번 +public class report8_5 { + public static void main(String[] args) { + MyTv2 t = new MyTv2(); + t.setChannel(10); + System.out.println("CH:" + t.getChannel()); + t.setChannel(20); + System.out.println("CH:" + t.getChannel()); + t.gotoPrevChannel(); + System.out.println("CH:" + t.getChannel()); + t.gotoPrevChannel(); + System.out.println("CH:" + t.getChannel()); + } +} +class MyTv2 { + private boolean isPowerOn; + private int channel; + private int volume; + // (구현) + private int prevChannel; + + final int MAX_VOLUME = 100; + final int MIN_VOLUME = 0; + final int MAX_CHANNEL = 100; + final int MIN_CHANNEL = 1; + + public void setVolume(int volume) { + if (volume > MAX_VOLUME || volume < MIN_VOLUME) + return; + this.volume = volume; + } + + public int getVolume() { + return volume; + } + + public void setChannel(int channel) { + if (channel > MAX_CHANNEL || channel < MIN_CHANNEL) + return; + + // (구현) + prevChannel = this.channel; + this.channel = channel; + } + + public int getChannel() { + return channel; + } + + // (구현) + public void gotoPrevChannel() { + setChannel(prevChannel); + } +} diff --git a/src/yuyeongwoo/solid/dip/AbstractOperation.java b/src/yuyeongwoo/solid/dip/AbstractOperation.java new file mode 100644 index 0000000..3fac70b --- /dev/null +++ b/src/yuyeongwoo/solid/dip/AbstractOperation.java @@ -0,0 +1,5 @@ +package yuyeongwoo.solid.dip; + +public abstract class AbstractOperation { + public abstract int operate(AbstractOperation operation, int firstNumber, int secondNumber); +} diff --git a/src/yuyeongwoo/solid/dip/AddOperation.java b/src/yuyeongwoo/solid/dip/AddOperation.java new file mode 100644 index 0000000..0452da6 --- /dev/null +++ b/src/yuyeongwoo/solid/dip/AddOperation.java @@ -0,0 +1,8 @@ +package yuyeongwoo.solid.dip; + +public class AddOperation extends AbstractOperation { + @Override + public int operate(AbstractOperation operation, int firstNumber, int secondNumber) { + return firstNumber + secondNumber; + } +} diff --git a/src/yuyeongwoo/solid/dip/Calculator.java b/src/yuyeongwoo/solid/dip/Calculator.java new file mode 100644 index 0000000..e686763 --- /dev/null +++ b/src/yuyeongwoo/solid/dip/Calculator.java @@ -0,0 +1,10 @@ +package yuyeongwoo.solid.dip; + +public class Calculator { + + AbstractOperation operation = new AddOperation(); + + int calculate(AbstractOperation operation, int firstNumber, int secondNumber) { + return operation.operate(operation, firstNumber, secondNumber); + } +} diff --git a/src/yuyeongwoo/solid/dip/Client.java b/src/yuyeongwoo/solid/dip/Client.java new file mode 100644 index 0000000..b64e5e3 --- /dev/null +++ b/src/yuyeongwoo/solid/dip/Client.java @@ -0,0 +1,21 @@ +package yuyeongwoo.solid.dip; + +public class Client { + public static void main(String[] args) { + Calculator calculator = new Calculator(); + int firstNumber = 4; + int secondNumber = 2; + + int answer = calculator.calculate(new AddOperation(), firstNumber, secondNumber); + System.out.println(firstNumber + " + " + secondNumber + " = " + answer); + + answer = calculator.calculate(new SubtractOperation(), firstNumber, secondNumber); + System.out.println(firstNumber + " - " + secondNumber + " = " + answer); + + answer = calculator.calculate(new MultiplyOperation(), firstNumber, secondNumber); + System.out.println(firstNumber + " * " + secondNumber + " = " + answer); + + answer = calculator.calculate(new DivideOperation(), firstNumber, secondNumber); + System.out.println(firstNumber + " / " + secondNumber + " = " + answer); + } +} diff --git a/src/yuyeongwoo/solid/dip/DivideOperation.java b/src/yuyeongwoo/solid/dip/DivideOperation.java new file mode 100644 index 0000000..8bd6013 --- /dev/null +++ b/src/yuyeongwoo/solid/dip/DivideOperation.java @@ -0,0 +1,8 @@ +package yuyeongwoo.solid.dip; + +public class DivideOperation extends AbstractOperation { + @Override + public int operate(AbstractOperation operation, int firstNumber, int secondNumber) { + return firstNumber / secondNumber; + } +} diff --git a/src/yuyeongwoo/solid/dip/MultiplyOperation.java b/src/yuyeongwoo/solid/dip/MultiplyOperation.java new file mode 100644 index 0000000..204cc75 --- /dev/null +++ b/src/yuyeongwoo/solid/dip/MultiplyOperation.java @@ -0,0 +1,8 @@ +package yuyeongwoo.solid.dip; + +public class MultiplyOperation extends AbstractOperation { + @Override + public int operate(AbstractOperation operation, int firstNumber, int secondNumber) { + return firstNumber * secondNumber; + } +} diff --git a/src/yuyeongwoo/solid/dip/SubtractOperation.java b/src/yuyeongwoo/solid/dip/SubtractOperation.java new file mode 100644 index 0000000..df5fb8f --- /dev/null +++ b/src/yuyeongwoo/solid/dip/SubtractOperation.java @@ -0,0 +1,8 @@ +package yuyeongwoo.solid.dip; + +public class SubtractOperation extends AbstractOperation{ + @Override + public int operate(AbstractOperation operation, int firstNumber, int secondNumber) { + return firstNumber - secondNumber; + } +} diff --git a/src/yuyeongwoo/solid/isp/AbstractOperation.java b/src/yuyeongwoo/solid/isp/AbstractOperation.java new file mode 100644 index 0000000..6ebd4ad --- /dev/null +++ b/src/yuyeongwoo/solid/isp/AbstractOperation.java @@ -0,0 +1,6 @@ +package yuyeongwoo.solid.isp; + +public abstract class AbstractOperation { + public abstract int operate(int firstNumber, int secondNumber); + public abstract String getOperator(); +} diff --git a/src/yuyeongwoo/solid/isp/AddOperation.java b/src/yuyeongwoo/solid/isp/AddOperation.java new file mode 100644 index 0000000..7c5bf91 --- /dev/null +++ b/src/yuyeongwoo/solid/isp/AddOperation.java @@ -0,0 +1,13 @@ +package yuyeongwoo.solid.isp; + +public class AddOperation extends AbstractOperation { + @Override + public int operate(int firstNumber, int secondNumber) { + return firstNumber + secondNumber; + } + + @Override + public String getOperator() { + return "+"; + } +} diff --git a/src/yuyeongwoo/solid/isp/Calculator.java b/src/yuyeongwoo/solid/isp/Calculator.java new file mode 100644 index 0000000..1db166a --- /dev/null +++ b/src/yuyeongwoo/solid/isp/Calculator.java @@ -0,0 +1,7 @@ +package yuyeongwoo.solid.isp; + +public class Calculator { + public int calculate(AbstractOperation operation, int firstNumber, int secondNumber){ + return operation.operate(firstNumber, secondNumber); + } +} diff --git a/src/yuyeongwoo/solid/isp/Client.java b/src/yuyeongwoo/solid/isp/Client.java new file mode 100644 index 0000000..3a3c59c --- /dev/null +++ b/src/yuyeongwoo/solid/isp/Client.java @@ -0,0 +1,14 @@ +package yuyeongwoo.solid.isp; + +public class Client { + public static void main(String[] args) { + int firNum = 140; + int secNum = 60; + + DisplayResult displayResult = new DisplayTypeA(); + displayResult.displayResult(new AddOperation(), firNum, secNum); + + DisplayWithOperator displayWithOperator = new DisplayTypeB(); + displayWithOperator.displayResultWithOperator(new AddOperation(), firNum, secNum); + } +} diff --git a/src/yuyeongwoo/solid/isp/DisplayResult.java b/src/yuyeongwoo/solid/isp/DisplayResult.java new file mode 100644 index 0000000..1d03774 --- /dev/null +++ b/src/yuyeongwoo/solid/isp/DisplayResult.java @@ -0,0 +1,6 @@ +package yuyeongwoo.solid.isp; + +public interface DisplayResult { + void displayResult(AbstractOperation operation, int firstNumber, int secondNumber); + +} diff --git a/src/yuyeongwoo/solid/isp/DisplayTypeA.java b/src/yuyeongwoo/solid/isp/DisplayTypeA.java new file mode 100644 index 0000000..a4d192c --- /dev/null +++ b/src/yuyeongwoo/solid/isp/DisplayTypeA.java @@ -0,0 +1,9 @@ +package yuyeongwoo.solid.isp; + +public class DisplayTypeA extends Calculator implements DisplayResult { + @Override + public void displayResult(AbstractOperation operation, int firstNumber, int secondNumber) { + int answer = operation.operate(firstNumber, secondNumber); + System.out.println(answer); + } +} diff --git a/src/yuyeongwoo/solid/isp/DisplayTypeB.java b/src/yuyeongwoo/solid/isp/DisplayTypeB.java new file mode 100644 index 0000000..5895644 --- /dev/null +++ b/src/yuyeongwoo/solid/isp/DisplayTypeB.java @@ -0,0 +1,10 @@ +package yuyeongwoo.solid.isp; + +public class DisplayTypeB extends Calculator implements DisplayWithOperator { + @Override + public void displayResultWithOperator(AbstractOperation operation, int firstNumber, int secondNumber) { + int answer = operation.operate(firstNumber, secondNumber); + String operator = operation.getOperator(); + System.out.println(firstNumber + " " + operator + " " + secondNumber + " = " + answer); + } +} diff --git a/src/yuyeongwoo/solid/isp/DisplayWithOperator.java b/src/yuyeongwoo/solid/isp/DisplayWithOperator.java new file mode 100644 index 0000000..d497348 --- /dev/null +++ b/src/yuyeongwoo/solid/isp/DisplayWithOperator.java @@ -0,0 +1,5 @@ +package yuyeongwoo.solid.isp; + +public interface DisplayWithOperator { + void displayResultWithOperator(AbstractOperation operation, int firstNumber, int secondNumber); +} diff --git a/src/yuyeongwoo/solid/isp/DivideOperation.java b/src/yuyeongwoo/solid/isp/DivideOperation.java new file mode 100644 index 0000000..5cd0f18 --- /dev/null +++ b/src/yuyeongwoo/solid/isp/DivideOperation.java @@ -0,0 +1,13 @@ +package yuyeongwoo.solid.isp; + +public class DivideOperation extends AbstractOperation { + @Override + public int operate(int firstNumber, int secondNumber) { + return firstNumber / secondNumber; + } + + @Override + public String getOperator() { + return "/"; + } +} diff --git a/src/yuyeongwoo/solid/isp/MultiplyOperation.java b/src/yuyeongwoo/solid/isp/MultiplyOperation.java new file mode 100644 index 0000000..1c569bb --- /dev/null +++ b/src/yuyeongwoo/solid/isp/MultiplyOperation.java @@ -0,0 +1,13 @@ +package yuyeongwoo.solid.isp; + +public class MultiplyOperation extends AbstractOperation { + @Override + public int operate(int firstNumber, int secondNumber) { + return firstNumber * secondNumber; + } + + @Override + public String getOperator() { + return "*"; + } +} diff --git a/src/yuyeongwoo/solid/isp/SubtractOperation.java b/src/yuyeongwoo/solid/isp/SubtractOperation.java new file mode 100644 index 0000000..534913a --- /dev/null +++ b/src/yuyeongwoo/solid/isp/SubtractOperation.java @@ -0,0 +1,13 @@ +package yuyeongwoo.solid.isp; + +public class SubtractOperation extends AbstractOperation { + @Override + public int operate(int firstNumber, int secondNumber) { + return firstNumber - secondNumber; + } + + @Override + public String getOperator() { + return "-"; + } +} diff --git a/src/yuyeongwoo/solid/lsp/AbstractOperation.java b/src/yuyeongwoo/solid/lsp/AbstractOperation.java new file mode 100644 index 0000000..a92dda3 --- /dev/null +++ b/src/yuyeongwoo/solid/lsp/AbstractOperation.java @@ -0,0 +1,7 @@ +package yuyeongwoo.solid.lsp; + +public abstract class AbstractOperation { + abstract int operate(int firstNumber, int secondNumber); + + abstract int isInvalid(AbstractOperation operation, int firstNumber, int secondNumber); +} diff --git a/src/yuyeongwoo/solid/lsp/AddOperation.java b/src/yuyeongwoo/solid/lsp/AddOperation.java new file mode 100644 index 0000000..efb6960 --- /dev/null +++ b/src/yuyeongwoo/solid/lsp/AddOperation.java @@ -0,0 +1,14 @@ +package yuyeongwoo.solid.lsp; + +public class AddOperation extends AbstractOperation { + + @Override + public int operate(int firstNumber, int secondNumber) { + return firstNumber + secondNumber; + } + + @Override + public int isInvalid(AbstractOperation operation, int firstNumber, int secondNumber) { + return operation.operate(firstNumber, secondNumber); + } +} diff --git a/src/yuyeongwoo/solid/lsp/Calculator.java b/src/yuyeongwoo/solid/lsp/Calculator.java new file mode 100644 index 0000000..ed0e81a --- /dev/null +++ b/src/yuyeongwoo/solid/lsp/Calculator.java @@ -0,0 +1,7 @@ +package yuyeongwoo.solid.lsp; + +public class Calculator { + public int calculate(AbstractOperation operation, int firstNumber, int secondNumber) { + return operation.isInvalid(operation, firstNumber, secondNumber); + } +} diff --git a/src/yuyeongwoo/solid/lsp/Client.java b/src/yuyeongwoo/solid/lsp/Client.java new file mode 100644 index 0000000..cdec845 --- /dev/null +++ b/src/yuyeongwoo/solid/lsp/Client.java @@ -0,0 +1,21 @@ +package yuyeongwoo.solid.lsp; + +public class Client { + public static void main(String[] args) { + Calculator calculator = new Calculator(); + int firNum = 4; + int secNum = 2; + + int answer = calculator.calculate(new AddOperation(), firNum, secNum); + System.out.println(firNum + " + " + secNum + " = " + answer); + + answer = calculator.calculate(new SubtractOperation(), firNum, secNum); + System.out.println(firNum + " - " + secNum + " = " + answer); + + answer = calculator.calculate(new MultiplyOperation(), firNum, secNum); + System.out.println(firNum + " * " + secNum + " = " + answer); + + answer = calculator.calculate(new DivideOperation(), firNum, secNum); + System.out.println(firNum + " / " + secNum + " = " + answer); + } +} diff --git a/src/yuyeongwoo/solid/lsp/DivideOperation.java b/src/yuyeongwoo/solid/lsp/DivideOperation.java new file mode 100644 index 0000000..131c018 --- /dev/null +++ b/src/yuyeongwoo/solid/lsp/DivideOperation.java @@ -0,0 +1,19 @@ +package yuyeongwoo.solid.lsp; + +public class DivideOperation extends AbstractOperation { + + @Override + public int operate(int firstNumber, int secondNumber) { + return firstNumber / secondNumber; + } + + @Override + public int isInvalid(AbstractOperation operation, int firstNumber, int secondNumber) { + if (operation instanceof DivideOperation) { + if (secondNumber == 0) { + return -9999; + } + } + return operation.operate(firstNumber, secondNumber); + } +} diff --git a/src/yuyeongwoo/solid/lsp/MultiplyOperation.java b/src/yuyeongwoo/solid/lsp/MultiplyOperation.java new file mode 100644 index 0000000..17aa1db --- /dev/null +++ b/src/yuyeongwoo/solid/lsp/MultiplyOperation.java @@ -0,0 +1,14 @@ +package yuyeongwoo.solid.lsp; + +public class MultiplyOperation extends AbstractOperation { + + @Override + public int operate(int firstNumber, int secondNumber) { + return firstNumber * secondNumber; + } + + @Override + public int isInvalid(AbstractOperation operation, int firstNumber, int secondNumber) { + return operation.operate(firstNumber, secondNumber); + } +} diff --git a/src/yuyeongwoo/solid/lsp/SubtractOperation.java b/src/yuyeongwoo/solid/lsp/SubtractOperation.java new file mode 100644 index 0000000..19aa47b --- /dev/null +++ b/src/yuyeongwoo/solid/lsp/SubtractOperation.java @@ -0,0 +1,14 @@ +package yuyeongwoo.solid.lsp; + +public class SubtractOperation extends AbstractOperation { + + @Override + public int operate(int firstNumber, int secondNumber) { + return firstNumber - secondNumber; + } + + @Override + public int isInvalid(AbstractOperation operation, int firstNumber, int secondNumber) { + return operation.operate(firstNumber, secondNumber); + } +} diff --git a/src/yuyeongwoo/solid/ocp/OCP.java b/src/yuyeongwoo/solid/ocp/OCP.java new file mode 100644 index 0000000..cdd5079 --- /dev/null +++ b/src/yuyeongwoo/solid/ocp/OCP.java @@ -0,0 +1,54 @@ +package yuyeongwoo.solid.ocp; + +public class OCP { + public static void main(String[] args) { + Calculator calculator = new Calculator(); + int firNum = 4; + int secNum = 2; + calculator.calculate(new AddOperation(), firNum, secNum); + calculator.calculate(new SubtractOperation(), firNum, secNum); + calculator.calculate(new MultiplyOperation(), firNum, secNum); + calculator.calculate(new DivideOperation(), firNum, secNum); + } +} + +class Calculator { + public void calculate(AbstractOperation operation, int firstNumber, int secondNumber) { + operation.operate(firstNumber, secondNumber); + } +} + +abstract class AbstractOperation { + abstract void operate(int firstNumber, int secondNumber); +} + +class AddOperation extends AbstractOperation { + + @Override + public void operate(int firstNumber, int secondNumber) { + System.out.println(firstNumber + " + " + secondNumber + " = " + (firstNumber + secondNumber)); + } +} + +class SubtractOperation extends AbstractOperation { + + @Override + public void operate(int firstNumber, int secondNumber) { + System.out.println(firstNumber + " - " + secondNumber + " = " + (firstNumber - secondNumber)); } +} + +class MultiplyOperation extends AbstractOperation { + + @Override + public void operate(int firstNumber, int secondNumber) { + System.out.println(firstNumber + " * " + secondNumber + " = " + (firstNumber * secondNumber)); + } +} + +class DivideOperation extends AbstractOperation { + + @Override + public void operate(int firstNumber, int secondNumber) { + System.out.println(firstNumber + " / " + secondNumber + " = " + (firstNumber / secondNumber)); + } +} \ No newline at end of file diff --git a/src/yuyeongwoo/solid/srp/SRP.java b/src/yuyeongwoo/solid/srp/SRP.java new file mode 100644 index 0000000..8c52638 --- /dev/null +++ b/src/yuyeongwoo/solid/srp/SRP.java @@ -0,0 +1,67 @@ +package yuyeongwoo.solid.srp; + +public class SRP { + public static void main(String[] args) { + Calculator calculator = new Calculator(new AddOperation(), new SubtractOperation(), new MultiplyOperation(), new DivideOperation()); + int firNum = 4; + int secNum = 2; + calculator.add(firNum, secNum); + calculator.divide(firNum, secNum); + calculator.subtract(firNum, secNum); + calculator.multiply(firNum, secNum); + } +} + +class Calculator { + AddOperation add; + SubtractOperation subtract; + MultiplyOperation multiply; + DivideOperation divide; + + public Calculator(AddOperation add, SubtractOperation subtract, MultiplyOperation multiply, DivideOperation divide) { + this.add = add; + this.subtract = subtract; + this.multiply = multiply; + this.divide = divide; + } + + public void add(int firstNumber, int secondNumber) { + add.operate(firstNumber, secondNumber); + } + + public void subtract(int firstNumber, int secondNumber) { + subtract.operate(firstNumber, secondNumber); + } + + public void multiply(int firstNumber, int secondNumber) { + multiply.operate(firstNumber, secondNumber); + } + + public void divide(int firstNumber, int secondNumber) { + divide.operate(firstNumber, secondNumber); + } +} + +class AddOperation { + + public void operate(int firstNumber, int secondNumber) { + System.out.println(firstNumber + " + " + secondNumber + " = " + (firstNumber + secondNumber)); + } +} + +class SubtractOperation { + public void operate(int firstNumber, int secondNumber) { + System.out.println(firstNumber + " - " + secondNumber + " = " + (firstNumber - secondNumber)); } +} + +class MultiplyOperation { + public void operate(int firstNumber, int secondNumber) { + System.out.println(firstNumber + " * " + secondNumber + " = " + (firstNumber * secondNumber)); + } +} + +class DivideOperation { + public void operate(int firstNumber, int secondNumber) { + System.out.println(firstNumber + " / " + secondNumber + " = " + (firstNumber / secondNumber)); + } +}