archive
6장 클래스와 메소드 심층 탐구 연습 문제 및 LAB 문제 본문
1) Television 클래스의 생성자를 추가하고 업그레이드
package 텔레비전생성자;
public class Television {
private int channel;
private int volume;
private boolean onOff;
Television(int channel, int volume, boolean onOff) {
this.channel=channel;
this.volume=volume;
this.onOff=onOff;
}// 메서드와 구분되는 가장 중요한 점은 반환값을 가지지 않기 때문에 맨 앞에 void같은 반환값 키워드가 붙지 않음!
//public 같은 접근자는 붙여도 됨
void print() {
System.out.println("채널은 "+channel+"이고, 볼륨은 "+volume+"입니다.");
}
}
package 텔레비전생성자;
public class TelevisionTest {
public static void main(String[] args) {
// TODO Auto-generated method stub
Television mytv=new Television(7, 10, true);
mytv.print();
Television yourtv=new Television(11,20,true);
yourtv.print();
}
}
2) 상자를 나타내는 box 클래스 작성
package Box클래스작성;
public class Box {
private int width;
private int length;
private int height;
private int volume;
public int getVolume() {
return volume;
}
Box(int width, int length, int height){
this.width=width;
this.length=length;
this.height=height;
volume=width*length*height; // 생성자는 초기화 과정이니까 되도록이면 모든 지역변수 포함하는게 좋은 거 같은 논리 같음
}
}
package Box클래스작성;
public class BoxTest {
public static void main(String[] args) {
// TODO Auto-generated method stub
Box b=new Box(20,20,30);
System.out.println("상자의 부피는 "+b.getVolume()+"입니다.");
}
}
3) Date 클래스를 작성하여 날짜를 나타내고 연도와 날짜는 정수형, 월은 문자열로 저장한다.
package Date클래스작성;
public class Date {
private int year;
private String month;
private int day;
public Date() {
this(1900,"1월",1);
}
public Date(int year) {
this(year, "1월", 1);
}
public Date(int year, String month, int day) {
this.year=year;
this.month=month;
this.day=day;
}
public String toString() {
String s="Date [ year="+year+", month="+month+", day="+day+" ]";
return s;
}
}
package Date클래스작성;
public class DateTest {
public static void main(String[] args) {
// TODO Auto-generated method stub
Date date1=new Date(2015, "8월", 10);
Date date2=new Date(2020);
Date date3=new Date();
System.out.println(date1);
System.out.println(date2);
System.out.println(date3);
}
}
4) 시간을 나타내는 Time 클래스 작성하고 전달되는 데이터를 검증하는 과정까지 구현하여라
package Time클래스작성;
public class Time {
private int hour;
private int minute;
private int second;
public Time() {
this(0,0,0);
}
public Time(int h, int m, int s) {
hour=((h>=0&&h<24)?h:0);
minute=((m>=0&&m<60)?m:0);
second=((s>=0&&s<60)?s:0);
}
public String toString() {
return String.format("%02d:%02d:%02d", hour, minute, second);
}
}
package Time클래스작성;
public class TimeTest {
public static void main(String[] args) {
// TODO Auto-generated method stub
Time time=new Time();
Time time2=new Time(13,27,6);
System.out.println("기본 생성자 호출 후 시간: "+time.toString());
System.out.println("두번째 생성자 호출 후 시간: "+time2.toString());
Time time3=new Time(99,66,77);
System.out.println("잘못된 생성자 호출 후 시간: "+time3.toString());
}
}
5)
'Java > 스터디 예제 풀이' 카테고리의 다른 글
7장 상속 연습 문제 및 LAB 문제 -2 (0) | 2023.03.03 |
---|---|
7장 상속 연습 문제 및 LAB 문제 -1 (0) | 2023.03.03 |
5장 클래스, 객체, 메소드 연습 문제 및 LAB 문제 (0) | 2023.02.05 |
4장 배열 연습 문제 및 LAB 문제 (0) | 2023.02.05 |
3장 선택과 반복 연습 문제 및 LAB 문제 (0) | 2023.02.03 |