diff --git a/src/choiwonbin/report1/Report1_1 b/src/choiwonbin/report1/Report1_1 index 3f37658..3bf93ba 100644 --- a/src/choiwonbin/report1/Report1_1 +++ b/src/choiwonbin/report1/Report1_1 @@ -1,3 +1,8 @@ -2-4번 문제 +//2-4. 다음 중 변수를 잘못 초기화 한 것은? +byte b = 256; +char c = ''; +char answer = 'no'; +float f = 3.14; +double d = 1.4e3f; -정답 : +정답 : b, c, answer, f \ No newline at end of file diff --git a/src/choiwonbin/report1/Report1_2 b/src/choiwonbin/report1/Report1_2 index f4e5811..b329ba7 100644 --- a/src/choiwonbin/report1/Report1_2 +++ b/src/choiwonbin/report1/Report1_2 @@ -1,3 +1,16 @@ -2-7번 번 문제 +//2-7. 다음 문장들의 출력 결과를 적으세요. 오류가 있는 문장의 경우, '오류' 라고 적으세요. +System.out.println("1" + "2"); +System.out.println(true+""); +System.out.println('A' + 'B'); +System.out.println('1' + 2); +System.out.println('1' + '2'); +System.out.println('J' +"ava"); +System.out.println(true + null); -정답 : \ No newline at end of file +정답 : 12 + true + 131 + 51 + 99 + Java + 오류 \ No newline at end of file diff --git a/src/choiwonbin/report1/Report1_3.java b/src/choiwonbin/report1/Report1_3.java index a474553..150509f 100644 --- a/src/choiwonbin/report1/Report1_3.java +++ b/src/choiwonbin/report1/Report1_3.java @@ -1,17 +1,25 @@ package choiwonbin.report1; -// 2-8 번 문제 <- 이렇게 문제 번호 작성 필수 -public class Report1_3 { - public static void main(String[] args) { - // 정답 작성 - // Ex) - AddClass addClass = new AddClass(); - addClass.test(); - } -} +//2-8. 아래는 변수 x, y, z의 값을 서로 바꾸는 예제이다. 결과와 같이 출력되도록 코드를 넣으세요. +//예상 결과 : x=2, y=3, z=1 + +class Exercise2_8 { + public static void main(String[] args){ -// 필요하다면 클래스 추가 -class AddClass { - void test() { - System.out.println("AddClass.test"); + int x = 1; + int y = 2; + int z = 3; + /* + 알맞은 코드를 넣어 완성하세요. + */ + int temp = x; + x = y; + y = z; + z = temp; + + System.out.println("x="+x); + System.out.println("y="+y); + System.out.println("z="+z); } + } + diff --git a/src/choiwonbin/report2/Report2_1 b/src/choiwonbin/report2/Report2_1 new file mode 100644 index 0000000..1e1d683 --- /dev/null +++ b/src/choiwonbin/report2/Report2_1 @@ -0,0 +1,14 @@ +//3-1. 다음 중 형변환을 생략할 수 있는 것은? (모두 고르시오) + +byte b = 10; +char ch = 'A'; +int i = 100; +long l = 1000L; + +b = (byte)i; +ch = (char)b; +short s = (short)ch; +float f = (float)l; +i = (int)ch; + +정답 : i = (int)ch; \ No newline at end of file diff --git a/src/choiwonbin/report2/Report2_2 b/src/choiwonbin/report2/Report2_2 new file mode 100644 index 0000000..aabc1d2 --- /dev/null +++ b/src/choiwonbin/report2/Report2_2 @@ -0,0 +1,31 @@ +//3-2. 다음 연산의 결과와 그 이유를 적으세요. +class Exercise3_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); + System.out.println(y += 10 - x++); + System.out.println(x += 2); + System.out.println(!('A' <= c && c <= 'Z')); + System.out.println('C' - c); + System.out.println('5' - '0'); + System.out.println(c + 1); + System.out.println(++c); + System.out.println(c++); + System.out.println(c); + } +} + +정답 : 1. true / 비교연산자는 &&가 ||보다 우위이고 y > 5가 참이므로 || 연산자에 의해 참입니다. + 2. 13 / value++는 참조 이후에 처리되기 때문에 y += 10 - 2 => y = 5 + 10 - 2 => y = 13 + 3. 4 / x는 수식에서 사용되어서 증감연산이 작용하지 않아서 x = 2이고 2 + 2 = 4 + 4. false / 'A' <= c = true, c <= 'Z' = true 둘다 모두 참이기에 ('A' <= c && c <= 'Z')는 참이고 이것의 부정은 false + 5. 2 / 'C'의 문자코드는 67이고 변수 c 즉 'A'의 문자코드는 65이므로 67 - 65 = 2 + 6. 5 / '5'는 5 '0'은 0이므로 5-0 = 5 + 7. 66 / 'A' 의 코드는 65이고 65+1=66 + 8. 'B' / ++c는 증가 후 대입 + 9. 'B' / c는 이전에 'B'가 되었고 c++는 대입후 출력 + 10. 'C' / 이전에 증가되었기에 'C' + diff --git a/src/choiwonbin/report2/Report2_2.java b/src/choiwonbin/report2/Report2_2.java deleted file mode 100644 index e2f2bfc..0000000 --- a/src/choiwonbin/report2/Report2_2.java +++ /dev/null @@ -1,7 +0,0 @@ -package choiwonbin.report2; -// 3-2번 문제 -public class Report2_2 { - public static void main(String[] args) { - - } -} diff --git a/src/choiwonbin/report2/Report2_3.java b/src/choiwonbin/report2/Report2_3.java new file mode 100644 index 0000000..fa99b7d --- /dev/null +++ b/src/choiwonbin/report2/Report2_3.java @@ -0,0 +1,10 @@ +package choiwonbin.report2; +//3-3. 아래는 변수의 num 값 중에서 백의 자리 이하를 버리는 코드이다. +//만일 변수 num의 값이 '456'이라면 '400'이 되고, '111'이라면 '100'이 된다. +//알맞은 코드를 넣으시오. +class Exercise3_3 { + public static void main(String[] args){ + int num = 456; + System.out.println(num/100*100); + } +} \ No newline at end of file diff --git a/src/choiwonbin/report2/Report2_4.java b/src/choiwonbin/report2/Report2_4.java new file mode 100644 index 0000000..29e8195 --- /dev/null +++ b/src/choiwonbin/report2/Report2_4.java @@ -0,0 +1,16 @@ +package choiwonbin.report2; + +//3-4. 아래의 코드는 사과를 담는데 필요한 바구니(버켓)의 수를 구하는 코드이다. +//만일 사과의 수가 123개이고 하나의 바구니에는 10개의 사과를 담을 수 있다면, 13개의 바구니가 필요할 것이다. +//알맞은 코드를 넣으시오. + +//예상 결과 -> 필요한 바구니의 수 :13 +class Exercise3_4 { + public static void main(String[] args){ + int numOfApples = 123; // 사과의 개수 + int sizeOfBucket = 10; // 바구니의 크기(바구니에 담을 수 있는 사과의 개수) + int numOfBucket = (numOfApples/sizeOfBucket + ((numOfApples%sizeOfBucket) > 0 ? 1 : 0)); // 모든 사과를 담는데 필요한 바구니의 수 + + System.out.println("필요한 바구니의 수 :"+numOfBucket); + } +} diff --git a/src/choiwonbin/report2/Report2_5.java b/src/choiwonbin/report2/Report2_5.java new file mode 100644 index 0000000..d21ac6c --- /dev/null +++ b/src/choiwonbin/report2/Report2_5.java @@ -0,0 +1,12 @@ +package choiwonbin.report2; + +//3-5. 아래는 변수 num의 값에 따라 '양수', '음수', '0'을 출력하는 코드이다. +//삼항연산자를 이용해서 빈칸에 알맞은 코드를 넣으시오. +//Hint : 삼항 연산자를 두 번 사용할 것! +class Exercise3_5{ + public static void main(String[] args){ + int num = 10; + System.out.println(num == 0 ? 0 : (num > 0 ? "양수" : "음수")); + } +} +//예상 결과 : 양수 \ No newline at end of file diff --git a/src/choiwonbin/report2/Report2_6.java b/src/choiwonbin/report2/Report2_6.java new file mode 100644 index 0000000..cc5c333 --- /dev/null +++ b/src/choiwonbin/report2/Report2_6.java @@ -0,0 +1,16 @@ +package choiwonbin.report2; + +//3-6. 아래는 화씨(Fahrenheit)를 섭씨(Celcius)로 변환하는 코드이다. +//변환 공식이 'C = 5/9*(F-32)'라고 할 때, 빈 칸에 알맞은 코드를 넣으시오. +// 단, 변환값은 소수점 셋째자리에서 반올림하며, Math.round() 함수를 사용하지 않고 처리할 것! + +//예상 결과 : Fahrenheit:100, Celcius:37.78 +class Exercise3_6 { + public static void main(String[] args){ + int fahrenheit = 100; + float celcius = (int)((5f/9f*(fahrenheit-32))*100 + 0.5f)/100f; + + System.out.println("Fahrenheit:"+fahrenheit); + System.out.println("Celcius:"+celcius); + } +} \ No newline at end of file diff --git a/src/choiwonbin/report3/Report3_1 b/src/choiwonbin/report3/Report3_1 index 5edd550..982327c 100644 --- a/src/choiwonbin/report3/Report3_1 +++ b/src/choiwonbin/report3/Report3_1 @@ -1,3 +1,25 @@ -// 4-1번 문제 +//4-1. 다음의 문장들을 조건식으로 표현해보세요. -정답 : +//int형 변수 x가 10보다 크고 20보다 작을 때 true인 조건식 + x > 10 && x < 20 + +//char형 변수 ch가 공백이나 탭이 아닐 때 true인 조건식 + ch != ' ' || ch != '₩t' + +//char형 변수 ch가 'x' 또는 'X'일 때 true인 조건식 + ch == 'x' || ch == 'X' + +//char형 변수 ch가 숫자('0'~'9')일 때 true인 조건식 + ch >= '0' && ch <= 9 + +//char형 변수 ch가 영문자(대문자 또는 소문자)일 때 true인 조건식 + (ch > 'a' && ch < 'z') || (ch > 'A' && 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") \ No newline at end of file diff --git a/src/choiwonbin/report3/Report3_2.java b/src/choiwonbin/report3/Report3_2.java new file mode 100644 index 0000000..99a9288 --- /dev/null +++ b/src/choiwonbin/report3/Report3_2.java @@ -0,0 +1,16 @@ +package choiwonbin.report3; + +//4-2. 1부터 20까지의 정수중에서 2 또는 3의 배수가 아닌 수의 총합을 구하세요. +class Exercise4_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); + } +} \ No newline at end of file diff --git a/src/choiwonbin/report3/Report3_3.java b/src/choiwonbin/report3/Report3_3.java new file mode 100644 index 0000000..5f0ba6e --- /dev/null +++ b/src/choiwonbin/report3/Report3_3.java @@ -0,0 +1,19 @@ +package choiwonbin.report3; + +//4-3. 1+(1+2)+(1+2+3)+(1+2+3+4)+...+(1+2+3+...+10)의 결과를 계산하세요. +class Exercise4_3 { + public static void main(String[] args) { + int sum = 0; + int totalSum = 0; + + for (int i = 1; i <= 10; i++) { + for (int j = 1; j <= i; j++) { + sum += j; + } + System.out.println(); + totalSum += sum; + sum = 0; + } + System.out.println("totalSum="+totalSum); + } +} \ No newline at end of file diff --git a/src/choiwonbin/report3/Report3_4.java b/src/choiwonbin/report3/Report3_4.java new file mode 100644 index 0000000..18a7c04 --- /dev/null +++ b/src/choiwonbin/report3/Report3_4.java @@ -0,0 +1,20 @@ +package choiwonbin.report3; + +//4-4. 1+(-2)+3+(-4)+...과 같은 식으로 계속 더해나갔을 때, +//몇까지 더해야 총합이 100 이상이 되는지 구하세요. +class Exercise4_4 { + public static void main(String[] args) { + int sum = 0; // 총합을 저장할 변수 + int s = 1; // 값의 부호를 바꿔주는데 사용할 변수 + int num = 0; + + for (int i = 1; sum < 100; i++) { + num = i * s; + sum += num; + s = -s; + } + + System.out.println("num="+num); + System.out.println("sum="+sum); + } +} \ No newline at end of file diff --git a/src/choiwonbin/report3/Report3_5 b/src/choiwonbin/report3/Report3_5 new file mode 100644 index 0000000..56ae002 --- /dev/null +++ b/src/choiwonbin/report3/Report3_5 @@ -0,0 +1,26 @@ +//4-5. 다음의 for문을 while문으로 변경하세요. +public class Exercise4_5 { + public static void main(String[] args) { + for(int i=0; i<=10; i++) { + for(int j=0; j<=i; j++) + System.out.print("*"); + System.out.println(); + } + }//end of main +} // end of class + +public class Exercise4_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++; + } + }//end of main +} // end of class \ No newline at end of file diff --git a/src/choiwonbin/report3/Report3_6.java b/src/choiwonbin/report3/Report3_6.java new file mode 100644 index 0000000..aeb2c56 --- /dev/null +++ b/src/choiwonbin/report3/Report3_6.java @@ -0,0 +1,15 @@ +package choiwonbin.report3; + +//4-6. 두 개의 주사위를 던졌을 때, 눈의 합이 6이 되는 모든 경우의 수를 출력하는 프로그램을 작성하세요. +class Exercise4_6 { + public static void main(String[] args) { + for (int i = 0; i < 6; i ++) { + for (int j = 0; j < 6; j ++) { + if (i + j == 6) { + System.out.printf("%d + %d = %d", i, j, i + j); + System.out.println(); + } + } + } + } +} \ No newline at end of file diff --git a/src/kwonseongmin/report1/Report1_1 b/src/kwonseongmin/report1/Report1_1 new file mode 100644 index 0000000..3bf93ba --- /dev/null +++ b/src/kwonseongmin/report1/Report1_1 @@ -0,0 +1,8 @@ +//2-4. 다음 중 변수를 잘못 초기화 한 것은? +byte b = 256; +char c = ''; +char answer = 'no'; +float f = 3.14; +double d = 1.4e3f; + +정답 : b, c, answer, f \ No newline at end of file diff --git a/src/kwonseongmin/report1/Report1_2 b/src/kwonseongmin/report1/Report1_2 new file mode 100644 index 0000000..b329ba7 --- /dev/null +++ b/src/kwonseongmin/report1/Report1_2 @@ -0,0 +1,16 @@ +//2-7. 다음 문장들의 출력 결과를 적으세요. 오류가 있는 문장의 경우, '오류' 라고 적으세요. +System.out.println("1" + "2"); +System.out.println(true+""); +System.out.println('A' + 'B'); +System.out.println('1' + 2); +System.out.println('1' + '2'); +System.out.println('J' +"ava"); +System.out.println(true + null); + +정답 : 12 + true + 131 + 51 + 99 + Java + 오류 \ No newline at end of file diff --git a/src/kwonseongmin/report1/Report1_3.java b/src/kwonseongmin/report1/Report1_3.java new file mode 100644 index 0000000..dc40bc5 --- /dev/null +++ b/src/kwonseongmin/report1/Report1_3.java @@ -0,0 +1,23 @@ +package kwonseongmin.report1; +//2-8. 아래는 변수 x, y, z의 값을 서로 바꾸는 예제이다. 결과와 같이 출력되도록 코드를 넣으세요. +//예상 결과 : x=2, y=3, z=1 + +class Exercise2_8 { + public static void main(String[] args){ + + int x = 1; + int y = 2; + int z = 3; + /* + 알맞은 코드를 넣어 완성하세요. + */ + int temp = x; + x = y; + y = z; + z = temp; + + System.out.println("x="+x); + System.out.println("y="+y); + System.out.println("z="+z); + } +} diff --git a/src/kwonseongmin/report2/Report2_1 b/src/kwonseongmin/report2/Report2_1 new file mode 100644 index 0000000..e0f14cf --- /dev/null +++ b/src/kwonseongmin/report2/Report2_1 @@ -0,0 +1,14 @@ +//3-1. 다음 중 형변환을 생략할 수 있는 것은? (모두 고르시오) + +byte b = 10; +char ch = 'A'; +int i = 100; +long l = 1000L; + +b = (byte)i; +ch = (char)b; +short s = (short)ch; +float f = (float)l; +i = (int)ch; + +정답 : i = (int)ch;, float f = (float)l; \ No newline at end of file diff --git a/src/kwonseongmin/report2/Report2_2 b/src/kwonseongmin/report2/Report2_2 new file mode 100644 index 0000000..aabc1d2 --- /dev/null +++ b/src/kwonseongmin/report2/Report2_2 @@ -0,0 +1,31 @@ +//3-2. 다음 연산의 결과와 그 이유를 적으세요. +class Exercise3_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); + System.out.println(y += 10 - x++); + System.out.println(x += 2); + System.out.println(!('A' <= c && c <= 'Z')); + System.out.println('C' - c); + System.out.println('5' - '0'); + System.out.println(c + 1); + System.out.println(++c); + System.out.println(c++); + System.out.println(c); + } +} + +정답 : 1. true / 비교연산자는 &&가 ||보다 우위이고 y > 5가 참이므로 || 연산자에 의해 참입니다. + 2. 13 / value++는 참조 이후에 처리되기 때문에 y += 10 - 2 => y = 5 + 10 - 2 => y = 13 + 3. 4 / x는 수식에서 사용되어서 증감연산이 작용하지 않아서 x = 2이고 2 + 2 = 4 + 4. false / 'A' <= c = true, c <= 'Z' = true 둘다 모두 참이기에 ('A' <= c && c <= 'Z')는 참이고 이것의 부정은 false + 5. 2 / 'C'의 문자코드는 67이고 변수 c 즉 'A'의 문자코드는 65이므로 67 - 65 = 2 + 6. 5 / '5'는 5 '0'은 0이므로 5-0 = 5 + 7. 66 / 'A' 의 코드는 65이고 65+1=66 + 8. 'B' / ++c는 증가 후 대입 + 9. 'B' / c는 이전에 'B'가 되었고 c++는 대입후 출력 + 10. 'C' / 이전에 증가되었기에 'C' + diff --git a/src/kwonseongmin/report2/Report2_3.java b/src/kwonseongmin/report2/Report2_3.java new file mode 100644 index 0000000..9971f59 --- /dev/null +++ b/src/kwonseongmin/report2/Report2_3.java @@ -0,0 +1,10 @@ +package kwonseongmin.report2; +//3-3. 아래는 변수의 num 값 중에서 백의 자리 이하를 버리는 코드이다. +//만일 변수 num의 값이 '456'이라면 '400'이 되고, '111'이라면 '100'이 된다. +//알맞은 코드를 넣으시오. +class Exercise3_3 { + public static void main(String[] args){ + int num = 456; + System.out.println(num/100*100); + } +} \ No newline at end of file diff --git a/src/kwonseongmin/report2/Report2_4.java b/src/kwonseongmin/report2/Report2_4.java new file mode 100644 index 0000000..05e72c2 --- /dev/null +++ b/src/kwonseongmin/report2/Report2_4.java @@ -0,0 +1,16 @@ +package kwonseongmin.report2; + +//3-4. 아래의 코드는 사과를 담는데 필요한 바구니(버켓)의 수를 구하는 코드이다. +//만일 사과의 수가 123개이고 하나의 바구니에는 10개의 사과를 담을 수 있다면, 13개의 바구니가 필요할 것이다. +//알맞은 코드를 넣으시오. + +//예상 결과 -> 필요한 바구니의 수 :13 +class Exercise3_4 { + public static void main(String[] args){ + int numOfApples = 123; // 사과의 개수 + int sizeOfBucket = 10; // 바구니의 크기(바구니에 담을 수 있는 사과의 개수) + int numOfBucket = (numOfApples/sizeOfBucket + ((numOfApples%sizeOfBucket) > 0 ? 1 : 0)); // 모든 사과를 담는데 필요한 바구니의 수 + + System.out.println("필요한 바구니의 수 :"+numOfBucket); + } +} diff --git a/src/kwonseongmin/report2/Report2_5.java b/src/kwonseongmin/report2/Report2_5.java new file mode 100644 index 0000000..ded0908 --- /dev/null +++ b/src/kwonseongmin/report2/Report2_5.java @@ -0,0 +1,12 @@ +package kwonseongmin.report2; + +//3-5. 아래는 변수 num의 값에 따라 '양수', '음수', '0'을 출력하는 코드이다. +//삼항연산자를 이용해서 빈칸에 알맞은 코드를 넣으시오. +//Hint : 삼항 연산자를 두 번 사용할 것! +class Exercise3_5{ + public static void main(String[] args){ + int num = 10; + System.out.println(num == 0 ? 0 : (num > 0 ? "양수" : "음수")); + } +} +//예상 결과 : 양수 \ No newline at end of file diff --git a/src/kwonseongmin/report2/Report2_6.java b/src/kwonseongmin/report2/Report2_6.java new file mode 100644 index 0000000..3b7c0db --- /dev/null +++ b/src/kwonseongmin/report2/Report2_6.java @@ -0,0 +1,16 @@ +package kwonseongmin.report2; + +//3-6. 아래는 화씨(Fahrenheit)를 섭씨(Celcius)로 변환하는 코드이다. +//변환 공식이 'C = 5/9*(F-32)'라고 할 때, 빈 칸에 알맞은 코드를 넣으시오. +// 단, 변환값은 소수점 셋째자리에서 반올림하며, Math.round() 함수를 사용하지 않고 처리할 것! + +//예상 결과 : Fahrenheit:100, Celcius:37.78 +class Exercise3_6 { + public static void main(String[] args){ + int fahrenheit = 100; + float celcius = (int)((5f/9f*(fahrenheit-32))*100 + 0.5f)/100f; + + System.out.println("Fahrenheit:"+fahrenheit); + System.out.println("Celcius:"+celcius); + } +} \ No newline at end of file diff --git a/src/kwonseongmin/report3/Report3_1 b/src/kwonseongmin/report3/Report3_1 new file mode 100644 index 0000000..982327c --- /dev/null +++ b/src/kwonseongmin/report3/Report3_1 @@ -0,0 +1,25 @@ +//4-1. 다음의 문장들을 조건식으로 표현해보세요. + +//int형 변수 x가 10보다 크고 20보다 작을 때 true인 조건식 + x > 10 && x < 20 + +//char형 변수 ch가 공백이나 탭이 아닐 때 true인 조건식 + ch != ' ' || ch != '₩t' + +//char형 변수 ch가 'x' 또는 'X'일 때 true인 조건식 + ch == 'x' || ch == 'X' + +//char형 변수 ch가 숫자('0'~'9')일 때 true인 조건식 + ch >= '0' && ch <= 9 + +//char형 변수 ch가 영문자(대문자 또는 소문자)일 때 true인 조건식 + (ch > 'a' && ch < 'z') || (ch > 'A' && 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") \ No newline at end of file diff --git a/src/kwonseongmin/report3/Report3_10.java b/src/kwonseongmin/report3/Report3_10.java new file mode 100644 index 0000000..b4cf0a9 --- /dev/null +++ b/src/kwonseongmin/report3/Report3_10.java @@ -0,0 +1,45 @@ +package kwonseongmin.report3; + +//4-10. 다음은 숫자맞추기 게임을 작성한 것이다. 1과 100사이의 값을 반복적으로 입력해서 +//컴퓨터가 생각한 값을 맞추면 게임이 끝난다. +//사용자가 값을 입력하면, 컴퓨터는 자신이 생각한 값과 비교해서 결과를 알려준다. +//사용자가 컴퓨터가 생각한 숫자를 맞추면 게임이 끝나고 몇 번 만에 숫자를 맞췄는지 알려준다. + +class Exercise4_14 { + public static void main(String[] args) { + // 1~100사이의 임의의 값을 얻어서 answer에 저장한다. + int answer = (int)(Math.random() * 100) + 1; + int input = 0; //사용자입력을 저장할 공간 + int count = 0; //시도횟수를 세기위한 변수 + + // 화면으로 부터 사용자입력을 받기 위해서 Scanner클래스 사용 + java.util.Scanner s = new java.util.Scanner(System.in); + do { + count++; + System.out.print("1과 100사이의 값을 입력하세요 : "); + input = s.nextInt(); //입력받은 값을 변수 input에 저장한다. + if (input == answer) { + System.out.println("맞혔습니다.\n시도횟수는" + count + "번입니다."); + break; + } else if (input > answer) { + System.out.println("더 작은 수를 입력하세요."); + } else { + System.out.println("더 큰 수를 입력하세요."); + } + } while(true); //무한반복문 + } // end of main +} // end of class +//예상 결과 +//1과 100사이의 값을 입력하세요 : 50 +//더 큰 수를 입력하세요. +//1과 100사이의 값을 입력하세요 : 75 +//더 큰 수를 입력하세요. +//1과 100사이의 값을 입력하세요 : 87 +//더 작은 수를 입력하세요. +//1과 100사이의 값을 입력하세요 : 80 +//더 작은 수를 입력하세요. +//1과 100사이의 값을 입력하세요 : 77 +//더 작은 수를 입력하세요. +//1과 100사이의 값을 입력하세요 : 76 +//맞혔습니다. +//시도횟수는 6번입니다. \ No newline at end of file diff --git a/src/kwonseongmin/report3/Report3_2.java b/src/kwonseongmin/report3/Report3_2.java new file mode 100644 index 0000000..1bcf0b0 --- /dev/null +++ b/src/kwonseongmin/report3/Report3_2.java @@ -0,0 +1,16 @@ +package kwonseongmin.report3; + +//4-2. 1부터 20까지의 정수중에서 2 또는 3의 배수가 아닌 수의 총합을 구하세요. +class Exercise4_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); + } +} \ No newline at end of file diff --git a/src/kwonseongmin/report3/Report3_3.java b/src/kwonseongmin/report3/Report3_3.java new file mode 100644 index 0000000..b85da54 --- /dev/null +++ b/src/kwonseongmin/report3/Report3_3.java @@ -0,0 +1,19 @@ +package kwonseongmin.report3; + +//4-3. 1+(1+2)+(1+2+3)+(1+2+3+4)+...+(1+2+3+...+10)의 결과를 계산하세요. +class Exercise4_3 { + public static void main(String[] args) { + int sum = 0; + int totalSum = 0; + + for (int i = 1; i <= 10; i++) { + for (int j = 1; j <= i; j++) { + sum += j; + } + System.out.println(); + totalSum += sum; + sum = 0; + } + System.out.println("totalSum="+totalSum); + } +} \ No newline at end of file diff --git a/src/kwonseongmin/report3/Report3_4.java b/src/kwonseongmin/report3/Report3_4.java new file mode 100644 index 0000000..a810be7 --- /dev/null +++ b/src/kwonseongmin/report3/Report3_4.java @@ -0,0 +1,20 @@ +package kwonseongmin.report3; + +//4-4. 1+(-2)+3+(-4)+...과 같은 식으로 계속 더해나갔을 때, +//몇까지 더해야 총합이 100 이상이 되는지 구하세요. +class Exercise4_4 { + public static void main(String[] args) { + int sum = 0; // 총합을 저장할 변수 + int s = 1; // 값의 부호를 바꿔주는데 사용할 변수 + int num = 0; + + for (int i = 1; sum < 100; i++) { + num = i * s; + sum += num; + s = -s; + } + + System.out.println("num="+num); + System.out.println("sum="+sum); + } +} \ No newline at end of file diff --git a/src/kwonseongmin/report3/Report3_5 b/src/kwonseongmin/report3/Report3_5 new file mode 100644 index 0000000..56ae002 --- /dev/null +++ b/src/kwonseongmin/report3/Report3_5 @@ -0,0 +1,26 @@ +//4-5. 다음의 for문을 while문으로 변경하세요. +public class Exercise4_5 { + public static void main(String[] args) { + for(int i=0; i<=10; i++) { + for(int j=0; j<=i; j++) + System.out.print("*"); + System.out.println(); + } + }//end of main +} // end of class + +public class Exercise4_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++; + } + }//end of main +} // end of class \ No newline at end of file diff --git a/src/kwonseongmin/report3/Report3_6.java b/src/kwonseongmin/report3/Report3_6.java new file mode 100644 index 0000000..79fa8a4 --- /dev/null +++ b/src/kwonseongmin/report3/Report3_6.java @@ -0,0 +1,15 @@ +package kwonseongmin.report3; + +//4-6. 두 개의 주사위를 던졌을 때, 눈의 합이 6이 되는 모든 경우의 수를 출력하는 프로그램을 작성하세요. +class Exercise4_6 { + public static void main(String[] args) { + for (int i = 0; i < 6; i ++) { + for (int j = 0; j < 6; j ++) { + if (i + j == 6) { + System.out.printf("%d + %d = %d", i, j, i + j); + System.out.println(); + } + } + } + } +} \ No newline at end of file diff --git a/src/kwonseongmin/report3/Report3_7.java b/src/kwonseongmin/report3/Report3_7.java new file mode 100644 index 0000000..c507fbc --- /dev/null +++ b/src/kwonseongmin/report3/Report3_7.java @@ -0,0 +1,18 @@ +package kwonseongmin.report3; + +import java.util.Arrays; + +//4-7. 숫자로 이루어진 문자열 str이 있을 때, 각 자리의 합을 더한 결과를 출력하는 코드를 완성하세요. +//만일 문자열이 "12345"라면, ‘1+2+3+4+5’의 결과인 15를 출력이 출력되어야 합니다. +class Exercise4_7 { + public static void main(String[] args) { + String str = "12345"; + int sum = 0; + + for (int i = 0; i < str.length(); i++) { + sum += Character.getNumericValue(str.charAt(i)); + } + System.out.println("sum=" + sum); + } + +}//예상 결과 : sum=15 \ No newline at end of file diff --git a/src/kwonseongmin/report3/Report3_8.java b/src/kwonseongmin/report3/Report3_8.java new file mode 100644 index 0000000..bcece48 --- /dev/null +++ b/src/kwonseongmin/report3/Report3_8.java @@ -0,0 +1,9 @@ +package kwonseongmin.report3; + +//4-8. Math.random()을 이용해서 1부터 6 사이의 임의의 정수를 변수 value에 저장하는 코드를 완성하세요. +class Exercise4_8{ + public static void main(String[] args){ + int value = (int)(Math.random() * 6 + 1); + System.out.println("value:"+ value); + } +} \ No newline at end of file diff --git a/src/kwonseongmin/report3/Report3_9.java b/src/kwonseongmin/report3/Report3_9.java new file mode 100644 index 0000000..8919b8b --- /dev/null +++ b/src/kwonseongmin/report3/Report3_9.java @@ -0,0 +1,18 @@ +package kwonseongmin.report3; + +//4-9. int 타입의 변수 num이 있을 때, 각 자리의 합을 더한 결과를 출력하는 코드를 완성하세요. +//만일 변수 num의 값이 12345라면, ‘1+2+3+4+5’의 결과인 15를 출력하세요. +//문자열로 변환하지 말고 숫자로만 처리하세요. +class Exercise4_9 { + public static void main(String[] args) { + int num = 12345; + int sum = 0; + + while(num > 0) { + sum += num % 10; + num = num / 10; + } + + System.out.println("sum="+sum); + } +}//예상 결과 : sum=15 \ No newline at end of file diff --git a/src/kwonseongmin/report4/Report4_1 b/src/kwonseongmin/report4/Report4_1 new file mode 100644 index 0000000..82f092c --- /dev/null +++ b/src/kwonseongmin/report4/Report4_1 @@ -0,0 +1,10 @@ +//5-1. 다음은 배열을 선언하거나 초기화한 것이다. 잘못된 것을 고르고 그 이유를 설명하세요. +int[] arr[]; +int[] arr = {1,2,3,}; +int[] arr = new int[5]; +int[] arr = new int[5]{1,2,3,4,5}; +int arr[5]; +int[] arr[] = new int[3][]; + +정답 : int[] arr = new int[]{1,2,3,4,5}; => 대괄호안에 배열의 크기를 지정할 수 없다. + int arr[5]; => 배열을 선언할 때는 배열의 크기를 지정할 수 없다. \ No newline at end of file diff --git a/src/kwonseongmin/report4/Report4_2 b/src/kwonseongmin/report4/Report4_2 new file mode 100644 index 0000000..c9d215e --- /dev/null +++ b/src/kwonseongmin/report4/Report4_2 @@ -0,0 +1,9 @@ +//5-2. 다음과 같은 배열이 있을 때, arr[3].length의 값은? +int[][]arr ={ + {5,5,5,5,5}, + {10,10,10}, + {20,20,20,20}, + {30,30} +}; + +정답 : 2 \ No newline at end of file diff --git a/src/kwonseongmin/report4/Report4_3.java b/src/kwonseongmin/report4/Report4_3.java new file mode 100644 index 0000000..fcb86ca --- /dev/null +++ b/src/kwonseongmin/report4/Report4_3.java @@ -0,0 +1,12 @@ +package kwonseongmin.report4; +//5-3. 배열 arr에 담긴 모든 값을 더하는 프로그램을 완성하세요. +class Exercise5_3{ + public static void main(String[] args){ + int[] arr = {10, 20, 30, 40, 50}; + int sum = 0; + for (int value : arr) { + sum += value; + } + System.out.println("sum="+sum); + } +}//예상 결과 : sum=150 \ No newline at end of file diff --git a/src/kwonseongmin/report4/Report4_4.java b/src/kwonseongmin/report4/Report4_4.java new file mode 100644 index 0000000..58b7d95 --- /dev/null +++ b/src/kwonseongmin/report4/Report4_4.java @@ -0,0 +1,29 @@ +package kwonseongmin.report4; + +//5-4. 2차원 배열 arr에 담긴 모든 총합과 평균을 구하는 프로그램을 완성하세요. +class Exercise5_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; + + int count = 0; + + for (int[] subArr : arr) { + for (int value : subArr) { + total += value; + count++; + } + } + + average = total / (float)count; + + System.out.println("total=" + total); + System.out.println("average=" + average); + } // end of main +} // end of class \ No newline at end of file diff --git a/src/kwonseongmin/report4/Report4_5.java b/src/kwonseongmin/report4/Report4_5.java new file mode 100644 index 0000000..88224ea --- /dev/null +++ b/src/kwonseongmin/report4/Report4_5.java @@ -0,0 +1,28 @@ +package kwonseongmin.report4; + +//5-5. 다음은 1과 9 사이의 중복되지 않은 숫자로 이루어진 3자리 숫자를 만들어내는 프로그램이다. +//코드를 완성하세요. 다만 Math.random()을 사용했기 때문에 실행 결과 예시와 다를 수 있습니다. +class Exercise5_5{ + public static void main(String[] args) { + int[] ballArr = { 1, 2, 3, 4, 5, 6, 7, 8, 9 }; + int[] ball3 = new int[3]; + + // 배열 ballArr의 임의의 요소를 골라서 위치를 바꾼다 + 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; + } + + // 배열 ballArr의 앞에서 3개의 수를 배열 ball3로 복사한다 + for (int i = 0; i < 3; i++) { + ball3[i] = ballArr[i]; + } + + for (int i = 0; i < ball3.length; i++) { + System.out.print(ball3[i]); + } + }//end of main +}//end of class \ No newline at end of file diff --git a/src/kwonseongmin/report4/Report4_6.java b/src/kwonseongmin/report4/Report4_6.java new file mode 100644 index 0000000..39750db --- /dev/null +++ b/src/kwonseongmin/report4/Report4_6.java @@ -0,0 +1,34 @@ +package kwonseongmin.report4; + +//5-6. 단어의 글자위치를 섞어서 보여주고 원래의 단어를 맞추는 예제이다. +//실행결과와 같이 동작하도록 빈 칸을 채우세요. +import java.util.Scanner; + +class Exercise5_13 { + 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(); // String을 char[]로 변환 + + + for (int j = 0; j < question.length; j++) { + int randomIndex = (int)(Math.random() * question.length); + char tmp = question[j]; + question[j] = question[randomIndex]; + question[randomIndex] = tmp; + } + + System.out.printf("Q%d. %s의 정답을 입력하세요 .>", i + 1, new String(question)); + String answer = scanner.nextLine(); + + // trim()으로 answer의 좌우 공백을 제거한 후, equals로 word[i]와 비교 + if (words[i].equals(answer.trim())) + System.out.printf("맞았습니다.%n%n"); + else + System.out.printf("틀렸습니다.%n%n"); + } + } //end of main +}//end of class \ No newline at end of file diff --git "a/src/kwonseongmin/report4/\353\241\234\352\267\270\354\235\270Flow.png" "b/src/kwonseongmin/report4/\353\241\234\352\267\270\354\235\270Flow.png" new file mode 100644 index 0000000..f0b79be Binary files /dev/null and "b/src/kwonseongmin/report4/\353\241\234\352\267\270\354\235\270Flow.png" differ diff --git a/src/kwonseongmin/report5/Report5_1.java b/src/kwonseongmin/report5/Report5_1.java new file mode 100644 index 0000000..eaecf9a --- /dev/null +++ b/src/kwonseongmin/report5/Report5_1.java @@ -0,0 +1,48 @@ +package kwonseongmin.report5; + + +//6-1. 다음과 같은 멤버 변수를 갖는 Student 클래스를 정의하세요. +//타입 : String, 변수명 : name, 설명 : 학생 이름 +//타입 : int, 변수명 : ban, 설명 : 반 +//타입 : int, 변수명 : no, 설명 : 번호 +//타입 : int, 변수명 : kor, 설명 : 국어 점수 +//타입 : int, 변수명 : eng, 설명 : 영어 점수 +//타입 : int, 변수명 : math, 설명 : 수학 점수 + +class Student { + String name; + int ban; + int no; + int kor; + int eng; + int math; + + public Student() { + } + + 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; + } + + // 6-2 정답 + String info() { + //홍길동, 1, 1, 100, 60, 76, 236, 78.7 + return name + ", " + ban + ", " + no + ", " + kor + ", " + eng + ", " + math + ", " + getTotal() + ", " + getAverage(); + } + + + // 6-3 정답 + int getTotal() { + return kor + eng + math; + } + + // 6-3 정답 + float getAverage() { + return Math.round(getTotal() / 3.0f * 10) / 10.0f; + } +} \ No newline at end of file diff --git a/src/kwonseongmin/report5/Report5_2.java b/src/kwonseongmin/report5/Report5_2.java new file mode 100644 index 0000000..bda94b6 --- /dev/null +++ b/src/kwonseongmin/report5/Report5_2.java @@ -0,0 +1,11 @@ +package kwonseongmin.report5; + +//6-2. 6-1에서 정의한 Student 클래스에 생성자와 info()를 추가해서 실행결과와 같은 결과를 얻도록 하세요. +class Exercise6_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 +} \ No newline at end of file diff --git a/src/kwonseongmin/report5/Report5_3.java b/src/kwonseongmin/report5/Report5_3.java new file mode 100644 index 0000000..68191ac --- /dev/null +++ b/src/kwonseongmin/report5/Report5_3.java @@ -0,0 +1,28 @@ +package kwonseongmin.report5; + +//6-2. 6-1에서 정의한 Student 클래스에 생성자와 info()를 추가해서 실행결과와 같은 결과를 얻도록 하세요. +//6-3. 연습문제 6-1에서 정의한 Student 클래스에 다음과 같이 정의된 두 개의 메서드를 추가하세요. +//1. 메서드명 : getTotal +//기능 : 국어(kor), 영어(eng), 수학(math)의 점수를 모두 더해서 반환한다. +//반환타입 : int +//매개변수 : 없음 +//2. 메서드명 : getAverage +//기능 : 총점(국어점수+영어점수+수학점수)을 과목수로 나눈 평균을 구한다. +//소수점 둘째자리에서 반올림할 것. +//반환타입 : float +//매개변수 : 없음 + +class Exercise6_3 { + public static void main(String args[]) { + Student s = new Student(); + 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()); + }//예상 결과 : 이름 : 홍길동, 총점 : 236, 평균 : 78.7 +} \ No newline at end of file diff --git a/src/kwonseongmin/report5/Report5_4 b/src/kwonseongmin/report5/Report5_4 new file mode 100644 index 0000000..78e199d --- /dev/null +++ b/src/kwonseongmin/report5/Report5_4 @@ -0,0 +1,14 @@ +//6-4. 다음의 코드에 정의된 변수들을 종류별(클래스 변수,인스턴스 변수, 지역변수)로 구분해서 적으세요. +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); //지역 변수 + } +} \ No newline at end of file diff --git a/src/kwonseongmin/report5/Report5_5 b/src/kwonseongmin/report5/Report5_5 new file mode 100644 index 0000000..6bb38ce --- /dev/null +++ b/src/kwonseongmin/report5/Report5_5 @@ -0,0 +1,22 @@ + +//6-7. 다음은 컴퓨터 게임의 병사(marine)를 클래스로 정의한 것이다. +//이 클래스의 멤버 중에 static을 붙여야 하는 것은 어떤 것들이고 그 이유는 무엇인가? +//(단, 모든 병사의 공격력과 방어력은 같아야 한다.) +class Marine { + int x=0, y=0; //Marine의 위치좌표 (x,y) + int hp = 60; //현재 체력 + int weapon = 6; //공격력 + int armor = 0; //방어력 + void weaponUp() { + weapon++; + } + void armorUp() { + armor++; + } + void move(int x, int y) { + this.x = x; + this.y = y; + } +} + +정답 : weapon, armor armorUp, weaponUp 등의 처리로 변경되었을 때 모든 Marine이 같은 값을 가져야 한다. \ No newline at end of file diff --git a/src/kwonseongmin/report6/Report6_1.txt b/src/kwonseongmin/report6/Report6_1.txt new file mode 100644 index 0000000..70246c6 --- /dev/null +++ b/src/kwonseongmin/report6/Report6_1.txt @@ -0,0 +1,70 @@ +6-8. 다음 중 생성자에 대한 설명으로 옳지 않은 것은? (모두 고르시오) +a. 모든 생성자의 이름은 클래스의 이름과 동일해야한다. +b. 생성자는 객체를 생성하기 위한 것이다. +c. 클래스에는 생성자가 반드시 하나 이상 있어야 한다. +d. 생성자가 없는 클래스는 컴파일러가 기본 생성자를 추가한다. +e. 생성자는 오버로딩 할 수 없다. + +정답 : b, e + +6-9. 다음 중 this에 대한 설명으로 맞지 않은 것은? (모두 고르시오) +a. 객체 자신을 가리키는 참조변수이다. +b. 클래스 내에서라면 어디서든 사용할 수 있다. +c. 지역변수와 인스턴스변수를 구별할 때 사용한다. +d. 클래스 메서드 내에서는 사용할 수 없다. + +정답 : b + +6-10. 다음 중 오버로딩이 성립하기 위한 조건이 아닌 것은? (모두 고르시오) +a. 메서드의 이름이 같아야 한다. +b. 매개변수의 개수나 타입이 달라야 한다. +c. 리턴타입이 달라야 한다. +d. 매개변수의 이름이 달라야 한다. + +정답 : c, d + +6-11. 다음 중 아래의 add메서드를 올바르게 오버로딩 한 것은? (모두 고르시오) +> long add(int a, int b) { return a+b; } + +a. long add(int x, int y) { return x+y; } +b. long add(long a, long b) { return a+b; } +c. int add(byte a, byte b) { return a+b; } +d. int add(long a, int b) { return (int)(a+b); } + +정답 : b, c, d + +6-12. 다음 중 초기화에 대한 설명으로 옳지 않은 것은? (모두 고르시오) +a. 멤버변수는 자동 초기화되므로 초기화하지 않고도 값을 참고할 수 있다. +b. 지역변수는 사용하기 전에 반드시 초기화해야 한다. +c. 초기화 블럭보다 생성자가 먼저 수행된다. +d. 명시적 초기화를 제일 우선적으로 고려해야 한다. +e. 클래스변수보다 인스턴스변수가 먼저 초기화된다 + +정답 : c, e + +6-13. 다음 중 인스턴스변수의 초기화 순서가 올바른 것은? +a. 기본값-명시적초기화-초기화블럭-생성자 +b. 기본값-명시적초기화-생성자-초기화블럭 +c. 기본값-초기화블럭-명시적초기화-생성자 +d. 기본값-초기화블럭-생성자-명시적초기화 + +정답 : a + +6-14. 다음 중 지역변수에 대한 설명으로 옳지 않은 것은? (모두 고르시오) +a. 자동 초기화되므로 별도의 초기화가 필요없다. +b. 지역변수가 선언된 메서드가 종료되면 지역변수도 함께 소멸된다. +c. 메서드의 매개변수로 선언된 변수도 지역변수이다. +d. 클래스변수나 인스턴스변수보다 메모리 부담이 적다. +e. 힙(heap)영역에 생성되며 가비지 컬렉터에 의해 소멸된다. + +정답 : a, e + +6-15. 호출스택이 다음과 같은 상황일 때 옳지 않은 설명은? (모두 고르시오) +a. 제일 먼저 호출스택에 저장된 것은 main메서드이다. +b. println메서드를 제외한 나머지 메서드들은 모두 종료된 상태이다. +c. method2메서드를 호출한 것은 main메서드이다. +d. println메서드가 종료되면 method1메서드가 수행을 재개한다. +e. main-method2-method1-println의 순서로 호출되었다. +f. 현재 실행중인 메서드는 println뿐이다. + +정답 : b \ No newline at end of file diff --git a/src/kwonseongmin/report6/Report6_2.txt b/src/kwonseongmin/report6/Report6_2.txt new file mode 100644 index 0000000..9b644c7 --- /dev/null +++ b/src/kwonseongmin/report6/Report6_2.txt @@ -0,0 +1,16 @@ +//6-16. 다음 코드의 실행 결과를 예측하여 적어주세요. +class Exercise6_19 { + 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 출력 \ No newline at end of file diff --git a/src/kwonseongmin/report7/Report7_1.java b/src/kwonseongmin/report7/Report7_1.java new file mode 100644 index 0000000..18d255d --- /dev/null +++ b/src/kwonseongmin/report7/Report7_1.java @@ -0,0 +1,32 @@ +package kwonseongmin.report7; +// 6-17번 문제 +class Exercise6_17 { + + + static int[] shuffle(int[] array) { + if(array==null || array.length==0) { + return array; + }; + for (int i = 0; i < array.length; i++) { + + int rIndex = (int)(Math.random() * array.length); + + int temp = array[i]; + + array[i] = array[rIndex]; + + array[rIndex] = temp; + } + + return array; + + } + + 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)); + } +} diff --git a/src/kwonseongmin/report7/Report7_2.java b/src/kwonseongmin/report7/Report7_2.java new file mode 100644 index 0000000..eee8449 --- /dev/null +++ b/src/kwonseongmin/report7/Report7_2.java @@ -0,0 +1,28 @@ +package kwonseongmin.report7; + +class Exercise6_18 { + + static boolean isNumber(String str) { + if(str==null || str.equals("")) { + return false; + }; + + char[] arr = str.toCharArray(); + + for (char ch : arr) { + if (ch < '0' || ch > '9') { + return false; + } + } + + return true; + } + + public static void main(String[] args) { + String str = "123"; + System.out.println(str + " 는 숫자입니까? " + isNumber(str)); + str = "1234o"; + System.out.println(str + " 는 숫자입니까? " + isNumber(str)); + } +} +//예상 결과 : 123는 숫자입니까? true, 1234o는 숫자입니까? false diff --git a/src/kwonseongmin/report7/Report7_3.java b/src/kwonseongmin/report7/Report7_3.java new file mode 100644 index 0000000..e9b1e6f --- /dev/null +++ b/src/kwonseongmin/report7/Report7_3.java @@ -0,0 +1,65 @@ +package kwonseongmin.report7; + +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로 바꾼다. + this.isPowerOn = !this.isPowerOn; + } + void volumeUp() { + // (2) volume의 값이 MAX_VOLUME보다 작을 때만 값을 1 증가시킨다. + if (this.volume < MAX_VOLUME) { + this.volume++; + } + } + void volumeDown() { + // (3) volume의 값이 MIN_VOLUME보다 클 때만 값을 1 감소시킨다. + if (this.volume > MIN_VOLUME) { + this.volume--; + } + } + void channelUp() { + // (4) channel의 값을 1 증가시킨다. + // 만일 channel이 MAX_CHANNEL이면 , channel의 값을 MIN_CHANNEL로 바꾼다. + this.channel = this.channel < MAX_CHANNEL ? ++this.channel : MIN_CHANNEL; + } + void channelDown() { + // (5) channel의 값을 1 감소시킨다 . + // 만일 channel이 MIN_CHANNEL이면, channel의 값을 MAX_CHANNEL로 바꾼다. + this.channel = this.channel > MIN_CHANNEL ? --this.channel : MAX_CHANNEL; + } + + public MyTv() { + } + + public MyTv(boolean isPowerOn, int channel, int volume) { + + this.isPowerOn = isPowerOn; + this.channel = channel; + this.volume = volume; + } +} + +class Exercise6_19 { + 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); + } +} + +//예상 결과 : CH: 100, VOL: 0 / CH: 99, VOL: 0 / CH: 100, VOL: 100 \ No newline at end of file diff --git a/src/kwonseongmin/report7/Report7_4.java b/src/kwonseongmin/report7/Report7_4.java new file mode 100644 index 0000000..41549d9 --- /dev/null +++ b/src/kwonseongmin/report7/Report7_4.java @@ -0,0 +1,18 @@ +package kwonseongmin.report7; + +import java.util.*; +class Exercise6_20 { + /* (1) max 메서드를 작성하시오 . */ + 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[]{})); // 크기가 0인 배열 } + } + + static int max(int[] arr) { + + return 0; + } +} \ No newline at end of file diff --git a/src/kwonseongmin/report7/Report7_5.java b/src/kwonseongmin/report7/Report7_5.java new file mode 100644 index 0000000..dc4abba --- /dev/null +++ b/src/kwonseongmin/report7/Report7_5.java @@ -0,0 +1,18 @@ +package kwonseongmin.report7; + +class Exercise6_21 { + /* (1) abs 메서드를 작성하시오. */ + static int abs(int value) { + return value >= 0 ? value : -value; + } + + public static void main(String[] args) { + int value = 5; + System.out.println(value + "의 절대값 :" + abs(value)); + value = -10; + System.out.println(value + "의 절대값 :" + abs(value)); + } + + +} +//예상 결과 : 5의 절대값 : 5 / -10의 절대값 : 10 \ No newline at end of file diff --git a/src/kwonseongmin/report8/MyTv.java b/src/kwonseongmin/report8/MyTv.java new file mode 100644 index 0000000..cfd1cf8 --- /dev/null +++ b/src/kwonseongmin/report8/MyTv.java @@ -0,0 +1,39 @@ +package kwonseongmin.report8; + +public 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 boolean isPowerOn() { + return isPowerOn; + } + + public void setPowerOn(boolean powerOn) { + isPowerOn = powerOn; + } + + 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; + this.channel = channel; + } + public int getChannel(){ + return channel; + } +} diff --git a/src/kwonseongmin/report8/MyTv2.java b/src/kwonseongmin/report8/MyTv2.java new file mode 100644 index 0000000..cdc7be7 --- /dev/null +++ b/src/kwonseongmin/report8/MyTv2.java @@ -0,0 +1,40 @@ +package kwonseongmin.report8; + +public 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() { + this.setChannel(this.prevChannel); + } +} diff --git a/src/kwonseongmin/report8/Product.java b/src/kwonseongmin/report8/Product.java new file mode 100644 index 0000000..09be1bc --- /dev/null +++ b/src/kwonseongmin/report8/Product.java @@ -0,0 +1,14 @@ +package kwonseongmin.report8; + +public class Product { + int price; // 제품의 가격 + int bonusPoint; // 제품구매 시 제공하는 보너스점수 + + public Product() { + } + + Product(int price) { + this.price = price; + bonusPoint = (int) (price / 10.0); + } +} \ No newline at end of file diff --git a/src/kwonseongmin/report8/Report8_1.java b/src/kwonseongmin/report8/Report8_1.java new file mode 100644 index 0000000..51a1a55 --- /dev/null +++ b/src/kwonseongmin/report8/Report8_1.java @@ -0,0 +1,13 @@ +package kwonseongmin.report8; + + + +class Exercise7_1 { + public static void main(String args[]) { + SutdaDeck deck = new SutdaDeck(); + + for (int i = 0; i < deck.cards.length; i++) + System.out.print(deck.cards[i] + ","); + } +} +// 예상결과) 1K,2,3K,4,5,6,7,8K,9,10,1,2,3,4,5,6,7,8,9,10, \ No newline at end of file diff --git a/src/kwonseongmin/report8/Report8_2.java b/src/kwonseongmin/report8/Report8_2.java new file mode 100644 index 0000000..b1f4e66 --- /dev/null +++ b/src/kwonseongmin/report8/Report8_2.java @@ -0,0 +1,22 @@ +package kwonseongmin.report8; + +class Exercise7_2 { + public static void main(String args[]) { + SutdaDeck deck = new SutdaDeck(); + + System.out.println(deck.pick(0)); + System.out.println(deck.pick()); + deck.shuffle(); + + for(int i=0; i < deck.cards.length;i++) + System.out.print(deck.cards[i]+","); + + System.out.println(); + System.out.println(deck.pick(0)); + } +} +//예상결과) +// 1K +// 7 +// 2,6,10,1K,7,3,10,5,7,8,5,1,2,9,6,9,4,8K,4,3K, +// 2 \ No newline at end of file diff --git a/src/kwonseongmin/report8/Report8_3.txt b/src/kwonseongmin/report8/Report8_3.txt new file mode 100644 index 0000000..b413af8 --- /dev/null +++ b/src/kwonseongmin/report8/Report8_3.txt @@ -0,0 +1,3 @@ +7-3. 다음의 코드는 컴파일하면 에러가 발생한다. 그 이유를 설명하고 에러를 수정하기 위해서는 코드를 어떻게 바꾸어야 하는가? + +정답 : Tv가 상속받는 Product의 기본 생성자가 없어서 발생하는 문제 Product에서 기본 생성자를 만들거나 Tv에서 Product에 존재하는 생성자를 통해 초기화시켜준다. \ No newline at end of file diff --git a/src/kwonseongmin/report8/Report8_4.java b/src/kwonseongmin/report8/Report8_4.java new file mode 100644 index 0000000..c299cdc --- /dev/null +++ b/src/kwonseongmin/report8/Report8_4.java @@ -0,0 +1,12 @@ +package kwonseongmin.report8; + +class Exercise7_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()); + } +} \ No newline at end of file diff --git a/src/kwonseongmin/report8/Report8_5.java b/src/kwonseongmin/report8/Report8_5.java new file mode 100644 index 0000000..0fa1c94 --- /dev/null +++ b/src/kwonseongmin/report8/Report8_5.java @@ -0,0 +1,21 @@ +package kwonseongmin.report8; + +class Exercise7_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()); + } +} + + +// CH:10 +// CH:20 +// CH:10 +// CH:20 \ No newline at end of file diff --git a/src/kwonseongmin/report8/SutdaCard.java b/src/kwonseongmin/report8/SutdaCard.java new file mode 100644 index 0000000..9374dbb --- /dev/null +++ b/src/kwonseongmin/report8/SutdaCard.java @@ -0,0 +1,20 @@ +package kwonseongmin.report8; + +public class SutdaCard { + int num; + boolean isKwang; + + SutdaCard() { + this(1, true); + } + + SutdaCard(int num, boolean isKwang) { + this.num = num; + this.isKwang = isKwang; + } + + // info()대신 Object클래스의 toString()을 오버라이딩했다. + public String toString() { + return num + ( isKwang ? "K":""); + } +} \ No newline at end of file diff --git a/src/kwonseongmin/report8/SutdaDeck.java b/src/kwonseongmin/report8/SutdaDeck.java new file mode 100644 index 0000000..dd9c72b --- /dev/null +++ b/src/kwonseongmin/report8/SutdaDeck.java @@ -0,0 +1,37 @@ +package kwonseongmin.report8; + + +public class SutdaDeck { + final int CARD_NUM = 20; + SutdaCard[] cards = new SutdaCard[CARD_NUM]; + + SutdaDeck() { + // (구현) 배열 SutdaCard를 적절히 초기화 하시오. + for (int i = 0; i < cards.length; i++) { + int num = i % 10 + 1; + boolean isKwang = i+1 == 1 || i+1 == 3 || i+1 == 8; + + SutdaCard card = new SutdaCard(num, isKwang); + cards[i] = card; + } + } + + void shuffle() { + for (int i = 0; i < cards.length - 1; i++) { + SutdaCard temp = cards[i]; + int randomIndex = (int)(Math.random() * cards.length); + + cards[i] = cards[randomIndex]; + + cards[randomIndex] = temp; + } + } + + SutdaCard pick(int index) { + return cards[index]; + } + + SutdaCard pick() { + return cards[(int)(Math.random() * cards.length)]; + } +} \ No newline at end of file diff --git a/src/kwonseongmin/report8/Tv.java b/src/kwonseongmin/report8/Tv.java new file mode 100644 index 0000000..7ae286d --- /dev/null +++ b/src/kwonseongmin/report8/Tv.java @@ -0,0 +1,11 @@ +package kwonseongmin.report8; + +public class Tv extends Product { + Tv() { +// super(0); + } + + public String toString() { + return "Tv"; + } +} \ No newline at end of file diff --git a/src/studentenglishname/report1/Report1_1 b/src/studentenglishname/report1/Report1_1 deleted file mode 100644 index 3f37658..0000000 --- a/src/studentenglishname/report1/Report1_1 +++ /dev/null @@ -1,3 +0,0 @@ -2-4번 문제 - -정답 : diff --git a/src/studentenglishname/report1/Report1_2 b/src/studentenglishname/report1/Report1_2 deleted file mode 100644 index f4e5811..0000000 --- a/src/studentenglishname/report1/Report1_2 +++ /dev/null @@ -1,3 +0,0 @@ -2-7번 번 문제 - -정답 : \ No newline at end of file diff --git a/src/studentenglishname/report1/Report1_3.java b/src/studentenglishname/report1/Report1_3.java deleted file mode 100644 index 5b75b9c..0000000 --- a/src/studentenglishname/report1/Report1_3.java +++ /dev/null @@ -1,17 +0,0 @@ -package studentenglishname.report1; -// 2-8 번 문제 <- 이렇게 문제 번호 작성 필수 -public class Report1_3 { - public static void main(String[] args) { - // 정답 작성 - // Ex) - AddClass addClass = new AddClass(); - addClass.test(); - } -} - -// 필요하다면 클래스 추가 -class AddClass { - void test() { - System.out.println("AddClass.test"); - } -} diff --git a/src/studentenglishname/report2/Report2_2.java b/src/studentenglishname/report2/Report2_2.java deleted file mode 100644 index 71dbb48..0000000 --- a/src/studentenglishname/report2/Report2_2.java +++ /dev/null @@ -1,7 +0,0 @@ -package studentenglishname.report2; -// 3-2번 문제 -public class Report2_2 { - public static void main(String[] args) { - - } -}