user-img
Language/Java 65
thumbnail
[JAVA]Overriding(๋ฎ์–ด์“ฐ๊ธฐ)
1. ์ •์˜ ๋ถ€๋ชจ ํด๋ž˜์Šค์˜ ๋งค์†Œ๋“œ๋ฅผ ์ž์‹ ํด๋ž˜์Šค๊ฐ€ ์žฌ์ •์˜ ํ•˜๋Š” ๊ฒƒ ๋”ฐ๋ผ์„œ ์ž์‹ํด๋ž˜์Šค์—์„œ ํ•ด๋‹น ๋งค์†Œ๋“œ๋ฅผ ํ˜ธ์ถœ ํ•  ๊ฒฝ์šฐ, ๋ถ€๋ชจ ํด๋ž˜์Šค์˜ ๋ณธ ๋งค์†Œ๋“œ๊ฐ€ ์•„๋‹Œ ์ƒˆ๋กœ ์ •์˜๋œ ์ž์‹ ํด๋ž˜์Šค์˜ ๋ฉ”์†Œ๋“œ๊ฐ€ ํ˜ธ์ถœ ๋จ. **Method Overloading ๊ณผ ๋‹ค๋ฅธ์  ์˜ค๋ฒ„๋กœ๋”ฉ์€ ํŒŒ๋ผ๋ฏธํ„ฐ์˜ ์ž๋ฃŒํ˜•, ํŒŒ๋ผ๋ฏธํ„ฐ์˜ ๊ฐฏ์ˆ˜๊ฐ€ ๋‹ค๋ฅผ ๊ฒฝ์šฐ์—๋งŒ ์žฌ์ •์˜ ํ•  ์ˆ˜ ์žˆ๋‹ค. ์˜ค๋ฒ„๋กœ๋”ฉ์€ ๊ณ„์ธต์ด ๋‚˜๋ˆ ์ ธ ์žˆ์ง€ ์•Š๋‹ค. ์˜ค๋ฒ„๋ผ์ด๋”ฉ์€ ๋ถ€๋ชจ-์ž์‹์ด๋ผ๋Š” ๊ณ„์ธต์ด ๋‚˜๋ˆ ์ ธ ์žˆ์„ ๋•Œ๋งŒ ๊ฐ€๋Šฅํ•˜๋‹ค. ์˜ค๋ฒ„๋ผ์ด๋”ฉ์€ ๋ถ€๋ชจ ํด๋ž˜์Šค์˜ ๋งค์†Œ๋“œ๋ฅผ ์žฌ์ •์˜ ํ•˜๋Š” ๊ฒƒ์ด๋ฏ€๋กœ, ์žฌ์ •์˜ํ•œ ๋งค์†Œ๋“œ์˜ ์ด๋ฆ„, ๋งค๊ฐœ๋ณ€์ˆ˜, ๋ฆฌํ„ด ๊ฐ’์ด ๋ถ€๋ชจ ํด๋ž˜์Šค์˜ ๋งค์†Œ๋“œ์™€ ์ผ์น˜ ํ•ด์•ผํ•œ๋‹ค! 2. ์ฝ”๋“œ ์˜ˆ์‹œ //๋ถ€๋ชจ ํด๋ž˜์Šค public class Camera { public String name; // ์˜ค๋ฒ„๋ผ์ด๋”ฉํ•  ๋งค์†Œ๋“œ pub..
2023.01.27
Language/Java
thumbnail
Chap07_09 ์ƒ์†(inheritance)
1. ์ •์˜ ์ž์‹ ํด๋ž˜์Šค๊ฐ€ ๋ถ€๋ชจ ํด๋ž˜์Šค์˜ ๋ชจ๋“  ๊ฒƒ๋“ค์„ ๊ฐ€์ ธ์˜ค๋Š” ๊ฒƒ. *์“ฐ๋Š” ์ด์œ  ํด๋ž˜์Šค๋งˆ๋‹ค ์ค‘๋ณต๋˜๋Š” ๋‚ด์šฉ์ด ์žˆ๋Š”๋ฐ ๊ฐ™์€ ๋‚ด์šฉ์„ ํ•ญ์ƒ ๋˜‘๊ฐ™์ด ์“ด๋‹ค๋ฉด ์ค‘๋ณต์„ฑ์ด ์‹ฌํ•ด์ ธ์„œ ํšจ์œจ์„ฑ์ด ์ €ํ•˜๋œ๋‹ค. ๋”ฐ๋ผ์„œ ๋ถ€๋ชจ ํด๋ž˜์Šค์— ์ด๋ฏธ ์žˆ๋Š” ๋‚ด์šฉ์€ ์ƒ์†์„ ์„ ์–ธํ•œ ์ž์‹ ํด๋ž˜์Šค์—์„œ ์„ ์–ธ ์•ˆ ํ•˜๋”๋ผ๋„ ์“ธ ์ˆ˜ ์žˆ๊ฒŒ ์กฐ์น˜ํ•œ ๊ฒƒ ์ด๋‹ค. 2. ์‚ฌ์šฉ๋ฒ• //๋ถ€๋ชจ class public class Camera { public String name; // ๋ถ€๋ชจ ํด๋ž˜์Šค public Camera() { this.name = "์นด๋ฉ”๋ผ"; } public void takePicture() { // ์‚ฌ์ง„ ์ดฌ์˜ System.out.println(this.name + ": ์‚ฌ์ง„์„ ์ดฌ์˜ํ•ฉ๋‹ˆ๋‹ค."); } public void recordVideo() { // ๋™์˜์ƒ..
2023.01.27
Language/Java
thumbnail
Chap07_08 Package ์˜๋ฏธ ์•Œ๊ธฐ (์˜ˆ์‹œ๋กœ Random ์ด์šฉ)
1.Package๋ž€? ๊ด€๋ จ ์žˆ๋Š” ํด๋ž˜์Šค๋ฅผ ํ•œ๋ฐ ๋ชจ์•„๋†“๋Š” ๊ฒƒ. ํด๋”์™€ ๋น„์Šทํ•œ ๊ฐœ๋… ๋งŒ์•ฝ Chap_07์— ๋ฌถ์—ฌ์žˆ๋Š” ํด๋ž˜์Šค๋“ค ์ด๋ผ๋ฉด ์ตœ์ƒ๋‹จ์— ํ•ด๋‹น ๋ช…๋ น์–ด ํ•„์š” (intellj์—์„œ๋Š” ์ž๋™์œผ๋กœ ์ฑ„์›Œ์ง.) package Chap_07; 2.Random ํด๋ž˜์Šค ์ด์šฉ (1) Random ํด๋ž˜์Šค๊ฐ€ ์žˆ๋Š” ํŒจํ‚ค์ง€ ๋ถˆ๋Ÿฌ์˜ค๋Š” ๋ฒ• random ํด๋ž˜์Šค๋Š” java ํŒจํ‚ค์ง€ ์•ˆ์˜ util ์ด๋ผ๋Š” ํŒจํ‚ค์ง€ ์†์— ์žˆ๋‹ค. ํ•ด๋‹น ํด๋ž˜์Šค๋ฅผ ์‚ฌ์šฉํ•˜๋ ค๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์ด main ๋งค์†Œ๋“œ์—์„œ ์‚ฌ์šฉ ์ „์— import๋ฅผ ํ•ด์•ผํ•œ๋‹ค. import java.util.Random; (2) Random ํด๋ž˜์Šค๋ฅผ ์ด์šฉํ•œ ๊ณ„์‚ฐ // (๋žœ๋ค ํด๋ž˜์Šค๋ฅผ ์‚ฌ์šฉ) // ๋žœ๋ค ํด๋ž˜์Šค ๋™์  ํ• ๋‹น Random random = new Random(); System.out.println("..
2023.01.27
Language/Java
thumbnail
Chap07_07 ์ ‘๊ทผ ์ œ์–ด์ž
1. ์ •์˜ ์šฐ๋ฆฌ๊ฐ€ ๋„ค์ด๋ฒ„ ์นดํŽ˜๋ฅผ ์šด์˜ํ•œ๋‹ค ์น˜๋ฉด, ๋ฐฉ๋ฌธ์ž๊ฐ€ ๋ณผ ์ˆ˜ ์žˆ๋Š” ๊ฒŒ์‹œ๋ฌผ, ์ƒˆ์‹น ํšŒ์›์ด ๋ณผ ์ˆ˜ ์žˆ๋Š” ๊ฒŒ์‹œ๋ฌผ, ๋†’์€ ๋“ฑ๊ธ‰์˜ ํšŒ์›์ด ๋ณผ ์ˆ˜ ์žˆ๋Š” ๊ฒŒ์‹œ๋ฌผ์„ ๋‹ค ๋‹ค๋ฅด๊ฒŒ ๊ตฌ๋ถ„ํ•  ์ˆ˜ ์žˆ๋‹ค. ์ด๊ฒƒ๊ณผ ๊ฐ™์€ ์›๋ฆฌ๋กœ ์ ‘๊ทผ์ œ์–ด์ž๋Š”, ํด๋ž˜์Šค๋‚˜ ํด๋ž˜์Šค์˜ ๊ตฌ์„ฑ์š”์†Œ(๋ณ€์ˆ˜, ๋งค์†Œ๋“œ)์— ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๋Š” ๊ถŒํ•œ์„ ๋ˆ„๊ตฌ๊นŒ์ง€ ์ค„ ๊ฒƒ์ธ์ง€ ์ •ํ•˜๋Š” Keyword ์ด๋‹ค. 2. ์ข…๋ฅ˜ (1) private ํ•ด๋‹น ํด๋ž˜์Šค ๋‚ด์—์„œ๋งŒ ์ ‘๊ทผ ๊ฐ€๋Šฅํ•˜๊ฒŒ ํ•˜๋Š” KEYWORD private int price; (2) public ๋ชจ๋“  ํŒจํ‚ค์ง€, ๋ชจ๋“  ํด๋ž˜์Šค์—์„œ ์ ‘๊ทผ ๊ฐ€๋Šฅํ•˜๋„๋ก ํ—ˆ์šฉํ•˜๋Š” KEYWORD (**public class ์ •์˜ ์‹œ ํด๋ž˜์Šค๋ช…๊ณผ ์ž๋ฐ” ํŒŒ์ผ๋ช…์€ ๊ฐ™์•„์•ผ ํ•œ๋‹ค.) public String modelName; (3) default (ํด๋ž˜์Šค๋‚˜ ๋ณ€์ˆ˜,๋งค์†Œ๋“œ ..
2023.01.27
Language/Java
thumbnail
Chap07_06 Setter & Getter
1.์—ญํ•  Getter : ๊ฐ์ฒด์˜ ์ธ์Šคํ„ด์Šค ๋ณ€์ˆ˜๋ฅผ ์กฐํšŒํ•˜๋Š” ๋งค์†Œ๋“œ //์ธ์Šคํ„ด์Šค ๋ณ€์ˆ˜ price์˜ ๊ฐ’์„ ๋ณด์—ฌ์ฃผ๋Š” ๋งค์†Œ๋“œ int getPrice() { return price; } Setter : ๊ฐ์ฒด์˜ ์ธ์Šคํ„ด์Šค ๋ณ€์ˆ˜๋ฅผ ์ดˆ๊ธฐํ™” ํ•˜๋Š” ๋งค์†Œ๋“œ // ์ธ์ˆ˜๋กœ ๋“ค์–ด์˜จ ๊ฐ’์„ resolution์˜ ๊ฐ’์œผ๋กœ ๋„ฃ๋Š” ๋งค์†Œ๋“œ void setResolution (String resolution){ this.resolution = resolution; } 2. ์ƒ์„ฑ์ž๋ฅผ ์‚ฌ์šฉํ•˜๊ฑฐ๋‚˜ ์ผ์ผํžˆ ์ดˆ๊ธฐํ™” ํ•  ์ˆ˜ ์žˆ๋Š”๋ฐ ์™œ ์“ฐ์ผ๊นŒ? ๊ฐ์ฒด์˜ ๋ฌด๊ฒฐ์„ฑ์„ ๋ณด์žฅํ•˜๊ธฐ ์œ„ํ•ด ์“ฐ์ธ๋‹ค. **๋ฌด๊ฒฐ์„ฑ์ด๋ž€ ๋ฐ์ดํ„ฐ์˜ ์ •ํ™•์„ฑ๊ณผ ์ผ๊ด€์„ฑ์„ ์œ ์ง€ํ•˜๊ณ  ๋ณด์ฆํ•˜๋Š” ๊ฒƒ์„ ๋งํ•œ๋‹ค. ๋งŒ์•ฝ ๋ณ€์ˆ˜๋ฅผ ์ผ์ผํžˆ ์ดˆ๊ธฐํ™” ํ•˜๊ฑฐ๋‚˜, ์ƒ์„ฑ์ž๋ฅผ ์ด์šฉํ•ด ์ดˆ๊ธฐํ™” ํ•˜๋Š” ๊ฒฝ์šฐ, ๋ณ€์ˆ˜์— ์กด์žฌํ•  ์ˆ˜ ์—†๋Š” ๊ฐ’์ด ๋“ค์–ด..
2023.01.26
Language/Java
thumbnail
Chap07_05 Constructor ์™€ this
1. Constructor(์ƒ์„ฑ์ž) ์ •์˜ ๊ฐ์ฒด๋ฅผ ์„ ์–ธํ•  ๋•Œ๋งˆ๋‹ค ์ธ์Šคํ„ด์Šค ๋ณ€์ˆ˜๋ฅผ ์ผ์ผํžˆ ์ดˆ๊ธฐํ™” ํ•ด์•ผํ•œ๋‹ค๋ฉด, ๋„ˆ๋ฌด ๋ฒˆ๊ฑฐ๋กญ๋‹ค. ์ƒ์„ฑ์ž๋Š” ๊ฐ์ฒด ์„ ์–ธ๊ณผ ๋™์‹œ์— ์ธ์Šคํ„ด์Šค ๋ณ€์ˆ˜๋ฅผ ์ž๋™ ์ดˆ๊ธฐํ™” ์‹œ์ผœ์ฃผ๋Š” ์—ญํ• ์„ ํ•œ๋‹ค. //๊ธฐ์กด์˜ ๋ฐฉ์‹ : ๊ฐ์ฒด ์„ ์–ธ, ์ธ์Šคํ„ด์Šค ๋ณ€์ˆ˜ ์ผ์ผํžˆ ์ดˆ๊ธฐํ™” BlackBox b1 = new BlackBox(); b1.modelName = "๊นŒ๋ง์ด"; b1.resolution = "FHD"; b1.price = 20000; b1.color = "๋ธ”๋ž™"; //์ƒ์„ฑ์ž ์‚ฌ์šฉ : ๊ฐ์ฒด ์„ ์–ธ๊ณผ ๋™์‹œ์— ์ดˆ๊ธฐํ™” //๊ฐ์ฒดb1์„ BlackBox ์ž๋ฃŒํ˜•์œผ๋กœ ์„ ์–ธ, ๋™์ ํ• ๋‹น, ()์•ˆ์€ ์ƒ์„ฑ์ž์— ๋“ค์–ด๊ฐˆ ์ธ์ˆ˜ BlackBox b1 = new BlackBox("๊นŒ๋ง์ด", "FHD", 200000, "Black"); ์ด๋ ‡๊ฒŒ ํŽธ๋ฆฌ..
2023.01.26
Language/Java
thumbnail
Chap07_04 Method ์™€ Method overloading
1. ์ •์˜ ํด๋ž˜์Šค์˜ ๊ธฐ๋Šฅ์„ ํ‘œํ˜„ํ•œ ๊ฒƒ. ํ•ด๋‹น ํด๋ž˜์Šค๋ฅผ ์‚ฌ์šฉํ•œ ๊ฐ์ฒด๋Š” ๋ชจ๋‘ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. ํ•ด๋‹น ๋งค์†Œ๋“œ๋ฅผ ์žฌ์ •์˜ ํ•ด์„œ ์‚ฌ์šฉ๋„ ๊ฐ€๋Šฅํ•˜๋‹ค. 2. ์„ ์–ธ ์ผ๋ฐ˜ ๋งค์†Œ๋“œ๋ž‘ ๋˜‘๊ฐ™์Œ. ์„ ์–ธ๋˜๋Š” ์œ„์น˜๊ฐ€ ํด๋ž˜์Šค ์•ˆ์ชฝ ์ผ ๋ฟ์ด๋‹ค. public class BlackBox{ // ์ž…๋ ฅ ๋ฐ˜ํ™˜ ๋ชจ๋‘ ์—†๋Š” ๋งค์†Œ๋“œ > ํ˜ธ์ถœ๋งŒ ํ•˜๋ฉด ๋จ. void autoReport() { if (canAutoReport) { System.out.println("์ถฉ๋Œ์ด ๊ฐ์ง€๋˜์–ด ์ž๋™์œผ๋กœ ์‹ ๊ณ ํ•ฉ๋‹ˆ๋‹ค."); } else { System.out.println("์ž๋™ ์‹ ๊ณ  ๊ธฐ๋Šฅ์ด ์ง€์›๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค."); } } //์ž…๋ ฅ์€ ํ•„์š”ํ•˜๋‚˜ ๋ฐ˜ํ™˜์€ ์—†๋Š” ๋งค์†Œ๋“œ void insertMemoryCard(int capacity) { System.out.println("๋ฉ”๋ชจ๋ฆฌ ..
2023.01.26
Language/Java
thumbnail
Chap07_03 Class Variable(ํด๋ž˜์Šค ๋ณ€์ˆ˜)
1.์ •์˜ ํด๋ž˜์Šค ๋‚ด์— ๊ธฐ์žฌ๋˜์–ด ๋ชจ๋“  ๊ฐ์ฒด์— ๋˜‘๊ฐ™์ด ์ ์šฉ๋˜๋Š” ๋ณ€์ˆ˜ ๊ฐ์ฒด์—์„œ ์ž„์˜๋กœ ๋ณ€๊ฒฝ ๋ถˆ๊ฐ€๋Šฅ public class BlackBox { // ์ธ์Šคํ„ด์Šค ๋ณ€์ˆ˜ -> ๊ฐ ๊ฐ์ฒด๋งˆ๋‹ค ๋‹ค๋ฅด๊ฒŒ ์ ์šฉ String modelName; // ๋ชจ๋ธ๋ช… String resolution; // ํ•ด์ƒ๋„ int price; // ๊ฐ€๊ฒฉ String color; // ์ƒ‰์ƒ int serialNumber; // ์‹œ๋ฆฌ์–ผ ๋ฒˆํ˜ธ //ํด๋ž˜์Šค ๋ณ€์ˆ˜ -> ๋ชจ๋“  ๊ฐ์ฒด์— ๋˜‘๊ฐ™์ด ์ ์šฉ //์ž๋™ ์‹ ๊ณ  ๊ธฐ๋Šฅ static boolean canAutoReport = false; } 2.์ดˆ๊ธฐํ™” ํด๋ž˜์Šค ๋‚ด์—์„œ ์„ ์–ธ๊ณผ ๋™์‹œ์— ์ดˆ๊ธฐํ™”. (๊ฐ์ฒด์—์„œ ๋ฐ”๊พธ๋ ค ํ•˜๋ฉด ์—๋Ÿฌ) 3. ์ ‘๊ทผ ํด๋ž˜์Šค์—์„œ ๋ฐ”๋กœ ์ ‘๊ทผ ๊ฐ€๋Šฅ. (ํด๋ž˜์Šค์ด๋ฆ„.ํด๋ž˜์Šค ๋ณ€์ˆ˜) ๊ฐ์ฒด๋งˆ๋‹ค ์ž„์˜๋กœ ๊ฐ’์„ ๋ณ€๊ฒฝํ•˜๋Š” ๊ฒƒ์€ ..
2023.01.26
Language/Java
thumbnail
Chap07_02 Instance Variable(์ธ์Šคํ„ด์Šค ๋ณ€์ˆ˜)
1. ์ธ์Šคํ„ด์Šค ๋ณ€์ˆ˜๋ž€? ์ธ์Šคํ„ด์Šค ๋ณ€์ˆ˜๋ž€ ํด๋ž˜์Šค์— ํฌํ•จ๋˜์–ด ์žˆ๋Š” ๋ณ€์ˆ˜๋กœ์„œ ํ•ด๋‹น ํด๋ž˜์Šค์˜ ํŠน์„ฑ์„ ํ‘œํ˜„ํ•œ๋‹ค. ์ธ์Šคํ„ด์Šค ๋ณ€์ˆ˜๋Š” ํ•ด๋‹น ํด๋ž˜์Šค๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฐ์ฒด๋งˆ๋‹ค ๊ฐ’์„ ๋‹ค๋ฅด๊ฒŒ ์กฐ์ •ํ•  ์ˆ˜ ์žˆ๋‹ค. 2. ์ธ์Šคํ„ด์Šค ๋ณ€์ˆ˜ ์„ ์–ธ ๋ฐ ์ดˆ๊ธฐํ™” ํ•˜๋Š” ๋ฒ• (1) ์„ ์–ธ ํด๋ž˜์Šค ๋‚ด์— ์„ ์–ธ public class BlackBox { // ์ธ์Šคํ„ด์Šค ๋ณ€์ˆ˜ -> ๊ฐ ๊ฐ์ฒด๋งˆ๋‹ค ๋‹ค๋ฅด๊ฒŒ ์ ์šฉ String modelName; // ๋ชจ๋ธ๋ช… String resolution; // ํ•ด์ƒ๋„ int price; // ๊ฐ€๊ฒฉ String color; // ์ƒ‰์ƒ int serialNumber; // ์‹œ๋ฆฌ์–ผ ๋ฒˆํ˜ธ } (2) ์ดˆ๊ธฐํ™” ๊ฐ์ฒด์ด๋ฆ„.์ดˆ๊ธฐํ™”ํ•  ์ธ์Šคํ„ด์Šค ๋ณ€์ˆ˜ = ์ดˆ๊ธฐํ™”ํ•  ๊ฐ’; //๊ฐ์ฒด ์„ ์–ธ BlackBox b1 = new BlackBox(); //์ธ์Šคํ„ด์Šค ๋ณ€์ˆ˜ ..
2023.01.26
Language/Java
thumbnail
Chap07_01 Class
1. ํด๋ž˜์Šค๋ฅผ ์“ฐ๋Š” ์ด์œ  ํ•˜๋‚˜์˜ ์‚ฌ๋ฌผ์— ๋Œ€ํ•œ ํŠน์ง•๋“ค์„ ํ•œ๋ฐ ๋ชจ์œผ์ง€ ์•Š๊ณ  ์ค‘๊ตฌ๋‚œ๋ฐฉ ์„ค๋ช…ํ•œ๋‹ค๋ฉด, ์žฅ๋‹˜์—๊ฒŒ ์ฝ”๋ผ๋ฆฌ๋ฅผ ์„ค๋ช…ํ•˜๋Š” ๊ฒƒ๊ณผ ๊ฐ™๋‹ค. ํŽ„๋ ์ด๋Š” ๊ท€, ๊ธด ์ฝ” , ๋‹์•„๋‚œ ์ƒ์•„ ๋“ฑ ๋Œ€ํ‘œ์ ์ธ ํŠน์ง•์„ ๋งํ•˜๋”๋ผ๋„ ์žฅ๋‹˜์€ ์ „์ฒด์ ์ธ ๊ทธ๋ฆผ์„ ๊ทธ๋ฆฌ์ง€ ๋ชป ํ•  ๊ฒƒ์ด๋‹ค. ํด๋ž˜์Šค๋Š” ํ•˜๋‚˜์˜ ์‚ฌ๋ฌผ์— ๋Œ€ํ•œ ํŠน์ง•์„ ํ•œ๋ฐ ๋ชจ์œผ๋Š” ์ž‘์—…์ด๋‹ค. ์šฐ๋ฆฌ๋Š” ์ฝ”๋ผ๋ฆฌ์— ๋Œ€ํ•ด ์„ค๋ช…ํ•  ๋•Œ ์ฝ”๋ผ๋ฆฌ ์„ค๋ช…์— ๋Œ€ํ•œ ํด๋ž˜์Šค๋ฅผ ๊บผ๋‚ด ์“ฐ๊ธฐ๋งŒ ํ•˜๋ฉด ๋œ๋‹ค. public class elephant { String elephantName; String nose; String ivory; //... } //ํด๋ž˜์Šค ์ƒ์„ฑ elephant doto = new elephant(); elephant jambo = new elephant(); ํ•ด๋‹น elephant๋ž€ ํด๋ž˜์Šค๋ฅผ ์‚ฌ์šฉํ•ด ๋„ํ† ๋‚˜..
2023.01.26
Language/Java
thumbnail
Chap07_00 ๊ฐ์ฒด์™€ Class์— ๊ด€ํ•˜์—ฌ (๊ด€๋ จ ๊ฐœ๋…๋“ค ์—ฌ๊ธฐ ๊ณ„์† ์ถ”๊ฐ€)
1.Class (1) ์ •์˜ ์ž๋™์ฐจ ๊ณต์žฅ์„ ์˜ˆ์‹œ๋กœ ๋“ค๋ฉด, Class๋Š” ํ•˜๋‚˜์˜ ์ž๋™์ฐจ ์„ค๊ณ„๋„ ์ด๋‹ค. ์šฐ๋ฆฌ๋Š” ์„ค๊ณ„๋„๋ฅผ ํ†ตํ•ด ์ง„์งœ ์ž๋™์ฐจ๋ฅผ ๋งŒ๋“ ๋‹ค. ์ฐจ ์„ค๊ณ„๋„์—๋Š” ํ•„์š”ํ•œ ๋ถ€ํ’ˆ๊ณผ ์ด ์ฐจ์˜ ์ž‘๋™์›๋ฆฌ๋“ค์ด ์ ํ˜€์žˆ๋‹ค. ์—ฌ๊ธฐ์„œ ์ฐจ ์„ค๊ณ„์— ํ•„์š”ํ•œ ๋ถ€ํ’ˆ๋“ค์„ Class ๋‚ด์˜ ๋ณ€์ˆ˜, ์ฐจ์˜ ์ž‘๋™ ์›๋ฆฌ๋ฅผ Class ๋‚ด์˜ Method๋ผ๊ณ  ํ•œ๋‹ค. public class vehicle_Blueprint { //์ธ์Šคํ„ด์Šค ๋ณ€์ˆ˜ int ๋ณผํŠธ์˜_์ˆ˜; int ๋„ˆํŠธ์˜_์ˆ˜; String ๋ฐฐ๊ธฐํ†ต; string ์ฐจ์ฒด; String ์ฐจ์ƒ‰๊น”; String ์•ž๋ฒ”ํผ์˜_ํฌ๊ธฐ; String ์ฐจ ์‹œ๋ฆฌ์–ผ ๋„˜๋ฒ„; //ํด๋ž˜์Šค ๋ณ€์ˆ˜ Static String ์ฐจ์ฒด์˜_์ˆ˜์ž…์› = "๊ตญ์‚ฐ"; //์ธ์Šคํ„ด์Šค ๋งค์†Œ๋“œ void Black_Box_alert (int speed, int ์ฃผ..
2023.01.25
Language/Java
thumbnail
Java์—์„œ switch- case๋ฌธ ์“ฐ๋Š” ๋ฒ•
switch (๋งค๊ฐœ๋ณ€์ˆ˜) { case A: A์ผ๋•Œ ์‹คํ–‰ํ•  ๋ช…๋ น break; // ํ•ด๋‹น ๋ช…๋ น ์‹คํ–‰ ํ›„ swtich๋ฌธ ํƒˆ์ถœ. (break ์•ˆ ์ ์„ ์‹œ ์ด ๋ฐ‘์— case๋ฌธ ์ „๋ถ€ ์‹คํ–‰๋จ.) case B: B์ผ๋•Œ ์‹คํ–‰ํ•  ๋ช…๋ น break; default: ๋ชจ๋“  ์กฐ๊ฑด์— ํ•ด๋‹น ์‚ฌํ•ญ ์—†์„ ๋•Œ ์‹คํ–‰ํ•˜๋Š” ๋ช…๋ น } * Case A์—์„œ break๋ฌธ์ด ์—†๋‹ค๋ฉด, A์˜ ๋ฌธ์žฅ ์‹คํ–‰ ํ›„ B์ผ ๋•Œ์˜ ๋ช…๋ น๋„ ์‹คํ–‰ํ•œ๋‹ค. ์™œ๋ƒํ•˜๋ฉด, A๋ž€ ์กฐ๊ฑด์„ ๋งŒ์กฑํ–ˆ๊ธฐ์— break๋ฅผ ๋งŒ๋‚˜๊ธฐ ์ „๊นŒ์ง€ ๋ชจ๋“  ๋ฌธ์žฅ์„ ์‹คํ–‰ํ•  ์ˆ˜ ์žˆ๋Š” ํ—ˆ๋ฝ์„ ๋“ํ–ˆ๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.
2023.01.16
Language/Java
thumbnail
02 Method - (7) Main Method
*main method์— ์ธ์ˆ˜ ์ „๋‹ฌํ•˜๋Š” ๋ฒ• (1)current file > edit configuration (2)์˜†์— + ํ‚ค ํด๋ฆญ > Application ํด๋ฆญ > ์ด๋ฆ„ test๋กœ ์ง“๊ณ  Main Class๋ผ ์ ํ˜€ ์žˆ๋Š” ๊ณณ์—์„œ ์ง€๊ธˆ ๋‚ด๊ฐ€ ์ธ์ˆ˜ ๋„ฃ์œผ๋ ค๋Š” java class file์„ ์ฐพ๋Š”๋‹ค. (3) program arguments์— ์›ํ•˜๋Š” ๊ฐ’ ์ง‘์–ด๋„ฃ๊ธฐ > ๋ (๊ณต๋ฐฑ ๋‹จ์œ„๋กœ ๋Š์–ด์„œ ๋ฐฐ์—ด์— ์ €์žฅ๋œ๋‹ค.) ** ์˜ˆ์ œ ์ฝ”๋“œ > arguments์— 1~3 ๊ฐ’์„ ์ง‘์–ด๋„ฃ์—ˆ์„๋•Œ, args ๋ฐฐ์—ด์˜ 0๋ฒˆ์งธ ์›์†Œ๋กœ ๋“ค์–ด๊ฐ„๋‹ค. ์ด๋ฅผ ์ด์šฉํ•ด ๋„์„œ ์กฐํšŒํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์ด๋‹ค. //main ๋งค์†Œ๋“œ == String ๋ฐฐ์—ด args๋ฅผ ํŒŒ๋ผ๋ฏธํ„ฐ๋กœ ๊ฐ–๋Š” ๋งค์†Œ๋“œ public static void main(String[] args ) { for (S..
2023.01.16
Language/Java
02 Method -(6) ๋ณ€์ˆ˜์˜ ๋ฒ”์œ„
๋ณ€์ˆ˜์˜ ๋ฒ”์œ„๋Š” ํ•ญ์ƒ ๊ทธ๊ฒƒ์ด ์„ ์–ธ๋œ ์ค‘๊ด„ํ˜ธ{} ๋‚ด ์ด๋‹ค! ๋ฒ”์œ„ ๋ฒ—์–ด๋‚˜๋ฉด ์‚ฌ์šฉ ๋ถˆ๊ฐ€! // ์ง€์—ญ๋ณ€์ˆ˜ == ๋ณ€์ˆ˜๊ฐ€ ์„ ์–ธ๋œ ์ค‘๊ด„ํ˜ธ(์˜์—ญ)์•ˆ์—์„œ๋งŒ ์“ธ ์ˆ˜ ์žˆ์Œ. // ์ง€์—ญ๋ณ€์ˆ˜๋ฅผ ๋‹ค๋ฅธ ๋งค์†Œ๋“œ์—์„œ ์“ฐ๋ ค๋ฉด, ํ•ด๋‹น ๋งค์†Œ๋“œ๋ฅผ ํ˜ธ์ถœํ•˜์—ฌ ๊ทธ ๋งค์†Œ๋“œ์˜ ํŒŒ๋ผ๋ฏธํ„ฐ์— ์ง€์—ญ๋ณ€์ˆ˜๋ฅผ ์ธ์ˆ˜๋กœ ๋„ฃ์–ด์„œ ์จ์•ผํ•จ. // ๋ฒ”์œ„ ๋ฐ–์ด๋ผ ์ถœ๋ ฅ ์•ˆ๋˜๋Š” ๊ฒƒ๋“ค ๋‹ค ์ฃผ์„ ์ฒ˜๋ฆฌ public static void methodA() { //System.out.println(number); //System.out.println(result); } public static void mehthodB() { int result = 1; } public static void main(String[] args) { int number = 3; //System.out.prin..
2023.01.16
Language/Java
02 Method - (5) Overrloading (๋งค์†Œ๋“œ ์žฌ์ •์˜)
1. ์˜ค๋ฒ„๋กœ๋”ฉ (์žฌ์ •์˜)์ด๋ž€? ๊ฐ™์€ ์ด๋ฆ„์˜ ๋งค์†Œ๋“œ๋ฅผ ์—ฌ๋Ÿฌ ๋ฒˆ ์„ ์–ธํ•˜๋Š” ๊ฒƒ์„ ๋งํ•œ๋‹ค. ์˜ค๋ฒ„๋กœ๋”ฉ์ด ๊ฐ€๋Šฅํ•œ ์กฐ๊ฑด์€ ๊ฐ™์€ ์ด๋ฆ„์˜ ๋งค์†Œ๋“œ๊ฐ€ ์ „๋‹ฌ ๋ฐ›๋Š” ์ธ์ˆ˜์˜ ์ž๋ฃŒํ˜•์ด ๋‹ค๋ฅด๊ฑฐ๋‚˜(1), ์ „๋‹ฌ ๋ฐ›๋Š” ์ธ์ˆ˜์˜ ๊ฐฏ์ˆ˜๊ฐ€ ๋‹ค๋ฅผ ๋•Œ(2) ์ด๋‹ค. *๋ฐ˜ํ™˜ ์ž๋ฃŒํ˜•์ด ๋‹ค๋ฅด๋‹ค๊ณ  ํ•ด์„œ ์˜ค๋ฒ„๋กœ๋”ฉ์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์—†๋‹ค! (> ๋ฐ˜ํ™˜ ์ž๋ฃŒํ˜•์ด ๋‹ค๋ฅธ ๊ฐ™์€ ์ด๋ฆ„์˜ ๋งค์†Œ๋“œ๋ฅผ ์žฌ์ •์˜ ์‹œ์—๋Š” ์ค‘๋ณต์œผ๋กœ ์˜ค๋ฅ˜! ) 2. ์˜ค๋ฒ„๋กœ๋”ฉ์€ ์–ด๋–ค ๋•Œ ์œ ์šฉํ•œ๊ฐ€? ์˜ค๋ฒ„๋กœ๋”ฉ์ด ์—†๋Š” ๊ฒฝ์šฐ, ๊ฐ™์€ ๊ธฐ๋Šฅ์„ ํ•˜๋Š” ๋งค์†Œ๋“œ๋ผ๋„, ์ธ์ˆ˜์˜ ๊ฐœ์ˆ˜์˜ ์ฐจ์ด, ์ธ์ˆ˜ ์ž๋ฃŒํ˜•์˜ ์ฐจ์ด์— ๋”ฐ๋ผ ๋ชจ๋‘ ๋‹ค๋ฅธ ์ด๋ฆ„์œผ๋กœ ์ •์˜ ํ•ด์•ผํ•œ๋‹ค. ์ด ๊ฒฝ์šฐ ํ˜„์žฌ์˜ ์ƒํ™ฉ๊ณผ ์ ํ•ฉํ•œ ๋งค์†Œ๋“œ๋ฅผ ํ˜ธ์ถœํ•˜๋Š”๋ฐ ํ˜ผ๋ž€์ด ์ƒ๊ธธ ๊ฐ€๋Šฅ์„ฑ์ด ํฌ๋‹ค. ๋”ฐ๋ผ์„œ ์ด ๊ฐ™์€ ๊ฒฝ์šฐ ์˜ค๋ฒ„๋กœ๋”ฉ์„ ํ†ตํ•ด ๊ฐ™์€ ๊ธฐ๋Šฅ์˜ ๋งค์†Œ๋“œ๋“ค์„ ํ•˜๋‚˜์˜ ์ด๋ฆ„์œผ๋กœ ํ†ต์ผํ•˜๋Š” ๊ฒƒ์ด ์œ ์šฉํ•˜๋‹ค...
2023.01.16
Language/Java
02 Method - (3) Return(๋ฐ˜ํ™˜ ๊ฐ’)
1. ๋ฐ˜ํ™˜ ๊ฐ’์ด๋ž€? ๋งค์†Œ๋“œ์˜ ๊ฒฐ๊ณผ๊ฐ’. ๋ฐ˜ํ™˜ ์ž๋ฃŒํ˜•๊ณผ ๊ฐ™์€ ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•ด์•ผํ•จ. //๋ฐ˜ํ™˜ ์ž๋ฃŒํ˜•์ด String ์ž„์„ ์•Œ ์ˆ˜ ์žˆ์Œ. //ํ˜ธํ…” ์ „ํ™”๋ฒˆํ˜ธ public static String getPhoneNumber () { String phoneNumber = "02 - 1234- 5678"; return phoneNumber; } //ํ˜ธํ…” ์ฃผ์†Œ public static String getAddress() { //์ด๋ ‡๊ฒŒ ์ŠคํŠธ๋ง ๊ฐ’์„ ๋ฐ”๋กœ ๋ฐ˜ํ™˜ ์‹œํ‚ฌ ์ˆ˜๋„ ์žˆ๋‹ค. return "์„œ์šธ์‹œ ์–ด๋”˜๊ฐ€"; } //ํ˜ธํ…” ์•กํ‹ฐ๋น„ํ‹ฐ public static String getActivities () { return "๋ณผ๋ง์žฅ, ํƒ๊ตฌ์žฅ, ๋…ธ๋ž˜๋ฐฉ"; } public static void main(String[] args) { //๋ฐ˜ํ™˜๊ฐ’..
2023.01.16
Language/Java
02 Method -(2) ํŒŒ๋ผ๋ฏธํ„ฐ
1. ํŒŒ๋ผ๋ฏธํ„ฐ๋ž€? ํŒŒ๋ผ๋ฏธํ„ฐ == ์ „๋‹ฌ ๊ฐ’ == ๋งค๊ฐœ๋ณ€์ˆ˜ == ๋งค์†Œ๋“œ์—์„œ ์ธ์ˆ˜๋ฅผ ๋ฐ›๋Š” ๊ทธ๋ฆ‡ // ํŒŒ๋ผ๋ฏธํ„ฐ, ์ „๋‹ฌ๊ฐ’, ๋งค๊ฐœ๋ณ€์ˆ˜ = ์ธ์ˆ˜๋ฅผ ๋ฐ›์„ ๊ทธ๋ฆ‡ // ๋งค์†Œ๋“œ๊ฐ€ void๋กœ ์„ค์ •๋˜์–ด ์žˆ๋Š” ๊ฒƒ์€ ๋ฐ˜ํ™˜ ๊ฐ’์ด ์—†๋Š” ๋œป์ด๋‹ค. //์ œ๊ณฑ ํ•ด์ฃผ๋Š” ๋งค์†Œ๋“œ public static void power (int number){ int result = number * number; System.out.println( number + "์˜ 2์Šน์€ " + result); } //์ง€์ˆ˜ ๊ณฑ ํ•ด์ฃผ๋Š” ๋งค์†Œ๋“œ public static void powerByExp(int number, int exponent) { int result = 1; for (int i = 0; i < exponent; i++) { result *= number; }..
2023.01.16
Language/Java
02 Method - (1) ๋งค์†Œ๋“œ๋ž€ ๋ฌด์—‡์ธ๊ฐ€?
1. ๋งค์†Œ๋“œ๋Š” c++์—์„œ ๋งํ•˜๋Š” ํ•จ์ˆ˜์ด๋‹ค. -> ํ•จ์ˆ˜๋Š” ํ”„๋กœ๊ทธ๋ž˜๋จธ๊ฐ€ ํŠน์ • ๊ธฐ๋Šฅ์„ ์ˆ˜ํ–‰ํ•˜๋„๋ก ํ•˜๊ธฐ ์œ„ํ•ด ๋งŒ๋“  ์ผ์ข…์˜ ๋จธ์‹ ์ด๋‹ค. public class _01_Method { // ๋งค์†Œ๋“œ = ํ•จ์ˆ˜ //๋ฉ”์†Œ๋“œ ์ •์˜ public static void sayHello () { System.out.println("์•ˆ๋…•ํ•˜์„ธ์š”? ๋ฉ”์†Œ๋“œ ์ž…๋‹ˆ๋‹ค."); } public static void main(String[] args) { // ๋งค์†Œ๋“œ ํ˜ธ์ถœ System.out.println("๋งค์†Œ๋“œ ํ˜ธ์ถœ ์ „"); sayHello(); sayHello(); sayHello(); System.out.println("๋งค์†Œ๋“œ ํ˜ธ์ถœ ํ›„"); } }
2023.01.16
Language/Java
๋ฐฐ์—ด_04_ASCII
1. ์•„์Šคํ‚ค ์ฝ”๋“œ๋ž€? ๋ฏธ๊ตญ ํ‘œ์ค€ ์ฝ”๋“œ : ๋ฌธ์ž, ์ˆซ์ž๋“ฑ์„ ์ฝ”๋“œ๋กœ ์ •์˜ ์•ŒํŒŒ๋ฒณ ๋Œ€๋ฌธ์ž (A)์˜ ์•„์Šคํ‚ค ์ฝ”๋“œ = 65 ์•ŒํŒŒ๋ฒณ ์†Œ๋ฌธ์ž (a)์˜ ์•„์Šคํ‚ค ์ฝ”๋“œ = 97 ์ˆซ์ž (0)์˜ ์•„์Šคํ‚ค ์ฝ”๋“œ = 48 char c = 'A'; System.out.println(c); System.out.println((int)c); c = 'B'; System.out.println(c); System.out.println((int)c); c++; System.out.println(c); System.out.println((int)c); 2. ์•„์Šคํ‚ค ์ฝ”๋“œ ์‘์šฉ ๋ฐฐ์—ด ์ž๋™ ์ดˆ๊ธฐํ™” ์‹œ ๋ฌธ์ž ๊ฐ’์„ ์ผ์ผํžˆ ์น˜์ง€ ์•Š๊ณ  char ๋ณ€์ˆ˜ ++; ์„ ํ†ตํ•ด ์•ŒํŒŒ๋ฒณ์„ ์ฐจ๋ก€๋Œ€๋กœ ์ด์šฉ ๊ฐ€๋Šฅ // ๋ฐฐ์—ด ์ž๋™ ์ดˆ๊ธฐํ™” // ์„ธ๋กœํฌ๊ธฐ 10 x ๊ฐ€๋กœ ํฌ๊ธฐ 15..
2023.01.16
Language/Java
1-(4) 2 ์ฐจ์› ๋ฐฐ์—ด ์ˆœํšŒ ๋ฐ ์ž๋™ ์ดˆ๊ธฐํ™” ๋ฐฉ๋ฒ•
1. 2์ฐจ์› ๋ฐฐ์—ด ์ˆœํšŒ (๋ชจ๋“  ํ–‰์˜ ๊ธธ์ด๊ฐ€ ๊ฐ™์€ 2 ์ฐจ์› ๋ฐฐ์—ด) String[][] seats = new String[][]{ {"A1", "A2", "A3", "A4", "A5"}, {"B1", "B2", "B3", "B4", "B5"}, {"C1", "C2", "C3", "C4", "C5"} }; for (int i = 0; i < 3; i++) { // ์„ธ๋กœ for (int j = 0; j < 5; j++) { // ๊ฐ€๋กœ System.out.print(seats[i][j] + " "); } System.out.println(); } // ํ•˜๋“œ์ฝ”๋”ฉ ํ”ผํ•˜๊ณ  ์‹ถ์œผ๋ฉด 3๊ณผ 5๋ฅผ ๊ฐ๊ฐ seats.length, seats[i].length๋กœ ๋ฐ”๊พธ๋ฉด ๋œ๋‹ค. 2. 2์ฐจ์› ๋ฐฐ์—ด ์ˆœํšŒ (ํ–‰ ๋งˆ๋‹ค ๊ธธ์ด๊ฐ€ ๋‹ค๋ฅธ 2์ฐจ์› ..
2023.01.14
Language/Java
1-(3) 2 ์ฐจ์› ๋ฐฐ์—ด ์„ ์–ธ ๋ฐ ์ดˆ๊ธฐํ™”
1. 2 ์ฐจ์› ๋ฐฐ์—ด ์„ ์–ธ ๋ฐฉ๋ฒ• 2 ์ฐจ์› ๋ฐฐ์—ด = { 1์ฐจ์› ๋ฐฐ์—ด, 1์ฐจ์› ๋ฐฐ์—ด, 1์ฐจ์› ๋ฐฐ์—ด} // 3 x 5 ํฌ๊ธฐ์˜ 2์ฐจ์›์œผ๋กœ ํ‘œํ˜„ํ•˜๊ธฐ String[][] seats = new String[][] { {"A1", "A2","A3","A4","A5"}, {"B1", "B2","B3","B4","B5"}, {"C1", "C2","C3","C4","C5"} }; // ๋ฐ”๋กœ ์ดˆ๊ธฐํ™” ์•ˆํ•  ๋•Œ๋Š” String[][] seats2 = new String[3][5]; // B2์— ์ ‘๊ทผํ•˜๋ ค๋ฉด? System.out.println(seats[1][1]); //C5์— ์ ‘๊ทผํ•˜๋ ค๋ฉด? System.out.println(seats[2][4]); 2. ๊ฐ ํ–‰ ๋งˆ๋‹ค ๊ธธ์ด๊ฐ€ ๋‹ค๋ฅธ 2 ์ฐจ์› ๋ฐฐ์—ด String[][] seats2 = { {"..
2023.01.14
Language/Java