(must be written in Java) 9.1 (The Time class) Design a class named Time. The cl
ID: 3616673 • Letter: #
Question
(must be written in Java)9.1 (The Time class) Design a class named Time. The classcontains:
Data fields hour, minute, and second that represent a time.
A no-arg constructor that creates a Time object for the currenttime. (the data fields value will represent the current time.)
A constructor that constructs a Time object witha specifiedelapsed time since midnight, Jan 1, 1970, in milliseconds. (thedata fields valu will represent this time.)
Three get methods for the data fields hour, minute, and second,respectively.
Draw the UML diagram for the class. Implement the class.Write a test program that creates two Time objects (using newTime() and new Time(555550000)) and display their hour, minute, andsecond
Explanation / Answer
please rate - thanks hope this gets you started class timeclass1tester{ public static void main(String[] args) {TimeClass a=new TimeClass(); TimeClass b=new TimeClass(System.currentTimeMillis()); System.out.println("object a"); System.out.println("Hour "+a.getHour()); System.out.println("Minute "+a.getMinute()); System.out.println("Seconds "+a.getSecond()); System.out.println(); System.out.println("object b"); System.out.println("Hour "+b.getHour()); System.out.println("Minute "+b.getMinute()); System.out.println("Seconds "+b.getSecond()); } } import java.util.*; class TimeClass { private int hour,min,sec; public TimeClass() {Calendar cal = new GregorianCalendar(); hour = cal.get(Calendar.HOUR_OF_DAY); min =cal.get(Calendar.MINUTE); sec = cal.get(Calendar.SECOND); } public TimeClass(long milli) {long time=milli/1000; hour=(int)((time / 3600)-5)%24; //adjust current time toGMT min=(int)((time % 3600) / 60); sec=(int)(time % 60); } public int getHour() { return hour; } public int getMinute() { return min; } public int getSecond() { return sec; } }
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.