Question

In: Computer Science

PYTHON ASSIGNMENT Problem: (1) The  __init__ method should initialize the values of the instance variables. Here is...

PYTHON ASSIGNMENT

Problem:

(1) The  __init__ method should initialize the values of the instance variables. Here is the beginning of __init__:

def __init__(self, the_hr, the_min, the_sec):
    self.hr = the_hr
    # Also initialize min and sec.

(2) Include a __str__ method that returns the current state of the clock object as a string. You can use the string format method format like this:

return "{0:02d}:{1:02d}:{2:02d}".format(self.hr, self.min, self.sec)

(3) When the tick method is executed, add one to sec. If the resulting value of sec is 60, set it back to 0 and update min and hr if necessary. Here is the beginning of the tick method:

def tick( ):
    self.sec += 1
    if self.sec == 60:
        self.sec = 0
        self.min += 1
  
    # Also check if self.min == 60 and 
    # and if self.hr == 0.

(4) Write testing code in test1.py to test your class. Use this import statement at the top:

from clock import Clock

(5) Also write a unit test file (test2.py file) for all of the methods in your Clockclass using assert statements.

(6) If you have time, add yr, mon, and day instance variables, modify the tickmethod to accomodate them.

I need the answer to 5 and 6

Solutions

Expert Solution

Clock.py:

Raw_code:

class Clock:
def __init__(self, the_hr, the_min, the_sec):
self.hr = the_hr
self.min = the_min
self.sec = the_sec

def __str__(self):
return "{0:02d}:{1:02d}:{2:02d}".format(self.hr, self.min, self.sec)

def tick(self):
self.sec += 1
if (self.sec == 60):
self.min += 1
self.sec = 0
if (self.min == 60):
self.hr += 1
self.min = 0
return self.__str__()

test1.py:

Raw_code:

from clock import Clock

#creating 3 Clock objects and calling tick method and printing clock object
c1 = Clock(1, 20, 22)
c1.tick()
print(c1)

c2 = Clock(1, 30, 59)
c2.tick()
print(c2)

c3 = Clock(1, 59, 59)
c3.tick()
print(c3)

test2.py:

Raw_code:

from clock import Clock
# creating 3 class Object and using assert statement
c1 = Clock(1, 20, 22)
c1.tick()
# assert, comparing string with return type of __str__ method of clock object
assert "01:20:23" == c1.__str__()
# comparing string with return type of tick() method of clock object,
# tick method changes data variables and returns the resultant string
assert "01:20:24" == c1.tick()

c2 = Clock(1, 30, 59)
c2.tick()
assert "01:31:00" == c2.__str__()
assert "01:31:01" == c2.tick()

c3 = Clock(1, 59, 59)
c3.tick()
assert "02:00:00" == c3.__str__()
# failing assert statement for testing
assert "02:00:00" == c3.tick()
#here the return value tick() method is "02:00:01"

Clock class after adding year, month, day:
clock2.py:

Raw_code:

class Clock:
#intializing required data variables
def __init__(self, yr, mon, day, the_hr, the_min, the_sec):
self.yr = yr
self.mon = mon
self.day = day
self.hr = the_hr
self.min = the_min
self.sec = the_sec
def __str__(self):
return "{0:4d}-{1:02d}-{2:02d}; {3:02d}:{4:02d}:{5:02d}".format(self.yr, self.mon, self.day, self.hr, self.min, self.sec)

def tick(self):
self.sec += 1
if (self.sec == 60):
self.min += 1
self.sec = 0
if (self.min == 60):
self.hr += 1
self.min = 0
self.sec = 0
# if hours == 24 incrementing day
if (self.hr == 24):
self.day += 1
self.hr = 0
self.min = 0
self.sec = 0
# incrementing month as per condition required for month
# if it is eight month
if (self.mon == 8):
#since, day is incremented in above if statement
# so if the eigth month days is 32, month is incremented
if (self.day == 32):
self.mon += 1
self.day = 1
self.hr = 0
self.min = 0
self.sec = 0
# if the month is second month, checking leap year condition
elif (self.mon == 2):
if (self.yr % 4) == 0:
if (self.yr % 100) == 0:
if (self.yr % 400) == 0:
if(self.day == 30):
self.mon += 1
self.day = 1
self.hr = 0
self.min = 0
self.sec = 0
else:
if(self.day == 29):
self.mon += 1
self.day = 1
self.hr = 0
self.min = 0
self.sec = 0
else:
if(self.day == 30):
self.mon += 1
self.day = 1
self.hr = 0
self.min = 0
self.sec = 0
else:
if(self.day == 29):
self.mon += 1
self.day = 1
self.hr = 0
self.min = 0
self.sec = 0
# if the month is divisible by 2 and days are 31(last day - 30), incrementing month
elif (self.mon % 2 == 0):
if (self.day == 31):
self.mon += 1
self.day = 1
self.hr = 0
self.min = 0
self.sec = 0
# if the month is not divisible by 2 and days are 32(last day -31), incrementing month
else:
if (self.day == 32):
self.mon += 1
self.day = 1
self.hr = 0
self.min = 0
self.sec = 0
# if month is 12th month , incrementing year
if (self.mon == 12):
self.yr += 1
self.mon = 1
self.day = 1
self.hr = 0
self.min = 0
self.sec = 0

test3.py:

Raw_code:

from clock2 import Clock

