๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Java

[Java] ์—ฐ์Šต๋ฌธ์ œ + ์‹ค์Šต

by ์ฝ”๋”ฉํ•˜๋Š” ๋ถ•์–ด 2021. 2. 18.
๋ฐ˜์‘ํ˜•

Q4) 05-7์—์„œ ์˜ˆ์ œ๋กœ ๋‚˜์˜จ MyDate์™€ MyDateTest ํด๋ž˜์Šค๋ฅผ ์™„์„ฑํ•ด ๋ด…์‹œ๋‹ค.

โ€‹

<์กฐ๊ฑด>

- day, month, year ๋ณ€์ˆ˜๋Š” private์œผ๋กœ ์„ ์–ธํ•œ๋‹ค

- ๊ฐ ๋ณ€์ˆ˜์˜ get, set ๋ฉ”์„œ๋“œ๋ฅผ public์œผ๋กœ ๋งŒ๋“ ๋‹ค

- MyDate(int day, int month, int year) ์ƒ์„ฑ์ž๋ฅผ ๋งŒ๋“ ๋‹ค

- public boolean isValid( ) ๋ฉ”์„œ๋“œ๋ฅผ ๋งŒ๋“ค์–ด ๋‚ ์งœ๊ฐ€ ์œ ํšจํ•œ์ง€ ํ™•์ธ

- MyDateTest ํด๋ž˜์Šค์—์„œ ์ƒ์„ฑํ•œ MyDate ๋‚ ์งœ๊ฐ€ ์œ ํšจํ•œ์ง€ ํ™•์ธํ•œ๋‹ค

 

package test;

public class MyDate {

	private int day;
	private int month;
	private int year;
	boolean valid = true;

	public MyDate(int day, int month, int year) {
		this.year(year);
		this.month(month);
		setDay(day);
	}

    public String inValid() {
        if (valid) {
            return "์œ ํšจํ•œ ๋‚ ์งœ์ž…๋‹ˆ๋‹ค.";
        } else {
            return "์œ ํšจํ•˜์ง€์•Š์€ ๋‚ ์งœ์ž…๋‹ˆ๋‹ค.";
        }
    }

	public int getDay() {
		return day;
	}

	public void setDay(int day) {
		if (month == 2) {
            if (day < 1 || day > 28) {
                valid = false;
            } else {
                this.day = day;
                valid = true;
            }
        } else {
            this.day = day;
            valid = true;
        }
    }

	public int getMonth() {
		return month;
	}

	public void setMonth(int month) {
	    this.month = month;
	}

	public int getYear() {
		return year;
	}

	public void setYear(int year) {
		this.year = year;
	}

}
package chapter5;

public class MyDateTest {
	public static void main(String[] args) {

		MyDate date1 = new MyDate(30, 2, 2000);
		System.out.println(date1.isValid());
		MyDate date2 = new MyDate(2, 10, 2006);
		System.out.println(date2.isValid());
	}
}

<๊ฒฐ๊ณผ๊ฐ’>

-month๋„ day์ฒ˜๋Ÿผ ์กฐ๊ฑด๋ฌธ์œผ๋กœ ์ˆ˜์ •ํ•  ์ˆ˜ ์žˆ์ง€๋งŒ ๊ทธ๊ฑด ๋‚˜์ค‘์— ๋ฐฐ์šฐ์ž

-setDay ์˜ if-์กฐ๊ฑด๋ฌธ์ด ์ดํ•ด์ž˜์•ˆ๊ฐ.

โ€‹

โ€‹

โ€‹

์‹ค์Šต 1) AddressBook + AddressBookMan ํด๋ž˜์Šค ์ž‘์„ฑ

โ€‹

<์กฐ๊ฑด>

-AddressBook ํด๋ž˜์Šค-

์†์„ฑ : ์ด๋ฆ„(name), ์ „ํ™”๋ฒˆํ˜ธ(tel_num), ์ฃผ์†Œ(address) → private ์œผ๋กœ!

์†์„ฑ์— ๋Œ€ํ•˜์—ฌ getter, setter ์ž‘์„ฑ

getInfo( ) ์ž‘์„ฑ

์ด๋ฆ„ :

์ „ํ™”๋ฒˆํ˜ธ :

์ฃผ์†Œ :


-AddressBookMan ํด๋ž˜์Šค-

๋ฉ”์ธ๋ฉ”์„œ๋“œ ์ž‘์„ฑ

AddressBook addr1 = new AddressBook( );

AddressBook addr2 = new AddressBook("์ด์ˆœ์‹ ", "010-2111-0222", "๋ถ€์‚ฐ");

โ€‹

addr1์— getInfo( ) ํ˜ธ์ถœ

addr2์— getInfo( ) ํ˜ธ์ถœ

 

 

AddressBook ํด๋ž˜์Šค

 

AddressBookMan ํด๋ž˜์Šค

 

<๊ฒฐ๊ณผ๊ฐ’>

โ€‹

 

โ€‹

์‹ค์Šต 2) ์‚ผ๊ฐํ˜•๊ณผ ์‚ฌ๊ฐํ˜•์˜ ๋†’์ด, ๋„“์ด, ๋ฉด์  ๊ฐ’ ์ถœ๋ ฅํ•˜๊ธฐ

โ€‹

<์กฐ๊ฑด>

-private์œผ๋กœ ํ•  ๊ฒƒ

-getter, setter ์‚ฌ์šฉํ•  ๊ฒƒ

-getArea( ) ์„ ์‚ฌ์šฉํ•ด ์ถœ๋ ฅํ•  ๊ฒƒ

๊ฒฐ๊ณผ๊ฐ’ ์ด๋ ‡๊ฒŒ ๋‚˜์˜ค๊ฒŒ!

โ€‹

Rectangle ํด๋ž˜์Šค

 

Triangle ํด๋ž˜์Šค

โ€‹

ShapeMain ํด๋ž˜์Šค

-์‹ค์Šต 1๋ณด๋‹ค ์‰ฌ์› ์Œ.

๋ฐ˜์‘ํ˜•

'Java' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

[Java] ๊ฐ์ฒด ๊ฐ„ ํ˜‘๋ ฅ  (0) 2021.02.18
[Java] this ์˜ˆ์•ฝ์–ด  (0) 2021.02.18
[Java] ์ •๋ณด ์€๋‹‰  (0) 2021.02.18
[Java] ์ฐธ์กฐ ์ž๋ฃŒํ˜•  (0) 2021.02.18
[Java] ์ƒ์„ฑ์ž ์˜ค๋ฒ„๋กœ๋“œ  (0) 2021.02.18

๋Œ“๊ธ€