Question

In: Computer Science

There are (at least) two ways in which you can make a 12-hour clock. One possibility...

There are (at least) two ways in which you can make a 12-hour clock. One possibility is to just store hour values from 1 to 12. On the other hand, you can simply leave the clock to work internally as a 24-hour clock but change the display string of the clock display to show4:23 or 4:23pm when the internal value is 16:23. Implement both versions. Which option is easier? Which is better? Why?

use python

Solutions

Expert Solution

Here is the completed code for this problem for both implementations. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks

#code

#a class representing a 12 hour clock
class Clock12Hour:
    #constructor taking hours, minutes and am/pm string
   
def __init__(self, hrs, mins, am_pm):
        #initializing values using setter methods
       
self.setHours(hrs)
        self.setMinutes(mins)
        self.setAMorPM(am_pm)
   
    #setter/mutator methods
   
   
def setHours(self, hrs):
        #validating hrs before assigning to hours
       
assert hrs >= 1 and hrs <= 12
        self.hours=hrs

    def setMinutes(self, mins):
        assert mins >= 0 and mins < 60
        self.minutes=mins

    def setAMorPM(self,am_pm):
        am_pm = am_pm.upper()
        assert am_pm == 'AM' or am_pm == 'PM'
       
self.am_pm = am_pm

    # getter/accessor methods
   
   
def getHours(self):
        return self.hours

    def getMinutes(self):
        return self.minutes

    def getAMorPM(self):
        return self.am_pm
   
    #returns a string in format hh:mm AM/PM
   
def __str__(self):
        return '{:02d}:{:02d} {}'.format(self.hours,self.minutes,self.am_pm)


#a class representing a 24 hour clock
class Clock24Hour:
    #constructor taking hours and minutes
   
def __init__(self, hrs, mins):
        #initializing values using setter methods
       
self.setHours(hrs)
       self.setMinutes(mins)
   
    def setHours(self, hrs):
        #ensuring that hrs is between 0 and 23
       
assert hrs >= 0 and hrs < 24
        self.hours = hrs

    def setMinutes(self, mins):
        assert mins >= 0 and mins < 60
        self.minutes = mins

    def getHours(self):
        return self.hours

    def getMinutes(self):
        return self.minutes
   
    #returns whether current time is AM or PM
   
def getAMorPM(self):
        if self.hours>=12:
            #afternoon
            
return 'PM'
       
else:
            #morning
           
return 'AM'
   
   
#returns a string in hh:mm AM/PM format
   
def __str__(self):
        #getting hours
       
hours=self.hours
        #initially assuming time is AM
       
am_pm='AM'
        
#if hours > 12, subtracting 12 from hours to convert to 12 hour system
       
if hours>12:
            hours=hours-12
            am_pm='PM'
       
#also, if hours is 12, it is PM
       
if hours==12:
            am_pm='PM'
       
#returning time in hh:mm AM/PM format
       
return '{:02d}:{:02d} {}'.format(hours, self.minutes, am_pm)



#creating objects of the two implementations and dispalaying time
c1=Clock12Hour(4,23,'pm')
c2=Clock24Hour(16,23)
#both c1 and c2 will display the same time
print(c1)
print(c2)
#storing time in 24 hour format is preferred, because it makes everything easy
#when we need to manipulate time, also it is not difficult to convert 24 hr clock
#into 12 hour system. one more benefit is that it's easier to sort time in 24 hr
#format than time in 12 hr format

#output

04:23 PM

04:23 PM


Related Solutions

In one hour Sonny can make 5 quarts of sauce or 20 meatballs. In one hour,...
In one hour Sonny can make 5 quarts of sauce or 20 meatballs. In one hour, Fredo can make 2 quarts of sauce or 12 meatballs. Suppose each person wants to have one quart of sauce whether he's cooking on his own or trading. If Sonny and Fredo specialize and trade according to comparative advantage, they will be able to produce which of the following combinations in total? A) Two quarts of sauce and 20 meatballs. B) Two quarts of...
Make a list of at least three ways you can influence clients to associate positive emotions...
Make a list of at least three ways you can influence clients to associate positive emotions with health promotion behavior. Explain the reasons for choosing these methods.
A)You have 12 songs on your ipod. In how many ways can you make a playlist...
A)You have 12 songs on your ipod. In how many ways can you make a playlist of 5 songs for your workout? Yes order matter; playing fight songs before thinder is totally different from thunder before fight song. B) the model if car you are thinking of buying is available in none different colors and three different styles(hatchback,sedan, or station wagon). In how many ways can you order the car?
Explain the working principle of 12- Hour digital clock using an electronic or block diagram.
Explain the working principle of 12- Hour digital clock using an electronic or block diagram.
2. Maya can produce 10 hamburgers in one hour or 12 milkshakes in ½ hour, while...
2. Maya can produce 10 hamburgers in one hour or 12 milkshakes in ½ hour, while Abe can produce 6 hamburgers in one hour or 5 milkshakes in ½ hour. We then know that: a.) Maya has absolute advantage and comparative advantage in making hamburgers b.) Abe has absolute advantage and comparative advantage in making milkshakes c.) Maya has absolute advantage and comparative advantage in making milkshakes d.) Abe has absolute advantage and comparative advantage in making hamburgers
In how many ways can a five horse race end, allowing for the possibility that two...
In how many ways can a five horse race end, allowing for the possibility that two horses tie?
Describe at least 3 ways in which you can improve the websites, give an example to...
Describe at least 3 ways in which you can improve the websites, give an example to explain each.
QUESTION 12 Which of the following is/are some(one) of the ways small businesses can obtain funding...
QUESTION 12 Which of the following is/are some(one) of the ways small businesses can obtain funding for their operations and purchases? A. Leasing instead of buying assets B. None of these. C. Having a credit card in the business's name D. Using retirement money in a 401(k) the entrepreneur has E. All of these. QUESTION 13 When entrepreneurs fail to put their own money into their business: A. They find it more difficult to get funding from investors B. They...
ECONOMICS QUESTION ONE [25] “The production possibility frontier measures the efficiency in which two commodities can...
ECONOMICS QUESTION ONE [25] “The production possibility frontier measures the efficiency in which two commodities can be produced together, helping managers and leaders to decide what mix of commodities is most beneficial.” In terms of the above statement, apply the basic economic model of the production possibility frontier, (PPF), to your country or a country of your choice, indicating how it illustrates the fundamental economic problem of scarcity, choice and opportunity cost. Include in your answer a discussion of the...
Explain at least two ways that the moment of inertia can be doubled.
Explain at least two ways that the moment of inertia can be doubled.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT