Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

In this assignment, you will create a Time class which will store, well, time. It should be able to store the current time and display

image text in transcribed

In this assignment, you will create a Time class which will store, well, time. It should be able to store the current time and display it in both 12-hour format (HH:MM:SS AM/PM, where 1

The constructor should take three arguments: the hour, the minute, and the second. Hour should be an int in the range 0-23 (0=midnight, 1=1AM, ..., 12=noon, 13=1PM, ..., 23=11PM). Minute and second should both ints be in the range 0-59. If an invalid hour, minute, or second is supplied, the class should raise a ValueError with a useful error message.

All internal variables should be private. I should be able to access them via getters. They should be called getHour(), getMinute(), and getSecond().

There should be no publicly accessible setters (e.g. setMinute(new_minute)). The only way to directly set the time should be via the constructor.

We need a class of functions that allow us to increment the hour, minute, or second by a given amount. These should be called incrementHour(), incrementMinute(), and incrementSecond(). When adding time, make sure to wrap around. For example, if the current time in 24-hour format is 23:59:59 and we call incrementSecond(5), the time should become 0:00:04.

Hint: you can specify the number of digits to print by appending :0N after your variable name in the formatted string, where N is the number of digits to print.

 
 

x=3

 

y=12

 

 

f'{x:04}, {x:02}, {y:02}' == '0003, 03, 12'

The increment functions should return the object after doing whatever internal modifications it needs (i.e. end in return self). This allows us to chain calls to the object as follows:

 
 

Time(12,0,0).incrementHour(2).incrementSecond(15).format12Hour()

 

=> '2:00:15 PM'

We also need two functions to print the time: format12Hour() and format24Hour(). They should return strings in the format specified at the top of this document. The hour should not be left-padded with a zero but the minutes and seconds should be. That is, 0:24:56 and 8:03:05 PM are correct but 10:5:3 and 08:23:09 PM are not.

Example code:

 
 

t = Time(11,50,12) t.format12Hour() == '11:50:12 AM' t.format24Hour() == '11:50:12'

 

 

t.incrementHour(3)

 

t.getHour() == 14

 

 

t.incrementMinute(10)

 

t.format12Hour() == '3:00:12 PM'

 

t.format24Hour() == '15:00:12'

 

 

t.incrementMinute(125)

 

t.getHour() == 17

 

t.getMinute() == 5

 

t.setSecond() == 12

 

 

t2 = Time(85,9,12) ==> Raises ValueError

back to claseroom Instructians from your teacher Time Class This assignment is worth double (10 points) nthis assigznent, you wall create a Time class whuch will store, well, tume. It should be able to store the curreant time and dasplay it in hoch 13-hour format(HH MM. SS AMPM where l HH12) and 24-hour former(HH MM. SS, where.HH 23) The 1-1 AM, . . . I 2-noon. 13-i PM , 23-11 PM). Minute and second should both ints be in the range 0-59. If an invalid bour, minute, or aesandk silipplied, the class should raise a ValueError with a itseful error message All interaal variables should be private. I should be able to access them via geters. They should be called gctllour(), getMinute), and gotsecand) There should be no publicly accessable setters (e.g. setMinute(nen minute)). Tbe only way to durectly set the time should be via the We need a class of fiuecthoms thst uliow us to mcrement the bur, mue, ut second by a vemouant. These should be called increnentHour), incrementMinute), and incrementSecond). When adding tune, make sure to wrap around. For example, if the carrent time in 24 bour focmst is 23:59:59 and we call increnentSccond(5), the tume should become 8:03:84 mumber of digits to peint 12 fx:84,x:021, ty:823893, 93, 12 The incrcment flanction shnonjeet after doimg whatever intemal madificationa it nerds(Ge end im return splf) rython 3.6.1 (delault, Dec 2815, 13:85:11) Thi allows us to chain calls to the object as follows 2:80:15 PN We also need two supctious to prist the time: format12Haur() and format24Hour). They should return strings in the format 0:24:56 and 8:03:85 PN are uzect but 10:5:3 md 98:23:09 PH are ot Example code t-Tine11,59,12) back to claseroom Instructians from your teacher Time Class This assignment is worth double (10 points) nthis assigznent, you wall create a Time class whuch will store, well, tume. It should be able to store the curreant time and dasplay it in hoch 13-hour format(HH MM. SS AMPM where l HH12) and 24-hour former(HH MM. SS, where.HH 23) The 1-1 AM, . . . I 2-noon. 13-i PM , 23-11 PM). Minute and second should both ints be in the range 0-59. If an invalid bour, minute, or aesandk silipplied, the class should raise a ValueError with a itseful error message All interaal variables should be private. I should be able to access them via geters. They should be called gctllour(), getMinute), and gotsecand) There should be no publicly accessable setters (e.g. setMinute(nen minute)). Tbe only way to durectly set the time should be via the We need a class of fiuecthoms thst uliow us to mcrement the bur, mue, ut second by a vemouant. These should be called increnentHour), incrementMinute), and incrementSecond). When adding tune, make sure to wrap around. For example, if the carrent time in 24 bour focmst is 23:59:59 and we call increnentSccond(5), the tume should become 8:03:84 mumber of digits to peint 12 fx:84,x:021, ty:823893, 93, 12 The incrcment flanction shnonjeet after doimg whatever intemal madificationa it nerds(Ge end im return splf) rython 3.6.1 (delault, Dec 2815, 13:85:11) Thi allows us to chain calls to the object as follows 2:80:15 PN We also need two supctious to prist the time: format12Haur() and format24Hour). They should return strings in the format 0:24:56 and 8:03:85 PN are uzect but 10:5:3 md 98:23:09 PH are ot Example code t-Tine11,59,12)

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

More Books

Students also viewed these Databases questions