Programming 3 (Nathan, Semester 1, 2012)

Assignment 1

This is your first assignment for Semester 1, 2012.

The purpose of this assignment is to get familiar with Objective-C. The assignment consists of a programming task and a number of exercises that will be made available incrementally as milestones (so check this web page regularly to make sure you won't miss out on your next milestone!) Milestones are due every week and need to be submitted in or before the lab they are due in. You will only get marks for a milestone if you hand in the milestone before its deadline. See below below for the milestones and deadlines.

To submit a milestone, you need to commit everything that belongs to the solution into your Subversion repository on dwarf.ict.griffith.edu.au and tag it accordingly (i.e. milestone1 for the first milestone, milestone2 for the second, etc.).

Please note that this is an individual assignment! Everything you hand in must be your original work (e.g. you cannot hand in any material created by other students or as group work, or downloaded from the internet)! Please see the assessment and the Course Policies section for more information about handing in your assignment.

Programming Task

The task for this assignment is to create (in stages), a number of small command line programs in C and Objective-C. For more details, see the milestones below.

All classes need to be tested and debugged before submission, including unit and integration tests! Include test code in your submission, and make sure that the final program behaves exactly as specified. (E.g., create separate test methods, using NSAssert(), assert(), and #ifdef).

The overall program and all classes need to be accompanied by documentation, a description of the API, and a test report.

Milestones and Deadlines

Week 1, 28/29 February 2012 (9 marks)

Subversion Repository

Log into dwarf.ict.griffith.edu.au (dwarf) using ssh, putty, or another ssh client, to create a Subversion Repository for Programming 3 and the first assignment:

  1. Set up the Subversion repository in your home directory, using the svn_setup p3 command on dwarf (type y when asked if this should be set up for your student ID).
  2. Log out of dwarf using the exit command (dwarf only acts as a server for the repository, you need to work on your assignment from the local computer you are logged in!).
  3. Now create a working directory for assignment 1 on your workstation using the remote svn checkout command from the lecture to, e.g. create an a1 directory that you can add your assignment 1 files to.

To get marks for the repository, you need to create a milestone1 tag for your final milestone submission using svn copy (as described in the lecture) after creating and committing the following code.

Hello World

Using Xcode, create a HelloWorld project with a single main.c module.

Compile and run the program from Xcode first, then use the clang compiler from the command line. The following command needs to run without any warning from the command line:

clang -o hello main.c

Add your program to the repository using svn add * from within the a1 directory (the one you checked out earlier). Commit the project using svn commit -m "log message" (replace log message with a descriptive log message). Make sure you commit every time you make changes!

Makefile

In the directory that contains your main.c module, create a Makefile that compiles your program. You need to be able to simply type make to compile your program (from within that same directory). This needs to result in a compiled program that you can run from the command line using ./hello (followed by enter).

Add your Makefile to the repository using svn add Makefile and commit it using svn commit -m "log message" (replace log message with a descriptive log message).

Tagging the Milestone

After your final commit (when everything works), tag your milestone by loggin in to dwarf and copying a snapshot of the repository trunk to a tag you call milestone1 (all lowercase!), e.g.:

ssh s1234567@dwarf.ict.griffith.edu.au
svn copy -m "milestone1" \
         file://$HOME/.p3svn-2012/ass1/trunk \
         file://$HOME/.p3svn-2012/ass1/tags/milestone1

svn list file://$HOME/.p3svn-2012/ass1/tags
svn list file://$HOME/.p3svn-2012/ass1/tags/milestone1

The commands in the last two lines allow you to check that the milestone actually exists and lists the actual files in the milestone. You can only submit your milestone this way once, so be very careful! If you need to re-submit your milestone, you need to move away the old milestone first (on dwarf), e.g.:

svn mv -m "un-submit first milestone" \
       file://$HOME/.p3svn-2012/ass1/tags/milestone1 \
       file://$HOME/.p3svn-2012/ass1/tags/milestone1-old

Week 2, 6/7 March 2012 (9 marks)

Number Tester

Create a sstudentnumber Xcode project (replacing studentnumber with your own student number) with a main.c that tests whether your student number is an integral multiple of the number given as a command line parameter (i.e. you should be able to divide your student number by the given number without a remainder or fraction).

The program should print nothing and return EXIT_SUCCESS (as defined in <stdlib.h>) if your student number can be divided by the number. Otherwise, the program should print studentnumber cannot be divided by number (where studentnumber again is your student number) and return EXIT_FAILURE.

A usage message should be printed to stderr if too few or too many command line parameters are given!

Don't forget to add and commit your number tester to the Subversion repository (see above)!

Makefile

Add a Makefile to your project that compiles your program when using make from the command line.

Integrated Makefile

Create a top level Makefile in your checked out assignment folder (e.g. a1 if you used that name on checkout). Add a hello target that compiles your first milestone. E.g., if your first milestone was in a HelloWorld project folder, you can use

cd HelloWorld/HelloWorld && make

as a command in your Makefile to change into the folder that contains your main.c and your Makefile for the first milestone, and then just runs make from in there.

Similarly, add a target named sstudentnumber to your top level Makefile that compiles main.c in your sstudentnumber project into an executable called sstudentnumber

Finally, add an all target that compiles all milestones' executables (so far, the Hello World program and the Number Tester).

Add a clean target to all Makefiles (including the top level Makefile), that deletes all compiled products (e.g. all .o files and all executables).

Don't forget to commit your changes to the Subversion repository at every step (see above)!

Week 3, 13/14 March 2012 (8 marks)

City Bus Class

The city's bus fleet is made up of a large number of busses. To keep track of the fleet, each bus has a unique number and information is stored on how much fuel (in litres per 100km) each bus uses.

Create a Bus class in Objective-C that contains the number of a bus (i.e. an integer) and the fuel consumption of the bus (keep in mind that litres per 100km usually is not a whole (integer) number).

Add all the access methods (getters and setters) required.

Create a method compare: that takes another Bus as a parameter and returns an NSComparisonResult that compares the fuel consumption of the receiver (the bus object the compare: method belongs to) with the consumption of the other bus (the pointer passed as a parameter to the compare: method).

The return value for the compare: method should be NSOrderedAscending if the fuel consumption of the receiver is less than the fuel consumption of the other bus, NSOrderedSame if the busses have equal fuel consumption, and NSOrderedDescending otherwise.

Create a method compareNumberWithBus: that does the same, but compares bus numbers rather than fuel consumption. I.e. NSOrderedAscending should be returned if the receiver has a lower number than the other bus, NSOrderedSame should be returned if both have the same number (e.g. if you compare a bus with itself), and NSOrderedDescending should be returned otherwise.

Test Code

Create a main module that contains a main() function that tests all methods of your Bus class. Test that the compare: and compareNumberWithBus: methods are correct for several different busses. Choose your test cases wisely!

Makefile

Extend your Makefile to include targets for all milestones so far. Modify your all target so that will compile the programs for all current milestones. This all target should remain the default (first) target. Also update your clean target to cater for the new milestone.

Documentation

Add reference documentation for your Bus class following the guidelines discussed in the lecture. This documentation needs to be present in the source code (as comments), and as an external reference document (plain text, HTML, or PDF). You can use document extraction tools (such as headerdoc or doxygen — see hints below) to automatically extract your documentation from your source code. If you use a document extraction tool, add a corresponding doc target to your Makefile (and to the all target there) to automatically create the external document when you run make.

Week 4, 20/21 March 2012 (7 marks)

Refactoring

The city plans to acquire electric busses. As a result, the engineers have discovered that their old data organisation (see milestone3) no longer works: instead of litres per 100km, electricity consumption is measured in Wh/km (Watt-hours per kilometre). To compare cost efficiency between different types of busses, the city now also needs to know the unit price (for 1 litre of fuel for diesel busses and for 1 Wh of electricity for electric busses).

Re-factor your code from milestone3 so that the Bus class now contains a unit price in cents (take into account that one unit (e.g. of electricity) may cost less than one cent) as well as the existing efficiency field for the bus (the field you used for litres per 100km in milestone3, if necessary, adapt the class to work for the generic case required here).

Create a costForKmTravelOf: that takes a number of kilometres as a parameter and returns the cost (in cents) for traveling the given distance.

Subclasses

Create subclasses called DieselBus and ElectricBus, that initialise instances that get created to the current fuel cost. Make the default fuel cost 153 cents per litre for diesel busses and 0.22 cents per Wh for electric busses.

Add a setDefaultCost: class method that allows to adjust the default cost for each subclass. This method should only change the cost for new instances that are created from then on, but not existing instances of DieselBus or ElectricBus (use the normal setter method from your superclass for these).

Testing, Makefile, and Documentation

As in the previous milestones, test and document your code properly and make the necessary additions to your Makefile.

Week 5, 27/28 March 2012 (7 marks)

Creating a GUI for bus data

Create an iOS program that offers a graphical user interface (GUI) for displaying and entering bus fleet data. For storing the actual data, use the bus (model) classes from your previous milestone.

Master and Detail View

Make sure you structre your GUI so that you have a master view that lists all the busses (at least the unique number of each bus should be listed) and a detail view that shows all the details of a selected bus, and lets the user edit (change) those details. Selecting (tapping, clicking on) a bus in the master view should bring up the detail view with the details of the bus. Make sure that any changes the user makes get reflected in the stored data and in both the detail and (if relevant) the master view.

Add and Edit Button

Make sure the master view contains an Add button that allows the user to add a new bus to the list, and an Edit button that (at least) allows the user to select and delete busses from the list.

See the Hints Section below for additional information on creating GUIs and Master/Detail view applications. Make sure that when using Xcode you switch off Automatic Reference Counting!

Milestone submission

Tag your final milestone as milestone5. Make sure your milestone submission contains everything emphasised so far, including documentation, an updated Makefile that compiles all milestones for this assignment, your assignment does proper error checking, compiles without warnings. (It is recommended to test with at least two different compilers, i.e. clang and gcc).

Make sure that your Subversion permissions are set correctly by running

setperms $HOME/.p3svn-2012

on dwarf! Also, from dwarf, check that all your milestones are submitted properly by listing the content of your repository, e.g.:

svn list file://$$OME/.p3svn-2012/ass1/tags
svn list file://$HOME/.p3svn-2012/ass1/tags/milestone1
svn list file://$HOME/.p3svn-2012/ass1/tags/milestone2
svn list file://$HOME/.p3svn-2012/ass1/tags/milestone3
svn list file://$HOME/.p3svn-2012/ass1/tags/milestone4
svn list file://$HOME/.p3svn-2012/ass1/tags/milestone5

Make sure you tag your milestone in time (at the end of your lab) so you can be marked before your next lab and receive feedback. Submissions that are tagged after the day they are due in will receive a late penalty!

Bonus Marks

A maximum of 10 bonus marks may be awarded for the assignment if you complete the assignment in C++ as well as Objective-C. While these bonus marks cannot take you past the maximum 40 marks (20 %) for the assignment, they can make up for any marks you lost elsewhere in the assignment!

Some Hints

Creating API documentation
You can create documentation directly from your source code using headerdoc (on the Macs) or doxygen (installed on dwarf) if you have the proper comments in your source code!

Converting Strings to Numbers
To convert a string from ASCII to an integer, you can use the atoi() function in C.

Testing whether numbers can be divided
To test whether a number can be divided by another number, you can use the modulo % operator: a % b will result in 0 if a can be divided by b, otherwise it will result in the (non-zero) remainder of the integer division a / b. E.g., 6 % 5 will result in 1, while 6 % 3 will result in 0.

Class methods and static variables
In Objective-C (as well as C++ and plain C) you can use a static variable that is only visible from within the scope you define that variable, and will store data between instances of classes. (See the Class Methods section from the lecture for an example and more details.)
Using Strings
Use the string classes offered by Objective-C (NSString) and C++ (std::string) rather than char * for your classes and objects!

Comparing Bus Numbers
The Objective-C NSNumber class already has a method for comparison with another number!

Creating a GUI
Using Xcode you can (from the template chooser) create a project that uses a GUI. Make sure you choose from the iOS Application Templates! When asked for project options, make sure that Core Data and Automatic Reference Counting are switched off, but Use Storyboards and Include Unit Tests are switched on! Also make sure that you don't create a local GIT repository for any projects you are going to submit through Subversion! Finally, don't forget to svn add your .xcodeproj folder (but make sure Xcode is not running when you commit!).
Creating a Master/Detail View Application
Xcode already offers a template for Master/Detail View under iOS Applications. This template will already create a program that works on iPhones and iPads (if Universal is selected for the Device Family). You will need to replace the default class that it uses for its model (NSDate) with your own model class(es). Also, you will need to change the detail view in the Storyboards to display (and allow to edit) the details relevant for your project.

Last Changed: $Date: Sun Mar 18 06:57:33 2012 +1000 $ by Rene Hexel

Online Help

Latest Announcements

12/06/2012
Last chance to get feedback before the exam tomorrow.
08/06/2012
Apologies, but please re-check your results — there was an error on the earlier page!
04/06/2012
Just a reminder to check your assignment results online!

Back to top