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

Business, Finance, Economics, Accounting, Operations Management, Computer Science, Electrical Engineering, Mechanical Engineering, Civil Engineering, Chemical Engineering, Algebra, Precalculus, Statistics and Probabilty, Advanced Math, Physics, Chemistry, Biology, Nursing, Psychology, Certifications, Tests, Prep, and more.
Post Reply
answerhappygod
Site Admin
Posts: 899603
Joined: Mon Aug 02, 2021 8:13 am

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

Post 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 10 times
it should be ticking by second, minute and hour. the iteration
of hour should be in 24hour format
00:00:04.
Join a community of subject matter experts. Register for FREE to view solutions, replies, and use search function. Request answer by replying!
Post Reply