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 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 | package com.test; public class Constructor { public static void main(String[] args) { //Time인스턴스 Time t1 = new Time(); t1.check(); Time t2 = new Time(2, 30, 50); t2.check(); Time t3 = new Time(1, 10, 80); t3.check(); //1:11:20 Time t4 = new Time(2, 150, 80); t4.check(); //4:31:20 Time t5 = new Time(10000); t5.check(); //2:46:40 } } //시간 클래스 class Time { private int hour; private int min; private int sec; //초기화 public Time() { this.hour = 1; this.min = 0; this.sec = 0; //요청:1시간0분0초가 필요하다 } //생성자 오버로딩 //1. new Time (2, 30, 50) //2.추가ask] new Time (2, 70, 50) public Time(int hour, int min, int sec) { /*1 this.hour = hour; this.min = min; this.sec = sec; */ //초단위 60이 넘을 경우 /*2 //30초 -> 0분 30초 설명용 //70초 -> 1분 10분 //150초 -> 2분 30초 if (sec<60) { this.sec = sec; } else { this.min = sec / 60; //1 this.sec = sec % 60; } 조건문 만들 필요가 없다 */ this.min = sec / 60; this.sec = sec % 60; //분단위 60이 넘을 경우 this.hour = min / 60; this.min += min % 60; this.hour += hour; } //오버로딩 연습 추가 //new Time(10000) 이거반올림해달라., public Time(int sec) { this.min = sec / 60; //160분 this.sec = sec % 60; //40초 this.hour = this.min / 60; //2시간 this.min = this.min % 60; //46분 } //확인용., public void check() { System.out.printf("%d:%s:%s\n", this.hour, setFormat(this.min), setFormat(this.sec)); //자릿수 맞춰준 분과 초는 setFormat으로 감싸준다 } //분,초 한자리 숫자 -> 두자리 숫자 //5 -> "05" (숫자->문자열) private String setFormat(int num) { if (num <10) { return "0" + num; } else { //두자리 숫자 return num + ""; //숫자->문자열 정석st //String.valueOf(100); } } } | cs |
'WEB > JAVA' 카테고리의 다른 글
OBJECT CLASS (0) | 2015.05.22 |
---|---|
INHERITANCE(상속) (0) | 2015.05.22 |
CONSTRUCTOR(생성자)2 (0) | 2015.05.22 |
CONSTRUCTOR(생성자)1 (0) | 2015.05.22 |
STATIC(정적)2 (0) | 2015.05.22 |