Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

There is more part and I will send you on text Part 2 (20 points): This is an improvement to class ClockOK. This part makes

There is more part and I will send you on text Part 2 (20 points): This is an improvement to class ClockOK. This part makes an improved version of ClockOK called ClockGOOD. Copy and paste the class ClockOK and name the new class to ClockGOOD. Now enhance the class ClockGOOD. Enhance the class by adding error checking. One of the advantages of encapsulating data within a class is that it can be made and maintained as valid. It can be sanity checked and kept correct and accurate. For example, valid hours are from 0 to 23; minutes from 0 to 59; seconds from 0 to 59. These rules can and should be enforced. In Part 2, enhance ClockGOOD in four ways: Part 2 enhancement 1 (5 points): Currently, the mutators (setHour, setMinute, setSecond) allow any values for arguments hr, min, sec. Invalid hr. min and sec values are accepted and copied into the internal class members hour, minute, second without regard to correctness. All values are accepted for hr, min, sec. They do not even have to be numbers! Enhance your class to avoid invalid values. Modify methods that set data members to check for valid values. Validate the input data (parameters) first. For example, ClockGOOD1 = ClockGOOD(34, -100, 4345) should NOT create a new instance of ClockGOOD1 with invalid data. Clearly, 34 is not a valid hour, -100 is not valid for minutes, 4345 is not valid for seconds. Enforce a policy to reject invalid data. If the client - the person using the class - attempts to set a ClockGOOD hour, minute or second to an invalid value, do not accept the invalid value! Leave the old, correct value in place. For example, setHour(-112233) should NOT change the hour, but setHour(15) should. You can assume a 0-23 hour clock, were 23 is 11pm. This policy works for changing hour, minute, second. But what if the client attempts to create a brand new ClockGOOD with invalid values? In this case, enforce a policy so that invalid values are NOT used. The policy is up to you. One policy might be to accept valid values, and use 0 for invalid values, or adjust invalid values to the nearest valie value. _( and all setters: setHour, setMinute, setSecond methods should be enhanced to prevent invalid data for hour, minute and second. Your constructor can call the setters to prevent duplicated code. Or, you can create a function that converts invalid data to valid data and call that function inside the constructor and setters. Part 2 enhancement 2 (5 points): The data attributes: hour, minute and second are currently exposed to the "outside world". That is, any code external to the ClockGOOD class can modify the data members. Example: watch.hour = 123 is allowed. This violates the concept of data hiding and allows the validation checks you just added to be bypassed. This is easy to fix. Change the names of all data members of the ClockGOOD class to begin with underscore-underscore (. You will now have: second. The makes them private so they are hidden. This "hides" the internal data members of the class inside a "black box". Now, only the class methods can access these private data members. They are now protected from contamination - outsiders making their values invalid. You may hear python programmers say "dunder init". This means . The term "dunder" refers to "double underscore". It is easier to say than *underscore underscore init underscore underscore". You will have class members: dunder init, hour, minute, second. These mean init hour, minute, second. Python reserved words have before and after, programmer defined variables have just at the start. Python has lots of dunders. Part 2 enhancement 3 (5 points): The method as described in Part 1 prints the data awkwardly: "hours=10, minutes=25, seconds=30" ". Better is: 10:25:30 AM. If the time is hours=20. minutes=55, seconds=42. print: 8:55:42 PM. If the hour is 0 to 11. that is AM. From 12 to 23 is PM. Adjust the str method to print in the nicer format. Hours 0:0 to 11:59 should be AM. Hours 12:00 to 23:59 should be PM. Part 2 enhancement 4 (5 points): When a clock "ticks", it advances by one second. Create a method tick which advances the time by one second: watch tick() increments by one second. Test your new ClockGOOD class with both valid and invalid data. The testing is important! Make sure to exercise all methods at least twice, to test on both valid and invalid data. This completes the regular parts of the lab. Put all code into one file and submit the file: DDHH_ L10. _Lastname_Clock.py. If you want to try the extra credit, you can save what you have so far as DDHH_L10_Lastname Clock_GOOD_ p2.py for safety and continue with extra credit. Part 3 (Extra credit: 5 points): This is an improvement to class ClockGOOD. This part makes an improved version of ClockGOOD called ClockBEST. Copy and paste the class ClockGOOD and name the new class to ClockBEST. Now enhance the class ClockBEST. Change ClockBEST to eliminate the hour and minute. Have only the second. Use the seconds to be any value 0 or more. Store the time inside class ClockBEST in seconds only. Let the seconds get higher and higher, such that 60 seconds is 1 minute, 3,600 seconds is 1 hour, 43.200 seconds is 12 hours, etc. To construct a new ClockBEST at 8:15:30, accept the hr. min, sec if valid, but then multiply the hr by 3600, the min by 60, and accept the sec. For example, 8:15:30 will be stored as a single large number: 8 * 3600 + 15 * 60 + 30. That is: 28,800 900 + 30 = 29,730. This simplifies the internal representation of time. To set the hour, the large value of seconds must be broken up into hours, minutes, seconds. Then the old minute replaced with the new minutes. Then the hour, minutes, seconds multiplied and added back up again, to be stored as one large number of seconds. This enhancement makes tick very easy. Seconds can be increased easily. There is no need to carefully synch hours, minutes and seconds every time the seconds go up a tick. Test ClockBEST by making several instances of ClockBEST. To the client using the class, it appears to work exactly the same as ClockGOOD. But internally, the time is stored as a large number of seconds instead of hours, minutes and seconds

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Mobile Communications

Authors: Jochen Schiller

2nd edition

978-0321123817, 321123816, 978-8131724262

More Books

Students also viewed these Programming questions