diff --git a/.gitignore b/.gitignore index e205848..f06f725 100644 --- a/.gitignore +++ b/.gitignore @@ -149,4 +149,3 @@ Icon Network Trash Folder Temporary Items .apdisk - diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..13566b8 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Editor-based HTTP Client requests +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..e208459 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..0b0b192 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..35eb1dd --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/README.md b/README.md index 6c74bd2..1794a08 100644 --- a/README.md +++ b/README.md @@ -1 +1,5 @@ ## Hanghae Java Study 12th +## 리포트 테스트중 + +## 리포트 테스트중 + 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/choiwonbin/report1/Report1_1 b/src/choiwonbin/report1/Report1_1 index 3f37658..0abc610 100644 --- a/src/choiwonbin/report1/Report1_1 +++ b/src/choiwonbin/report1/Report1_1 @@ -1,3 +1,7 @@ 2-4번 문제 정답 : +byte b = 256; +char c = ''; +char answer = 'no'; +float f = 3.14 \ No newline at end of file diff --git "a/src/choiwonbin/report3/\353\241\234\352\267\270\354\235\270 \355\224\214\353\241\234\354\232\260\354\260\250\355\212\270.png" "b/src/choiwonbin/report3/\353\241\234\352\267\270\354\235\270 \355\224\214\353\241\234\354\232\260\354\260\250\355\212\270.png" new file mode 100644 index 0000000..9d22737 Binary files /dev/null and "b/src/choiwonbin/report3/\353\241\234\352\267\270\354\235\270 \355\224\214\353\241\234\354\232\260\354\260\250\355\212\270.png" differ diff --git a/src/leehyunbeen/report1/Report1_1 b/src/leehyunbeen/report1/Report1_1 new file mode 100644 index 0000000..c120f37 --- /dev/null +++ b/src/leehyunbeen/report1/Report1_1 @@ -0,0 +1,3 @@ +2-4번 문제 + +정답 : 1, 2, 3, 4 diff --git a/src/leehyunbeen/report1/Report1_2 b/src/leehyunbeen/report1/Report1_2 new file mode 100644 index 0000000..c57dab6 --- /dev/null +++ b/src/leehyunbeen/report1/Report1_2 @@ -0,0 +1,11 @@ +2-7번 번 문제 + +정답 : + +12 +true +131 +51 +99 +Java +오류 \ No newline at end of file diff --git a/src/leehyunbeen/report1/Report1_3.java b/src/leehyunbeen/report1/Report1_3.java new file mode 100644 index 0000000..ce3f01c --- /dev/null +++ b/src/leehyunbeen/report1/Report1_3.java @@ -0,0 +1,20 @@ +package leehyunbeen.report1; +// 2-8 번 문제 <- 이렇게 문제 번호 작성 필수 +public class Report1_3 { + 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); + } +} \ No newline at end of file diff --git a/src/leehyunbeen/report2/Report2_1 b/src/leehyunbeen/report2/Report2_1 new file mode 100644 index 0000000..43e7e25 --- /dev/null +++ b/src/leehyunbeen/report2/Report2_1 @@ -0,0 +1,7 @@ +Report 2 + +정답 : +byte b = 256; +char c = ''; +char answer = 'no'; +float f = 3.14 \ No newline at end of file diff --git a/src/leehyunbeen/report2/Report2_2.java b/src/leehyunbeen/report2/Report2_2.java new file mode 100644 index 0000000..e7664db --- /dev/null +++ b/src/leehyunbeen/report2/Report2_2.java @@ -0,0 +1,104 @@ +package leehyunbeen.report2; +// 3-2번 문제 +public class Report2_2 { + public static void main(String[] args) { + + + byte b = 10; + char ch = 'A'; + int i = 100; + long l = 1000L; + //3-1. 다음 중 형변환을 생략할 수 있는 것은? (모두 고르시오) +// b = (byte)i; 불가 +// ch = (char)b; 불가 +// short s = (short)ch; 불가 + float f = (float)l; // 정답 + i = (int)ch; //정답 + } +} + + +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); + // 정답 : true, 이유 :true || false && false >>>>> true + System.out.println(y += 10 - x++); + //장답 : 13, 이유 : y = 5+10 -2 계산 이후 x +=1 발생 + System.out.println(x += 2); + // 정답 : 5 , 이유 : 앞서 x는 3이 됐고 +2를 해줌 + System.out.println(!('A' <= c && c <= 'Z')); + // 정답 : true , 이유: 'A"는 65, "Z"는 90임으로 true&&true => true + System.out.println('C' - c); + //정답 : 66 , 67-65 = 2 + System.out.println('5' - '0'); + // 정답 : 5, 53-48 = 5 + System.out.println(c + 1); + // 정답 B , 'A' + 1 = 'B' + System.out.println(++c); + //정답 B, 출력 전 +1 + System.out.println(c++); + //정답 B, 출력 후 +1 c = 'C' + System.out.println(c); + //정답 C, 앞선 계산에서 증가함 + + System.out.println(false && false || true); + System.out.println((int)'0'); + } +} + + +//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); + } +} + + +//3-4. 아래의 코드는 사과를 담는데 필요한 바구니(버켓)의 수를 구하는 코드이다. +//만일 사과의 수가 123개이고 하나의 바구니에는 10개의 사과를 담을 수 있다면, 13개의 바구니가 필요할 것이다. +//알맞은 코드를 넣으시오. +class Exercise3_4{ + public static void main(String[] args){ + int numOfApples = 123; // 사과의 개수 + int sizeOfBucket = 10; // 바구니의 크기(바구니에 담을 수 있는 사과의 개수) + int numOfBucket = ((int)Math.ceil(numOfApples/(float)sizeOfBucket)); // 모든 사과를 담는데 필요한 바구니의 수 + + System.out.println("필요한 바구니의 수 :"+numOfBucket); + } +} +//예상 결과 -> 필요한 바구니의 수 :13 + + + +//3-5. 아래는 변수 num의 값에 따라 '양수', '음수', '0'을 출력하는 코드이다. +//삼항연산자를 이용해서 빈칸에 알맞은 코드를 넣으시오. +//Hint : 삼항 연산자를 두 번 사용할 것! +class Exercise3_5{ + public static void main(String[] args){ + int num = 10; + System.out.println((num>0) ? "양수" : ((num == 0) ? "0" : "음수")); + } +} +//예상 결과 : 양수 + +//3-6. 아래는 화씨(Fahrenheit)를 섭씨(Celcius)로 변환하는 코드이다. +//변환 공식이 'C = 5/9*(F-32)'라고 할 때, 빈 칸에 알맞은 코드를 넣으시오. +// 단, 변환값은 소수점 셋째자리에서 반올림하며, Math.round() 함수를 사용하지 않고 처리할 것! +class Exercise3_6{ + public static void main(String[] args){ + int fahrenheit = 100; + float celcius = (int)(2*(5/(float)9*(fahrenheit-32))*1000)-(int)((5/(float)9*(fahrenheit-32))*1000)/(float)1000; + + System.out.println("Fahrenheit:"+fahrenheit); + System.out.println("Celcius:"+celcius); + } +} +//예상 결과 : Fahrenheit:100, Celcius:37.78 \ No newline at end of file diff --git a/src/leehyunbeen/report3/Report3.java b/src/leehyunbeen/report3/Report3.java new file mode 100644 index 0000000..3b88276 --- /dev/null +++ b/src/leehyunbeen/report3/Report3.java @@ -0,0 +1,200 @@ +package leehyunbeen.report3; +import java.util.Scanner; + +//4-2. 1부터 20까지의 정수중에서 2 또는 3의 배수가 아닌 수의 총합을 구하세요. +class Exercise4_2 { + public static void main(String[] args) { + int sum = 0; + + int i = 0; + for (i = 0; i<21 ; i++){ + if ((i%2 !=0) && (i%3 != 0)) { + sum += i; + } + } + + System.out.println("sum="+sum); + } +} + + + +//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 i = 0; + int Sum = 0; + int finalsum = 0; + + while (i < 10) { + i ++; + Sum += i; + finalsum += Sum; + System.out.println("Sum="+Sum); + } + System.out.println(finalsum); + } +} + + + +//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; + + while (true) { + num += 1; + sum += num * s; + s *= -1; + if (sum > 100){ + break; + } + } + + System.out.println("num="+num); + System.out.println("sum="+sum); + } +} + + + +//4-5. 다음의 for문을 while문으로 변경하세요. +class Exercise4_5 { + public static void main(String[] args) { + + int i = 0; + int j = 0; + while (i<11){ + j=0; + while (j<=i){ + System.out.print("*"); + j++; + } + System.out.println(); + i++; + } + + }//end of main +} // end of class + + + +//4-6. 두 개의 주사위를 던졌을 때, 눈의 합이 6이 되는 모든 경우의 수를 출력하는 프로그램을 작성하세요. +class Exercise4_6 { + public static void main(String[] args) { + + int i = 0; + int j = 0; + for (i=1;i<7;i++){ + for (j = 1;j<7;j++){ + if ((i+j) == 6 ) { + System.out.print(i); + System.out.print(" "); + System.out.println(j); + } + } + } + + } +} + + + +//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 += Integer.parseInt(str.substring(i, (i+1))); + + } + + System.out.println("sum=" + sum); + } +}//예상 결과 : sum=15 + + + +//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); + } +} + + + +//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 /= 10; + } + + System.out.println("sum="+sum); + } +}//예상 결과 : sum=15 + + + +//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 (answer > input) { + System.out.println("더 큰 수를 입력하세요"); + } else if (answer < input) { + System.out.println("더 작은 수를 입력하세요"); + } else { + System.out.println("맞혔습니다."); + System.out.println("시도횟수는 "+count+"번입니다."); + break; + } + + } 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/leehyunbeen/report3/Report3_1 b/src/leehyunbeen/report3/Report3_1 new file mode 100644 index 0000000..7418860 --- /dev/null +++ b/src/leehyunbeen/report3/Report3_1 @@ -0,0 +1,23 @@ +// 4-1번 문제 + +정답 : +int형 변수 x가 10보다 크고 20보다 작을 때 true인 조건식 +10 < x < 20 + +char형 변수 ch가 공백이나 탭이 아닐 때 true인 조건식 + ch != " " && ch != "\t" +char형 변수 ch가 'x' 또는 'X'일 때 true인 조건식 +ch == 'x' && ch != 'X' +char형 변수 ch가 숫자('0'~'9')일 때 true인 조건식 + 48<=(int)ch<58 + +char형 변수 ch가 영문자(대문자 또는 소문자)일 때 true인 조건식 +65<= (int)ch < 123 + +int형 변수 year가 400으로 나눠떨어지거나 또는 4로 나눠떨어지고 100으로 나눠떨어지지 않을때 true인 조건식 + ((year % 400 == 0) || ((year % 4 == 0) && (year % 100 != 0))) +boolean형 변수 powerOn이 false일 때 true인 조건식 + (powerOn == false) + +문자열 참조변수 str이 "yes"일 때 true인 조건식 + str == "yes" \ No newline at end of file diff --git a/src/choiwonbin/report3/Untitled.drawio.png b/src/leehyunbeen/report3/Untitled.drawio.png similarity index 100% rename from src/choiwonbin/report3/Untitled.drawio.png rename to src/leehyunbeen/report3/Untitled.drawio.png diff --git a/src/leehyunbeen/report4/Report4.java b/src/leehyunbeen/report4/Report4.java new file mode 100644 index 0000000..2eefd98 --- /dev/null +++ b/src/leehyunbeen/report4/Report4.java @@ -0,0 +1,115 @@ +package leehyunbeen.report4; + +import java.util.Scanner; + +class Exercise5_3{ + public static void main(String[] args){ + int[] arr = {10, 20, 30, 40, 50}; + int sum = 0; + + for (int i = 0; i", 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/leehyunbeen/report4/Report4_1 b/src/leehyunbeen/report4/Report4_1 new file mode 100644 index 0000000..e7553cf --- /dev/null +++ b/src/leehyunbeen/report4/Report4_1 @@ -0,0 +1,19 @@ + //5-1. 다음은 배열을 선언하거나 초기화한 것이다. 잘못된 것을 고르고 그 이유를 설명하세요. + int[] arr[]; 바로 배열 초기화를 해주어야 한다. + int[] arr = {1,2,3,}; + int[] arr = new int[5]; + int[] arr = new int[5]{1,2,3,4,5}; []에 숫자 넣기 or {1,2,3,4,5} 배열 요소 넣기 중 하나 만 해야한다. + int arr[5]; 선언 시 []안에 숫자 넣을 수 없다. + int[] arr[] = new int[3][]; + + + +//5-2. 다음과 같은 배열이 있을 때, arr[3].length의 값은? 정답 : 2 + int[][]arr ={ + {5,5,5,5,5}, + {10,10,10}, + {20,20,20,20}, + {30,30} + }; + + diff --git a/src/leehyunbeen/report5/Report5_2.java b/src/leehyunbeen/report5/Report5_2.java new file mode 100644 index 0000000..8b884a6 --- /dev/null +++ b/src/leehyunbeen/report5/Report5_2.java @@ -0,0 +1,154 @@ +package leehyunbeen.report5;//6-1. 다음과 같은 멤버 변수를 갖는 Student 클래스를 정의하세요. +import java.util.Arrays; + +class Student { + String name; //타입 : String, 변수명 name, 설명 : 학생 이름 + int ban;//타입 : int, 변수명 : ban, 설명 : 반 + int no;//타입 : int, 변수명 : no, 설명 : 번호 + int kor;//타입 : int, 변수명 : kor, 설명 : 국어 점수 + int eng;//타입 : int, 변수명 : eng, 설명 : 영어 점수 + int math;//타입 : int, 변수명 : math, 설명 : 수학 점수 + + public Student(){ + this.name = "name"; + this.ban =0; + this.no = 0; + this.kor = 0; + this.eng = 0; + this.math = 0; + } + 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 int getTotal(){ + int total= 0; + total = kor + eng + math; + + return total; + } + public float getAverage(){ + float avg = (kor+eng+math)/3f; + avg = ((int)(avg*10+0.5))/10f; + + return avg; + } + + public String info() { + String[] str = new String[8]; + str[0] = name; + str[1] = Integer.toString(ban); + str[2] = Integer.toString(no); + str[3] = Integer.toString(kor); + str[4] = Integer.toString(eng); + str[5] = Integer.toString(math); + str[6] = Integer.toString(getTotal()); + str[7] = Float.toString(getAverage()); + return Arrays.toString(str); + } +} + + +//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 +} + + +//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 +} + + +//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); + } +} + + +//6-7. 다음은 컴퓨터 게임의 병사(marine)를 클래스로 정의한 것이다. +//이 클래스의 멤버 중에 static을 붙여야 하는 것은 어떤 것들이고 그 이유는 무엇인가? +//(단, 모든 병사의 공격력과 방어력은 같아야 한다.) + +//int weapon, int amor, void weaponUp(), void armorUp() 앞에 붙여줘야 한다. +//모든 병사들의 공격력과 방어력이 같다는 것은 weaponUp()이나 armorUp()을 실행했을 떄 +//모든 병사들의 weapon과 armor가 변해야한다. 즉 static을 붙여주어 각각의 Marine에서만 +//weapon()과 armor()가 실행하지 못하게 해야하며, Marine 클래스를 상속받은 모든 객체의 +//weapon과 armor가 같아야 하고 똑같이 증가 해야하한다. +class Marine { + int x=0, y=0; //Marine의 위치좌표 (x,y) + int hp = 60; //현재 체력 + static int weapon = 6; //공격력 + static int armor = 0; //방어력 + void weaponUp() { + weapon++; + } + static void armorUp() { + armor++; + } + void move(int x, int y) { + this.x = x; + this.y = y; + } +} + +class Exercise6_7 { + public static void main(String args[]) { + + Marine n = new Marine(); + n.hp = 55; + n.weapon = 6; + Marine g = new Marine(); + g.hp = 400; + g.weapon = 18; + System.out.println(n.hp +" 과 "+ n.weapon); + System.out.println(g.hp +" 과 "+ g.weapon); + + n.weaponUp(); + + System.out.println(n.hp +" 과 "+ n.weapon); + System.out.println(g.hp +" 과 "+ g.weapon); + + } +} diff --git a/src/leehyunbeen/report6/Report6_1 b/src/leehyunbeen/report6/Report6_1 new file mode 100644 index 0000000..167ee1e --- /dev/null +++ b/src/leehyunbeen/report6/Report6_1 @@ -0,0 +1,79 @@ +6-8. 다음 중 생성자에 대한 설명으로 옳지 않은 것은? (모두 고르시오) +a. 모든 생성자의 이름은 클래스의 이름과 동일해야한다. 맞음 +b. 생성자는 객체를 생성하기 위한 것이다. 틀림(인스턴스 생성 시) +c. 클래스에는 생성자가 반드시 하나 이상 있어야 한다. 맞음(없으면 컴파일러가 자동 생성) +d. 생성자가 없는 클래스는 컴파일러가 기본 생성자를 추가한다. 맞음 +e. 생성자는 오버로딩 할 수 없다. 틀림(여러개 생성 가능) + +6-9. 다음 중 this에 대한 설명으로 맞지 않은 것은? (모두 고르시오) +a. 객체 자신을 가리키는 참조변수이다. 틀림(인스턴스 자신을 가리키는 참조 변수) +b. 클래스 내에서라면 어디서든 사용할 수 있다. 맞음 +c. 지역변수와 인스턴스변수를 구별할 때 사용한다. 틀림(static) +d. 클래스 메서드 내에서는 사용할 수 없다. 틀림(사용 가능) + +6-10. 다음 중 오버로딩이 성립하기 위한 조건이 아닌 것은? (모두 고르시오) +a. 메서드의 이름이 같아야 한다. 맞음 +b. 매개변수의 개수나 타입이 달라야 한다. 맞음 +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); } 가능 + +6-12. 다음 중 초기화에 대한 설명으로 옳지 않은 것은? (모두 고르시오) +a. 멤버변수는 자동 초기화되므로 초기화하지 않고도 값을 참고할 수 있다. 맞음 +b. 지역변수는 사용하기 전에 반드시 초기화해야 한다. 맞음(수동으로 초기화 해야함) +c. 초기화 블럭보다 생성자가 먼저 수행된다. 틀림(생성자가 나중에) +d. 명시적 초기화를 제일 우선적으로 고려해야 한다. 맞음(가장 간단하고 기초적이다) +e. 클래스변수보다 인스턴스변수가 먼저 초기화된다 틀림(클래스변수가 먼저) + +6-13. 다음 중 인스턴스변수의 초기화 순서가 올바른 것은? +a. 기본값-명시적초기화-초기화블럭-생성자 정답 +b. 기본값-명시적초기화-생성자-초기화블럭 +c. 기본값-초기화블럭-명시적초기화-생성자 +d. 기본값-초기화블럭-생성자-명시적초기화 + +6-14. 다음 중 지역변수에 대한 설명으로 옳지 않은 것은? (모두 고르시오) +a. 자동 초기화되므로 별도의 초기화가 필요없다. 맞음 +b. 지역변수가 선언된 메서드가 종료되면 지역변수도 함께 소멸된다. 맞음 +c. 메서드의 매개변수로 선언된 변수도 지역변수이다. 맞음 +d. 클래스변수나 인스턴스변수보다 메모리 부담이 적다. 맞음(자신이 선언된 블럭이나 메서드가 종료되면 소멸함으로) +e. 힙(heap)영역에 생성되며 가비지 컬렉터에 의해 소멸된다. 틀림(힙영역은 인스턴스가 생성되는 영역으로 지역변수는 호출스택에 생성된다) + + +println +method1 +method2 +main +6-15. 호출스택이 다음과 같은 상황일 때 옳지 않은 설명은? (모두 고르시오) +a. 제일 먼저 호출스택에 저장된 것은 main메서드이다. 맞다 +b. println메서드를 제외한 나머지 메서드들은 모두 종료된 상태이다. 틀림(아직 작동중) +c. method2메서드를 호출한 것은 main메서드이다. 맞다 +d. println메서드가 종료되면 method1메서드가 수행을 재개한다. 맞다 +e. main-method2-method1-println의 순서로 호출되었다. 맞다 +f. 현재 실행중인 메서드는 println뿐이다. 틀림(아직 다 실행중) + +//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 + + + diff --git a/src/leehyunbeen/report7/Report7_1.java b/src/leehyunbeen/report7/Report7_1.java new file mode 100644 index 0000000..79411e8 --- /dev/null +++ b/src/leehyunbeen/report7/Report7_1.java @@ -0,0 +1,153 @@ +package leehyunbeen.report7; +// 6-17번 문제 +class Exercise6_17 { + + public static int[] shuffle(int[] arr){ + + for (int i=0; ichk) || (chk>57)) { + ans = false; + break; + } + } + return ans; + } + + public static void main(String[] args) { + String str = "123"; + isNumber(str); + System.out.println(str + " 는 숫자입니까? " + isNumber(str)); + str = "1234o"; + isNumber(str); + System.out.println(str + " 는 숫자입니까? " + isNumber(str)); + } +} +//예상 결과 : 123는 숫자입니까? true, 1234o는 숫자입니까? false + +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 (volumeMIN_VOLUME){ + volume -= 1; + } + } + void channelUp() { + if (channel == MAX_CHANNEL){ + channel = MIN_CHANNEL; + } else { + channel +=1; + } + // (4) channel의 값을 1 증가시킨다. + // 만일 channel이 MAX_CHANNEL이면 , channel의 값을 MIN_CHANNEL로 바꾼다. + } + void channelDown() { + // (5) channel의 값을 1 감소시킨다 . + // 만일 channel이 MIN_CHANNEL이면, channel의 값을 MAX_CHANNEL로 바꾼다. + if (channel == MIN_CHANNEL){ + channel = MAX_CHANNEL; + } else { + channel -=1; + } + } +} + +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 + + +class Exercise6_20 { + public static int max(int[] arr){ + int ans = -999999; + if ((arr == null) || (arr.length == 0)){ + return ans; + } + for (int num : arr){ + if(num > ans){ + ans = num; + } + } + return ans; + } + + + 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 + + +class Exercise6_21 { + public static int abs(int value){ + int ans = 0; + ans = value>0 ? value : value * -1; + return ans; + } + + 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 + diff --git a/src/leehyunbeen/report8/Report8_1.java b/src/leehyunbeen/report8/Report8_1.java new file mode 100644 index 0000000..2b1f0e1 --- /dev/null +++ b/src/leehyunbeen/report8/Report8_1.java @@ -0,0 +1,231 @@ +package leehyunbeen.report8; +// 7-1번 문제 +public class Report8_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]+","); + } +} + +class SutdaDeck { + final int CARD_NUM = 20; + SutdaCard[] cards = new SutdaCard[CARD_NUM]; + + SutdaDeck() { + for (int i = 0; i < CARD_NUM; i++) { + cards[i] = new SutdaCard(i + 1, false); + } + cards[0].isKwang = true; + cards[2].isKwang = true; + cards[7].isKwang = true; + + } + + void shuffle(){ + SutdaCard temp_card = new SutdaCard(); + for (int i=0; i Product() 로 변경하였다 +//Tv(){}에서 부모 클래스의 값을 상속할때 자동으로 super();가 삽입되는데 이때 price값이 없음으로 진행이 안된다. + + +class Product { + int price; // 제품의 가격 + int bonusPoint; // 제품구매 시 제공하는 보너스점수 + + // (구현) + + Product() { + 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(); + } +} + + +//Exercise7_4 시작-------------------------------------------- +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 void setChannel(int num){ + channel = num; + } + public void setVolume(int num){ + volume = num; + } + public int getChannel(){ + return channel; + } + public int getVolume(){ + return volume; + } +} + +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 + +//Exercise7_5 시작-------------------------------------------- + +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(){ + int temp = channel; + channel=prevChannel; + prevChannel = temp; + } +} + +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 + + +