1 / 17

상속을 이용한 Time Class

상속을 이용한 Time Class. Dept. of Computer Eng, Kyungil Univ. 98109135 정규민. Date 클래스의 구조 #1. 1 . 맴버변수 년 – private int year 월 – private int month 일 – private int day 2. Set 메서드 , Get 메서드 3. 날짜를 지정하는 SetDate 메서드 4. ‘ 일 ’ 을 증가시키는 addDate 메서드 5. 각각의 월에 대한 날짜의 최대값을

Télécharger la présentation

상속을 이용한 Time Class

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 상속을 이용한 Time Class Dept. of Computer Eng, Kyungil Univ. 98109135 정규민

  2. Date 클래스의 구조 #1 1 .맴버변수 년 – private int year 월 – private int month 일 – private int day 2. Set 메서드, Get메서드 3. 날짜를 지정하는 SetDate 메서드 4. ‘일’을 증가시키는 addDate 메서드 5. 각각의 월에 대한 날짜의 최대값을 가지는 배열 6. toString() 메서드

  3. Date 클래스의 구조 #2 private int day; private int month; private int year; static int[] daysPerMonth ={ 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; public void setMonth(int month) { if((month>0) && (month<=12)) this.month = month; else { this.month = 1; } } public void setYear(int year) { if(year>0) this.year = year; else this.year =2000; } public int getYear() { return this.year; } public int getMonth() { return this.month; } public int getDay() { return this.day; }

  4. public void setDay(int day) { if ((day<daysPerMonth[month])&&(day>0)) { this.day =day; } else if (this.month ==2 && day==29 &&(year%400==0)) { this.day = day; } else { this.day =1; } } 윤달 체크를 위한 부분

  5. Date 클래스의 구조 #3 public void addDay() { setDay(getDay()+1); if (day==1) { setMonth(getMonth()+1); if (month==1) { year++; } } } 각각의 날짜와 월수를 증가시키고 증가 시킨 값에 따라 달수와 년도가 증가될 것인지 확인한다.

  6. Date 클래스의 구조 #4 public Date(int year) { this(year, 1, 1); } public Date(int year, int month) { this(year, month, 1); } public Date(int year, int month, int day) { setYear(year); setMonth(month); setDay(day); } public String toString() { return new String(year + " Year " + month + " Month " + day + " Day "); }

  7. Date클래스로 부터 상속 Time 클래스의 구조 #1 1. 멤버변수 Privtae int hour Private int monite Private int second 2. 각각의 set, get메서드 3. 초를 증가시키는 addTIme 메서드 4. 날짜를 넣는 setTime메서드 5. toString() 메서드

  8. Time 클래스의 구조 #2 private int hour; private int minute; private int second; public void setHour(int hour) { if ((hour>=0) && (hour <24)) this.hour = hour; else this.hour = 0; } public void setMinute(int minute) { if ((minute>=0) &&(minute<60)) this.minute = minute; else this.minute =0; } public void setSecond(int second) { if ((second>=0) &&(second<60)) this.second = second; else this.second = 0; } public int getHour() {return hour;} public int getMinute() {return minute;} public int getSecond() {return second;}

  9. public void addSecond() { setSecond(getSecond()+1); if (second==0) { setMinute(getMinute()+1); if (minute==0) { setHour(getHour()+1); if(getHour()==0) { addDate(); } } } } Time 클래스의 구조 #3 초의 증가에 따른 시간의 증가부분

  10. Time 클래스의 구조 #4 public String toString() { return new String (super.toString() "\n" + "현재 시간은 " +getHour() + "시 " +getMinute() + "분 " + getSecond() + "초 입니다."); } public void setTime(int hour, int minute, int second) { setHour(hour); setMinute(minute); setSecond(second); } 기타 생성자 부분 부분 필요 public Time(int year, int month, int day, int hour) { this(year, month, day, hour, 0, 0); }

  11. Main 부분의 구조 #1 Time class 를 오늘의 날짜로 지정을 하여 생성을 하고 반복문을 돌면서 처음 시작 날짜와 비교 이틀이 지났으면 중단을 한다.

  12. Main 부분의 구조 #2 public class curTime { public static void main(String[] args) { //오늘의 날짜를 입력한다. Time cur = new Time(2002, 11, 14); System.out.println(cur.toString()); while(true) { cur.addSecond(); if (cur.getMinute() ==0) System.out.println(cur); if (cur.getDay() ==16) break; } System.out.println(cur.toString()); } }

  13. 오버로딩을 통한 스택 구현 19991555 정재형

  14. Sub_Stack(); Sub_Stack Class iTop = -1; cTop = -1; iStack = new int[5]; cStack = new char[5]; private int iStack[]; private char cStack[]; private int iTop; private int cTop; Sub_Stack(); public void push(int) Public void push(char) private int iPop() private int cPop() public void sPrint() Main_Stack Class BufferedReader in Sub_Stack oStack String cString; int cnt = 0;

  15. import java.io.*; class Sub_Stack { private int iStack[]; private char cStack[]; private int iTop; private int cTop; Sub_Stack() { iTop = -1; cTop = -1; iStack = new int[5]; cStack = new char[5]; } public void push(int value) { if (iTop < 5) { System.out.println("Stack(int) is full!!!"); return; } else iStack[iTop++] = value; } public void push(char value) { if (cTop < 5) { System.out.println("Stack(char) is full!!!"); return; } else cStack[cTop++] = value; }

  16. private int iPop() { if(iTop < 0) { System.out.println("Stack(int) is empty!!!"); return 0; } return iStack[--iTop]; } private char cPop() { if(cTop < 0) { System.out.println("Stack(char) is empty!!!"); return 0; } return cStack[--cTop]; } public void sPrint() { int i; System.out.println("Int Stack"); for(i = 4 ; i > -1 ; i--) { System.out.print(" " + this.iPop()); } System.out.println("Char Stack"); for(i = 4 ; i > -1 ; i--) { System.out.print(" " + this.cPop()); } } }

  17. class Main_stack { public static void main(String args[]) throws java.io.IOException { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); Sub_Stack oStack = new Sub_Stack(); String cString; int cnt = 0; System.out.println("숫자 5개 , 문자 5 개를 입력하라."); while(cnt != 10) { cString = in.readLine(); if(cString.charAt(0) >= '0' && cString.charAt(0) <= '9') { oStack.push(Integer.parseInt(cString)); } else oStack.push(cString.charAt(0)); cnt =+ 1; } oStack.sPrint(); } }

More Related