Total seconds since midnight, Java -
i new programming. doing assignment intro java. in assignment, need find total number of seconds since midnight , changed number hours, minutes , seconds show current time. have small problem. when test code, totalseconds show 0! appreciated. sorry code chaos
package clock; import java.text.decimalformat; import java.util.calendar; // current time public class clock { public static int totalseconds; public static int seconds; public static int minutes; public static int hours; public static int test; public clock(int hours, int minutes, int seconds ) { sethours(hours); setminutes(minutes); setseconds(seconds); } // use current time public clock() { calendar c = calendar.getinstance(); long = c.gettimeinmillis(); c.set(calendar.hour_of_day, 0); c.set(calendar.minute, 0); c.set(calendar.second, 0); c.set(calendar.millisecond, 0); long passed = - c.gettimeinmillis(); long secondspassed = passed / 1000; totalseconds = (int) secondspassed; } public void tick() { addsecond(); } private void addsecond() { seconds = totalseconds%60; } private void addminute() { minutes = totalseconds/60 % 60; } private void addhour() { hours = totalseconds / 3600; } public string tostring() { decimalformat f = new decimalformat("00"); return f.format(hours) + ":" + f.format(minutes) + ":" + f.format(seconds); } public int gethours() { return hours; } public int getminutes() { return minutes; } public int getseconds() { return seconds; } // total number of minutes sinces midnight public int gettotalminutes() { return totalseconds / 60 % 60; } // total number of seconds since midnight public int gettotalseconds() { return totalseconds; } public void sethours(int hours) { if (hours > 23 || hours < 0) { this.hours = 0; } else this.hours = hours; } public void setminutes(int minutes) { if (minutes > 59 || minutes < 0) this.minutes = 0; else this.minutes = minutes; } public void setseconds(int seconds) { if (seconds > 59 || seconds < 0) this.seconds = 0; else this.seconds = seconds; } // reset hours, minutes , seconds 0 public void reset() { hours = minutes = seconds = 0; } public static void main(string [] args){ system.out.println("this total seconds " + test + totalseconds ); } }
change main this.
public static void main(string [] args){ clock clock = new clock(); system.out.println("this total seconds " + test + totalseconds ); }
new make instance of clock , constructor called, magic happens.
Comments
Post a Comment