Assignment 2

Due Date

Tuesday, September 22nd — 11:59pm


This project is designed to give you some hands on experience with…

  • Using Xcode as a development environment
  • Using Interface Builder to create iPhone user interfaces
  • Declaring and wiring up outlets
  • Declaring and wiring up actions
  • Testing applications in the iPhone Simulator


For this assignment you will be writing a basic tip calculator program app for the iPhone. Your application must contain the following elements…

  • An overall title label noting that the app is a tip calculator
  • A label and corresponding text input to enter the amount of the check
  • A label and corresponding text input to enter the tip percentage
  • A button that calculates the tip amount based on the specified check amount and tip
  • A label (or set of labels) that are used to identify and display the tip amount in dollars
  • A label (or set of labels) that are used to identify and display the overall total in dollars

An example user interface is shown below.

Tip Calculation App

Tip Calculation App


When your app initially loads it should look similar to the image shown below.

Initinal App State

Initinal App State

When a user clicks on either of the text fields, they should automatically be presented with the numbers and punctuation keyboard so the user can enter a floating point amount. The “Return” key should be changed so that it says “Done” — additionally, when the user presses this key, the keyboard should automatically go away.

Keyboard Entry

Keyboard Entry

If the user presses the button to calculate the tip, the computed tip amount and overall check amount should be set, neatly formatted to 2 decimal places with a leading dollar sign. Additionally, the text fields should be cleaned up and displayed with 2 places after the decimal.

Resulting Tip Calculation

Resulting Tip Calculation

Error Handling

Since we haven’t yet learned how to restrict and format the text fields in real-time as the user types, these fields could really contain anything once the user is done with the keyboard. As such, we need to be prepared to account for a user that has entered non-numeric input. One such error handling strategy is detailed below.

Let’s say that the user entered some bad text into one of the text fields as illustrated below.

Entering Bad Input

Entering Bad Input

When the user presses the calculate button, we validate both of the text fields to ensure that they are floating point numbers. If they are not, we could replace the contents of the bad text field with an error message, and clear out the calculated tip amount and overall check amount.

Handling Bad Input

Handling Bad Input

String Parsing

You will need to turn the string representations of check amount and tip percentage into actual numbers for performing both validation and then computation. There are several techniques available to achieve this — though for this assignment, I’d recommend using the NSScanner class. This class can extract numerical (and other) values out of strings (similar to Java’s java.util.Scanner class).

Additional Ideas

The following items are provided as thoughts as to how you might extend or improve the project beyond the basic requirements. You are not required to implement any of these.

  • Try out alternate color combinations — just don’t make my eyes bleed
  • Feel free to play with the layout and style of elements — just be sure that things are neatly aligned and that there is at least some justifiable method to your madness
  • Add an image to the app if you think it will improve the look and feel of the application

Getting Started

Create a new project by selecting File → New Project… then under iPhone OS select View-based Application. Select Choose…

Creating a View-based Application

Creating a View-based Application

Feel free to name the project as you see fit — though remember that things such as the app name and class names will get generated based on the name you give the overall project.


Your projects will be graded on the following criteria:

  • Correctness of application
  • Appearance of application
  • Adherence to Objective-C and iPhone coding conventions
  • Neatly formatted and indented code
  • Well documented header files
  • Absence of significant performance issues
  • Absence of memory leaks


Like the previous assignment, you are required to submit a zip file of your project directory. Be sure to manually remove the build directory before zipping things up.

To submit the assignment, open up a terminal, navigate to your zip file then issue the command:

submit cs491i assignment2

If developing on your own Mac, you’ll need to transfer the file into your GL account (via SCP), then SSH into GL and issue the submit command.


2 Responses to “Assignment 2”

  1. Kevin Says:


    Pretty neat class — wish I was able to take a class like this in my comp sci curriculum.

    Just one bit of feedback if I may about the design for your students (hope you don’t mind)…

    “When a user clicks on either of the text fields, they should automatically be presented with the numbers and punctuation keyboard so the user can enter a floating point amount.”

    You actually don’t need (or want) a decimal point for this type of app. I blogged about this earlier this year here:

    In addition to improving usability, it would be an extra bit of logic your students will have to account for in their code. Of course, allowing the full punctuation keyboard removes the bit about filtering out all non-numeric/decimal input. =)

    And finally, NSDecimalNumber (think Java’s BigDecimal) would be a much better choice over NSScanner in this scenario. =)

    • Dan Says:


      I contemplated requiring exactly the input mechanism you described before releasing the project – it’s certainly the way I’d go for most text field based floating-point numerical input if distributing an app : )

      However, since this was the first iPhone project my primary concern was more that students became familiar with the SDK tools and the concepts of outlets and actions. When the project came out we had not yet discussed delegates, and I felt that the description as written didn’t really stop anyone from getting started on it. Adding a delegate (once we discussed them) to dismiss the keyboard was more-or-less an auxiliary thing, whereas utilizing UITextFieldDelegate’s -textField:shouldChangeCharactersInRange:replacementString: seemed more ingrained in the overall process.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: