From cfd6a3d8017ac59b707ebefaecd47b440f414bda Mon Sep 17 00:00:00 2001 From: alpaca07 <108653023+alpaca07@users.noreply.github.com> Date: Fri, 2 May 2025 01:15:09 +0900 Subject: [PATCH 01/16] Create chapter _3.md --- "\354\244\200\355\231\230/chapter _3.md" | 1 + 1 file changed, 1 insertion(+) create mode 100644 "\354\244\200\355\231\230/chapter _3.md" diff --git "a/\354\244\200\355\231\230/chapter _3.md" "b/\354\244\200\355\231\230/chapter _3.md" new file mode 100644 index 0000000..b4ed009 --- /dev/null +++ "b/\354\244\200\355\231\230/chapter _3.md" @@ -0,0 +1 @@ +sadasdasd From 82aa9708bbfc2593044f49a9cc963890344b5fc8 Mon Sep 17 00:00:00 2001 From: alpaca07 <108653023+alpaca07@users.noreply.github.com> Date: Fri, 2 May 2025 01:21:56 +0900 Subject: [PATCH 02/16] =?UTF-8?q?Delete=20=EC=A4=80=ED=99=98/chapter=20=5F?= =?UTF-8?q?3.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- "\354\244\200\355\231\230/chapter _3.md" | 1 - 1 file changed, 1 deletion(-) delete mode 100644 "\354\244\200\355\231\230/chapter _3.md" diff --git "a/\354\244\200\355\231\230/chapter _3.md" "b/\354\244\200\355\231\230/chapter _3.md" deleted file mode 100644 index b4ed009..0000000 --- "a/\354\244\200\355\231\230/chapter _3.md" +++ /dev/null @@ -1 +0,0 @@ -sadasdasd From 1051a6799390ef5cfc92b6bd94aaf371cadf1beb Mon Sep 17 00:00:00 2001 From: alpaca07 <108653023+alpaca07@users.noreply.github.com> Date: Sun, 4 May 2025 18:45:33 +0900 Subject: [PATCH 03/16] Create chapter_05.md --- "\354\244\200\355\231\230/chapter_05.md" | 48 ++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 "\354\244\200\355\231\230/chapter_05.md" diff --git "a/\354\244\200\355\231\230/chapter_05.md" "b/\354\244\200\355\231\230/chapter_05.md" new file mode 100644 index 0000000..c0b1459 --- /dev/null +++ "b/\354\244\200\355\231\230/chapter_05.md" @@ -0,0 +1,48 @@ + + +## 🌅 공부기록 +1. 객체 지향 프로그래밍과 클래스 +2. 클래스 살펴보기 +3. 메서드 +4. 클래스와 인스턴스 +5. 생성자 +6. 참조 자료형 +### 🧠 배운 점 +- 클래스 정의 + ![image](https://github.com/user-attachments/assets/e930df43-063d-402a-8fe4-4422491c243b) +- 변수 자료형 +- 기본 자료형:int,long,float,double +- 참조 자료형:String, Data, Student +- ![image](https://github.com/user-attachments/assets/9f16a709-1d18-4dcd-904a-7a18a26df88d) +- 메서드 : 클래스의 기능을 구현한 것 +- ![image](https://github.com/user-attachments/assets/26856059-7cc7-4bb2-8124-bc8891b22172) +- 함수 호출하고 값 반환하기 +- ![image](https://github.com/user-attachments/assets/2bb1dac2-9a56-4397-99c8-ef88c6ecaeb8) +- ![image](https://github.com/user-attachments/assets/c91c6bb9-b45c-4594-8ac7-d3930c118f49) +- 메서드 구현하기 +- ![image](https://github.com/user-attachments/assets/b31bfed3-daba-41bd-a365-a8418c26683b) +- 클래스에 함수 추가 하기 +- ![image](https://github.com/user-attachments/assets/0a19c7ee-12a0-4f5d-b6e4-162c5e0bc068) +- ![image](https://github.com/user-attachments/assets/cf245be0-f71f-4337-8d7f-ad270f492a5d) +- 인스턴스 : 메모리 공간에 생성된 상태 -> 생성된 클래스의 인스턴스 = 객체 +- 참조 값 출력하기 +- ![image](https://github.com/user-attachments/assets/784b08d8-39fa-4560-9d4a-6b616eab4cf6) +- ![image](https://github.com/user-attachments/assets/3f6745af-eee9-45d9-9e65-db3daeae7879) +- 생성자 객체를 처음 만들 때 멤버 변수나 상수를 초기화하는 것 +- 디폴트 생성자: 자동으로 만들어 주는 생성자 +- 생성자 만들기, 생성자 테스트 하기, 사용하기, 구현하기 +- ![image](https://github.com/user-attachments/assets/17b3d29b-0c64-467a-9a1b-a637af706166) +- ![image](https://github.com/user-attachments/assets/e1ddca78-45b0-40f8-9932-0ec832379d0d) +- 참조 자료형 +- 클래스 자료형으로 선언하는 것 +- 클래스 생성 +- ![image](https://github.com/user-attachments/assets/c625e222-f954-41bd-8c6f-acee457be2f2) +- 변수 추가 +- ![image](https://github.com/user-attachments/assets/964d150c-6c17-48fc-a04f-bb31318a2e45) +- Subject형을 사용하여 선언 +- ![image](https://github.com/user-attachments/assets/3bfbe61d-172a-4278-bc1a-a6622b0f27b5) + +- + From 57ea8249e858ded1e6adb249ea18481f9b1c4c30 Mon Sep 17 00:00:00 2001 From: alpaca07 <108653023+alpaca07@users.noreply.github.com> Date: Sun, 4 May 2025 19:16:36 +0900 Subject: [PATCH 04/16] Create chapter06.md --- "\354\244\200\355\231\230/chapter06.md" | 54 +++++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 "\354\244\200\355\231\230/chapter06.md" diff --git "a/\354\244\200\355\231\230/chapter06.md" "b/\354\244\200\355\231\230/chapter06.md" new file mode 100644 index 0000000..d4e0dbb --- /dev/null +++ "b/\354\244\200\355\231\230/chapter06.md" @@ -0,0 +1,54 @@ + +## 🌅 공부기록 +1. this 예약어 +2. 객체 간 협력 +3. static 변수 +4. 변수 유효 범위 +5. static 응용 - 싱글톤 패턴 +### 🧠 배운 점 +- this : 생성된 인스턴스 스스로를 가르키는 예약어 +- this 출력하기 +- ![image](https://github.com/user-attachments/assets/584c5b29-db10-4e5e-bfd1-08b030993deb) +- ![image](https://github.com/user-attachments/assets/e6935719-46ae-4f38-8bda-71060e4259ca) +- this로 다른 생성자 호출하기 +- ![image](https://github.com/user-attachments/assets/26e36732-baeb-4fd2-ba49-989195294dd3) +- ![image](https://github.com/user-attachments/assets/66445622-8787-4ffe-8092-fc2c009cbcb0) +- this를 사용하여 주소 값 반환하기 +- ![image](https://github.com/user-attachments/assets/0beee8b9-7072-4f59-b1e6-5ebdfae6936a) +- ![image](https://github.com/user-attachments/assets/7693e78d-94e6-43d7-9bd4-6fab44504c9a) +- 객체 지향 프로그램은 객체를 정의 하고 객체 간 협력으로 만들어 진다고 했다 +- ex) ![image](https://github.com/user-attachments/assets/fd09f943-adfa-42ba-a580-0e9a289d6325) +- 학생 클래스 구현 +- ![image](https://github.com/user-attachments/assets/67e5a1d2-d781-49c2-a762-b940b5a04e47) +- 버스 클래스 구현 +- ![image](https://github.com/user-attachments/assets/5885083f-01a4-442f-b07d-5510c29c6d52) +- 지하철 클래스 구현 +- ![image](https://github.com/user-attachments/assets/79692f53-e370-4d2b-9b73-56e12388eeae) +- 버스와 지하철 타기 +- ![image](https://github.com/user-attachments/assets/41fa3b5f-2840-4c94-9498-e1ae50857096) +- ![image](https://github.com/user-attachments/assets/025d1611-e1d7-459c-8243-ca52b20567ad) +- static 변수 : 클래스에서 공통으로 사용하는 변수 +- static 변수 사용하기 +- ![image](https://github.com/user-attachments/assets/b6610d1e-f807-43df-a922-cf5ab879bfe3) +- 변수 테스트 하기 +- ![image](https://github.com/user-attachments/assets/2c751c62-9b7b-43ba-b1a1-ef9ec7b33425) +- ![image](https://github.com/user-attachments/assets/f13bfa10-1c1e-415e-8bc3-90e5d6410646) +- 클래스 이름으로 static 변수 참조하기 +- ![image](https://github.com/user-attachments/assets/af656a11-a19b-4334-89b4-e9c3d203a792) +- ![image](https://github.com/user-attachments/assets/da71ff0b-1276-4647-a39b-20bb40bf091b) +- seriaNum의 get(),set() 메서드 사용하기 +- ![image](https://github.com/user-attachments/assets/d902ab83-62e9-4e87-b728-32a365143b38) +- 변수 유효 범위 +- ![image](https://github.com/user-attachments/assets/803c0a3b-4fb4-47bc-aba6-d517ef697a94) +- 싱글톤 패턴 +- 객체 지향 프로그램에서 인스턴스를 단 하나만 생성하는 디자인 패턴 +- 싱글톤 패턴으로 회사 클래스 구현하기 +- 1단계 생성자를 private으로 만들기 +- ![image](https://github.com/user-attachments/assets/96b0e174-d6bc-4aa4-9ae5-a42a353dc6c6) +- 2단계 static으로 유일 인스턴스 생성 +- ![image](https://github.com/user-attachments/assets/e9c4f9b8-7278-4997-b299-dcd0cbcc716d) +- 3단계 외부에서 참조할 수 있는 public 메서드 만들기 +- ![image](https://github.com/user-attachments/assets/f908eb3e-7e63-47c0-ba6f-22b45b286381) +- 4단계 실제로 사용하는 코드 만들기 +- ![image](https://github.com/user-attachments/assets/cd0dd064-f765-4a97-ab21-e3fea554f62a) + From 62cb5208cf0333ae19d153234cb6b00090d721cf Mon Sep 17 00:00:00 2001 From: alpaca07 <108653023+alpaca07@users.noreply.github.com> Date: Sun, 4 May 2025 19:44:32 +0900 Subject: [PATCH 05/16] Create chapter07.md --- "\354\244\200\355\231\230/chapter07.md" | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 "\354\244\200\355\231\230/chapter07.md" diff --git "a/\354\244\200\355\231\230/chapter07.md" "b/\354\244\200\355\231\230/chapter07.md" new file mode 100644 index 0000000..0acb7c8 --- /dev/null +++ "b/\354\244\200\355\231\230/chapter07.md" @@ -0,0 +1,24 @@ + +## 🌅 공부기록 + 1. 배열이란 + 2. 다차원 배열 + 3. ArrayList 클래스 사용하기 + 4. 배열 응용 프로그램 +### 🧠 배운 점 +- 배열 초기화 하고 출력하기 +- ![image](https://github.com/user-attachments/assets/7f461faf-aadc-40af-bd6f-28bd936f1ba9) +- ![image](https://github.com/user-attachments/assets/df80d09f-e43e-41cc-8abe-39b6afbad0cc) +- 배열 길이만큼 출력하기 +- ![image](https://github.com/user-attachments/assets/c51a594e-abcd-4b83-9362-88926a071dcf) +- 배열의 유효한 요소 값 출력하기 +- ![image](https://github.com/user-attachments/assets/69e68b8a-86dd-4559-b675-37326ff351cf) +- 문자 저장 배열 만들기 +- ![image](https://github.com/user-attachments/assets/0a26aa89-b937-4df0-a5d2-a89817d2e24d) +- 객체 배열 사용하기 +- ![image](https://github.com/user-attachments/assets/5fca6865-4030-466a-9199-acc974547fe0) +- 각 배열 요소에 인스턴스를 생성해 넣기 +- ![image](https://github.com/user-attachments/assets/f8ed8189-559a-420a-8133-7973eb1a237d) +- 이차원 배열 초기화 하기 +- ![image](https://github.com/user-attachments/assets/ae6627f7-7e34-4d93-8cd4-f05d5bc40758) +- ArraryList 클래스 사용하기 +- ![image](https://github.com/user-attachments/assets/b733ce6f-d6cb-462e-9c0a-a3ede3bfb1b0) From 1282a2cbb36f4db80c8cd4e851b389e75c2b2d24 Mon Sep 17 00:00:00 2001 From: alpaca07 <108653023+alpaca07@users.noreply.github.com> Date: Sun, 4 May 2025 20:33:37 +0900 Subject: [PATCH 06/16] Create chapter08.md --- "\354\244\200\355\231\230/chapter08.md" | 144 ++++++++++++++++++++++++ 1 file changed, 144 insertions(+) create mode 100644 "\354\244\200\355\231\230/chapter08.md" diff --git "a/\354\244\200\355\231\230/chapter08.md" "b/\354\244\200\355\231\230/chapter08.md" new file mode 100644 index 0000000..4a386dc --- /dev/null +++ "b/\354\244\200\355\231\230/chapter08.md" @@ -0,0 +1,144 @@ + + +## 🌅 공부기록 +1. 상속이란 +2. 상속에서 클래스 생성과 형 변환 +3. 메서드 오버라이딩 +4. 다형성 +5. 다형성 활용 +6. 다운 캐스팅과 instanceof +### 🧠 배운 점 +- 상속이란 +- B클래스가 A클래스를 상속 받았을때 B클래스는 A클래스의 변수나 메서드를 사용이 가능하다. +- Customer 클래스 구현하기 +- ![image](https://github.com/user-attachments/assets/7c266bc1-c9d9-42dd-9be4-d3f43d4b90c2) +- vip 클래스 구현하기 +- ![image](https://github.com/user-attachments/assets/b54b24a0-e7c6-4d6a-8f39-15d0b3099968) +customer 코드에서 뭔가 잘못해서 그 부분만 나중에 다시 추가하겠습니다. +- 다형성이란 +- 하나의 코드가 여러 자료형으로 구분되어 실행되는 것을 말한다. +- 다향성 테스트 하기 +``` +package chapter08.polymorphism; + +class Animal { + public void move() { + System.out.println("동물이 움직입니다"); + } +} + +class Human extends Animal { + public void move() { + System.out.println("사람이 두 발로 걷습니다"); + } +} + +class Tiger extends Animal { + public void move() { + System.out.println("호랑이가 네 발로 뜁니다."); + } +} + +class Eagle extends Animal { + public void move() { + System.out.println("독수리가 하늘을 납니다"); + } +} + +public class AnimalTest1 { + public static void main(String[] args) { + AnimalTest1 aTest = new AnimalTest1(); // 자기 자신 객체 생성 + aTest.moveAnimal(new Human()); + aTest.moveAnimal(new Tiger()); + aTest.moveAnimal(new Eagle()); + } + + public void moveAnimal(Animal animal) { + animal.move(); + } +} + +``` +![image](https://github.com/user-attachments/assets/809f2846-37de-44ce-9898-dce2da34f779) +- instanceof로 원래 인스턴스형 확인 후 다운 캐스팅 하기 +''' +package chapter08.polymorphism; +import java.util.ArrayList; + +class Animal { + public void move() { + System.out.println("동물이 움직입니다."); + } +} + +class Human extends Animal { + public void move() { + System.out.println("사람이 두 발로 걷습니다."); + } + + public void readBook() { + System.out.println("사람이 책을 읽습니다."); + } +} + +class Tiger extends Animal { + public void move() { + System.out.println("호랑이가 네 발로 뜁니다."); + } + + public void hunting() { + System.out.println("호랑이가 사냥을 합니다."); + } +} + +class Eagle extends Animal { + public void move() { + System.out.println("독수리가 하늘을 납니다."); + } + + public void flying() { + System.out.println("독수리가 날개를 쭉 펴고 멀리 날아갑니다."); + } +} +public class AnimalTest { + ArrayList aniList = new ArrayList(); // Animal 타입의 ArrayList 선언 + + public static void main(String[] args) { + AnimalTest aTest = new AnimalTest(); + System.out.println("원래 융으로 다룰 캐스팅"); + aTest.addAnimal(); + aTest.testCasting(); + } + + public void addAnimal() { + aniList.add(new Human()); // Human 객체 추가 + aniList.add(new Tiger()); // Tiger 객체 추가 + aniList.add(new Eagle()); // Eagle 객체 추가 + + for (Animal ani : aniList) { + ani.move(); // 각 동물의 move 메서드 호출 + } + } + + public void testCasting() { + for (int i = 0; i < aniList.size(); i++) { // 모든 요소를 하나씩 돌면서 + Animal ani = aniList.get(i); // Animal 타입으로 가져옴 + if (ani instanceof Human) { + Human h = (Human) ani; // Human이면 캐스팅 + h.readBook(); // Human의 readBook 메서드 호출 + } else if (ani instanceof Tiger) { + Tiger t = (Tiger) ani; // Tiger이면 캐스팅 + t.hunting(); // Tiger의 hunting 메서드 호출 + } else if (ani instanceof Eagle) { + Eagle e = (Eagle) ani; // Eagle이면 캐스팅 + e.flying(); // Eagle의 flying 메서드 호출 + } else { + System.out.println("지원되지 않는 형입니다."); // 지원되지 않는 형일 경우 + } + } + } +} + +''' +- ![image](https://github.com/user-attachments/assets/889a55fc-6336-48d1-a8a8-be06c785e2b3) +- From e63cdf513f1b3a0cfd880b1de9f817e2822e9c1d Mon Sep 17 00:00:00 2001 From: alpaca07 <108653023+alpaca07@users.noreply.github.com> Date: Sun, 4 May 2025 21:14:48 +0900 Subject: [PATCH 07/16] Create chapter09.md --- "\354\244\200\355\231\230/chapter09.md" | 47 +++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 "\354\244\200\355\231\230/chapter09.md" diff --git "a/\354\244\200\355\231\230/chapter09.md" "b/\354\244\200\355\231\230/chapter09.md" new file mode 100644 index 0000000..71e2183 --- /dev/null +++ "b/\354\244\200\355\231\230/chapter09.md" @@ -0,0 +1,47 @@ + + +## 🌅 공부기록 +1. 추상 클래스 +2. 탬플릿 메서드 +3. 템플릿 메소드 응용하기 +4. final 예약어 +### 🧠 배운 점 +- 추상 클래스란? +- 추상 클래스는 항상 추상 메서드를 포함합니다. 추상 메서드는 구현 코드가 없습니다. 함수의 구현 코드가 없다는 것은 함수 몸체가 없다는 뜻 +![image](https://github.com/user-attachments/assets/f8c82850-a8ff-4e6f-883c-af3c03d74355) +중괄호로 감싼 부분은 함수의 구현부라고 한다. +이 부분이 없는 함수는 추상 함수이고 자바에서는 추상 메서드라고 합니다. +추상메서드는 그림과 같이 선언만 하며 abstract 예약어를 사용합니다. 그리고 {} 대신에 ;를 쓴다. +![image](https://github.com/user-attachments/assets/b2a1393e-1bd9-41e5-8189-9cef5d7a6486) +- 추상 클래스 구현하기 +![image](https://github.com/user-attachments/assets/e97e8182-45b2-427f-9a53-978c36cebf5e) +- 추상 메서드 구현하기 +![image](https://github.com/user-attachments/assets/d88f1a8a-3a67-4342-a592-f64cb6fbc8fe) +- NoteBook 클래스 구현하기 +![image](https://github.com/user-attachments/assets/799a67fe-1d38-4117-89ce-ffe274d5f710) +- MyNotebook 클래스 구현하기 +![image](https://github.com/user-attachments/assets/e8c392b6-0b6a-4e1f-bc2a-7102fa1e6bc3) +- 추상 클래스 테스트 하기 +![image](https://github.com/user-attachments/assets/ed7929a1-e7cb-4017-bf54-0b428abfa91e) +하지만 클래스를 인스턴스로 생성 할 수 없다고 나온다. + +- 추상클래스와 템플릿 메서드 +템플릿 메서드 : 틀이 있는 메서드 +![image](https://github.com/user-attachments/assets/d38aae5b-0c5f-435d-aab8-c1c587285a58) +- AIcar 클래스 구현 +![image](https://github.com/user-attachments/assets/2300a96f-bc06-4592-898b-aed083535dee) +- ManualCar +![image](https://github.com/user-attachments/assets/fb9927ad-e821-4c95-abd8-26671da8680f) +- 두 자동차가 어떻게 움직이는지 확인 +![image](https://github.com/user-attachments/assets/b3db1d3c-cb41-43e6-8512-3b34773173fd) + +playerLevel 추상 클래스 구현하기 +![image](https://github.com/user-attachments/assets/c169f37b-7eac-4076-9637-47617524bf64) +초보자 레벨 클래스 구현하기 +![image](https://github.com/user-attachments/assets/e07b3674-49ec-4a3f-b122-640c4b460424) +중급자 +![image](https://github.com/user-attachments/assets/f7db5135-c74d-4d7e-81a6-a629793f3d23) +고급자 +![image](https://github.com/user-attachments/assets/218094a1-acae-4700-815c-c0dd5c388df2) +테스트 프로그램 실행하기 +![image](https://github.com/user-attachments/assets/aa0b19f9-e7bf-4b47-865b-b50aa438670b) From 972cf216703102b0ea53d3a8211476fd5a225f03 Mon Sep 17 00:00:00 2001 From: alpaca07 <108653023+alpaca07@users.noreply.github.com> Date: Sun, 4 May 2025 21:37:30 +0900 Subject: [PATCH 08/16] Create chapter10.md --- "\354\244\200\355\231\230/chapter10.md" | 34 +++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 "\354\244\200\355\231\230/chapter10.md" diff --git "a/\354\244\200\355\231\230/chapter10.md" "b/\354\244\200\355\231\230/chapter10.md" new file mode 100644 index 0000000..0bbb71c --- /dev/null +++ "b/\354\244\200\355\231\230/chapter10.md" @@ -0,0 +1,34 @@ + + +## 🌅 공부기록 +1.인터페이스란 +2. 인터페이스와 다형성 +3. 인터페이스 요소 살펴보기 +4. 인터페이스 활용하기 +### 🧠 배운 점 +인터페이스란 +- 클래스 혹은 프로그램이 제공하는 기능을 명시적으로 선언하는 역할 +- 추상 메서드와 상수로만 이루어져 있다. +calc 인터페이스 부분 다시해서 수정하겠습니다. + scheduler 인터페이스 정의하기 + ![image](https://github.com/user-attachments/assets/ceaf39d2-bd97-4fcb-96c8-c08f96456e61) + 순서대로 배분하기 + ![image](https://github.com/user-attachments/assets/99dabce0-8682-4400-a454-1c4eaa04466c) + 짧은 대기열 먼저 배분하기 + ![image](https://github.com/user-attachments/assets/73dbcee1-30db-4ed1-8204-77d3097a6624) + 우선순위에 따라 배분하기 + ![image](https://github.com/user-attachments/assets/402b034b-8a0a-41c6-98e6-777ccccfea4b) + 입력 문자에 따라 배분 정책 수행하기 + ![image](https://github.com/user-attachments/assets/11fa8ed9-a9c4-4375-ba88-efe4d61dea2b) + + shelf 클래스 만들기 + ![image](https://github.com/user-attachments/assets/982966f5-3234-4dae-af15-e140eab137f3) + Queue 인터페이스 정의하기 + ![image](https://github.com/user-attachments/assets/a1b310c8-285c-46bb-90e7-90359236e912) + BookShelf 클래스 구현하기 + ![image](https://github.com/user-attachments/assets/fbb6e1c1-0888-4e7f-9ce6-fdea716f6e24) + BookShelf 테스트 하기 + ![image](https://github.com/user-attachments/assets/41e2469f-4618-4005-bb5f-faab4ed7986e) + + + From ca4d93f7e7691371f5a3da4c2df00cd2ea165183 Mon Sep 17 00:00:00 2001 From: alpaca07 <108653023+alpaca07@users.noreply.github.com> Date: Mon, 5 May 2025 16:16:34 +0900 Subject: [PATCH 09/16] Update chapter10.md --- "\354\244\200\355\231\230/chapter10.md" | 34 +++++++++++++++---------- 1 file changed, 21 insertions(+), 13 deletions(-) diff --git "a/\354\244\200\355\231\230/chapter10.md" "b/\354\244\200\355\231\230/chapter10.md" index 0bbb71c..5b9a83f 100644 --- "a/\354\244\200\355\231\230/chapter10.md" +++ "b/\354\244\200\355\231\230/chapter10.md" @@ -10,25 +10,33 @@ - 클래스 혹은 프로그램이 제공하는 기능을 명시적으로 선언하는 역할 - 추상 메서드와 상수로만 이루어져 있다. calc 인터페이스 부분 다시해서 수정하겠습니다. - scheduler 인터페이스 정의하기 +- scheduler 인터페이스 정의하기 + ![image](https://github.com/user-attachments/assets/ceaf39d2-bd97-4fcb-96c8-c08f96456e61) - 순서대로 배분하기 +- 순서대로 배분하기 ![image](https://github.com/user-attachments/assets/99dabce0-8682-4400-a454-1c4eaa04466c) - 짧은 대기열 먼저 배분하기 +- 짧은 대기열 먼저 배분하기 ![image](https://github.com/user-attachments/assets/73dbcee1-30db-4ed1-8204-77d3097a6624) - 우선순위에 따라 배분하기 +- 우선순위에 따라 배분하기 ![image](https://github.com/user-attachments/assets/402b034b-8a0a-41c6-98e6-777ccccfea4b) - 입력 문자에 따라 배분 정책 수행하기 +- 입력 문자에 따라 배분 정책 수행하기 ![image](https://github.com/user-attachments/assets/11fa8ed9-a9c4-4375-ba88-efe4d61dea2b) - shelf 클래스 만들기 - ![image](https://github.com/user-attachments/assets/982966f5-3234-4dae-af15-e140eab137f3) - Queue 인터페이스 정의하기 - ![image](https://github.com/user-attachments/assets/a1b310c8-285c-46bb-90e7-90359236e912) - BookShelf 클래스 구현하기 - ![image](https://github.com/user-attachments/assets/fbb6e1c1-0888-4e7f-9ce6-fdea716f6e24) - BookShelf 테스트 하기 - ![image](https://github.com/user-attachments/assets/41e2469f-4618-4005-bb5f-faab4ed7986e) + ![image](https://github.com/user-attachments/assets/0e196306-2bdb-4a53-9caf-c856f880b2ed) + +- shelf 클래스 만들기 + + ![image](https://github.com/user-attachments/assets/f5488a2d-2388-4dae-b85f-0db5a7cca845) +- Queue 인터페이스 정의하기 + + ![image](https://github.com/user-attachments/assets/8618936a-fcae-44e8-8e09-76c0216663c9) +- BookShelf 클래스 구현하기 + + ![image](https://github.com/user-attachments/assets/d9974401-88ad-430d-9b7a-18b6ebc2c90e) +- BookShelf 테스트 하기 + + ![image](https://github.com/user-attachments/assets/f3e8860c-5b7c-4efa-8742-801f2d963ef5) + ![image](https://github.com/user-attachments/assets/316f8a50-97cb-4452-9ea9-6e3fea0b8d70) From 0f40c3126cb46eebf4770a3c988fcfb39e930b89 Mon Sep 17 00:00:00 2001 From: alpaca07 <108653023+alpaca07@users.noreply.github.com> Date: Sun, 11 May 2025 18:28:39 +0900 Subject: [PATCH 10/16] Create chapter_11.md --- "\354\244\200\355\231\230/chapter11.md" | 235 ++++++++++++++++++++++++ 1 file changed, 235 insertions(+) create mode 100644 "\354\244\200\355\231\230/chapter11.md" diff --git "a/\354\244\200\355\231\230/chapter11.md" "b/\354\244\200\355\231\230/chapter11.md" new file mode 100644 index 0000000..1d278bf --- /dev/null +++ "b/\354\244\200\355\231\230/chapter11.md" @@ -0,0 +1,235 @@ + +# 11 기본클래스 +## 🌅 공부기록 +1. Object 클래스 +2. String 클래스 +3. Wrapper 클래스 +4. Class 클래스 +### 🧠 배운 점 +object 클래스 +- 모든 자바 클래스의 최상위 클래스 +- 컴파일 과정에서 extends object가 자동으로 쓰여진다. +- Object 메서드 + | 메서드 | 설명 | + | --- | --- | + | String toString( ) | 객체를 문자열로 표현하여 반환, 재정의하여 객체에 대한 설명이나 특정 멤버 변수 값을 반환 | + | boolean equals(Object obj) | 두 인스턴스가 동일한지 여부를 반환합니다. 재정의하여 논리적으로 동일한 인스턴스임을 정의할 수 있다. | + | int hashCode( ) | 객체의 해시 코드 값을 반환 | + | Object clone( ) | 객체를 복제하여 동일한 멤버 변수 값을 가진 새로운 인스턴스 생성 | + | Class getClass( ) | 객체의 Class 클래스를 반환 | + | void finalize( ) | 인스턴스가 힙 메모리에서 제거될 때 가비지 컬렉터에 의해 호출되는 메서드, 네트워크 해제, 열려 있는 파일 스트림 해체 등 구현 | + | void wait( ) | 멀티스레드 프로그램에서 사용하는 메서드, 스레드를 기다리는 상태로 만듬 | + | void notify( ) | wai( ) 메서드에 의해 기다리고 있는 스레드를 실행 가능한 상태로 가지고 온다 | + +- Object 클래스의 toString( ) 메서드 + - 생성된 인스턴스의 클래스 이름과 주소값을 보여준다 +![image](https://github.com/user-attachments/assets/040dc097-4bc1-4f8a-acff-e2181b0ced24) +![image](https://github.com/user-attachments/assets/0773d725-e313-458d-b249-6ad6d1d8e51d) + - toString( ) 메서드 정의하기 +![image](https://github.com/user-attachments/assets/87f9d888-0cb1-4e2b-a082-8066cda57aaf) +![image](https://github.com/user-attachments/assets/cf685a83-7d19-40ea-8c25-d293823afe94) + +- equals( ) 메서드 + - 두 인스턴스가 있을 때 `==` 는 단순히 물리적으로 같은 메모리 주소인지 여부를 확인 할 수있고, 논리적으로 같은 인스턴스인지 확인하도록 구현할 수 있습니다. +``` +package chapter11.object; + +class Student { + int studentId; + String studentName; + + public Student(int studentid, String studentName) { + this.studentId = studentid; + this.studentName = studentName; + } + + public String toString() { + return studentId + "," + studentName; + } +} + +public class EqualsTest { + public static void main(String[] args) { + Student studentLee = new Student(100, "이상원"); + Student studentLee2 = studentLee; //주소 복사 + Student studentSang = new Student(100, "이상원"); + + if(studentLee == studentLee2) // == 기호로 비교 + System.out.println("studentLee와 studentLee2의 주소는 같습니다."); + else + System.out.println("studentLee와 studentLee2의 주소는 다릅니다."); + + if(studentLee.equals(studentLee2)) // equals() 메서드로 비교 + System.out.println("studentLee와 studentLee2의 주소는 동일합니다."); + else + System.out.println("studentLee와 studentLee2의 주소는 동일하지 않습니다."); + + if(studentLee == studentSang)// == 기호로 비교 + System.out.println("studentLee와 studentSang의 주소는 같습니다."); + else + System.out.println("studentLee와 studentSang의 주소는 다릅니다."); + + if(studentLee.equals(studentSang))// equals() 메소드로 비교교 + System.out.println("studentLee와 studentSang은 동일합니다."); + else + System.out.println("studentLee와 studentSang은 동일하지 않습니다."); + } +} + +``` +![image](https://github.com/user-attachments/assets/66cb370b-edcd-4e86-8687-ea82a20357d6) + + - String과 Integer 클래스의 equals()메서드 +![image](https://github.com/user-attachments/assets/2cb5603f-72e2-4c61-9ef0-0e0788ea1839) +![image](https://github.com/user-attachments/assets/dccf3834-1685-4e8d-bb07-65f0505db69f) + + - equals() 메서드 재정의하기 +![image](https://github.com/user-attachments/assets/83eebda9-11dc-4592-a4d9-91a47788bc04) + +- hashCode()메서드 + - 객체의 특정 정보를 매개변수 값으로 넣으면 그 객체가 저장되어야 할 위치나 저장된 해시 테이블 주소를 반환 합니다 + - 객체 정보를 알면 해당 객체의 위치를 빠르게 검색할 수 있다. + + - String과 Integer 클래스의 hashCode() 메서드 +![image](https://github.com/user-attachments/assets/a459b6bc-edbe-4402-8fb5-e07aef889eec) +![image](https://github.com/user-attachments/assets/73df6209-d274-4ab2-a579-c915f2a93691) + + - hashCode() 메서드 재정의하기 +![image](https://github.com/user-attachments/assets/a76d85bd-e692-4487-8831-4c66148b3f6b) +![image](https://github.com/user-attachments/assets/0d81cd9b-7d81-45ec-a73f-823a011ff786) + + +- clone() 메서드 + - 가본 틀의 복사본을 사용해 동일한 인스턴스를 만들어 복잡한 생성 과정을 간단히 하려는 경우에 사용 + + - clone 메서드로 인스턴스 복제하기 +``` +package chapter11.object; +//원점을 의미하는 Point 클래스 +class Point { + int x; + int y; + + Point(int x, int y) { + this.x = x; + this.y = y; + } + + public String toString() { + return "x = " + x + ", " + "y = " + y; + } +} +// 객체를 복제해도 된다는 의미로 Cloneable 인터페이스를 함께 선언 +class Circle implements Cloneable { + Point point; + int radius; + + Circle(int x, int y, int radius) { + this.radius = radius; + point = new Point(x, y); + } + + public String toString() { + return "원점 = " + point + ", " + "반지름은 " + radius + "입니다."; + } +//clone() 메서드를 사용할 때 발생할 수 있는 오류를 예외 처리함함 + @Override + public Object clone() throws CloneNotSupportedException { + return super.clone(); + } +} + +public class ObjectCloneTest { + public static void main(String[] args) throws CloneNotSupportedException { + Circle circle = new Circle(10, 20, 30); + Circle copyCircle = (Circle)circle.clone(); // clone() 메서드를 사용해 circle 인스턴스를 copyCircle에 복제함함 + + System.out.println(circle); + System.out.println(copyCircle); + System.out.println(System.identityHashCode(circle)); + System.out.println(System.identityHashCode(copyCircle)); + } +} + +``` +![image](https://github.com/user-attachments/assets/664f0bdb-3e77-40ca-b2b3-d0b9a31836cf) + +- String 클래스 + - 생성자의 매개변수로 하여 생성하는 방식 + - 생성된 문자열 상수를 가리키는 방식 + +- 주소 값 비교하기 + +![image](https://github.com/user-attachments/assets/16cda20a-35b3-4ebf-8f49-27f2582ccdca) +![image](https://github.com/user-attachments/assets/0890f9bb-a6e0-4614-9ace-1250abe46910) + +- 두 문자열 연결하기 +- 두 문자열이 연결될때 변경이 되는게 아니라 두 문자열이 연결된 새로운 문자열이 생성 + +![image](https://github.com/user-attachments/assets/940922e7-5b37-4492-a87f-1a73016997f9) +![image](https://github.com/user-attachments/assets/e583a462-62e1-4941-a7b5-1ace0bdbd0e1) + +- StringBuilder 클래스 +- 내부 변경 가능한 Char[]를 변수로 가지고 있다. +- 기존 사용하던 char[] 배열이 확장되므로 추가 메모리를 사용하지 않는다. + +![image](https://github.com/user-attachments/assets/505b5214-307e-4094-8446-787588deefe2) +![image](https://github.com/user-attachments/assets/96d849e5-f642-4bed-8795-6974a087d695) + +- Class 클래스란? +- 클래스의 정보를 사용할 경우에 클래스 정보를 찾을때 사용 + 1. Object 클래스의 getClass() 메서드 사용하기 + 2. 클래스 파일 이름을 Class 변수에 직접 대입하기 + 3. Class.forName("클래스 이름") 메서드 사용하기 + +- 클래스 생성 +``` +package chapter11.classex; +public class Person { + private String name; + private int age; + + public Person() {} + + public Person(String name) { + this.name = name; + } + + public Person(String name, int age) { + this.name = name; + this.age = age; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public int getAge() { + return age; + } + + public void setAge(int age) { + this.age = age; + } +} + +``` +- Person의 Class 클래스 가져오기 + +![image](https://github.com/user-attachments/assets/3e187a41-a6b6-46e8-be56-72278c19d504) +![image](https://github.com/user-attachments/assets/50c2ae1a-a0d6-49f3-8b07-15ec16a6ae5e) + +- String 클래스 정보 가져오기 + +![image](https://github.com/user-attachments/assets/4c5d9d9e-94f6-4034-a78f-2220d81c88e9) + +- Person 클래스의 인스턴스 생성하기 + +![image](https://github.com/user-attachments/assets/a3d33ec1-d116-46e3-8962-474f9dd446c5) +![image](https://github.com/user-attachments/assets/f5cd9294-c6e7-4d77-b1d5-320321190ca3) + + From 81073cbc1da186698b48c34beac3c5f2e2981f1d Mon Sep 17 00:00:00 2001 From: alpaca07 <108653023+alpaca07@users.noreply.github.com> Date: Sun, 11 May 2025 18:33:01 +0900 Subject: [PATCH 11/16] Rename chapter11.md to chapter_11.md --- .../chapter11.md" => "\354\244\200\355\231\230/chapter_11.md" | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename "\354\244\200\355\231\230/chapter11.md" => "\354\244\200\355\231\230/chapter_11.md" (100%) diff --git "a/\354\244\200\355\231\230/chapter11.md" "b/\354\244\200\355\231\230/chapter_11.md" similarity index 100% rename from "\354\244\200\355\231\230/chapter11.md" rename to "\354\244\200\355\231\230/chapter_11.md" From 7de8165f1975f9fc043185a970be9d836b257b35 Mon Sep 17 00:00:00 2001 From: alpaca07 <108653023+alpaca07@users.noreply.github.com> Date: Sun, 11 May 2025 21:28:41 +0900 Subject: [PATCH 12/16] Create chapter_12.md --- "\354\244\200\355\231\230/chapter_12.md" | 404 +++++++++++++++++++++++ 1 file changed, 404 insertions(+) create mode 100644 "\354\244\200\355\231\230/chapter_12.md" diff --git "a/\354\244\200\355\231\230/chapter_12.md" "b/\354\244\200\355\231\230/chapter_12.md" new file mode 100644 index 0000000..f2ff63c --- /dev/null +++ "b/\354\244\200\355\231\230/chapter_12.md" @@ -0,0 +1,404 @@ + +# 12 컬렉션 프레임워크 +## 🌅 공부기록 +1. 제네릭 +2. 컬렉션 프레임워크 +3. List 인터페이스 +4. Set 인터페이스 +5. Map 인터페이스 +## 🧠 배운 점 +### 제네릭이란? + - 어떤 값이 하나의 참조 자료형이 아닌 여러 참조 자료형을 사용할 수 있도록 프로그래밍하는 것을 제네릭 프로그래밍이라고 합니다. + +- Powder,Plastic,GenericPrinter클래서 정의하기 + +![image](https://github.com/user-attachments/assets/e92ca533-95c2-4fe8-9fb7-d4091a53c4c2) +![image](https://github.com/user-attachments/assets/a8b2a94a-11d8-4727-96c1-2b241e74d115) +![image](https://github.com/user-attachments/assets/301f9f39-4f08-4414-958d-732102e7639b) + +- GenericPrinter 클래스 사용하기 +- 자료형을 명시하지 않은 경우 +![image](https://github.com/user-attachments/assets/c8462d30-4f10-43bb-9d1a-1584a05809e2) +- 자료형을 명시한 경우 +![image](https://github.com/user-attachments/assets/7ec00ffc-a374-46ef-bbdc-deaa542e0e0a) +![image](https://github.com/user-attachments/assets/a80bdc5e-7569-4201-8630-25f16dacddca) + +- 제네릭 클래스에서 T 자료형에 사용할 자료형에 제한을 두는것이 가능 +- 3D 출력에서 물을 출력할 수 없는거 처럼 사용할 클래스에 자료형 제한을 두는 방식으로 extends 예약어를 사용할 수 있다. +- 추상 클래스에서 상속을 받음 + +- Material 추상 클래스 +![image](https://github.com/user-attachments/assets/336acc70-def8-407b-8628-809ff433c072) +- 그리고 다른 클래스에서 extend로 받아쓴다. + +- 자료형 매개변수를 여러 개 사용하는 제네릭 메서드 예제 +- 자료형 매개변수를 두 개 사용하는 클래스 +![image](https://github.com/user-attachments/assets/351cce22-b933-4392-b418-646ae09039bb) + +- 제네락 메서드 구현하기 +![image](https://github.com/user-attachments/assets/8ceeb4bd-781d-4952-9efa-2561bab057bc) +![image](https://github.com/user-attachments/assets/745cf910-592a-42af-9324-8cc52ff66c15) + +### 컬러 프레임 워크란? +- 자료를 어떤 구조로 관리할 것인가? -> 자료구조 +- 자바에서 자료 구조를 미리 구현하여 java.util 패키지에서 제공한다. -> 컬렉션 프레임워크 +- Collection 인터페이스 + - List를 구현한 클래스는 순차적인 자료를 관리하는 데 사용하는 클래스 + - Set 인터페이스는 중복을 허용하지 않는다. + ![image](https://github.com/user-attachments/assets/fef0c348-039a-4a3f-a391-eddc658dcb70) + + | 메서드 | 설명 | + | --- | --- | + | boolean add(E e) | Collection에 객체를 추가한다 | + | void clear() | Collection의 모든 객체를 제거 | + | Iterator Iterator | Collection를 순환할 반복자를 반환 | + | boolean reamove(Object o) | Collection에 매개변수에 해당하는 인스턴스가 존재하면 제거 | + | int size | Collection에 있는 요소의 개수를 반환 | + +- Map 인터페이스 + - 하나가 아닌 쌍으로 되어 있는 자료를 관리하는 메서드들이 선언되어 있다. + - key-value 쌍이라고 표현하는데 이때 키 값은 중복될 수 없다. + + | 메서드 | 설명 | + | --- | --- | + | put(K key, V value) | key에 해당하는 value 값을 map에 넣습니다. | + | get(K key) | key에 해당하는 value 값을 반환합니다. | + | boolean isEmpty() | Map이 비었는지 여부를 반환합니다. | + | boolean containsKey(Object key) | Map에 해당 key가 있는지 여부를 반환합니다. | + | boolean containsValue(Object value) | Map에 해당 value가 있는지 여부를 반환합니다. | + | Set keySet() | key 집합을 Set으로 반환합니다 (중복 안 되므로 Set). | + | Collection values() | value를 Collection으로 반환합니다 (중복 무관). | + | remove(K key) | key가 있는 경우 삭제합니다. | + | boolean remove(Object key, Object value) | key가 있는 경우 key에 해당하는 value가 매개변수와 일치할 때 삭제합니다. + + - Member 클래스 구현하기 +![image](https://github.com/user-attachments/assets/cc04eb07-7696-40fd-9959-5309bd123a38) + +- ArrayList 활용하기 +``` +package chapter12.collection.arraylist; + +import java.util.ArrayList; +import chapter12.collection.Member; + +public class MemberArrayList { + private ArrayList arrayList; //ArrayList 선언 + + public MemberArrayList() { + arrayList = new ArrayList(); // Member형으로 선언한 ArrayList 생성 + } + //ArrayList에 회원을 추가하는 메서드드 + public void addMember (Member member) { + arrayList.add(member); + } + //해당 아이디를 가진 회원을 ArrayList에서 찾아 제거함함 + public boolean removeMember(int memberId) { + for(int i = 0; i < arrayList.size(); i++) { + Member member = arrayList.get(i); // get() 메서드로 회원을 순차적으로 가져옴 + int tempId = member.getMemberId(); + if(tempId == memberId) { // 회원 아이디가 매개변수와 일치하면 + arrayList.remove(i); // 해당 회원을 삭제제 + return true; + } + } + System.out.println(memberId + "가 존재하지 않습니다."); // 반복문이 끝날 때까지 해당 아이디를 찾지 못한 경우 + return false; + } + // 전체회원을 출력하는 메서드 + public void showAllMember() { + for(Member member : arrayList) { + System.out.println(member); + } + System.out.println(); + } +} + +``` +- MemberArrayListTest 클래스 구현하기 +![image](https://github.com/user-attachments/assets/db4c6b62-1a38-4736-b850-791b49dd8d21) +![image](https://github.com/user-attachments/assets/54c06b8a-dec7-428b-9593-22886b90821f) + +- LinkedList 클래스 + - 각 요소는 다음 요소를 가리키는 주소 값을 가진다. + - 따라서 물리적인 메모리는 떨어져 있어도 논리적으로는 앞 뒤 순서가 존재 + - 중간에 자료를 넣고 제거하는데 시간이 적게 걸린다는 장점이 있다. + - 크기를 동적으로 증가시킬 수 있다. + +- LinkedList 테스트하기 +![image](https://github.com/user-attachments/assets/529a63f5-c073-4656-91e2-3a2697eb428e) + +- 스택 구현하기 +``` +package chapter12.collection.arraylist; + +import java.util.ArrayList; + +class MyStack { + private ArrayList arrayStack = new ArrayList(); + //스택의 맨 뒤에 요소를 추가 + public void push(String data) { + arrayStack.add(data); + } + //스택의 맨 뒤에서 요소 꺼냄 + public String pop() { + int len = arrayStack.size();//ArrayList에 저장된 유효한 자료의 개수 + if(len == 0) { + System.out.println("스택이 비었습니다."); + return null; + } + return(arrayStack.remove(len-1));// 맨 뒤에 있는 자료 반환하고 배열에서 제거거 + } +} + +public class StackTest { + public static void main(String[] args) { + MyStack stack = new MyStack(); + stack.push("A"); + stack.push("B"); + stack.push("C"); + + System.out.println(stack.pop()); + System.out.println(stack.pop()); + System.out.println(stack.pop()); + } +} + +``` +![image](https://github.com/user-attachments/assets/4fe2f9a7-3693-4b9c-a41d-5b883ab13435) + +- 큐 구현하기 +``` +package chapter12.collection.arraylist; + +import java.util.ArrayList; + +class MyQueue { + private ArrayList arrayQueue = new ArrayList(); + // 큐의 맨 뒤에 추가 + public void enQueue(String data) { + arrayQueue.add(data); + } + // 큐의 맨 앞에서 꺼냄냄 + public String deQueue() { + int len = arrayQueue.size(); + if(len == 0) { + System.out.println("큐가 비었습니다"); + return null; + } + return(arrayQueue.remove(0)); // 맨 앞의 자료 반환하고 배열에서 제거 + } +} +public class QueueTest { + public static void main(String[] args) { + MyQueue queue = new MyQueue(); + queue.enQueue("A"); + queue.enQueue("B"); + queue.enQueue("C"); + + System.out.println(queue.deQueue()); + System.out.println(queue.deQueue()); + System.out.println(queue.deQueue()); + } +} + +``` +![image](https://github.com/user-attachments/assets/2d6bfe38-3458-4582-a3a1-e81a03fd2178) + +- HashSet 클래스 + - 집합 자료 구조를 구현하며 중복을 허용하지 않습니다. + +- HashSet 테스트하기 +![image](https://github.com/user-attachments/assets/9d586b52-9704-42e7-a3d0-4c688499bfe6) +![image](https://github.com/user-attachments/assets/f038bee1-6b17-423b-8f5e-b54845e3c9d4) + +- HashSet 활용하기 +``` +package chapter12.collection.hashset; + +import java.util.HashSet; +import java.util.Iterator; + +import chapter12.collection.Member; +public class MemberHashSet { + private HashSet hashSet; + + public MemberHashSet() { + hashSet = new HashSet(); + } + + public void addMember(Member member) { + hashSet.add(member); + } + + public boolean removeMember(int memberId) { + Iterator ir = hashSet.iterator(); + + while(ir.hasNext()) { + Member member = ir.next(); + int tempId = member.getMemberId(); + if(tempId == memberId) { + hashSet.remove(member); + return true; + } + } + System.out.println(memberId + "가 존재하지 않습니다."); + return false; + } + + public void showAllMember() { + for(Member member : hashSet) { + System.out.println(member); + } + System.out.println(); + } +} + +``` +![image](https://github.com/user-attachments/assets/662000b7-99ad-464c-ace1-bce94a2f9c40) +![image](https://github.com/user-attachments/assets/c351a2cf-c7a8-4726-972b-4b1cbee4d4c5) + +- TreeSet 클래스 + - 자료의 중복을 허용하지 않으면서 출력 결과 값을 정렬하는 클래스 입니다. +![image](https://github.com/user-attachments/assets/bb8d3bc8-af31-4de9-bec3-3de1f28a1563) +![image](https://github.com/user-attachments/assets/8f35b362-220b-4532-a6f6-34f52455e2d3) +``` +package chapter12.collection.treeset; + +import java.util.Iterator; +import java.util.TreeSet; + +import chapter12.collection.Member; + +public class MemberTreeSet { + private TreeSet treeSet; + + public MemberTreeSet() { + treeSet = new TreeSet(); + } + //회원을 추가하는 메서드 + public void addMember(Member member) { + treeSet.add(member); + } + // 회원을 삭제하는 메서드드 + public boolean removeMember(int memberId) { + Iterator ir = treeSet.iterator(); + + while(ir.hasNext()) { + Member member = ir.next(); + int tempId = member.getMemberId(); + if(tempId == memberId) { + treeSet.remove(member); + return true; + } + } + System.out.println(memberId + "가 존재하지 않습니다."); + return false; + } + //전체 회원을 출력하는 메서드드 + public void showAllMember() { + for(Member member : treeSet) { + System.out.println(member); + } + System.out.println(); + } +} + +``` +![image](https://github.com/user-attachments/assets/dcbc96c0-9634-4c75-8e58-ecd95e165586) + +- Comparable 인터페이스 + - 자기 자신과 전달받은 매개변수를 비교하는 인터페이스 +![image](https://github.com/user-attachments/assets/0561ffd2-611e-44de-9613-7fb3f96c1d4c) + +![image](https://github.com/user-attachments/assets/22e51f91-15ee-4eb3-a13b-780dba8d6433) + +- HashMap 클래스 +- HashMap 활용하기 +``` +package chapter12.collection.hashmap; + +import java.util.HashMap; +import java.util.Iterator; + +import chapter12.collection.Member; + +public class MemberHashMap { + private HashMap hashMap; + + public MemberHashMap() { + hashMap = new HashMap(); + } + //회원을 추가하는 메서드 + public void addMember(Member member) { + hashMap.put(member.getMemberId(), member);//key-value 쌍으로 추가가 + } + //회원을 삭제하는 메서드드 + public boolean removeMember(int memberId) { + if(hashMap.containsKey(memberId)) { // 매개변수로 받은 키 값인 회원 아이디가 있다면 + hashMap.remove(memberId);// 해당 회원 삭제제 + return true; + } + System.out.println(memberId + "가 존재하지 않습니다."); + return false; + } + //Iterator를 사용하여 전체 회원을 출력하는 메서드 + public void showAllMember() { + Iterator ir = hashMap.keySet().iterator(); + while (ir.hasNext()) { + int key = ir.next(); + Member member = hashMap.get(key); + System.out.println(member); + } + System.out.println(); + } +} + +``` +![image](https://github.com/user-attachments/assets/d5d56eaa-bbeb-4b45-a889-e0558c37bb64) +![image](https://github.com/user-attachments/assets/87bf6ee7-5fdc-4815-9f63-cd568a6b7800) + +- TreeMap 클래스 + - key 값으로 자료를 정렬할려면 사용 +``` +package chapter12.collection.treemap; + +import java.util.Iterator; +import java.util.TreeMap; + +import chapter12.collection.Member; +public class MemberTreeMap { + private TreeMap treeMap; + + public MemberTreeMap() { + treeMap = new TreeMap(); + } + + public void addMember(Member member) { + treeMap.put(member.getMemberId(), member);//key-value 쌍으로 추가가 + } + + public boolean removeMember(int memberId) { + if(treeMap.containsKey(memberId)){ + treeMap.remove(memberId);//key 값에 맞는 자료 삭제제 + return true; + } + System.out.println(memberId + "가 존재하지 않습니다."); + return false; + + } + + public void showAllMember() { + Iterator ir = treeMap.keySet().iterator(); + while (ir.hasNext()) { + int key = ir.next(); + Member member = treeMap.get(key); + System.out.println(member); + } + System.out.println(); + } +} + + +``` +![image](https://github.com/user-attachments/assets/fae73b33-5105-4368-8348-c2b680f0a677) +![image](https://github.com/user-attachments/assets/afb0ca8c-67ac-46c0-8650-41a81a1a2dab) From 7a9e7c225e1541be1a0d1c8445c819edec745f60 Mon Sep 17 00:00:00 2001 From: alpaca07 <108653023+alpaca07@users.noreply.github.com> Date: Sun, 11 May 2025 22:21:29 +0900 Subject: [PATCH 13/16] Create chapter_13.md --- "\354\244\200\355\231\230/chapter_13.md" | 225 +++++++++++++++++++++++ 1 file changed, 225 insertions(+) create mode 100644 "\354\244\200\355\231\230/chapter_13.md" diff --git "a/\354\244\200\355\231\230/chapter_13.md" "b/\354\244\200\355\231\230/chapter_13.md" new file mode 100644 index 0000000..8b76aa9 --- /dev/null +++ "b/\354\244\200\355\231\230/chapter_13.md" @@ -0,0 +1,225 @@ + +# 12 컬렉션 프레임워크 +## 🌅 공부기록 +1. 내부 클래스 +2. 람다식 +3. 스트림 +## 🧠 배운 점 +### 내부 클래스 +- 선언하는 위치나 예약어에 따라 네가지 유형 + 1. 인스턴스 내부 클래스 + 2. 정적 내부 클래스 + 3. 지역 내부 클래스 + 4. 익명 내부 클래스 + +- 인스턴스 내부 클래스 + - 인스턴스 변수를 선언할 때와 같은 위치에 선언하며, 외부 클래스 내부에서만 생성하여 사용하는 객체를 선언할 때 사용 + - 외부 클래스를 먼저 생성하지 않고 인스턴스 내부 클래스를 사용할 수 없습니다. + +- 인스턴스 내부 클래스 +``` +package chapter13.innerclass; + +class OutClass { + private int num = 10; + private static int sNum = 20; + + private InClass inClass;// 내부 클래스 자료형 변수를 먼저 선언 + + // 외부 클래스 디폴트 생성자. 외부 클래스가 생성된 후에 내부 클래스 생성가능능 + public OutClass() { + inClass = new InClass(); + } + + class InClass { + int inNum = 100; + + void inTest() { + System.out.println("OutClass num = " + num + " (외부 클래스의 인스턴스 변수)"); + System.out.println("OutClass sNum = " + sNum + " (외부 클래스의 정적 변수)"); + } + } + + public void usingClass() { + inClass.inTest(); + } +} + +public class InnerTest { + public static void main(String[] args) { + OutClass outClass = new OutClass(); + System.out.println("외부 클래스 이용하여 내부 클래스 기능 호출"); + outClass.usingClass();// 내부 클래스 기능 호출출 + } +} + +``` +![image](https://github.com/user-attachments/assets/72c020cc-51ad-40b9-9db5-d77c1b1c224b) + +- 정적 내부 클래스 + - 내부 클래스가 외부 클래스 생성과 무관하게 사용할 수 있어야 하고 정적 변수도 사용 할 수 있어야 한다면 사용 + - 내부 클래스처럼 외부 클래스의 멤버 변수와 같은 위치에 정의하며 static 예약어를 함께 사용합니다. + +``` +package chapter13.innerclass; + +class OutClass { + private int num = 10; + private static int sNum = 20; + + static class InStaticClass { + int inNum = 100; + static int sInNum = 200; + //정적 내부 클래스의 일반 메서드드 + void inTest() { + System.out.println("InStaticClass inNum" +inNum+ "(내부 클래스의 인스턴스 변수 사용)"); + System.out.println("InStaticClass sInNum=" + sInNum + "(내부 클래스의 정적 변수 사용)"); + System.out.println("OutClass sNum=" +sNum + "(외부 클래스의 정적 변수 사용)"); + } + // 정적 내부 클래스의 정적 메서드드 + static void sTest() { + System.out.println("OutClass sNum="+sNum+"(외부 클래스의 정적 변수 사용)"); + System.out.println("InStaticClass sInNum="+sInNum+"외부 클래스의 정적 변수 사용"); + } + } +} + +public class InnerTest2 { + public static void main(String[] args) { + OutClass.InStaticClass sInClass = new OutClass.InStaticClass(); // 외부 클래스를 생성하지 않고 바로 정적 내부 클래스 생성 가능능 + System.out.println("정적 내부 클래스 일반 메서드 호출"); + sInClass.inTest(); + + System.out.println(); + System.out.println("정적 내부 클래스의 정적 메서드 호출"); + OutClass.InStaticClass.sTest(); + } +} + +``` +![image](https://github.com/user-attachments/assets/2ae1a9d3-e23d-4d7e-8945-ad73389a661e) + +| 정적 내부 클래스 메서드 | 변수 유형 | 사용 가능 여부 | +| --- | --- | --- | +| **일반 메서드** void inTest() | 외부 클래스의 인스턴스 변수(num) | ✕ | +| | 외부 클래스의 정적 변수(sNum) | ○ | +| | 정적 내부 클래스의 인스턴스 변수(inNum) | ○ | +| | 정적 내부 클래스의 정적 변수(sInNum) | ○ | +| **정적 메서드** static void sTest() | 외부 클래스의 인스턴스 변수(num) | ✕ | +| | 외부 클래스의 정적 변수(sNum) | ○ | +| | 정적 내부 클래스의 인스턴스 변수(inNum) | ✕ | +| | 정적 내부 클래스의 정적 변수(sInNum) | ○ | + +- 정적 내부 클래스에서 사용하는 메서드가 정적 메서드인 경우에는 외부 클래스와 정적 내부 클래스에 선언된 변수 중 정적 변수만 사용 가능 + + +- 지역 내부 클래스 + - 지역 변수처럼 메서드 내부에 클래스를 정의하여 사용하는 것 처럼 말한다. + +``` +package chapter13.innerclass; + +class Outer { + int outNum = 100; + static int sNum = 200; + + Runnable getRunnable(int i) { + int num = 100; + + class MyRunnable implements Runnable { + int localNum = 10; + + @Override + public void run() { + System.out.println("i=" + i); + System.out.println("num=" +num); + System.out.println("localNum=" +localNum); + System.out.println("outNum=" +outNum + "(외부 클래스 인스턴스 변수)"); + System.out.println("Outer.sNUm=" + Outer.sNum + "(외부 클래스 정적 변수)"); + } + } + return new MyRunnable(); + } +} +public class LocallnnerTest { + public static void main(String[] args) { + Outer out = new Outer(); + Runnable runner = out.getRunnable(10); + runner.run(); + } +} + +``` +![image](https://github.com/user-attachments/assets/bed7475c-413d-4e0f-be49-d04687a33b38) + + +- 익명 내부 클래스 + - 클래스 이름을 사용하지 않는 클래스 + +![image](https://github.com/user-attachments/assets/1d8fd07f-9aa7-4f07-986c-57f4e871ed17) +![image](https://github.com/user-attachments/assets/208b97d3-eb51-41fa-9cef-15bf6fd42fea) + +내부 클래스 표 정리 +| 종류 | 구현 위치 | 사용할 수 있는 외부 클래스 변수 | 생성 방법 | +| --- | --- | --- | --- | +| 인스턴스 내부 클래스 | 외부 클래스 멤버 변수와 동일 | 외부 인스턴스 변수, 외부 정적 변수 | 외부 클래스를 먼저 만든 후 내부 클래스 생성 | +| 정적 내부 클래스 | 외부 클래스 멤버 변수와 동일 | 외부 정적 변수 | 외부 클래스와 무관하게 생성 | +| 지역 내부 클래스 | 메서드 내부에 구현 | 외부 인스턴스 변수, 외부 정적 변수 | 메서드를 호출할 때 생성 | +| 익명 내부 클래스 | 메서드 내부에 구현, 변수에 대입하여 직접 구현 | 외부 인스턴스 변수, 외부 정적 변수 | 메서드를 호출할 때 생성. 되거나, 인터페이스 타입 변수에 대입할 때 new 예약어를 사용하여 생성 | + +### 람다식 +- 함수형 프로그래밍 방식 +- 람다식 문법 (매개변수) -> {실행문;} + +- 함수형 인터페이스 선언하기 +![image](https://github.com/user-attachments/assets/2405be59-ac45-4997-9ce7-795ab76592b8) + +- 람다식 구현과 호출 +![image](https://github.com/user-attachments/assets/0662dd5d-6c0c-45fc-82ab-4676a9fbbbbf) +![image](https://github.com/user-attachments/assets/62535ec0-56a4-4b39-b58e-2c171608fa1d) + +- 함수형 인터페이스 + - 자바에서 참조 변수 없이 메서드를 호출할 수는 없다 + - 람다식을 구현하기 위해 함수형 인터페이스 제작 -> 인터페이스에 람다식으로 구현할 메서드 선언 + - 람다식은 하나의 메서드를 구현하여 인터페이스형 변수에 대립하므로 두 개 이상의 메서드를 가져서는 안된다. + +- 객체 지향 프로그래밍과 람다식 비교 + +- 인터페이스 구현 +![image](https://github.com/user-attachments/assets/b6126e35-ac65-42b6-b502-7db88b21d195) + +- 추상 메서드 구현 +![image](https://github.com/user-attachments/assets/73f288ae-9336-4e57-9c9b-1451b9f8fcad) + +- 메서드 테스트 +![image](https://github.com/user-attachments/assets/ab8d62c5-fee6-4a42-81a2-69454acc4e97) +![image](https://github.com/user-attachments/assets/a15fb6fc-4b92-4052-a363-e401955eb732) + +- 람다식으로 인터페이스 구현하기 +![image](https://github.com/user-attachments/assets/cba81268-7ec0-4807-8786-338248ae1ca7) +![image](https://github.com/user-attachments/assets/216c4b3d-ed05-454f-9e58-fb0644bc6c8b) + +###스트림이란? +- 여러 자료의 처리에 대한 기능을 구현해 놓은 클래스 + +- 정수 배열에서 스트림 활용 +![image](https://github.com/user-attachments/assets/b9cc9363-6aac-4443-a5c7-f1b5326d5e7d) + +- ArrayList에서 스트림 활용하기 +![image](https://github.com/user-attachments/assets/0806432c-29b1-4efd-a0dd-827f67d64bfb) +![image](https://github.com/user-attachments/assets/45681a86-0a21-4584-b460-b5e6051b88d7) + +- 스트림의 특징 + 1. 자료의 대상과 관계없이 동일한 연산을 수행한다. + 2. 한 번 생성하고 사용한 스트림은 재사용할 수 없다. + 3. 스트림의 연산은 기존 자료를 변경하지 않는다. + 4. 스트림의 연산은 중간 연산과 최종 연산이 있다. + +- reduce() 연산 + - 내부적으로 스트림의 요소를 하나씩 소모하면서 프로그래머가 직접 지정한 기능을 수행 +![image](https://github.com/user-attachments/assets/51ecfb0c-d276-4f32-a58b-f424c29a0c68) +![image](https://github.com/user-attachments/assets/dfa8ebee-df73-4bce-94e0-f9750b62ff75) + + + + From 02c8713b2f00ac04afa6f2a7e20aae394f19f30f Mon Sep 17 00:00:00 2001 From: alpaca07 <108653023+alpaca07@users.noreply.github.com> Date: Sun, 11 May 2025 22:55:21 +0900 Subject: [PATCH 14/16] Create chapter_14.md --- "\354\244\200\355\231\230/chapter_14.md" | 108 +++++++++++++++++++++++ 1 file changed, 108 insertions(+) create mode 100644 "\354\244\200\355\231\230/chapter_14.md" diff --git "a/\354\244\200\355\231\230/chapter_14.md" "b/\354\244\200\355\231\230/chapter_14.md" new file mode 100644 index 0000000..da8fd5f --- /dev/null +++ "b/\354\244\200\355\231\230/chapter_14.md" @@ -0,0 +1,108 @@ + +# 14 예외 처리 +## 🌅 공부기록 +1. 예외 클래스 +2. 예외 처리하기 +3. 예외 처리 미루기 +4. 사용자 정의 예외 +## 🧠 배운 점 +###오류란? +- 프로그램 오류는 2가지가 있다. + - 컴파일 오류 + - 실행 오류 + +- 실행 오류 + - 시스템 오류 + - 사용 가능한 동적 메모리가 없는 경우, 스택 메모리의 오버플로가 발생한 경우 + - 예외 + - 파일을 읽어 사용하려는데 파일이 없는경우, 네트워크로 데이터를 전송하려는데 연결이 안된 경우 +- 오류클래스 + - 모두 Throwable 클래스에서 상속을 받습니다. Error 클래스의 하위 클래스는 시스템에서 발생하는 오류를 다루며 프로그램에서 제어하지 않는다. + - 프로그램 에서 제어하는 부분은 Exception 클래스와 그 하위에 있는 예외 클래스 이다. + + - try-catch문 + ``` + try{ + 예외가 발생할 수 있는 코드 부분 + }catch(처리할 예외 타입 e) { + try 블록 안에서 예외가 발생했을 때 예외를 처리하는 부분 + } + ``` + ![image](https://github.com/user-attachments/assets/2f98e6a7-508a-4d90-8258-1708a2773260) + ![image](https://github.com/user-attachments/assets/53fd3c1c-bbb1-488c-880c-2fcf50d7ff5f) + + ![image](https://github.com/user-attachments/assets/b81d5c39-2dfb-4edd-b67c-888ec678c774) + ![image](https://github.com/user-attachments/assets/6efe9b21-c45a-479b-9640-831352d4adcf) + + - try-catch-finally + ``` + try{ + 예외가 발생할 수 있는 코드 부분 + }catch(처리할 예외 타입 e) { + 예외를 처리하는 부분 + } finally { + 항상 수행되는 부분 + } + ``` + - finally 블록 사용하기 + ![image](https://github.com/user-attachments/assets/32e116c1-08bc-42fd-863d-fd643e2ad366) + ![image](https://github.com/user-attachments/assets/bbb09efe-691e-4a25-8024-aa6b5b313e4c) + + - try-with-resources + + - AutoCloseable 인터페이스 구현 + ![image](https://github.com/user-attachments/assets/c80db23d-b1a0-43b3-b5c3-f7087c58305e) + + + - try-with-resources 사용하기 + ![image](https://github.com/user-attachments/assets/d1a1b11e-f3ad-4fdf-9b05-31a463152202) + ![image](https://github.com/user-attachments/assets/d6262fe9-fb3f-4490-868c-2a141d6a702c) + + + - throws로 예외 미루기 + ![image](https://github.com/user-attachments/assets/29d851f8-699f-4e0c-8ea7-3e29ab338cc1) + + + - 사용자 정의 예외 클래스 구현하기 + ![image](https://github.com/user-attachments/assets/d18c1639-7539-40f6-bb9b-832065e59c47) + ``` + package chapter14.exception; + + public class IDFormatTest { + private String userID; + + public String getUserID() { + return userID; + } + + public void setUserID(String userID) throws IDFormatException { + if (userID == null) { + throw new IDFormatException("아이디는 null일 수 없습니다"); + } else if (userID.length() < 8 || userID.length() > 20) { + throw new IDFormatException("아이디는 8자 이상 20자 이하로 쓰세요"); + } + this.userID = userID; + } + + public static void main(String[] args) { + IDFormatTest test = new IDFormatTest(); + + String userID = null; + try { + test.setUserID(userID); + } catch (IDFormatException e) { + System.out.println(e.getMessage()); + } + + userID = "1234567"; + try { + test.setUserID(userID); + } catch (IDFormatException e) { + System.out.println(e.getMessage()); + } + } + } + ``` + ![image](https://github.com/user-attachments/assets/44c61179-dbd1-41d7-bba2-3fa94e13adf6) + + From a31c62693fb8c19cb0018d4bdd839eb5ab986ccd Mon Sep 17 00:00:00 2001 From: alpaca07 <108653023+alpaca07@users.noreply.github.com> Date: Mon, 12 May 2025 00:20:39 +0900 Subject: [PATCH 15/16] Create chapter_15.md --- "\354\244\200\355\231\230/chapter_15.md" | 141 +++++++++++++++++++++++ 1 file changed, 141 insertions(+) create mode 100644 "\354\244\200\355\231\230/chapter_15.md" diff --git "a/\354\244\200\355\231\230/chapter_15.md" "b/\354\244\200\355\231\230/chapter_15.md" new file mode 100644 index 0000000..e8db377 --- /dev/null +++ "b/\354\244\200\355\231\230/chapter_15.md" @@ -0,0 +1,141 @@ + +# 14 자바 입출력 +## 🌅 공부기록 +1. 자바 입출력과 스트림 +2. 표준 입출력 +3. 바이트 단위 스트림 +4. 문자 단위 스트림 +5. 보조 스트림 +6. 직렬화 +7. 그 외 입출력 클래스 +## 🧠 배운 점 +### 스트림이란? +- 입출력 장치와 무관하고 일관성 있게 프로그램을 구현할 수 있도록 일종의 가상 통로인 스트림을 제공하는 것 +- 자료를 읽어 들이려는 소스 와 자료를 쓰려는 대상에 따라 각각 다른 스트림 클래스를 제공합니다. + +- 입력 스트림과 출력 스트림 + - 입력 자료의 이동이 출력 자료의 이동과 한 스트림에서 동시에 일어날 수 없기 때문에(단방향) + - 어떤 스트림이 있다고 하면 그 스트림은 입력 스트림이거나 출력 스트림 입니다 + + +- 바이트 단위 스트림과 문자 스트림 + - 자바의 스트림은 바이트 단위로 입출력이 이루어진다. + - 라지만 문자를 나타내는 char형 2바이트이기 때문에 1바이트만 읽으면 한글 같은 문자는 깨진다 + - 입출력 중 가장 많이 사용하는 자료인 문자를 위해 문자 스트림을 별도로 제공 + +- 기반 스트림과 보조 스트림 + - 기반 스트림 + - 읽어 들일 곳이나 써야 할 곳에서 직접 읽고 쓸 수 있으며 입출력 대상에 직접 연결되어 생성되는 스트림 + - 보조 스트림 + - 직접 읽고 쓰는 기능은 없음 따라서 항상 다른 스트림을 포함하여 생성 된다. + + +- 표준 입출력 + +- 문자 하나를 입력받기 +![image](https://github.com/user-attachments/assets/a1decf3c-ca06-43b1-a5d3-24fa9737ad54) +![image](https://github.com/user-attachments/assets/f18eba71-3309-4271-8677-d36be935733e) + +- 문자 여러 개를 입력받기 +![image](https://github.com/user-attachments/assets/4d4d153c-1c17-4bca-ab2c-19e133b7f563) +![image](https://github.com/user-attachments/assets/f673f57b-f9ed-49ae-a6eb-0fcffe19209b) + +- Scanner 테스트 하기 +![image](https://github.com/user-attachments/assets/cf0d1b07-166f-47f0-b3e2-e86f57161489) +![image](https://github.com/user-attachments/assets/22fc1630-e024-4bb2-8982-5fce083e7966) + +- Console 테스트 하기 +![image](https://github.com/user-attachments/assets/5c1f5718-0e98-4569-b0dc-ebc777f3c488) +![image](https://github.com/user-attachments/assets/57fc307f-6b3a-4231-a54d-debe408eb78b) + +- FileInputStream 사용하기 +![image](https://github.com/user-attachments/assets/f0c5fe00-a471-480c-b450-a48e4c584f5a) +![image](https://github.com/user-attachments/assets/73055784-c910-45c0-a1f6-d4aadbfb0731) + +- 파일 끝까지 읽기 +![image](https://github.com/user-attachments/assets/50a91e98-2483-46d8-9ed5-3fb4727cad69) +![image](https://github.com/user-attachments/assets/9faea9ec-119e-42db-8a8b-49c7d0d69b92) + +- byte 배열로 읽기 +![image](https://github.com/user-attachments/assets/5c907cfc-f2db-4454-b451-1ad9c8306ae8) +![image](https://github.com/user-attachments/assets/f4ef282f-32f6-46ca-8cda-8a92c26ba21e) + +- 파일에 한 바이트씩 출력하기 +![image](https://github.com/user-attachments/assets/746e0957-386f-4890-bad0-9269093111c6) +![image](https://github.com/user-attachments/assets/dac7c117-9458-452c-bc5a-e133b590e17b) +![image](https://github.com/user-attachments/assets/389de19e-4457-4a98-a82c-23d0780a72aa) + +- 파일에 바이트 배열로 출력하기 +![image](https://github.com/user-attachments/assets/0ef5be67-cc1f-4263-8081-be5766df9178) +![image](https://github.com/user-attachments/assets/02fbe494-4d47-48b1-9af8-269170a4fb54) +![image](https://github.com/user-attachments/assets/a652cef7-24ce-4e55-b7a8-2126296beb49) + +- 파일에 바이트 배열로 출력하기 +![image](https://github.com/user-attachments/assets/4c656b77-1020-4078-9e8d-b9c38ccabe46) +![image](https://github.com/user-attachments/assets/4c2b4501-3bd9-47f3-aa8b-80df9c606f66) +![image](https://github.com/user-attachments/assets/dfdf1eb8-9b1c-4e7c-87b6-d56ccc46008f) + +- FileReader로 읽기 +![image](https://github.com/user-attachments/assets/5fad2c66-48a9-4c9d-9182-1b96540eb512) +![image](https://github.com/user-attachments/assets/8ca83561-7626-403b-a079-5456e8a36e04) + +- FileWriter로 쓰기 +![image](https://github.com/user-attachments/assets/8039b272-06eb-42d0-8c1d-62d0843648df) +![image](https://github.com/user-attachments/assets/685e8364-aa5b-413c-8e51-b4efb81eb7db) +![image](https://github.com/user-attachments/assets/9214bb68-4a6e-4509-847e-645f2435bcd0) + +- InputStreamReader 사용하기 +![image](https://github.com/user-attachments/assets/b6f46926-065f-4274-ac16-b9ac6e683dfb) +![image](https://github.com/user-attachments/assets/4a4f588a-e707-45d0-959f-597aed8d8db1) + +- 파일 복사하기 +![image](https://github.com/user-attachments/assets/6fff3198-9731-4b0f-a11c-61c1e2d495d7) + +- 버퍼링 기능으로 파일 복사하기 +![image](https://github.com/user-attachments/assets/c1da8d50-b228-4f2e-a56e-98c5650d1ed3) + +- DataInputStream / DataOutputStream 테스트 하기 +``` +package chapter15.stream.decorator; + +import java.io.DataInputStream; +import java.io.DataOutputStream; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.IOException; + +public class DataStreamTest { + public static void main(String[] args) { + // 데이터를 파일에 쓰기 + try (FileOutputStream fos = new FileOutputStream("/home/alpaca/Java/src/chapter15/data.txt"); + DataOutputStream dos = new DataOutputStream(fos)) { + // 각 자료형에 맞게 자료를 씀 + dos.writeByte(100); + dos.writeChar('A'); + dos.writeInt(10); + dos.writeFloat(3.14f); + dos.writeUTF("Test"); + + } catch (IOException e) { + e.printStackTrace(); + } + + // 파일에서 데이터 읽기 + try (FileInputStream fis = new FileInputStream("/home/alpaca/Java/src/chapter15/data.txt"); + DataInputStream dis = new DataInputStream(fis)) { + // 자료형에 맞게 자료를 읽어 출력함. 파일에 쓴 순서와 같은 순서 같은 메서드로 읽어야 함함 + System.out.println(dis.readByte()); + System.out.println(dis.readChar()); + System.out.println(dis.readInt()); + System.out.println(dis.readFloat()); + System.out.println(dis.readUTF()); + + } catch (IOException e) { + e.printStackTrace(); + } + } +} + +``` +![image](https://github.com/user-attachments/assets/456c15b9-9b2b-4cc5-8512-3b0f485d77b2) + From fcfb0a0fde4e45c8370e9a4c1f267a8142d7843d Mon Sep 17 00:00:00 2001 From: alpaca07 <108653023+alpaca07@users.noreply.github.com> Date: Sat, 17 May 2025 00:38:30 +0900 Subject: [PATCH 16/16] Update chapter_15.md --- "\354\244\200\355\231\230/chapter_15.md" | 387 +++++++++++++++++++++++ 1 file changed, 387 insertions(+) diff --git "a/\354\244\200\355\231\230/chapter_15.md" "b/\354\244\200\355\231\230/chapter_15.md" index e8db377..f163e58 100644 --- "a/\354\244\200\355\231\230/chapter_15.md" +++ "b/\354\244\200\355\231\230/chapter_15.md" @@ -139,3 +139,390 @@ public class DataStreamTest { ``` ![image](https://github.com/user-attachments/assets/456c15b9-9b2b-4cc5-8512-3b0f485d77b2) +### 학점 산출 프로그램 만들기 + +- 학생 클래스 구현 +``` +package project.school; + +import java.util.ArrayList; + + +public class Student { + private int studentId; + private String studentName; + private Subject majorSubject; + + private ArrayList scoreList = new ArrayList(); + + public Student (int studentId, String studentName, Subject majSubject) { + this.studentId = studentId; + this.studentName = studentName; + this.majorSubject = majSubject; + } + + public void addSubjectScore(Score score) { + scoreList.add(score); + } + + public int getStudentId() { + return studentId; + } + + public void setStudentId(int studentId) { + this.studentId = studentId; + } + + public String getStudentName() { + return studentName; + } + + public void setStudentName(String studentName) { + this.studentName = studentName; + } + + public Subject getMajorSubject() { + return majorSubject; + } + + public void setMajorSubject(Subject majorSubject) { + this.majorSubject = majorSubject; + } + + public ArrayList getScoreList() { + return scoreList; + } + + public void setScoreList(ArrayList scoreList) { + this.scoreList = scoreList; + } + + +} +``` + +- 과목 클래스 구현 +``` +package project.school; + +import java.util.ArrayList; +import project.school.utils.Define; + + +public class Subject { + private String subjectName; + private int subjectId; + private int gradeType; + + private ArrayList studentList = new ArrayList(); + + public Subject(String subjectName, int subjectId) { + this.subjectName = subjectName; + this.subjectId = subjectId; + this.gradeType = Define.AB_TYPE; + } + + public String getSubjectName() { + return subjectName; + } + + public void getSubjectName(String subjectNaem, String subjectName) { + this.subjectName = subjectName; + } + + public int getSubjectId() { + return subjectId; + } + + public void setSubjectId(int subjectId) { + this.subjectId = subjectId; + } + + public ArrayList getStudentList() { + return studentList; + } + + public void setStudentList(ArrayList studentList) { + this.studentList = studentList; + } + + public int getGradeType() { + return gradeType; + } + + public void setGradeType(int gradeType) { + this.gradeType = gradeType; + } + + public void register(Student student) { + studentList.add(student); + } + +} + +``` + +- 점수 클래스 구현 +``` +package project.school; +public class Score { + int studentId; + Subject subject; + int point; + + public Score(int studentId, Subject subject, int point) { + this.studentId = studentId; + this.subject = subject; + this.point = point; + } + + public int getStudenId() { + return studentId; + } + + public void setStudentId(int studentId) { + this.studentId = studentId; + } + + public Subject getSubject() { + return subject; + } + + public void setSubject(Subject subject) { + this.subject = subject; + } + + public int getPoint() { + return point; + } + + public void setPoint(int point) { + this.point = point; + } + + public String toString() { + return "학번:" + studentId + "," + subject.getSubjectName() + ":" + point; + } +} +``` + +- 상수 값 정의 +![image](https://github.com/user-attachments/assets/f898f1f9-8f49-4d81-8a08-d35936f3ff41) + +- 인터페이스 정의 +![image](https://github.com/user-attachments/assets/22f29475-0f71-47ab-8139-f4c482151d0d) + +- 일반 과목 학점 정책 클래스 +![image](https://github.com/user-attachments/assets/c054f6a4-ce6f-4629-bbdc-b73d3dab283f) + +- 필수 과목 학점 정책 클래스 +![image](https://github.com/user-attachments/assets/12a01cab-3223-4efb-b3d4-9db8b4368c29) + +- 리포트 클래스 구현 +``` +package project.school.report; + +import java.util.ArrayList; + +import project.grade.BasicEvaluation; +import project.grade.GradeEvaluation; +import project.grade.MajorEvaluation; +import project.grade.PassFailEvaluation; +import project.school.School; +import project.school.Score; +import project.school.Student; +import project.school.Subject; +import project.school.utils.Define; + +public class GenerateGradeReport { + + School school = School.getInstance(); + + public static final String TITLE = "수강생 학점 \t\t\n"; + public static final String HEADER = " 이름 | 학번 |필수과목 | 점수 \n"; + public static final String LINE = "-------------------------------------\n"; + + private StringBuffer buffer = new StringBuffer(); + + public String getReport() { + ArrayList subjectList = school.getSubjectList(); + + for (Subject subject : subjectList) { + makeHeader(subject); + makeBody(subject); + makeFooter(); + } + + return buffer.toString(); + } + + public void makeHeader(Subject subject) { + buffer.append(GenerateGradeReport.LINE); + buffer.append("\t" + subject.getSubjectName() + "\n"); + buffer.append(GenerateGradeReport.TITLE); + buffer.append(GenerateGradeReport.HEADER); + buffer.append(GenerateGradeReport.LINE); + } + + public void makeBody(Subject subject) { + ArrayList studentList = subject.getStudentList(); + + for (int i = 0; i < studentList.size(); i++) { + Student student = studentList.get(i); + buffer.append(student.getStudentName()); + buffer.append(" | "); + buffer.append(student.getStudentId()); + buffer.append(" | "); + buffer.append(student.getMajorSubject().getSubjectName()); + buffer.append(" \t"); + + getScoreGrade(student, subject.getSubjectId()); + buffer.append("\n"); + buffer.append(LINE); + } + } + + public void getScoreGrade(Student student, int subjectId) { + ArrayList scoreList = student.getScoreList(); + int majorId = student.getMajorSubject().getSubjectId(); + + GradeEvaluation[] gradeEvaluation = { + new BasicEvaluation(), // 일반 과목 + new MajorEvaluation(), // 필수 과목 + new PassFailEvaluation() + }; + + for (int i = 0; i < scoreList.size(); i++) { + Score score = scoreList.get(i); + + if (score.getSubject().getSubjectId() == subjectId) { + String grade; + + if (score.getSubject().getSubjectId() == majorId) { + grade = gradeEvaluation[Define.SAB_TYPE].getGrade(score.getPoint()); + } else { + grade = gradeEvaluation[Define.AB_TYPE].getGrade(score.getPoint()); + } + + buffer.append(score.getPoint()); + buffer.append(":"); + buffer.append(grade); + buffer.append(" "); + } + } + } + + public void makeFooter() { + buffer.append("\n"); + } +} + +``` +- Pass/Fail 학점 클래스 구현 +![image](https://github.com/user-attachments/assets/8d7b8a69-2792-44b9-a948-4500b44f25ff) + +- 학교 클래스 +![image](https://github.com/user-attachments/assets/71680f59-41f7-46b4-9799-ee262eb0c32e) + +- 테스트 클래스 +``` +package project.test; + +import project.school.School; +import project.school.Score; +import project.school.Student; +import project.school.Subject; +import project.school.report.GenerateGradeReport; +import project.school.utils.Define; + +public class TestMain { + + School goodSchool = School.getInstance(); + Subject korean; + Subject math; + Subject dance; + + GenerateGradeReport gradeReport = new GenerateGradeReport(); + + public static void main(String[] args) { + TestMain test = new TestMain(); + + test.createSubject(); + test.createStudent(); + + String report = test.gradeReport.getReport(); // 성적 결과 생성 + System.out.println(report); + } + + public void createSubject() { + korean = new Subject("국어", Define.KOREAN); + math = new Subject("수학", Define.MATH); + dance = new Subject("방송댄스", Define.DANCE); + + dance.setGradeType(Define.PF_TYPE); + + goodSchool.addSubject(korean); // 테스트 과목 생성 + goodSchool.addSubject(math); + goodSchool.addSubject(dance); + } + + public void createStudent() { + Student student1 = new Student(181213, "안성일", korean); + Student student2 = new Student(181518, "윤태웅", math); + Student student3 = new Student(171230, "이순신", korean); + Student student4 = new Student(171255, "조승연", korean); + Student student5 = new Student(171590, "최태영", math); + + goodSchool.addStudent(student1); // goodSchool에 학생 추가 + goodSchool.addStudent(student2); + goodSchool.addStudent(student3); + goodSchool.addStudent(student4); + goodSchool.addStudent(student5); + + korean.register(student1); // 국어 과목을 수강하는 학생 등록 + korean.register(student2); + korean.register(student3); + korean.register(student4); + korean.register(student5); + + math.register(student1); // 수학 과목을 수강하는 학생 등록 + math.register(student2); + math.register(student3); + math.register(student4); + math.register(student5); + + dance.register(student1); + dance.register(student2); + dance.register(student3); + + // 각 학생의 과목 점수 추가 + addScoreForStudent(student1, korean, 95); + addScoreForStudent(student1, math, 56); + addScoreForStudent(student1, dance, 95); + + addScoreForStudent(student2, korean, 95); + addScoreForStudent(student2, math, 95); + addScoreForStudent(student2, dance, 85); + + addScoreForStudent(student3, korean, 100); + addScoreForStudent(student3, math, 88); + addScoreForStudent(student3, dance, 85); + + addScoreForStudent(student4, korean, 89); + addScoreForStudent(student4, math, 95); + + addScoreForStudent(student5, korean, 85); + addScoreForStudent(student5, math, 56); + } + + // 과목별 점수를 추가하는 메서드 + public void addScoreForStudent(Student student, Subject subject, int point) { + Score score = new Score(student.getStudentId(), subject, point); + student.addSubjectScore(score); + } +} + +``` +![image](https://github.com/user-attachments/assets/afd1535c-0416-4344-ad34-b11eaf64b257) + +