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

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,:minutes,:second

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 should look like this. It should be in 24 hours
format, and it should be ticking by second, minute and
hour.
Please Check My Ruby Code Clock Class Clock Attr Reader Hours Minutes Seconds Attr Accessor Hours Minutes Second 1
Please Check My Ruby Code Clock Class Clock Attr Reader Hours Minutes Seconds Attr Accessor Hours Minutes Second 1 (451 Bytes) Viewed 15 times
01:41:50
Join a community of subject matter experts. Register for FREE to view solutions, replies, and use search function. Request answer by replying!
Post Reply