Toggle menu
Toggle personal menu
Not logged in
Your IP address will be publicly visible if you make any edits.

새싹교실/2022/Java보시던지/05.19: Difference between revisions

From ZeroWiki
No edit summary
(Repair batch-0006 pages from live compare)
 
(3 intermediate revisions by 2 users not shown)
Line 28: Line 28:
복습차원에서 좋은것 같아요 마음것 질문할 수 있어서 좋습니다~
복습차원에서 좋은것 같아요 마음것 질문할 수 있어서 좋습니다~


=== [[박창훈]] ===
=== 박창훈 ===
===== 난이도 =====
===== 난이도 =====
Line 50: Line 50:
== 과제 ==
== 과제 ==
* [[임지민]]
* [[임지민]]
**  
** ParenCt 클래스를 부모로 하는 자식 클래스 Child를 작성하세요.
class Child extends Parent {
    /* <Child 클래스 조건>
### Child의 age, name, schoolName, friendNum을 초기화하는 생성자를 만드세요.
### Child의 필드는 String schoolName, int friendNum이 있습니다.
### minusFriends 메소드 : 메소드를 호출할 때마다 friendNum이 1씩 줄어듭니다.
### showChildInfo 메소드 : 메소드를 호출하면 Child의 나이, 이름, 학교이름, 친구 숫자를 출력합니다.
    출력 예시 - 나이 : 10, 이름 : 은솔, 학교 : 중앙대학교, 친구 숫자 : 0 */


* [[박창훈]]
    String schoolName = "";
    int friendNum = 0;
 
    Child(){
        System.out.println("Child Default Constructor");
    }
 
    Child (int _age, String _name, String _schoolName, int _friendNum) {
        age=_age;
        name=_name;
        schoolName = _schoolName;
        friendNum = _friendNum;
        System.out.println("Child Constructor");
    }
 
    void minusFriends() {
        if(friendNum > 0){
            friendNum--;
        } else {
            friendNum = 0;
        }
 
    }
 
    void showChildInfo() {
        System.out.println("나이 : "+ age+ ", 이름 : "+name+", 학교 : "+schoolName+", 친구 숫자 : "+friendNum);
    }
}
** 14467: 소가 길을 건너간 이유 1
import java.util.Scanner;
 
public class CowCrossRoad {
    public static void main(String[] args) {
    final int numCow = 10; //소 10마리
    final int maxNumObserve = 100; //관찰 횟수 100 이하의 양의 정수
   
    //관찰값을 기록할 이차원 배열의 선언 및 생성과 초기화
    int[][] cau = new intnumCow+1maxNumObserve+2;
    for(int i = 1; i < cau.length; i++) {
    cau&#91;i&#93;&#91;0&#93; = 0;
    }
   
        Scanner sc = new Scanner(System.in);
        int totalNumObserve = Integer.parseInt(sc.nextLine()); //첫 줄에 관찰 횟수 N
       
        for( int i = 0; i < totalNumObserve; i++){ //다음 N줄에는 한 줄에 하나씩 관찰 결과
            String[] arr = sc.nextLine().split(" "); //관찰 결과는 소의 번호와 위치(0 또는 1)로 이루어져 있다. " "기준으로 파싱
            int cauNum = Integer.parseInt(arr&#91;0&#93;); //소의 번호를 저장
            int cauNumObserve = ++caucauNum&#91;0&#93;; //caucauNum&#91;0&#93;에는 소의 번호별로 저장된 위치값의 개수를 기록함.
            caucauNumcauNumObserve = Integer.parseInt(arr&#91;1&#93;); // 해당위치에 차례대로 위치를 기록.
        }
        sc.close();
       
        int crossroad = 0; //'소가 길을 건너간 최소 횟수' 변수 초기화
 
        for( int i = 1; i < cau.length; i++) {
        int oldValue = cau&#91;i&#93;&#91;1&#93;; //과거의 값과 비교해야하므로 값을 저장해둠.
            for( int j = 1; j <= cau&#91;i&#93;&#91;0&#93;; j++){
                if(oldValue != cau&#91;i&#93;&#91;j&#93;) { //앞의 값과 다를 경우, 길을 최소 한번 건넘.
                    crossroad++;
                }
                oldValue = cau&#91;i&#93;&#91;j&#93;; //과거의 값 업데이트
            }
        }
        System.out.println(crossroad);
    }
}
 
 
* 박창훈
** 14467: 소가 길을 건너간 이유 1
** 14467: 소가 길을 건너간 이유 1


Line 91: Line 167:


}
}
** 상속 클래스 Child 작성하기


class Parent {
int age=45;
String name="Parent";
public Parent(){
System.out.println("Parent Default Constructor");
}
public Parent(int _age, String _name){
age=_age;
name=_name;
System.out.println("Parent Constructor");
}
public void showInfo(){
System.out.println("Name:"+name+", age:"+age);
}
}
class Child extends Parent{
String schoolName;
int friendNum;
Child(int age, String name, String schoolName, int friendNum){
super.age = age;
super.name = name;
this.schoolName = schoolName;
this.friendNum = friendNum;
}
void minusFriends() {
friendNum--;
}
void showChildInfo() {
System.out.println("나이 :"+age+", 이름 :"+name+", 학교 :"+schoolName+", 친구 숫자 :"+friendNum);
}
}

Latest revision as of 01:08, 27 March 2026

Describe 새싹교실/2022/Java보시던지/05.19 here

회고

임지민

난이도

익힌 내용들 - 클래스에 대한 두가지 접근법(관계가 달라짐): 1. 조상 2. 객체생성 이미 알던내용들 - 접근제한자, static

수업내용

상속 중복되는 코드 줄여줌 유지 보수 시간 최소화 (부모 클래스의 수정으로 모든 자식 클래스들도 수정) 클래스 상속 여러개의 부모 클래스 상속 x 부모 생성자 호출 자식 객체 생성시: 부모 객체 먼저 생성 → 자식 객체 생성 부모 객체를 생성하기 위해 부모 생성자를 호출하는 법: super(); 없으면 부모 클래스 안에 컴파일러에 의해 만들어짐 ⇒부모의 기본 생성자를 만들어줘야한다. 메소드 재정의 자식 클래스에 맞게 메소드 수정해서 사용 ⇒ 메소드 재정의 (오버라이딩)

새롭게 배운 내용

코드리뷰 조건이 있을경우 좀 무식하게 코딩해도 괜찮다. super.method() VS 객체.method() 클래스에 대한 두가지 접근법: 1. 조상 2. 객체생성 =>관계의 차이

느낀점/건의사항

복습차원에서 좋은것 같아요 마음것 질문할 수 있어서 좋습니다~

박창훈

난이도

수업내용
  • 상속
    • class 자식클래스 extends 부모클래스
    • if 부모 클래스와 자식 클래스에 같은 필드가 있을 때, 생성자의 this 키워드는 가장 가까운 자식 클래스를 참조한다. (부모 클래스 참조 X)
    • this 키워드로 부모 클래스를 참조할 때, 부모 클래스에 필드가 private로 접근 제한되어 있으면 참조 불가 (protected를 써야함)
    • 여러 개의 부모 클래스를 상속할 수 없다.
    • super() → 자식 클래스가 부모 클래스의 필드나 메소드를 참조할 때 사용함.
  • 메소드 재정의
    • 부모 클래스의 메소드와 자식 클래스의 메소드를 같게 하여, 자식 클래스의 특성을 고려해 메소드 오버라이딩 가능
새롭게 배운 내용

수업 때 배운 내용들

느낀점/건의사항

저도 난이도 있는 과제가 좋은 것 같아요~ 감사합니다

과제

  • 임지민
    • ParenCt 클래스를 부모로 하는 자식 클래스 Child를 작성하세요.

class Child extends Parent {

   /* <Child 클래스 조건>
      1. Child의 age, name, schoolName, friendNum을 초기화하는 생성자를 만드세요.
      2. Child의 필드는 String schoolName, int friendNum이 있습니다.
      3. minusFriends 메소드 : 메소드를 호출할 때마다 friendNum이 1씩 줄어듭니다.
      4. showChildInfo 메소드 : 메소드를 호출하면 Child의 나이, 이름, 학교이름, 친구 숫자를 출력합니다.
   출력 예시 - 나이 : 10, 이름 : 은솔, 학교 : 중앙대학교, 친구 숫자 : 0 */
   String schoolName = "";
   int friendNum = 0;
   Child(){
       System.out.println("Child Default Constructor");
   }
   Child (int _age, String _name, String _schoolName, int _friendNum) {
       age=_age;
       name=_name;
       schoolName = _schoolName;
       friendNum = _friendNum;
       System.out.println("Child Constructor");
   }
   void minusFriends() {
       if(friendNum > 0){
           friendNum--;
       } else {
           friendNum = 0;
       }
   }
   void showChildInfo() {
       System.out.println("나이 : "+ age+ ", 이름 : "+name+", 학교 : "+schoolName+", 친구 숫자 : "+friendNum);
   }

}

    • 14467: 소가 길을 건너간 이유 1

import java.util.Scanner;

public class CowCrossRoad {

   public static void main(String[] args) {
   	final int numCow = 10; //소 10마리
   	final int maxNumObserve = 100; //관찰 횟수 100 이하의 양의 정수
   	
   	//관찰값을 기록할 이차원 배열의 선언 및 생성과 초기화
   	int[][] cau = new intnumCow+1maxNumObserve+2;
   	for(int i = 1; i < cau.length; i++) {
   		cau[i][0] = 0;
   	}
   	
       Scanner sc = new Scanner(System.in);
       int totalNumObserve = Integer.parseInt(sc.nextLine()); //첫 줄에 관찰 횟수 N 
       
       for( int i = 0; i < totalNumObserve; i++){ //다음 N줄에는 한 줄에 하나씩 관찰 결과
           String[] arr = sc.nextLine().split(" "); //관찰 결과는 소의 번호와 위치(0 또는 1)로 이루어져 있다. " "기준으로 파싱 
           int cauNum = Integer.parseInt(arr[0]); //소의 번호를 저장 
           int cauNumObserve = ++caucauNum[0]; //caucauNum[0]에는 소의 번호별로 저장된 위치값의 개수를 기록함. 
           caucauNumcauNumObserve = Integer.parseInt(arr[1]); // 해당위치에 차례대로 위치를 기록. 
       }
       sc.close();
       
       int crossroad = 0; //'소가 길을 건너간 최소 횟수' 변수 초기화 
       for( int i = 1; i < cau.length; i++) {
       	int oldValue = cau[i][1]; //과거의 값과 비교해야하므로 값을 저장해둠. 
           for( int j = 1; j <= cau[i][0]; j++){
               if(oldValue != cau[i][j]) { //앞의 값과 다를 경우, 길을 최소 한번 건넘. 
                   crossroad++;
               }
               oldValue = cau[i][j]; //과거의 값 업데이트 
           }
       }
       System.out.println(crossroad);
   }

}


  • 박창훈
    • 14467: 소가 길을 건너간 이유 1

import java.util.Scanner;

public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int num = sc.nextInt(); int count=0; int[] cowNum = new int[num]; int[] cowPos = new int[num]; for (int i=0; i<num; i++) { cowNum[i] = sc.nextInt(); cowPos[i] = sc.nextInt(); } for(int i=0; i<num; i++) { int checker = cowPos[i]; for(int j=1; j<num; j++) { if ((cowNum[i] == cowNum[j]) && (cowNum[i]!=0)) { if (checker != cowPos[j]) { count++; checker = cowPos[j]; } } } int temp = cowNum[i]; for(int j=0; j<num; j++) { if(temp==cowNum[j]) { cowNum[j]=0; } }

} System.out.println(count); sc.close(); }

}

    • 상속 클래스 Child 작성하기

class Parent { int age=45; String name="Parent";

public Parent(){ System.out.println("Parent Default Constructor"); }

public Parent(int _age, String _name){ age=_age; name=_name; System.out.println("Parent Constructor"); }

public void showInfo(){ System.out.println("Name:"+name+", age:"+age); } }

class Child extends Parent{ String schoolName; int friendNum; Child(int age, String name, String schoolName, int friendNum){ super.age = age; super.name = name; this.schoolName = schoolName; this.friendNum = friendNum; } void minusFriends() { friendNum--; } void showChildInfo() { System.out.println("나이 :"+age+", 이름 :"+name+", 학교 :"+schoolName+", 친구 숫자 :"+friendNum); } }