diff --git a/hanghae-java-study-12.iml b/hanghae-java-study-12.iml new file mode 100644 index 0000000..c90834f --- /dev/null +++ b/hanghae-java-study-12.iml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/src/LEESEUNGRYEOL/report1/Report1_1 b/src/LEESEUNGRYEOL/report1/Report1_1 new file mode 100644 index 0000000..890cc3b --- /dev/null +++ b/src/LEESEUNGRYEOL/report1/Report1_1 @@ -0,0 +1,3 @@ +2-4번 문제 + +정답 : 1,2,3,4,5 diff --git a/src/LEESEUNGRYEOL/report1/Report1_2 b/src/LEESEUNGRYEOL/report1/Report1_2 new file mode 100644 index 0000000..7ea19d0 --- /dev/null +++ b/src/LEESEUNGRYEOL/report1/Report1_2 @@ -0,0 +1,9 @@ +2-7번 번 문제 + +정답 : 1: 12 + 2: true + 3: 131 + 4: 51 + 5: 99 + 6: Java + 7: 오류 \ No newline at end of file diff --git a/src/LEESEUNGRYEOL/report1/Report1_3.java b/src/LEESEUNGRYEOL/report1/Report1_3.java new file mode 100644 index 0000000..030ce1d --- /dev/null +++ b/src/LEESEUNGRYEOL/report1/Report1_3.java @@ -0,0 +1,28 @@ +package LEESEUNGRYEOL.report1; +// 2-8번 문제 +public class Report1_3 { + public static void main(String[] args) { + AddClass addClass = new AddClass(); + addClass.test(); + } +} + +// 필요하다면 클래스 추가 +class AddClass { + void test() { + int x = 1; + int y = 2; + int z = 3; + + int tmp; + + tmp = x; + x = y; + y = z; + z = tmp; + + System.out.println("x=" + x); + System.out.println("y=" + y); + System.out.println("z=" + z); + } +} diff --git a/src/LEESEUNGRYEOL/report2/Report2_1 b/src/LEESEUNGRYEOL/report2/Report2_1 new file mode 100644 index 0000000..8339568 --- /dev/null +++ b/src/LEESEUNGRYEOL/report2/Report2_1 @@ -0,0 +1,3 @@ +3-1번 문제 + +정답 : 4,5 \ No newline at end of file diff --git a/src/LEESEUNGRYEOL/report2/Report2_2 b/src/LEESEUNGRYEOL/report2/Report2_2 new file mode 100644 index 0000000..b85ede3 --- /dev/null +++ b/src/LEESEUNGRYEOL/report2/Report2_2 @@ -0,0 +1,13 @@ +3-2번 문제 + +정답: +true (y가 0보다 크기 때문에) +13 ( 15-2 = 13 ) +5 ( 3 + 2 = 5 ) +false ( +2 (c의 아스키코드는 68 - 65 = 3) +5 ('5'의 아스키코드 53에서 '0'의 아스키코드 48을 빼면 53 - 48 = 5) +66 (65 + 1 = 66 => 'B'의 아스키코드) +B (66 => 'B'의 아스키코드) +B (참조 후 연산이므로 아직,66 =>'B'의 아스키코드) +C (c++ 로 참조 된 이후에 67이 되었으므로 67 => 'C'의 아스키 코드) \ No newline at end of file diff --git a/src/LEESEUNGRYEOL/report2/Report2_3.java b/src/LEESEUNGRYEOL/report2/Report2_3.java new file mode 100644 index 0000000..267f2b9 --- /dev/null +++ b/src/LEESEUNGRYEOL/report2/Report2_3.java @@ -0,0 +1,18 @@ +package LEESEUNGRYEOL.report2; +// 3-3번 문제 +public class Report2_3 { + public static void main(String[] args) { + Find_num findNum = new Find_num(); + int num = 456; + findNum.result(num); + } +} + +class Find_num{ + void result(int num) + { + int remainder = num % 100; + int result = num - remainder; + System.out.println(result); + } +} diff --git a/src/LEESEUNGRYEOL/report2/Report2_4.java b/src/LEESEUNGRYEOL/report2/Report2_4.java new file mode 100644 index 0000000..000d389 --- /dev/null +++ b/src/LEESEUNGRYEOL/report2/Report2_4.java @@ -0,0 +1,28 @@ +package LEESEUNGRYEOL.report2; +// 3-4번 문제 +public class Report2_4 { + public static void main(String[] args){ + + int numOfApples = 1431; // 사과의 개수 + int sizeOfBucket = 10; // 바구니의 크기(바구니에 담을 수 있는 사과의 개수) + Find_bucket find_bucket = new Find_bucket(); + find_bucket.result(numOfApples,sizeOfBucket); + + } +} + +class Find_bucket{ + void result(int a, int b) + { + int numOfBucket; + if (a%b != 0) + { + numOfBucket = (a/b + 1); // 모든 사과를 담는데 필요한 바구니의 수 + } + else { + numOfBucket = a/b; + } + System.out.println("필요한 바구니의 수 :"+numOfBucket); + } + +} \ No newline at end of file diff --git a/src/LEESEUNGRYEOL/report2/Report2_5.java b/src/LEESEUNGRYEOL/report2/Report2_5.java new file mode 100644 index 0000000..ea76811 --- /dev/null +++ b/src/LEESEUNGRYEOL/report2/Report2_5.java @@ -0,0 +1,18 @@ +package LEESEUNGRYEOL.report2; +// 3-5번 +public class Report2_5 { + public static void main(String[] args) { + int num = 10; + Num_Category num_category = new Num_Category(); + num_category.print_category(num); + } +} + +class Num_Category{ + void print_category(int num) + { + String result; + result = num > 0 ? "양수" : (num == 0 ? "0" : "음수"); + System.out.println(result); + } +} \ No newline at end of file diff --git a/src/LEESEUNGRYEOL/report2/Report2_6.java b/src/LEESEUNGRYEOL/report2/Report2_6.java new file mode 100644 index 0000000..3747bc5 --- /dev/null +++ b/src/LEESEUNGRYEOL/report2/Report2_6.java @@ -0,0 +1,36 @@ +package LEESEUNGRYEOL.report2; +// 3-6번 문제 +public class Report2_6 { + public static void main(String[] args) { + int fahrenheit = 100; + Calculate_cel calculate_cel = new Calculate_cel(); + calculate_cel.cal(fahrenheit); + +// int a = 3; +// float b = 5/9f; +// float c = a*b; +// System.out.println(b); + } +} + +class Calculate_cel{ + void cal(int fahrenheit) + { + float celcius; + celcius = (5/9f) * (fahrenheit -32); + int a = (int) (celcius * 1000); + + // b = 올림할껀지 반올림할껀지 판단하는 숫자 + int b = a % 10; + if (b >= 5) + { + a += 10; + } + a = a/10; + + celcius = (float) (a * 0.01); + + System.out.println("Fahrenheit:" + fahrenheit); + System.out.println("Celcius:" + celcius); + } +} \ No newline at end of file diff --git a/src/LEESEUNGRYEOL/report3/Report3_1 b/src/LEESEUNGRYEOL/report3/Report3_1 new file mode 100644 index 0000000..472ba31 --- /dev/null +++ b/src/LEESEUNGRYEOL/report3/Report3_1 @@ -0,0 +1,20 @@ +// 4-1번 문제 + +정답 : + +1. int형 변수 x가 10보다 크고 20보다 작을 때 true인 조건식 + 답: 10 < x && x <20 +2. char형 변수 ch가 공백이나 탭이 아닐 때 true인 조건식 + 답: ch != ' ' && ch != '\t' +3. char형 변수 ch가 'x' 또는 'X'일 때 true인 조건식 + 답: ch == 'x' || ch == 'X' +4. char형 변수 ch가 숫자('0'~'9')일 때 true인 조건식 + 답: ch >= '0' && ch <= '9' +5. char형 변수 ch가 영문자(대문자 또는 소문자)일 때 true인 조건식 + 답: ch >= 'a' && ch < 'Z' +6. int형 변수 year가 400으로 나눠떨어지거나 또는 4로 나눠떨어지고 100으로 나눠떨어지지 않을때 true인 조건식 + 답: year%400 ==0 || year%400==0 && year%100 !=0 +7. boolean형 변수 powerOn이 false일 때 true인 조건식 + 답: !(powerOn = false) +8. 문자열 참조변수 str이 "yes"일 때 true인 조건식 + 답: str.equals("yes") diff --git a/src/LEESEUNGRYEOL/report3/Report3_10.java b/src/LEESEUNGRYEOL/report3/Report3_10.java new file mode 100644 index 0000000..e934432 --- /dev/null +++ b/src/LEESEUNGRYEOL/report3/Report3_10.java @@ -0,0 +1,32 @@ +package LEESEUNGRYEOL.report3; +//4-10 문제 +public class Report3_10 { + 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(answer < input) + { + System.out.println("더 작은 수를 입력하세요."); + } + if(answer == input) { + System.out.println("맞혔습니다."); + System.out.println("시도횟수는 " + count + "번입니다."); + break; + } + if(answer > input) + { + System.out.println("더 큰 수를 입력하세요."); + } + } while(true); //무한반복문 + } // end of main + +} diff --git a/src/LEESEUNGRYEOL/report3/Report3_2.java b/src/LEESEUNGRYEOL/report3/Report3_2.java new file mode 100644 index 0000000..c060ff4 --- /dev/null +++ b/src/LEESEUNGRYEOL/report3/Report3_2.java @@ -0,0 +1,14 @@ +package LEESEUNGRYEOL.report3; +// 4-2번 문제 +public class Report3_2 { + public static void main(String[] args){ + int sum = 0; + for (int i = 1; i <= 10 ; i++) { + if(i%2 != 0 && i%3 != 0) + { + sum += i; + } + } + System.out.println("sum = " + sum); + } +} diff --git a/src/LEESEUNGRYEOL/report3/Report3_3.java b/src/LEESEUNGRYEOL/report3/Report3_3.java new file mode 100644 index 0000000..2a458b2 --- /dev/null +++ b/src/LEESEUNGRYEOL/report3/Report3_3.java @@ -0,0 +1,21 @@ +package LEESEUNGRYEOL.report3; +// 4-3 문제 +public class Report3_3 { + public static void main(String [] args) + { + int sum = 0; + int totalSum = 0; + + for (int i = 1; i < 6 ; i++) { + + for (int j = 1; j <= i; j++) { + sum += j; + } + + totalSum += sum; + sum = 0; + } + + System.out.println("totalSum = " + totalSum); + } +} diff --git a/src/LEESEUNGRYEOL/report3/Report3_4.java b/src/LEESEUNGRYEOL/report3/Report3_4.java new file mode 100644 index 0000000..2606da0 --- /dev/null +++ b/src/LEESEUNGRYEOL/report3/Report3_4.java @@ -0,0 +1,32 @@ +package LEESEUNGRYEOL.report3; +// 4-4 문제 +public class Report3_4 { + public static void main(String [] args) + { + int sum = 0; + int s = 1; + int num = 0; + + + + while (sum < 5) + { + num++; + + if(s == 1) + { + sum = sum + num; + } + else + { + sum = sum -num; + } + + s = -s; + } + + System.out.println("num = " + num); + System.out.println("sum = " + sum); + } + +} diff --git a/src/LEESEUNGRYEOL/report3/Report3_5.java b/src/LEESEUNGRYEOL/report3/Report3_5.java new file mode 100644 index 0000000..999033f --- /dev/null +++ b/src/LEESEUNGRYEOL/report3/Report3_5.java @@ -0,0 +1,27 @@ +package LEESEUNGRYEOL.report3; + +// 4-5 문제 +public class Report3_5 { + public static void main(String args[]) { + int i = 0, j = 0; + + + while (i < 5) + { + //i = 2 j = 3 + + while (j <= i) { + System.out.print('*'); + j++; + } + + + i++; + j = 0; + System.out.println(); + + } + + + } +} diff --git a/src/LEESEUNGRYEOL/report3/Report3_6.java b/src/LEESEUNGRYEOL/report3/Report3_6.java new file mode 100644 index 0000000..197b737 --- /dev/null +++ b/src/LEESEUNGRYEOL/report3/Report3_6.java @@ -0,0 +1,15 @@ +package LEESEUNGRYEOL.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+")"); + } + } + } + } +} diff --git a/src/LEESEUNGRYEOL/report3/Report3_7.java b/src/LEESEUNGRYEOL/report3/Report3_7.java new file mode 100644 index 0000000..d59e234 --- /dev/null +++ b/src/LEESEUNGRYEOL/report3/Report3_7.java @@ -0,0 +1,17 @@ +package LEESEUNGRYEOL.report3; +//4-7 문제 +public class Report3_7 { + public static void main(String[] args) { + String str = "12345"; + int sum = 0;; + int number; + + + for (int i = 0; i < str.length(); i++) { + number = str.charAt(i) - 48; + sum += number; + } + + System.out.println("sum=" + sum); + } +} diff --git a/src/LEESEUNGRYEOL/report3/Report3_8.java b/src/LEESEUNGRYEOL/report3/Report3_8.java new file mode 100644 index 0000000..d86d6b0 --- /dev/null +++ b/src/LEESEUNGRYEOL/report3/Report3_8.java @@ -0,0 +1,8 @@ +package LEESEUNGRYEOL.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/LEESEUNGRYEOL/report3/Report3_9.java b/src/LEESEUNGRYEOL/report3/Report3_9.java new file mode 100644 index 0000000..33c467d --- /dev/null +++ b/src/LEESEUNGRYEOL/report3/Report3_9.java @@ -0,0 +1,19 @@ +package LEESEUNGRYEOL.report3; +//4-9 문제 +public class Report3_9 { + public static void main(String[] args) { + int num = 12345; + int sum = 0; + int a =1,b =1; + + while (a != 0) + { + a = num / 10; + b = num % 10; + sum = sum +b; + num = a; + } + + System.out.println("sum="+sum); + } +} diff --git a/src/LEESEUNGRYEOL/report4/Login_diagram.drawio.png b/src/LEESEUNGRYEOL/report4/Login_diagram.drawio.png new file mode 100644 index 0000000..2326a14 Binary files /dev/null and b/src/LEESEUNGRYEOL/report4/Login_diagram.drawio.png differ diff --git a/src/LEESEUNGRYEOL/report4/Report4_1 b/src/LEESEUNGRYEOL/report4/Report4_1 new file mode 100644 index 0000000..7f1d80f --- /dev/null +++ b/src/LEESEUNGRYEOL/report4/Report4_1 @@ -0,0 +1,9 @@ +// 5-1 번 문제 + +정답 : + 1. int[] arr[]; (가능) + 2. int[] arr = {1,2,3,}; (마지막에 쉼표를 넣어도 가능) + 3. int[] arr = new int[5]; (가능) + 4. int[] arr = new int[5]{1,2,3,4,5}; (불가능, 뒤에 5를 빼야함.) + 5. int arr[5]; (불가능, 배열을 선언할때는 크기 지정 불가능) + 6. int[] arr[] = new int[3][]; (가능) \ No newline at end of file diff --git a/src/LEESEUNGRYEOL/report4/Report4_2 b/src/LEESEUNGRYEOL/report4/Report4_2 new file mode 100644 index 0000000..d4b33aa --- /dev/null +++ b/src/LEESEUNGRYEOL/report4/Report4_2 @@ -0,0 +1,3 @@ +// 5-2 번 문제 + +정답 :2 \ No newline at end of file diff --git a/src/LEESEUNGRYEOL/report4/Report4_3.java b/src/LEESEUNGRYEOL/report4/Report4_3.java new file mode 100644 index 0000000..49de35a --- /dev/null +++ b/src/LEESEUNGRYEOL/report4/Report4_3.java @@ -0,0 +1,12 @@ +package LEESEUNGRYEOL.report4; + +public class Report4_3 { + public static void main(String[] args){ + int[] arr = {10, 20, 30, 40, 50}; + int sum = 0; + for (int i = 0; i < arr.length; i++) { + sum += arr[i]; + } + System.out.println("sum="+sum); + } +} diff --git a/src/LEESEUNGRYEOL/report4/Report4_4.java b/src/LEESEUNGRYEOL/report4/Report4_4.java new file mode 100644 index 0000000..f3dfb83 --- /dev/null +++ b/src/LEESEUNGRYEOL/report4/Report4_4.java @@ -0,0 +1,28 @@ +package LEESEUNGRYEOL.report4; + +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; + int count = 0; + float average = 0; + + + for (int i = 0; i < arr.length; i++) { + for (int j = 0; j < arr[i].length; j++) { + total += arr[i][j]; + count++; + } + } + + average = (float)(total)/count; + System.out.println("total=" + total); + System.out.println("average=" + average); + } +} diff --git a/src/LEESEUNGRYEOL/report4/Report4_5.java b/src/LEESEUNGRYEOL/report4/Report4_5.java new file mode 100644 index 0000000..d23c0a9 --- /dev/null +++ b/src/LEESEUNGRYEOL/report4/Report4_5.java @@ -0,0 +1,24 @@ +package LEESEUNGRYEOL.report4; + +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]; + } + + for (int i = 0; i < ball3.length; i++) { + System.out.print(ball3[i]); + } + } +} diff --git a/src/LEESEUNGRYEOL/report4/Report4_6.java b/src/LEESEUNGRYEOL/report4/Report4_6.java new file mode 100644 index 0000000..b01c1fd --- /dev/null +++ b/src/LEESEUNGRYEOL/report4/Report4_6.java @@ -0,0 +1,31 @@ +package LEESEUNGRYEOL.report4; +import java.util.Scanner; + +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(); // String을 char[]로 변환 + char tmp; + for (int j = 0; j < question.length; j++) { + int k = (int) (Math.random() * question.length); + tmp = question[j]; + question[j] = question[k]; + question[k] = 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"); + } + } +} diff --git a/src/LEESEUNGRYEOL/report5/Report5_1 b/src/LEESEUNGRYEOL/report5/Report5_1 new file mode 100644 index 0000000..a531885 --- /dev/null +++ b/src/LEESEUNGRYEOL/report5/Report5_1 @@ -0,0 +1,20 @@ +//6-1번 문제 + +// 타입 : 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; +} + + + diff --git a/src/LEESEUNGRYEOL/report5/Report5_2.java b/src/LEESEUNGRYEOL/report5/Report5_2.java new file mode 100644 index 0000000..cde1cc4 --- /dev/null +++ b/src/LEESEUNGRYEOL/report5/Report5_2.java @@ -0,0 +1,39 @@ +package LEESEUNGRYEOL.report5; + +// 6-2 번 문제 +public class Report5_2 { + public static void main(String[] args) { + Student1 s = new Student1("홍길동",1,1,100,60,76); + String str = s.info(); + System.out.println(str); + } +} + +class Student1 { + String name; + int ban; + int no; + int kor; + int eng; + int math; + double sum; + double avg; + + // 생성자 + Student1(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; + this.sum = kor+eng+math; + this.avg = Math.ceil(sum/3 * 10)/ 10; + } + String info () + { + return (name+", "+ban+", "+ no+", "+kor+", "+eng+", "+math+", "+(int)sum+", "+avg); + } + +} diff --git a/src/LEESEUNGRYEOL/report5/Report5_3.java b/src/LEESEUNGRYEOL/report5/Report5_3.java new file mode 100644 index 0000000..0aa71a0 --- /dev/null +++ b/src/LEESEUNGRYEOL/report5/Report5_3.java @@ -0,0 +1,41 @@ +package LEESEUNGRYEOL.report5; +// 6-3 문제 +public class Report5_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 +} + +class Student { + String name; + int ban; + int no; + int kor; + int eng; + int math; + double sum; + double avg; + int getTotal() + { + int total; + total = kor + eng + math; + return total; + } + + float getAverage() + { + double average; + average = (double)(kor+eng+math)/3; + average = Math.ceil(average *10)/10; + return (float)average; + } +} \ No newline at end of file diff --git a/src/LEESEUNGRYEOL/report5/Report5_4 b/src/LEESEUNGRYEOL/report5/Report5_4 new file mode 100644 index 0000000..31ae231 --- /dev/null +++ b/src/LEESEUNGRYEOL/report5/Report5_4 @@ -0,0 +1,25 @@ +다음의 코드에 정의된 변수들을 종류별(클래스 변수,인스턴스 변수, 지역변수)로 구분해서 적으세요. +// 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); + } +} + +클래스변수(CV) +: width, height + +인스턴스변수(IV) +: kind, num + +지역변수(LV) +: k , n \ No newline at end of file diff --git a/src/LEESEUNGRYEOL/report5/Report5_5 b/src/LEESEUNGRYEOL/report5/Report5_5 new file mode 100644 index 0000000..b152723 --- /dev/null +++ b/src/LEESEUNGRYEOL/report5/Report5_5 @@ -0,0 +1,37 @@ +다음은 컴퓨터 게임의 병사(marine)를 클래스로 정의한 것이다. +// 6-7번 문제 + +//이 클래스의 멤버 중에 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; + } +} + +클래스 변수 (CV) +: weapon, armor + +인스턴스 변수 (IV) +: x, y, hp, + +지역변수 (LV) +: x, y + +static을 붙여야 하는 것 +: weapon, armor, weaponUp(), armorUp() (클래스변수의 증가 또한 모든 인스턴스가 동일한 값을 가져야 함.) \ No newline at end of file diff --git a/src/LEESEUNGRYEOL/report6/Report6_1.md b/src/LEESEUNGRYEOL/report6/Report6_1.md new file mode 100644 index 0000000..40290ca --- /dev/null +++ b/src/LEESEUNGRYEOL/report6/Report6_1.md @@ -0,0 +1,164 @@ +6-8. 다음 중 생성자에 대한 설명으로 옳지 않은 것은? (모두 고르시오) + +a. 모든 생성자의 이름은 클래스의 이름과 동일해야한다. + +b. 생성자는 객체를 생성하기 위한 것이다. + +c. 클래스에는 생성자가 반드시 하나 이상 있어야 한다. + +d. 생성자가 없는 클래스는 컴파일러가 기본 생성자를 추가한다. + +e. 생성자는 오버로딩 할 수 없다. + +답: e + + +6-9. 다음 중 this에 대한 설명으로 맞지 않은 것은? (모두 고르시오) + +a. 객체 자신을 가리키는 참조변수이다. + +b. 클래스 내에서라면 어디서든 사용할 수 있다. + +c. 지역변수와 인스턴스변수를 구별할 때 사용한다. + +d. 클래스 메서드 내에서는 사용할 수 없다. + + +답: b(클래스멤버에서는 사용할 수 없다) + +6-10. 다음 중 오버로딩이 성립하기 위한 조건이 아닌 것은? (모두 고르시오) + +a. 메서드의 이름이 같아야 한다. + +b. 매개변수의 개수나 타입이 달라야 한다. + +c. 리턴타입이 달라야 한다. + +d. 매개변수의 이름이 달라야 한다. + + +답: c , d + +6-11. 다음 중 아래의 add메서드를 올바르게 오버로딩 한 것은? (모두 고르시오) +```java +> 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, f + +(현제 println이 실해되는 것은 맞지만, 그 아래의 method들이 실행이 종료된 것은 아니기 때문에 전체가 실행되고 있다고 볼 수있다.) + + +6-16. 다음 코드의 실행 결과를 예측하여 적어주세요. +```java +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 + +이유: 클래스 메소드는 인스턴스 메소드와는 관련이 없다. \ No newline at end of file diff --git a/src/LEESEUNGRYEOL/report7/Report7_1.java b/src/LEESEUNGRYEOL/report7/Report7_1.java new file mode 100644 index 0000000..79fb40a --- /dev/null +++ b/src/LEESEUNGRYEOL/report7/Report7_1.java @@ -0,0 +1,26 @@ +package LEESEUNGRYEOL.report7; + +// 6-17번 문제 +// shuffle method 작성 -> 주어진 배열에 담긴 값의 위치를 바꾸는 작업을 반복 +// 반환타입 int 리스트, 매개변수 int[] arr (정수값이 담긴 배열) +public class Report7_1 { + + static int[] shuffle(int[] original) { + int tmp; + for (int i = 0; i < original.length; i++) { + int random_num = (int) (Math.random() * original.length); + tmp = original[i]; + original[i] = original[random_num]; + original[random_num] = tmp; + } + return original; + } + + 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/LEESEUNGRYEOL/report7/Report7_2.java b/src/LEESEUNGRYEOL/report7/Report7_2.java new file mode 100644 index 0000000..51e7f64 --- /dev/null +++ b/src/LEESEUNGRYEOL/report7/Report7_2.java @@ -0,0 +1,29 @@ +package LEESEUNGRYEOL.report7; +// 6-18 문제 +class Exercise6_18 { + + static boolean isNumber(String str){ + int cnt = 0; + for (int i = 0; i = 48 && str.charAt(i) <=57) + { + cnt++; + } + } + if(cnt == str.length()) + { + return true; + } + else{ + return false; + } + } + + 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 \ No newline at end of file diff --git a/src/LEESEUNGRYEOL/report7/Report7_3.java b/src/LEESEUNGRYEOL/report7/Report7_3.java new file mode 100644 index 0000000..77d78ea --- /dev/null +++ b/src/LEESEUNGRYEOL/report7/Report7_3.java @@ -0,0 +1,68 @@ +package LEESEUNGRYEOL.report7; +//6-19번 문제 +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로 바꾼다. + channel++; + + if(channel > MAX_CHANNEL) + { + channel = MIN_CHANNEL; + } + + } + void channelDown() { + // (5) channel의 값을 1 감소시킨다 . + // 만일 channel이 MIN_CHANNEL이면, channel의 값을 MAX_CHANNEL로 바꾼다. + channel--; + if(channel < MIN_CHANNEL) + { + channel = MAX_CHANNEL; + } + + } +} + +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 diff --git a/src/LEESEUNGRYEOL/report7/Report7_4.java b/src/LEESEUNGRYEOL/report7/Report7_4.java new file mode 100644 index 0000000..7559b3a --- /dev/null +++ b/src/LEESEUNGRYEOL/report7/Report7_4.java @@ -0,0 +1,37 @@ +package LEESEUNGRYEOL.report7; + +// 6-21번 문제 +public class Report7_4 { + /* (1) max 메서드를 작성하시오 . */ + + static int max(int[] arr) + { + if(arr == null || arr.length ==0) + { + return -999999; + } + + int max = 0; + for (int i = 0; i = max) + { + max = arr[i]; + } + } + + return max; + } + + public static void main(String[] args) { + int[] data = {3, 2, 9, 4, 7}; + System.out.println(java.util.Arrays.toString(data)); + System.out.println("최대값 :" + max(data)); + System.out.println("최대값 :" + max(null)); + System.out.println("최대값 :" + max(new int[]{})); // 크기가 0인 배열 } + } + + +//예상 결과 : 최대값: 9 / 최대값: -99999 최대값: -999999 + + +} diff --git a/src/LEESEUNGRYEOL/report7/Report7_5.java b/src/LEESEUNGRYEOL/report7/Report7_5.java new file mode 100644 index 0000000..9fbfa04 --- /dev/null +++ b/src/LEESEUNGRYEOL/report7/Report7_5.java @@ -0,0 +1,19 @@ +package LEESEUNGRYEOL.report7; + +public class Report7_5 { + /* (1) abs 메서드를 작성하시오. */ + static int abs(int value) + { + int answer; + answer = (value<0 ? -value:value); + return answer; + } + + public static void main(String[] args) { + int value = 7; + System.out.println(value + "의 절대값 :" + abs(value)); + value = -10; + System.out.println(value + "의 절대값 :" + abs(value)); + } +//예상 결과 : 5의 절대값 : 5 / -10의 절대값 : 10 +} diff --git a/src/LEESEUNGRYEOL/report8/Report8_2.java b/src/LEESEUNGRYEOL/report8/Report8_2.java new file mode 100644 index 0000000..4406e6b --- /dev/null +++ b/src/LEESEUNGRYEOL/report8/Report8_2.java @@ -0,0 +1,94 @@ +package LEESEUNGRYEOL.report8; + +import java.util.Arrays; + +class SutdaDeck1 { + final int CARD_NUM = 20; + SutdaCard1[] cards = new SutdaCard1[CARD_NUM]; + + SutdaDeck1() { + + boolean iskwang; + int[] checklist = new int[3]; + + for (int i = 0; i < CARD_NUM / 2; i++) { + if (i + 1 == 1 || i + 1 == 3 || i + 1 == 8) { + cards[i] = new SutdaCard1(i + 1, true); + } else { + cards[i] = new SutdaCard1(i + 1, false); + } + } + + + for (int i = CARD_NUM / 2; i < CARD_NUM; i++) { + if (i == 1 || i == 3 || i == 8) { + cards[i] = new SutdaCard1(i - 9, false); + } else { + cards[i] = new SutdaCard1(i - 9, false); + } + } + + + } + + void shuffle() { + SutdaCard1 tmp; + for (int i = 0; i < cards.length ; i++) { + int random_num =(int) (Math.random()*cards.length); + tmp = cards[i]; + cards[i] = cards[random_num]; + cards[random_num] = tmp; +// System.out.println("random_num = " + random_num); +// System.out.println(Arrays.toString(cards)); + } + } + + SutdaCard1 pick(int index) { + return cards[index]; + } + + SutdaCard1 pick() { + int random_num =(int) (Math.random()*cards.length); + return cards[random_num]; + } +} // SutdaDeck + +class SutdaCard1 { + int num; + boolean isKwang; + + + SutdaCard1() { + this(1, true); + } + + SutdaCard1(int num, boolean isKwang) { + this.num = num; + this.isKwang = isKwang; + } + + public String toString() { + return num + ( isKwang ? "K":""); + } +} + +class Exercise7_2 { + public static void main(String args[]) { + SutdaDeck1 deck = new SutdaDeck1(); + + 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/LEESEUNGRYEOL/report8/Report8_3.java b/src/LEESEUNGRYEOL/report8/Report8_3.java new file mode 100644 index 0000000..e02472f --- /dev/null +++ b/src/LEESEUNGRYEOL/report8/Report8_3.java @@ -0,0 +1,36 @@ +package LEESEUNGRYEOL.report8; + +class Product { + int price; // 제품의 가격 + int bonusPoint; // 제품구매 시 제공하는 보너스점수 + + // (구현) + Product() + { + } + Product(int price) { + this.price = price; + bonusPoint = (int) (price / 10.0); + } +} + +class Tv extends Product { + Tv() { + } + + public String toString() { + return "Tv"; + } +} + +class Exercise7_3 { + public static void main(String[] args) { + Tv t = new Tv(); + } +} + +// 틀린 이유 +// 일단 틀린 부분은 TV(){} 기본생성자에서 문제가 발생하는데, 이때 문제가 발생하는 이유는 +// Tv 생성자에서 모든 생성자는 첫줄에 다른 생성자를 호출해야 된다. 이때 Tv class 에서는 기본 생성자만 존재 +// 따라서 자동적으로 super() 생성자를 호출한다. 하지만 super 은 Tv 클래스의 조상인 Product 인데 여기에는 기본생성자가 없다. +// 따라서 기본생성자를 생성하고 나면 오류가 사라진다. \ No newline at end of file diff --git a/src/LEESEUNGRYEOL/report8/Report8_4.java b/src/LEESEUNGRYEOL/report8/Report8_4.java new file mode 100644 index 0000000..53a3d9a --- /dev/null +++ b/src/LEESEUNGRYEOL/report8/Report8_4.java @@ -0,0 +1,49 @@ +package LEESEUNGRYEOL.report8; + +class MyTv { + public boolean isPowerOn() { + return isPowerOn; + } + + private boolean isPowerOn; + + public int getChannel() { + return channel; + } + + public void setChannel(int channel) { + this.channel = channel; + } + + private int channel; + + public int getVolume() { + return volume; + } + + public void setVolume(int volume) { + this.volume = volume; + } + + private int volume; + + final int MAX_VOLUME = 100; + final int MIN_VOLUME = 0; + final int MAX_CHANNEL = 100; + final int MIN_CHANNEL = 1; + + // (구현) + +} + +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()); + } +} +//예상결과) CH:10 VOL:20 \ No newline at end of file diff --git a/src/LEESEUNGRYEOL/report8/Report8_5.java b/src/LEESEUNGRYEOL/report8/Report8_5.java new file mode 100644 index 0000000..85d9f11 --- /dev/null +++ b/src/LEESEUNGRYEOL/report8/Report8_5.java @@ -0,0 +1,46 @@ +package LEESEUNGRYEOL.report8; + +class MyTv2 { + private int channel; + private int prevChannel; + final int MAX_CHANNEL = 100; + final int MIN_CHANNEL = 1; + + public void gotoPrevChannel() { + int tmp; + tmp = this.channel; + this.channel = this.prevChannel; + this.prevChannel = tmp; + } + + public void setChannel(int channel) { + if (channel > MAX_CHANNEL || channel < MIN_CHANNEL) + return; + + this.prevChannel = this.channel; + this.channel = channel; + } + + public int getChannel() { + return channel; + } +} + +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 diff --git a/src/LEESEUNGRYEOL/report8/report8_1.java b/src/LEESEUNGRYEOL/report8/report8_1.java new file mode 100644 index 0000000..0a063c9 --- /dev/null +++ b/src/LEESEUNGRYEOL/report8/report8_1.java @@ -0,0 +1,61 @@ +package LEESEUNGRYEOL.report8; + +class SutdaDeck { + final int CARD_NUM = 20; + SutdaCard[] cards = new SutdaCard[CARD_NUM]; + + SutdaDeck() { + // (구현) 배열 SutdaCard를 적절히 초기화 하시오. + boolean iskwang; + int[] checklist = new int[3]; + + for (int i = 0; i < CARD_NUM/2; i++) { + if (i+1 == 1 || i+1 == 3 || i+1 == 8) { + cards[i] = new SutdaCard(i+1, true); + } + else{ + cards[i] =new SutdaCard(i+1 ,false); + } + } + + + for (int i = CARD_NUM/2; i < CARD_NUM; i++) { + if (i == 1 || i == 3 || i == 8) { + cards[i] = new SutdaCard(i-9, false); + } + else{ + cards[i] =new SutdaCard(i-9,false); + } + } + + } +} + +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" : ""); + } +} + +class Exercise7_1 { + public static void main(String args[]) { + SutdaDeck1 deck = new SutdaDeck1(); + + 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/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) { - - } -}