1. 아래의 BankAccount(소스 PPT 참고) 에 대하여 노란색으로 표시된 부분을 메모리 그림 그리시오.


2. 생성자란 무엇인가요?
- 그 class 를 초기화하는 함수입니다.
- Java 에서는 생성자를 넣지 않은 경우에도 자동으로 default 생성자를 컴파일합니다.
3.디폴트 생성자에 대하여 설명하시오.
- parameter 를 공백으로 두고 그 class 를 default 값으로 초기화하는 함수.
- 정수는 0, 실수는 0.0 이 기본으로 적용된다.
4.생성자의 용도는?
5. null 에 대하여 설명하시오.
- 참조형에서만 가능하다.
- 참조형 값을 null 로 두면 메모리와 연결된 주소가 지워진다.
- null 인 참조형 변수를 사용하려고 하면 NullPointerException 이 발생한다.
6.아래의 TV 클래스를 만드시오.
public static void main(String[] args) {
TV myTV = new TV("LG", 2017, 32); //LG에서 만든 2017년 32인치
myTV.show();
}
//
//출력
LG에서 만든 2017년형 32인치 TV
// 코드
class TV {
String brand;
int year;
int inch;
TV(String brand, int year, int inch) {
brand = this.brand;
year = this.year;
inch = this.inch;
}
void show() {
String print = new String();
print += this.brand;
print += "에서 만든 ";
print += this.year;
print += "년형 ";
print += this.inch;
print += " TV";
System.out.println(print);
}
}
7. 아래의 Grade 를 만드시오.
public static void main(String[] args) {
//
int math, science, english;
math = 90;
science = 80;
english = 80;
//
Grade me = new Grade(math, science, english);
System.out.println("평균은 " + me.average());
System.out.println(me.getGrade()); //우 입니다.
}
// 코드
class Grade {
int math;
int science;
int english;
Grade(int math, int science, int english) {
this.math = math;
this.science = science;
this.english = english;
}
double average() {
double avg = ((double)(this.math + this.science + this.english) / 3.);
return avg;
}
String getGrade() {
double avg = this.average();
String print = new String();
if (avg >= 90.) {
print += "수";
} else if (avg >= 80.) {
print += "우";
} else if (avg >= 70.) {
print += "미";
} else if (avg >= 60.) {
print += "양";
} else {
print += "가";
};
print += " 입니다.";
return print;
}
}
8.노래 한 곡을 나타내는 Song 클래스를 작성하라. Song은 다음 필드로 구성된다.
- 노래의 제목을 나타내는 title
- 가수를 나타내는 artist
- 노래가 발표된 연도를 나타내는 year
- 국적을 나타내는 country
//
또한 Song 클래스에 다음 생성자와 메소드를 작성하라.
//
- 생성자 2개: 기본 생성자와 매개변수로 모든 필드를 초기화하는 생성자
- 노래 정보를 출력하는 show() 메소드
- main() 메소드에서는
//
Song 객체로 생성하고
show()를 이용하여 노래의 정보를 다음과 같이 출력하라.
//
출력:
1978년, 스웨덴 국적의 ABBA가 부른 "Dancing Queen"
// 코드
class Song {
String title;
String artist;
int year;
String country;
Song(String title, String artist, int year, String country) {
this.title = title;
this.artist = artist;
this.year = year;
this.country = country;
}
void show() {
String print = new String();
print += this.year;
print += "년, ";
print += this.country;
print += " 국적의 ";
print += this.artist;
print += "가 부른 \"";
print += this.title;
print += "\"";
System.out.println(print);
}
}
public static void main(String[] args) {
String title = "Dancing Queen";
String artist = "ABBA";
int year = 1978;
String country = "스웨덴";
Song song = new Song(title, artist, year, country);
song.show();
}