Skip to content

Latest commit

 

History

History
94 lines (69 loc) · 4.6 KB

2015-06-04_variables-2.md

File metadata and controls

94 lines (69 loc) · 4.6 KB

#06/04 - Variables & Types pt. II

Homework

Use https://gist.github.com to create code samples. Paste the gist url into slack so everyone can see it. You can solve either of the 2 exercises below. Or Both. This isn't about getting the right solution on your own as much as it's about sharing code, reading each others code, and learning from one another.

It's not necessary to use loops to solve these problems. We'll be discussing loops a little later on in class. However, if you choose to solve the exercises with loops and arrays, here are a few resources to help you out:

If you choose this route I would HIGHLY recommend reading up on loops and arrays outside of the context of Objective-C.

Logic Problem

https://gist.github.com/8bfefcf3764d387ce22b#file-gistfile1-c

Logic

Objective

  • Become proficient in using different types and variables in Objective-C

Numbers

The types short, int, and long represent integers. They vary in size. For our purposes, we useNSInteger to represent any integer type. float and double represent real numbers. This means that they can have decimals in them.In Objective-C, we replace float with CGFloat. For example, 2 is an integer but 2.1 is a real number.

Review: Variables and Math http://www.binpress.com/tutorial/objective-c-lesson-2-basic-variables/44 Review: Variables and Math https://gist.github.com/mikekavouras/701663763fd5b5921d8e

Exercise: Variables and their sizes What happens when you:

  • Divide an NSInteger and an NSInteger?
  • Add a CGFloat and a NSInteger?
  • Add an NSInteger and a double?

Booleans

A BOOL represents the values YES and NO. Booleans are useful when data has a "yes" or "no" answer. For example, the question "Is this a banana?" has a boolean answer. Booleans can also be created by comparing two variables. For example, "Is 7 greater than 3?" has a boolean answer. In Objective-C, we write this:

BOOL answer = 7 > 3;

The value of answer will be the boolean value YES. Try it!

The boolean comparators are:

  • <
  • <=
  • >
  • >=
  • ==
  • !=

What else can you compare?

Chars and Strings

A char represents a single character. In Objective-C, you denote a char by placing it between single quotes ' '. For example, the following are chars: 'a', 'c', '$', '7', and '_'. Strings are sequences of characters and denoted with double quotes "". For example, the following are strings: "hello", "Queens!", "$99.999". Strings are not primitive types but that distinction does not matter to us now.

Exercise: Play with characters and Strings. What happens if you do the following:

  • char c = "a";
  • NSString *b = 'foo';
  • What happens if you add, using +, two chars together?
  • Do any other mathematical operations work on chars?

Exercises

Exercise #1: Your Schedule

Use several variables to store the names of your classes and their teachers. Then, display a nice little table displaying your schedule. Just FYI, my column of courses has a width of 26 characters, and the teacher column has a width of 15. The first and last lines are a plus sign, fifty dashes (a.k.a. minus signs) and another plus sign.

Your table doesn't need to look exactly like this, or even line up. I used a total of sixteen variables (course1, course2, ... course8, teacher1, teacher2, etc.). You should do the same.

+------------------------------------------------------------+
| 1 |                          English III |       Ms. Lapan |
| 2 |                          Precalculus |     Mrs. Gideon |
| 3 |                         Music Theory |       Mr. Davis |
| 4 |                        Biotechnology |      Ms. Palmer |
| 5 |           Principles of Technology I |      Ms. Garcia |
| 6 |                             Latin II |    Mrs. Barnett |
| 7 |                        AP US History | Ms. Johannessen |
| 8 | Business Computer Infomation Systems |       Mr. James |
+------------------------------------------------------------+

Exercise #2: 12 Days of Christmas

Print "The Twelve Days of Christmas", an English carol with a lot of repetition! You can find the structure of the lyrics here: http://en.wikipedia.org/wiki/The_Twelve_Days_of_Christmas_%28song%29#Lyrics. Use variables to store bits of repeated data and print the full lyrics.

Extra reading http://www.bottomupcs.com/chapter01.html