Using something similar to the MyTime class from Chapter #21 of the How to Think
ID: 3790471 • Letter: U
Question
Using something similar to the MyTime class from Chapter #21 of the How to Think Like a Computer Scientist online book, create a class that in addition to the hour, minutes, and seconds property, also contains an am pm property that denotes the whether the time is in the am or the pm: Your code should contain the multiple methods as specified below: An init method that allows you to create a time object with default values A str method that allows you to print the values of a time object Accessor methods that return the value of the properties MuMoj. methods that allow you modify the values of the properties Provide code that demonstrates the correctness of your program. Create a class called RgalRange. that can be used to store a real number between a specified range of values. The class should contain three properties Your code should contain the following five methods: An init method that initialize the real number to a value between the lower_bound and upper_bound values, inclusively. This constructor must receive three valid real numbers as arguments. If any of the values provided are invalid in any way, the constructor should initialize the value to 0, the lower bound to 0, and the upper bound to 1. A str method that allows you to print the value of the real number an accessor method that returns the value of the real number A nutator method that allow you to directly modify value of the real number by assignment. This method should return a Boolean value of True if the assignment is successful. If the specified assignment is invalid, the method should return False and leave the value unchanged. A nutator method that allow you to modify value of the real number by getting input from the user. This method should continually. prompt the user for valid input and should only return when the assignment is successful. Provide code that demonstrates the correctness of your program.Explanation / Answer
first program
class MyTime:
def __init__(self,hrs=0,mins=0,secs=0):
self.hours=hrs
self.minutes=mins
self.seconds=secs
self.am_pm=''
total_seconds = hrs*3600 + mins*60 + secs
self.hours = total_seconds // 3600
balance_seconds = total_seconds % 3600
self.minutes = balance_seconds // 60
self.seconds = balance_seconds % 60
def __str__(self):
if self.hours > 12:
self.am_pm='PM'
else:
self.am_pm='AM'
return "The time is %s hours %s minutes %s seconds %s " % (self.hours,self.minutes,self.seconds,self.am_pm)
def get_hours(self):
return self.hours
def get_minutes(self):
return self.minutes
def get_seconds(self):
return self.seconds
def set_hours(self,hours):
self.hours=hours
def set_minutes(self,minutes):
self.minutes+=minutes
while self.minutes > 60:
self.minutes-=60
self.hours+=1
def set_seconds(self,seconds):
self.seconds+=seconds
while self.seconds >60:
self.seconds-=60
self.minutes+=1
if __name__=="__main__":
ob=MyTime(10,10,10)
print(ob)
ob2=MyTime(13,10,10)
print(ob2)
print("getting hours " +str( ob.get_hours()))
print("getting minutes " +str( ob.get_minutes()))
print("getting seconds " +str( ob.get_seconds()))
ob.set_minutes(79);
print("now the time is after adding minutes "+ str(ob))
second program
import random
class RealRange:
def __init__(self,value=0,lower_bound=0,upper_bound=1):
self.value=value
self.lower=lower_bound
self.upper=upper_bound
self.value=random.uniform(self.lower,self.upper+1)
print(self.value)
def __str__(self):
return "The real value is %s" % self.value
def get_value(self):
return self.value
def set_value(self):
while True:
v=float(input("enter any real number "))
if isinstance(v,float):
if v >= self.lower and v <= self.upper:
self.value=v
return True
return False
if __name__=="__main__":
ob=RealRange(1,3,4)
print(ob)
b=ob.set_value()
print(b)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.