티스토리 뷰
1. 아래의 BankAccount(소스 PPT 참고) 에 대하여 메모리 그림을 그리시오.
BankAccount ref1 = new BankAccount();
BankAccount ref2 = ref1;
2.생성자(Constructor)란 무엇인가요?
클래스 이름과 같은 함수. new 뒷 부분.
new연산자를 통해 인스턴스를 만들고 인스턴스의 변수를 초기화시키는 역할
public 클래스이름 ( 매개변수 ) {
this. = new
}
3.디폴트 생성자에 대하여 설명하시오.
개발자가 생성자를 만들지 않아서 컴파일러가 만들어준 생성자
매개변수가 없다.
(예를 들어주기)
4.생성자의 용도는?
객체의 초기화
(예를 들어주기)
5. null 에 대하여 설명하시오.
가리키는 주소 없음. 참조변수와 인스턴스와의 관계 끊는 역할
6.아래의 TV 클래스를 만드시오.
public static void main(String[] args) {
TV myTV = new TV("LG", 2017, 32); //LG에서 만든 2017년 32인치
myTV.show();
}
출력:
LG에서 만든 2017년형 32인치 TV
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
public class TV {
String brand;
int year;
int inch;
TV(String brand, int year, int inch){
this.brand = brand;
this.year = year;
this.inch = inch;
}
void show(){
System.out.println(brand + "에서 만든 " + year + "년 " +inch+"인치 TV" );
}
}
|
cs |
1
2
3
4
5
6
7
8
9
|
public class Practice {
public static void main(String[] args) {
TV myTV = new TV("LG", 2017, 32); // LG에서 만든 2017년 32인치
myTV.show();
}
}
|
cs |

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()); //우 입니다.
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
|
public class Grade {
int math;
int science;
int english;
double average;
Grade(int math, int science, int english) {
this.math = math;
this.science = science;
this.english = english;
}
public double average() {
return average = (math + science + english) / 3.0;
}
public String getGrade() {
double avg = average();
String str;
if (avg >= 90) {
return "수 입니다";
} else if (avg >= 80) {
return "우 입니다";
} else if (avg >= 70) {
return "미 입니다";
} else if (avg >= 60) {
return "양 입니다";
} else {
return "가 입니다";
}
}
}
|
cs |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
public class Practice {
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());
String grade = me.getGrade();
System.out.println(me.getGrade());
}
}
|
cs |

8.노래 한 곡을 나타내는 Song 클래스를 작성하라.
Song은 다음 필드로 구성된다.
- 노래의 제목을 나타내는 title
- 가수를 나타내는 artist
- 노래가 발표된 연도를 나타내는 year
- 국적을 나타내는 country
또한 Song 클래스에 다음 생성자와 메소드를 작성하라.
- 생성자 2개: 기본 생성자와 매개변수로 모든 필드를 초기화하는 생성자
- 노래 정보를 출력하는 show() 메소드
- main() 메소드에서는 Song 객체로 생성하고
show()를 이용하여 노래의 정보를 다음과 같이 출력하라.
출력:
1978년, 스웨덴 국적의 ABBA가 부른 "Dancing Queen"
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
public class Song {
String title, artist, country;
int year;
Song(int year, String country, String artist, String title) {
this.title = title;
this.artist = artist;
this.year = year;
this.country = country;
}
void show() {
System.out.println(year + "년, " + country + " 국적의 " +
artist + "가 부른 " + "\"" + title + "\"");
}
}
|
cs |
1
2
3
4
5
6
7
8
9
10
|
public class Practice {
public static void main(String[] args) {
Song song = new Song(1978, "스웨덴", "ABBA", "Dancing Queen");
song.show();
}
}
|
cs |

'면접준비 > KOSMO 허쌤 숙제' 카테고리의 다른 글
학습정리-10-15 (자바 가위바위보) (0) | 2021.10.27 |
---|---|
절대경로, 상대경로, 정보은닉, 접근제한자, 지역변수, Rec함수좌표(21/10/14) (0) | 2021.10.27 |
변수 스코프, 지역변수, 인스턴스, 클래스, 객체 (21/10/12) (0) | 2021.10.27 |
함수,별찍기 복습, 도형넓이(21/10/11) (0) | 2021.10.27 |
화폐, 구구단, 별찍기 (21/10/08) (0) | 2021.10.27 |