Please check my ruby code
# Clock
class Clock
attr_reader :hours,:minutes,:seconds
attr_accessor :hours,:minutes,:seconds
def initialize()
currentTime = LocalDateTime.now()
self.hours =
currentTime.getHour()
self.minutes =
currentTime.getMinute()
self.seconds =
currentTime.getSecond()
end
def tick()
self.seconds = (self.seconds + 1) %
60
if (self.seconds == 0)
self.minutes =
(self.minutes + 1) % 60
if (self.minutes ==
0)
self.hours
= (self.hours + 1) % 24
end
end
end
def write()
return String.format("%02d : %02d :
%02d",self.hours,self.minutes,self.seconds)
end
def self.main() throws InterruptedException
clock = Clock.new()
while (true)
print(clock,"\n")
clock.tick()
Thread.sleep(1000)
end
end
end
main()
The output of the program should be as below
it should be ticking by second, minute and hour. the iteration
of hour should be in 24hour format
00:00:04.
Please check my ruby code # Clock class Clock attr_reader :hours,:minutes,:seconds attr_accessor :hours,:minute
-
- Site Admin
- Posts: 899603
- Joined: Mon Aug 02, 2021 8:13 am