728x90
Do it! 자바 프로그래밍 입문 5장 연습문제
1. 클래스를 생성할 때 호출하는 [생성자]는 멤버 변수를 초기화하는데 사용합니다.
2. 클래스를 생성하여 메모리에 있는 상태를 [인스터스] 라하고 멤버 변수를 다른 말로 [인스터스 변수]라고 합니다.
3. [메서드]는 일반 함수에 객체 지향의 개념을 추가하여, 클래스 내부에 선언하고 클래스 멤버 변수를 사용하여 클래스 기능을 구현합니다.
4. 05-7에서 예제로 나온 MyDate와 MyDateTest 클래스를 완성해 봅시다.
728x90
public class MyDate {
private int day; private int month; private int year;
public int getDay() {
return day;
}
public void setDay(int day) {
if(month == 2) {
if(day<1 || day>28) {
System.out.println("오류입니다.");
}
else {
this.day = day;
}
}
}
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;
}
public MyDate(int day, int month, int year){
this.day = day;
this.month = month;
this.year = year;
}
public boolean isValid() {
if(1 <= day && day<= 31 && 1<=year && 1<= month && month <= 12) {
System.out.println("유효한 날짜입니다.");
return true;
}
else {
System.out.println("유효하지 않은 날짜입니다.");
return false;
}
}
public void show() {
}
}
public boolean isValid() 메서드의 반환값은 true or false 여야한다. 하지만 여기서는 문자열이 출력됐으므로 문제 오류인것같다.
728x90
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());
}
}
728x90
'JAVA Programming > Doit 자바프로그래밍 입문' 카테고리의 다른 글
Do it! 자바 프로그래밍 입문 6장 (0) | 2021.11.09 |
---|---|
Do it! 자바 프로그래밍 입문 3장 4장 연습문제 (4) | 2021.10.06 |
Do it! 자바 프로그래밍 입문 1장 2장 연습문제 (7) | 2021.10.05 |