# normal month with 30 day
c1 = Clock(2000, 4, 30, 23, 59, 59)
c1.tick()
print(c1, end = "\n\n")

# normal month with 31 day
c2 = Clock(2000, 1, 31, 23, 59, 59)
c2.tick()
print(c2, end = "\n\n")

# 2 month in leap year
c3 = Clock(2000, 2, 28, 23, 59, 59)
c3.tick()
print(c3, end = "\n\n")

# 2 month in leap year
c4 = Clock(2000, 2, 29, 23, 59, 59)
c4.tick()
print(c4, end = "\n\n")

# 2 month of not leap years
c5 = Clock(2006, 2, 28, 23, 59, 59)
c5.tick()
print(c5, end = "\n\n")

# 12 month
c6 = Clock(2010, 12, 31, 23, 59, 59)
c6.tick()
print(c6, end = "\n\n")


Related Solutions

By using Python: a. Make a class called Book. The __init__() method for Book should store...
By using Python: a. Make a class called Book. The __init__() method for Book should store two attributes: a title and an author. Make a method called book_info() that prints these two pieces of information, and a method called book_available() that prints a message indicating that the book is available in the library. b. Create an instance called book1 from your class. Print the two attributes individually, and then call both methods. c. Create three different instances from the class,...
This is python #Create a class called Rectangle. Rectangle should #have two attributes (instance variables): length...
This is python #Create a class called Rectangle. Rectangle should #have two attributes (instance variables): length and #width. Make sure the variable names match those words. #Both will be floats. # #Rectangle should have a constructor with two required #parameters, one for each of those attributes (length and #width, in that order). # #Rectangle should also have a method called #find_perimeter. find_perimeter should calculate the #perimeter of the rectangle based on the current values for #length and width. # #perimeter...
Create three 8-bit variables in the “.text” section and initialize it to any values you like....
Create three 8-bit variables in the “.text” section and initialize it to any values you like. Load three registers (R1, R2, and R3) using the initialized values. Now, add the values of register R2 and R3 and store the result on the stack. Multiply the content of R1 and top of the stack, and again store the result on to the stack. Make sure to use appropriate instructions. (ARM assembly language)
Solve the LP problem using graphical method. Determine the optimal values of the decision variables and...
Solve the LP problem using graphical method. Determine the optimal values of the decision variables and compute the objective function. Maximize Z = 2A + 10B Subject to 10A + 4B ≥ 40    A + 6B ≥ 24                A + 2B ≤ 14    A, B  ≥ 0 with soln pls thank you!
#Write a class called "Burrito". A Burrito should have the #following attributes (instance variables): # #...
#Write a class called "Burrito". A Burrito should have the #following attributes (instance variables): # # - meat # - to_go # - rice # - beans # - extra_meat (default: False) # - guacamole (default: False) # - cheese (default: False) # - pico (default: False) # - corn (default: False) # #The constructor should let any of these attributes be #changed when the object is instantiated. The attributes #with a default value should be optional. Both positional #and...
PYTHON 1- First, initialize the constant NUMS as 3: NUMS=3 2- Print the title of the...
PYTHON 1- First, initialize the constant NUMS as 3: NUMS=3 2- Print the title of the application. >---=== Python Temperature Analyzer ===---< 3- Using a for loop, prompt the user to enter the high and low values for each of NUMS days. The values entered must be between -40 and 40, and high must be greater than low. Print the following messages: > * Read the high value. > Enter the high value for day 1: < (or day 2,...
Task 1: Implement the constructor for the Hostess struct. It must initialize all member variables, including...
Task 1: Implement the constructor for the Hostess struct. It must initialize all member variables, including any arrays. There is another function that performs initialization. You must change the program so that the object’s initialization is done automatically, i.e. no explicit call is needed for an initialization function. #ifndef HOSTESS_H #define HOSTESS_H #include "Table.h" #include <iostream> struct Hostess{ void Init(); void PlaceArrivalInQueue(int arrivals); void ShiftLeft(); void Print(int hour, int minute, int arrivals); int GetNumTables(){return numTables;} int Seat(int table, int time);...
(java) Write a class called CoinFlip. The class should have two instance variables: an int named...
(java) Write a class called CoinFlip. The class should have two instance variables: an int named coin and an object of the class Random called r. Coin will have a value of 0 or 1 (corresponding to heads or tails respectively). The constructor should take a single parameter, an int that indicates whether the coin is currently heads (0) or tails (1). There is no need to error check the input. The constructor should initialize the coin instance variable to...
1. Implement a public method named initialize. It takes a twodimensional square array of integers...
1. Implement a public method named initialize. It takes a two dimensional square array of integers namedarray as a parameter. It initializes all of the elements of the array to the sum of their indices except for themajor diagonal (upper left to lower right) where each element is initialized to -1. (For testing use a 4X4 or5X5 array and have the application print out the array in 2 dimension format.2. Implement a method named totals that takes a two dimensional...
1) Dynamic Allocation (c++) a. Create two integer variables x and y, initialize them with different...
1) Dynamic Allocation (c++) a. Create two integer variables x and y, initialize them with different values. b. Use dynamic memory allocation, declare px and py as address of x and y separately. c. Print out x, y, px, py, &x, &y, *px, *py.   d. Let py = px, and *py = 100 e. Print out x, y, px, py, &x, &y, *px, *py. g. Print out *px++, x, px
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT