Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

NEED HELP: Write a class that simulates a clock. The clock should have three att

ID: 3624155 • Letter: N

Question

NEED HELP: Write a class that simulates a clock. The clock should have three attributes to represent the seconds,minutes, and hours of the class. The methods should be as follows:__init__() that will initialize the clock. If no values are passed to the class when an object is instantiated, the clock should should be set to 0 hours, 0 minutes and 0 seconds. setTime() that will accept values for hours, minutes and seconds and set the clock to these values. getTime() that will return the time in the format HH:MM:SS. Increment() that will increase the time by 1 second. __str__() that will display the time in the format HH:MM:SS.
Give the user the option of displaying the time as military (hours0-23) or standard(hours0-12 Am or Pm)

side information:
1.) Hours should be a value between 0 and 23
2.) Minutes and seconds should be values between 0 and 59.
3.) When incrementing the time, if seconds is greater that 59, minutes should be incremented and if minutes is greater than 59 hours should be incremented. If hours is greater than 23, it shoud go back to 0.
Language: PYTHON.

Explanation / Answer

//class definition

class clock:

   def __init__( self ):

      self.hour = 0     # 0-23

      self.minute = 0   # 0-59

      self.second = 0   # 0-59

   def setTime(self,hr,min,sec):

     if hr<24:

       self.hour = hr     # 0-23

     else:

       print'Invalid hour'

     if min<59:

      self.minute = 0   # 0-59

     else:

        print'Invalid minutes'

     if sec<59:

     self.second = sec   # 0-59

     else:

        print'Invalid seconds'

   def printMilitary( self ):

   def getTime( self ):

     return self.hour,self.minute,self.second

def increment(self):

       self.sec=self.sec+1

   def printStandard( self ):

      standardTime = ""

      if self.hour == 0 or self.hour == 12:

         standardTime += "12:"

      else:

         standardTime += "%d:" % ( self.hour % 12 )

      if self.hour < 12:

         standardTime += " AM"

      else:

         standardTime += " PM"

     

      print standardTime,

  

t1= clock()

t1.setTime(13,20,56)

//printing time in milatary format

t1.printMilitary()

t1.increment()

//printing time in standard format

t1.printStandard()

print t1.getTime()