Page 1 of 1

Please check my ruby code # Clock class Clock attr_reader :hours,:minutes,:seconds attr_accessor :hours,:minute

Posted: Thu May 26, 2022 9:02 am
by answerhappygod
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
Please Check My Ruby Code Clock Class Clock Attr Reader Hours Minutes Seconds Attr Accessor Hours Minute 1
Please Check My Ruby Code Clock Class Clock Attr Reader Hours Minutes Seconds Attr Accessor Hours Minute 1 (1.48 KiB) Viewed 11 times
it should be ticking by second, minute and hour. the iteration
of hour should be in 24hour format
00:00:04.