4003-231: CS1 Homework Programs #2 Due: Thur. week 6 CONDITIONAL STATEMENTS (if & switch) ======================== Grading Policy ================================ Each question below has a number of points associated with it, you are required to turn in 3 points worth of problems. ===================== Things to Turn in ================================ You should turn in a printout of your program (make sure you name and the homework number is at the top. In addition, you should turn in a printout of the running of your program. To get a file that contains the output of everything that is printed on the screen (in Unix) you can use the "script" command. For information on how to use the command type man script on the Unix command line. When providing output from your program you should run your program multiple times, with a couple of the runs using data not provided in the examples. ===================== Homework Programs #2 ============================= 1. (1 pt) Write a program that takes a number from the user and prints out the time. Have the number entered as a muti-digit number with the lower 2 digits being the minutes, and the upper 2 digits being the hours. So: 1020: is 10:20am 2202: is 10:02pm 523: is 05:23am 015: is 12:15am You should check the number to make sure it is a legal time (hours less than 23 and minutes less than 59). You should use the hour 0 as midnight. 2. (2 pts) Write a program that asks the user to enter a time, and then prints a message of the time in fuzzy words. This should include things like: * if the minutes of the time is 15, then you should say its quarter past * if minutes are 30, then call it half past * if minutes are 45, then call it quarter to the next hour * if the time is after 9pm and before 5am say is "at night" * if the time is before noon, then say "in the morning" * if the time is after noon then say its "in the afternoon: * if the time is after 5pm say is "in the evening" * 12:00 is noon, 0:00 is midnight You might want to go a little more fuzzy and do "almost" or "just past" so 17min after the hour would be "just past quarter after". Have the user enter time in the 24hour clock format. So if the user enters 1017 you could print "its just past quarter after 10 in the morning". Other examples: 2250: its 10 to 11 at night 700: its 7 in the morning 702: its just past 7 in the morning 1902: its just past 7 in the evening 1230: its half past noon 2345: its quarter to midnight 3. (2 pts) Write a program that takes the time from the user and writes it out in Roman numerals. The Roman numerals you need are: I = 1 V = 5 X = 10 L = 50 You can place up to 3 of 1's (or 10's) digits in a row to represent values up to 3 (or 30). To represent 4 (or 40) you use a subtraction method by placing a lower value numeral in front of a higher value one (normally numbers are written with the highest value digit first), so 4 is "IV", 40 is "XL" and 49 is "XLIX" (10 from 50, plus 1 from 10) Some other examples: 1015: X:XV 2312: XI:XII 650: VI:L 449: IV:XLIX 813: VIII:XIII 4. (+1 pt to previous total) Modify any of the above problems to move the printing code into a method. Here you can either pass the time to the method, or just have the method ask the user for the time. Once the code is in the method, you will need to call the method from the main program. 5. (3 points) write a program that will print out the day of the week of any date from 1752 to 2299 (in 1752 the calendar switched over from the old Julian calendar to the new Gregorian calendar, and dates got messed up from that). To compute the day of the week use the formula: 0) start with the date in the form dd, mm, yyyy 1) take the last 2 digits of the year and multiply it by 1.25 dropping any fraction 2) add the corresponding Century code to this 1700's add 5 1800's add 3 1900's add 1 2000's add 0 2100's subtract 2 2200's subtract 4 NOTE: For this method we have to consider a '00' year as part of the new century 3) For Leap years: subtract 1 from the total if the month is January or February. Remember that leap years are not always every 4 years. There are exceptions. Years that end in 00 are not leap years unless it is a multiple of 400. Therefore 1700, 1800, 1900, and 2100 are not leap years, but 2000 is. 4) Add in the month code where: Month: Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec code: 6 2 2 5 0 3 5 1 4 6 2 4 5) Add in the day 6) get the remainder of dividing this total by 7. This is the day of the week (0=Sun, 1=Mon, 2=Tue, 3=Wed...) Credits for the formula go to Guy Rimmer at: http://www.terra.es/personal2/grimmer/): ######################### Advanced Question ########################### Note the following question goes beyond what we covered in class, I present it here as just something for people who want more of a challenge. If you can't do this problem at the moment, don't worry, we have not gone over writing the code inside a class, so don't get discouraged. ######################### Advanced Question ########################### 6. (5 pts) Write a class that represents the concept of time, and provides all (or some) of the printing methods above. You should make the class hold a time in private data values, and should provide methods that return the strings for displaying the time. Once you have the Time class you should write a main program that creates a time object, then set the value for the time object (here you can use constructors if you are ambitious, or just make a method inside time that asks the user for the time and sets the data values). and finally call the print method(s) on the object.