Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Create a class called Time that has private member variables for hours, minutes

ID: 654368 • Letter: C

Question

Create a class called Time that has private member variables for hours, minutes and seconds. A default constructor should initialize the time to 0:0:0. A three argument constructor accepts arguments for hours, minutes and seconds. Include a member function named display that displays the time in hh:mm:ss format and a member function named tick that adds one second. Make sure that seconds, minutes and hours round up correctly. Use the class in a program that demonstrates each function. Start with the time at 11:59:59 and call the tick function. The new time should be 12:0:0

Explanation / Answer

import java.util.ArrayList; public class Time implements TimeTicker { private int years; private int days; private int hours; private int seconds; private int minutes; ArrayList listeners = new ArrayList(); public Time(int years, int days, int hours, int seconds, int minutes){ this.minutes = minutes; this.seconds = seconds; this.hours = hours; this.days = days; this.years = years; } public void addListener(TheTimeListener ttl) { listeners.add(ttl); } public String theTimePassed() { return years + ":" + days + ":" + hours + ":" + seconds + ":" + minutes; } public void setTimeChange(int seconds, int minutes, int hours, int days, int years) { this.days += days; this.years += years; this.hours += hours; this.minutes += minutes; this.seconds += seconds; } public int getYears() { return years; } public void begin() { while(true) { if (Thread.currentThread().isInterrupted()) { System.out.println("Time was interrupted"); break; } seconds++; if (seconds == 30) { tickHappened(); } if (seconds >= 60) { seconds = 0; minutes ++; if (minutes >= 60) { minutes = 0; hours++; if (hours >= 24) { hours = 0; days ++; if (days > 365 ) { days = 0; years++; } } } } } } @Override public String toString() { return hours + ":" + minutes + ":" + seconds; } @Override public void tickHappened() { for (TheTimeListener ttl : listeners) { ttl.updateTheTime(this); } } }
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote