Please check my ruby code # Clock class Clock attr_reader :hours,:minutes,:seconds attr_accessor :hours,:minutes,:second
Posted: Thu May 26, 2022 9:46 am
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 should look like this. It should be in 24 hours
format, and it should be ticking by second, minute and
hour.
01:41:50
# 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 should look like this. It should be in 24 hours
format, and it should be ticking by second, minute and
hour.
01:41:50