iOS Development

Course No. 10-152-139

Assignment 14-1

Objects!

  • Create a new Xcode command-line project. Name the project Assignment-14-1.
    • The Type of the project must be Foundation.
  • Write a program that determines on what day of the week every 21st century leap day occurs.
    • It will output a line for each found leap year with the NSLog function.
    • It will then output a line with the execution time of the program.
  • Your program must:
    • Calculate which years from 2000 to 2100 are leap years.
    • For each leap year, determine the day of the week on which it occurs.
    • Use the following Objective-C classes or functions to complete the program.
      • NSDate
      • NSCalendar
      • NSDateComponents
      • NSLog
  • Strong recommendations:
    • Use several functions, at least one for each task. More is OK and is usually better.
  • Sample output:
    • Please Note: This is not real data. It is also not the correct number of years.

Project Notes:

  • What you’ll need to know and do:
    • You will need to understand and use the code from the Challenge on page 80.
  • Here’s the algorithm for getting the weekday.
    • Create an instance of the NSCalendar class.
    • Create an instance of the NSDateComponents class.
    • Set the month of the components instance to 2 and the day to 29.
    • For the years 2000 to 2100, determine which ones are leap years.
    • For each leap year from 2000 to 2100 you will need to set the year of the same components instance, don’t bother making a new one.
    • For each found leap year create an instance of NSDate using the NSCalendar instance.
    • Next, use the instance of NSDate to find the weekday. You do that by sending this message to the calendar.
    • The components instance will now contain the weekday as a number from 1 to 7. Sunday is 1.
    • You use the method named weekday of the NSDateComponents class to get the weekday number. Here is some of the documentation for the method. The receiver is the instance of NSDateComponents.
    • The weekday number will be converted to a string using a C function that you write.
    • Finally, output the information for that day with the NSLog function.
  • Here is a diagram for how the objects flow into each other.


  • The algorithm for determining if a year is a leap year can be found here:
  • Execution time is determined by retrieving the current system time before and after the program runs. Then you subtract the beginning time from the end time to get the execution time. Use this code to get the current system time:
    • NSTimeInterval is a double with 6 digit precision. It’s very accurate.

Program verification

  • Remember! Don’t skip this part.
  • Find a source to confirm the years and weekdays that you calculate. Add the link to the source to a comment at the end of your program.