๐Ÿ’ป JAVA_

JAVA#16 _ ๊ฐ์ฒด ์ง€ํ–ฅ ์–ธ์–ด ๋ฌธ์ œ ์—ฐ์Šต

CodeLoge 2025. 1. 12. 01:27

Q1. 

์› ํด๋ž˜์Šค๋ฅผ ์„ ์–ธํ•ด์ฃผ์„ธ์š”. ์› ๊ฐ์ฒด๋Š” ์ด๋ฆ„, ๋ฐ˜์ง€๋ฆ„, ๋„“์ด๋ฅผ ๊ฐ€์ง‘๋‹ˆ๋‹ค.

class Circle {

String name;

int radius;

double area; // ๋ฉค๋ฒ„ ๋ณ€์ˆ˜ ์„ ์–ธ

 

Circle(String name, int radius) {

this.name = name;

this.radius = radius;

this.area = this.radius * this.radius * 3.14; // ๋ฉค๋ฒ„ ๋ณ€์ˆ˜ ์ดˆ๊ธฐํ™”

}

 

void printCircleArea() {

System.out.println("์› ์ด๋ฆ„ : " + this.name);

System.out.println("๋ฐ˜์ง€๋ฆ„ : " + this.radius);

}

}

 

Q2.

์ฑ… ๊ฐ์ฒด๋“ค์„ ์ €์žฅํ•˜๊ณ  ์‹ถ์Šต๋‹ˆ๋‹ค. ์ฑ… ๊ฐ์ฒด๋Š” ์ œ๋ชฉ๊ณผ ์ž‘๊ฐ€๋ฅผ ๊ฐ€์ง‘๋‹ˆ๋‹ค. ์ž‘๊ฐ€๋ฅผ ์•Œ ์ˆ˜ ์—†๋Š” ๊ฒฝ์šฐ, "์ž‘์ž๋ฏธ์ƒ"์œผ๋กœ ํ‘œ๊ธฐํ•ฉ๋‹ˆ๋‹ค.

class Book {

String title;

String writter; // ๋ฉค๋ฒ„ ๋ณ€์ˆ˜ ์„ ์–ธ

 

Book(String title) {

this.title = title;

this.writter = "์ž‘์ž๋ฏธ์ƒ"; // ๋ฉค๋ฒ„ ๋ณ€์ˆ˜ ์ดˆ๊ธฐํ™”

}

 

Book(String title, String writter) {

this.title = title;

this.writter = writter;

}

 

void printBookInfo() {

System.out.println("์ฑ… ์ด๋ฆ„ : " + this.title);

System.out.println("์ž‘๊ฐ€ ์ด๋ฆ„ : " + this.writter);

}

}

 

Q3.

์ƒํ’ˆ ๊ฐ์ฒด๋ฅผ ๋‹ค๋ฃฐ ์˜ˆ์ •์ž…๋‹ˆ๋‹ค. ์ƒํ’ˆ ๊ฐ์ฒด๋Š” ์ด๋ฆ„, ๊ฐ€๊ฒฉ, ์žฌ๊ณ ๋ฅผ ๊ฐ€์ง‘๋‹ˆ๋‹ค. ์žฌ๊ณ ๋ฅผ ์„ค์ •ํ•˜์ง€ ์•Š์€ ๊ฒฝ์šฐ "0"์œผ๋กœ ํ‘œ๊ธฐํ•ฉ๋‹ˆ๋‹ค.

class Product {

String name;

int price;

int num // ๋ฉค๋ฒ„ ๋ณ€์ˆ˜ ์„ ์–ธ

 

Product(String name, int price) {

this.name = name;

this.price = price;

this.num = 0; // ๋ฉค๋ฒ„ ๋ณ€์ˆ˜ ์ดˆ๊ธฐํ™”

}

 

Product(String name, int price, int num) {

this.name = name;

this.price = price;

this.num = num;

}

 

void printProductInfo() {

System.out.println("์ œํ’ˆ ์ด๋ฆ„ : " + this.name);

System.out.println("์ œํ’ˆ ๊ฐ€๊ฒฉ : " + this.price + "์›");

System.out.println("์ œํ’ˆ ์žฌ๊ณ  : " + this.num + "๊ฐœ");

}

}

 

Q4.

์ž๋™์ฐจ ํด๋ž˜์Šค๋ฅผ ๊ตฌํ˜„ํ•ด์ฃผ์„ธ์š”. ์ž๋™์ฐจ๋Š” ํ˜„์žฌ ์†๋„๋ฅผ ๋ณด์—ฌ์ค„ ์ˆ˜ ์žˆ๊ณ , ๋ชจ๋“  ์ž๋™์ฐจ๋“ค์€ ์ตœ๋Œ€ ์†๋„๊ฐ€ 120์„ ๋„˜๊ธธ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.

class Car {

int carCurrentSpeed;

int carMaxSpeed;  // ๋ฉค๋ฒ„ ๋ณ€์ˆ˜ ์„ ์–ธ

 

Car(int carCurrentSpeed) {

this.carCurrentSpeed = carCurrentSpeed;

this.carMaxSpeed = 120; // ๋ฉค๋ฒ„ ๋ณ€์ˆ˜ ์ดˆ๊ธฐํ™”

 

if (this.carCurrentSpeed > this.carMaxSpeed) {

this.carCurrentSpeed = this.carMaxSpeed;

System.out.println("์ž๋™์ฐจ์˜ ์ตœ๊ณ  ์†๋„๋Š” " + this.carMaxSpeed + " km/h ์ž…๋‹ˆ๋‹ค.");

}

}

}

 

Q1~4 ์ถœ๋ ฅ

Circle circle01 = new Circle("ํ”ผ์ž", 3);

circle01.printCircleArea();

 

System.out.println("================================");

 

Book book01 = new Book("ํ•ด๋ฆฌ ํฌํ„ฐ", "JK ๋กค๋ง");

book01.printBookInfo();

 

System.out.println("================================");

 

Product prod01 = new Product("ํ•ธ๋“œํฐ", 1000000, 333);

prod01.printProductInfo();

 

System.out.println("================================");

 

Car car01 = new Car(110);

System.out.println("์ž๋™์ฐจ์˜ ํ˜„์žฌ ์†๋„ : " + car01.carCurrentSpeed);

 

Console ์ฐฝ ์ถœ๋ ฅ

์› ์ด๋ฆ„ : ํ”ผ์ž

๋ฐ˜์ง€๋ฆ„ : 3

================================

์ฑ… ์ด๋ฆ„ : ํ•ด๋ฆฌ ํฌํ„ฐ

์ž‘๊ฐ€ ์ด๋ฆ„ : JK ๋กค๋ง

================================

์ œํ’ˆ ์ด๋ฆ„ : ํ•ธ๋“œํฐ

์ œํ’ˆ ๊ฐ€๊ฒฉ : 1000000์›

์ œํ’ˆ ์žฌ๊ณ  : 333๊ฐœ

================================

์ž๋™์ฐจ์˜ ํ˜„์žฌ ์†๋„ : 110