public class TestDateTime { public static void main(String[] args){ Date today = new Date(2006, 10, 16); Time now = new Time(2006, 10, 16, 11, 38, 0); today = new Date(); now = new Time(); System.out.println("today is " + today.toString()); System.out.println("time now is " + now.toString()); /* for (int i=0; i<10; i++){ int k = (int) (12*Math.random()); System.out.println(k); } */ } } class Date { private int year; private int month; private int day; public Date(){ this((int) (2000*Math.random()) + 1000, (int)(12*Math.random()) + 1, (int)(31*Math.random()) + 1); } public Date(int yr, int mo, int da){ year = yr; month = mo; day = da; } // advance the day by n public void advanceDay(){ if (day < monthLength()) { day = day + 1; } else { day = 1; month++; } } public String toString(){ return "Year " + year + " Month " + month + " Day " + day; } public int monthLength(){ if (month == 2) return 28; else if (month == 9 || month == 4 || month == 6 || month == 11) return 30; else return 31; } } class Time extends Date{ private int hour; private int minute; private int second; public Time() { super(); hour = (int) (24*Math.random()); minute = (int) (60*Math.random()); second = (int) (60*Math.random()); } public Time(int yr, int mo, int da, int hr, int min, int sec){ super(yr, mo, da); hour = hr; minute = min; second = sec; } public String toString(){ return super.toString() + " Hour " + hour + " Minute " + minute + " Second " + second; } public boolean morning(){ return (hour < 12); } }