Question
help with cse problem
Lab Exercise #11 Assignment Overview You will work with a partner on this exercise during your lab session. Two people should
D-date.Date O D.from mdyMarch 15, 2015) printD print D.to isoO print D.to mdy printD.is_valid)) print ) E date.Date print E
2. Save the file containing your class as clock. py, then experiment with your module in the ipython shell >>>import clock
c. Add a doc string to the constructor documenting the purpo se of the function 8. Save the file containing your class as
Lab Exercise #11 Assignment Overview You will work with a partner on this exercise during your lab session. Two people should work at one computer. Occasionally switch the person who is typing. Talk to each other about what you are doing and why so that both of you understand each step Part A: Class Date . Download the files for this laboratory exercise, then run the Python shell and enter the following commands: >>>import date >>help( date) >> help( date.Date >>> x-date . Date() >>>print(x 2. Consider the Python statements shown below (from the file named "labllap. Using the output produced by the commands above, predict what the output will be, then test your predictions by executing the statements import date A-date.Date (i, 1, 2014) print A) print A.to iso0 print A.to mdy)) print A.is_valid)) print o B date.Date( 12, 31, 2014) print B print B.to iso0 print B.to mdy print B.is valid) print 0 C date. Date ( ) C.from iso( "2014-07-04" ) print( c) print( c.to iso0 print ( С.to.mdy() ) print( c.isvalid) print o
D-date.Date O D.from mdy"March 15, 2015") printD print D.to isoO print D.to mdy printD.is_valid)) print ) E date.Date print E printE.to isoO printE.to mdy print E.is_valid print ) 3. Revise the program to test the behavior of the member functionnwhen one or more of the arguments to that function are erroneous. That is, test a date such as 13/40/2017 4. Revise the program to test the behavior of the member functions "from iso and "from mdy when the arguments to those functions are strings which contains spaces. 5. Revise the program to test the behavior of the member functions "from iso" and "from mdy" when the arguments to those functions are erroneous. Demonstrate your completed program toy our TA. On-line students should submit the completed program (named "labi la.ру") for grading via the Mimir system. (Note that there are no automatic tests for this program your TA will grade it by hand.) Part B: Class Time You will develop a new data type to model the time on a 24 hour clock, where the three data members represent the hour, the minutes and the seconds. Use the date-py module as a guide. It will be named "class Time", and each object of that type will have three instance variables hour" mins" and" secs"). The module will be named "clock.py 1. Develop the function member (method) namedinwhich will be used to initialize an object oftype Time" when it is created. That function will receive four parameters: a reference to the current object (self"), the hour, the minutes and the seconds a. Assume that the last three of the four parameters are integers. b. Use a default value of0 for those last three parameters. c. Assign those last three parameters to the three instance variables. d. Add a "doc string" to the constructor documenting the purpose of the function
2. Save the file containing your class as "clock. py", then experiment with your module in the ipython shell >>>import clock >>help( clock) >>>help( clock.Time >>> A-clock·Time() >>print( A 3. Develop the function member named" repr"which will be used to display the formal representation of an object of type Time" in the Python shell. a. Return a string with the fomat "Class Time: hh:mm:ss" b. Be sure to place leading zeroes in the string for the hour, minutes and seconds c. Add a "doc string" to the constructor documenting the purpose of the function. Save the file containing your class as "clock.py", then experiment with your module in the iPython shell >mport clock >>A -clock.Time >>B-clock.Time (7,12,3) >>> B should display: Class Time: 07:12:03 5. Develop the function member named" str"which will be used to display a human-readable representation of an object of type Time" in Python programs: Return a string with the format "hh:mm:ss". b. a. Be sure to place leading zeroes in the string for the hour, minutes and seconds. Add a "doc string" to the constructor documenting the purpose of the function. c. 6. Save the file containing your class as "clock.py", then experiment with your module in the iPythorn shell >>import clock >>A -clock.Time >print( A >>B-clock.Time (7,12,3) >>>print( B 7. Develop the function member named "from str" which will be used to update an object of type Time" after it has been created. That function will receive two parameters a reference to the current object (self") and a string of the form "hh:mmss" Assume that the string parameter can be split into three integers. That is, assume that the parameter is in the format specified no errors. a. b. Assign those three integers to the three instance variables
c. Add a "doc string" to the constructor documenting the purpo se of the function 8. Save the file containing your class as "clock.py", then experiment with your module in the iPython shell >import clock >>>A-clock.Time () >>>print A) >A.from str('07:12:03' >>print( A 9. We provide the Python program named "clockDemo.ру" that demonstrates the use of your class Time". Test your class using clockDemo.py 10. Create a new file named "lab11b.py" which contains the source code from the files named "clock .ру" and "clockDemo.ру". That is, create a file which contains both the definition of class Time" and the demonstration program. The class definition should be at the top of the file. To create that "stand alone" file you will need to remove (or comment out) the "import clock" line (you don't need to import t because it is already in the file) Also, since you have not imported "clock" you need to change "clock.Time" to Time". Test that your file is working before submitting. Demonstrate your completed program to your TA. On-line students should submit the completed program (named "labb.py") for grading via the Mimir system. Optional: If you have the time and interest, develop the function member named "add times" which will receive two objects of type Time" and return a new object of type "Time". The new object will represent the sum of the two parameters. Use modulo arithmetic to handle the addition of the seconds, minutes and hours. If the sum of the seconds exceeds 60, the minutes should be increased by 1. Similarly, if the sum of the minutes exceeds 60, the hour should be increased by 1. Note that we are not keeping track of days, so if the sum of the hours exceeds 24, you should ignore the "carry". Revise your program to demonstrate the use of "add times". Call the new program l your TA know that you have done the extra work labllc.py and let
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Thanks for the question. Here is the code for Part B. Also the bonus function has been implemented.

(thanks and please a thumbs up !)

====================================================================================

class Time():

    def __init__(self,hour=0,minutes=0,seconds=0):
        self.__hour=hour
        self.__mins=minutes
        self.__secs=seconds

    def __repr__(self):
        secs=mins=hour='0'
        if self.__hour<10:
            hour+=str(self.__hour)
        else:
            hour=str(self.__hour)
        if self.__mins<10:
            mins+=str(self.__mins)
        else:
            mins=str(self.__mins)
        if self.__secs<10:
            secs+=str(self.__secs)
        else:
            secs=str(self.__secs)
        return 'Class Time: {0}:{1}:{2}'.format(hour,mins,secs)

    def __str__(self):
        secs=mins=hour='0'
        if self.__hour<10:
            hour+=str(self.__hour)
        else:
            hour=str(self.__hour)
        if self.__mins<10:
            mins+=str(self.__mins)
        else:
            mins=str(self.__mins)
        if self.__secs<10:
            secs+=str(self.__secs)
        else:
            secs=str(self.__secs)

        return '{0}:{1}:{2}'.format(hour,mins,secs)

    def from_str(self,time):
        time=time.split(':')
        self.__hour=int(time[0])
        self.__mins=int(time[1])
        self.__secs=int(time[2])
    @classmethod
    def add_times(self,t1,t2):
        hours=t1.__hour+t2.__hour
        mins=t1.__mins+t2.__mins
        secs=t1.__secs+t2.__secs
        mins+=secs//60
        hours+=mins//60
        hours=hours%60
        mins=mins%60
        secs=secs%60
        return Time(hours,mins,secs)


A=Time(7,12,3)
print(A)
A.from_str('07:12:03')
print(A)
B=Time(1,48,57)
print(Time.add_times(A,B))

=====================================================================================

Add a comment
Know the answer?
Add Answer to:
Lab Exercise #11 Assignment Overview You will work with a partner on this exercise during your la...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Can someone please help me with the following java assignment. So in this assignment you will...

    Can someone please help me with the following java assignment. So in this assignment you will begin with the starterProject I provide and then add to it.  I am also asking that before you begin you really look at the starterProject because this is the object-oriented basics. The starterProject is the exact same as the ClassClockExample.zip in Module 5.  I’ve also done this using a clock because it is something that you already know how it works.  Remember that OOD and OOP is...

  • Exercise 11 - in Java please complete the following: For this exercise, you need to work...

    Exercise 11 - in Java please complete the following: For this exercise, you need to work on your own. In this exercise you will write the implementation of the pre-written implementation of the class CAR. The class CAR has the following data and methods listed below: . Data fields: A String model that stores the car model, and an int year that stores the year the car was built. . Default constructor . Overloaded constructor that passes values for both...

  • CSCI 161 - Lab 2: "Give Me a Second!" Overview This lab will have you developing...

    CSCI 161 - Lab 2: "Give Me a Second!" Overview This lab will have you developing a program that asks the user to input a time period as a number of seconds and in turn calculates the whole number of days, hours and minutes that are contained within the entered time period. Refer to the "Output Format" below for an example of what your program must do. The objectives of this lab are to reinforce and help teach: proper Java...

  • This assignment uses functions, files, and strings. Enough flexibility is provided for you to apply your...

    This assignment uses functions, files, and strings. Enough flexibility is provided for you to apply your knowledge of basic C++ programing to develop your solution. Develop a functional flowchart and then write a C++ program to solve the following problem. 1. Create a text file named file1.txt and write your brand of car (like Honda, Toyota, etc) in the file. You will be reading the name of the file from the keyboard as a string, using the string class. Your...

  • IN PYTHON Assignment Overview This assignment will give you experience on the use of classes. Understand...

    IN PYTHON Assignment Overview This assignment will give you experience on the use of classes. Understand the Application The assignment is to first create a class calledTripleString.TripleStringwill consist of threeinstance attribute strings as its basic data. It will also contain a few instance methods to support that data. Once defined, we will use it to instantiate TripleString objects that can be used in our main program. TripleString will contain three member strings as its main data: string1, string2, and string3....

  • Assignment Overview In Part 1 of this assignment, you will write a main program and several...

    Assignment Overview In Part 1 of this assignment, you will write a main program and several classes to create and print a small database of baseball player data. The assignment has been split into two parts to encourage you to code your program in an incremental fashion, a technique that will be increasingly important as the semester goes on. Purpose This assignment reviews object-oriented programming concepts such as classes, methods, constructors, accessor methods, and access modifiers. It makes use of...

  • please do a and b Lab Exercise 9 Assignment Overview This lab exercise provides practice with...

    please do a and b Lab Exercise 9 Assignment Overview This lab exercise provides practice with dictionaries of lists and sets in Python. A. Write a program using Dictionaries of lists Consider the file named "lab9a.ру" Given two files named exactly continents. txt and cities.txt (no error checking of the file name is needed) of continents, countries and cities. Write a program to read continents, countries and their cities, put them in a nested dictionary and print them (no duplicates...

  • 3. write a c++ program: Design and implement a class called Clock that describes the time...

    3. write a c++ program: Design and implement a class called Clock that describes the time of a clock: a) Include in the class 3 constructors with one, two, and three parameters to set the hours, the minutes, and the seconds, respectively. b) Include also a default constructor that sets the member variables of the class to 00:00:00. c) Write a member function to increment the time by a given amount, and second member function to reset the clock. The...

  • Please use my Lab 3.2 code for this assignment Lab 4.2 instruction Using the code from...

    Please use my Lab 3.2 code for this assignment Lab 4.2 instruction Using the code from lab 4.1, add the ability to read from a file. Modify the input function:       * Move the input function out of the Cargo class to just below the end of the Cargo class       * At the bottom of the input function, declare a Cargo object named         temp using the constructor that takes the six parameters.       * Use the Cargo output...

  • // Enter your name as a comment for program identification // Program assignment testEmployeeAB.cpp // Enter...

    // Enter your name as a comment for program identification // Program assignment testEmployeeAB.cpp // Enter your class section, and time /* The program testEmployeeAB.cpp tests the class Employee. The class Date is included so that the Employee class can use the Date data type. */ /* Data is entered to create an employee data file. */ /* A payroll report and equal employment opportunity report showing ethnicity data is displayed. */ //header files /* use the correct preprocessor directives...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT