iOS Dev – Assignment #2 – Extra Task

Add a memo-function to store a number

Start my rearranging the buttons to make space for three new ones:

iOS Dev – Assignment #2 - Extra Task – Additional Buttons
iOS Dev – Assignment #2 – Extra Task – Additional Buttons

Create a variable to store the value:

float numberInMemory;

Initialize it:

- (void)viewDidLoad
{
    ...
    numberInMemory = 0;
}

And connect the buttons to suitable methods:

- (IBAction)addMemoryButtonPressed:(UIButton *)sender {
    numberInMemory += [self.numberTextField.text floatValue];
}

- (IBAction)subtractMemoryButtonPresed:(UIButton *)sender {
    numberInMemory -= [self.numberTextField.text floatValue];
}

- (IBAction)getMemoryButtonPressed:(UIButton *)sender {
     self.numberTextField.text = [NSString stringWithFormat:@"%@", [NSNumber numberWithFloat:numberInMemory]];
}
FacebooktwitterredditpinterestlinkedintumblrmailFacebooktwitterredditpinterestlinkedintumblrmail

iOS Dev – Assignment #2 – Task #4

Delegation The BasicCalculator communicates its result to a delegate. This asynchronous implementation is a bit overhead in this simple example, but think of a scientific calculator with more elaborate functionality. You can start a very complex operation and the Calculator-Class takes care of processing this operation in the background. After some time it finishes and communicates its result to the controller. Have a close look at the implementation of the delegation in the Basic- Calculator class. Now make your ViewController class the delegate of the calculator and make the project a fully working calculator.

When the view has loaded initiate the calculator and set the view controller as delegate of the calculator:
Continue reading “iOS Dev – Assignment #2 – Task #4”

FacebooktwitterredditpinterestlinkedintumblrmailFacebooktwitterredditpinterestlinkedintumblrmail

iOS Dev – Assignment #2 – Task #3

Protocols In the header file of the BasicCalculator you will find a protocol that defines a method

- (void)operationDidCompleteWithResult:(NSNumber*)result;

Make your ViewController-Class comply to that protocol.

First define the view controller as delegate of the calculator, then define required method of the protocol and let it display the result in the view:

// ViewController.h
@interface ViewController : UIViewController <BasicCalculatorDelegate>

- (void)operationDidCompleteWithResult:(NSNumber*)result;

// ViewController.m
- (void)operationDidCompleteWithResult:(NSNumber*)result {
    self.numberTextField.text = [NSString stringWithFormat:@"%@", result];
}

Please note that at the moment the view controller class only complies to the protocol. Even if its methods would already work as intended, nothing changed in the functionality compared with the previous task, because nobody actually uses the protocol …

FacebooktwitterredditpinterestlinkedintumblrmailFacebooktwitterredditpinterestlinkedintumblrmail

iOS Dev – Assignment #2 – Task #2

Check some edge-cases Division by 0, double decimal points, leading 0s, …

Division by 0 is already handled in the code from the previous task.

For the double decimal points expand the commaPressed: method like in assignment #1:
Continue reading “iOS Dev – Assignment #2 – Task #2”

FacebooktwitterredditpinterestlinkedintumblrmailFacebooktwitterredditpinterestlinkedintumblrmail

iOS Dev – Assignment #2 – Task #1

Model-View-Controller Since a calculator is something that might be used in various situations, you decide to rewrite your code to something more reusable. One advantage of MVC is that you can exchange the view but keep your model and parts of your controller. We provide you with a template for a class called BasicCalculator. Extend that class to provide a fully functional basic calculator. This class is your model.

The provided template is similar to the one from Assignment #1, where the calculator functionality is moved to the new model class. You could either use your code from the previous assignment and adjust it or redo the changes from the previous assignment inside the new template. Here we will choose the second approach.

First link the missing number buttons to the view controller class and change the tags of the buttons accordingly.
Continue reading “iOS Dev – Assignment #2 – Task #1”

FacebooktwitterredditpinterestlinkedintumblrmailFacebooktwitterredditpinterestlinkedintumblrmail

cs106a – Assignment #2 – Task #6

Douglas Hofstadter’s Pulitzer-prize-winning book Gödel, Escher, Bach contains many interesting mathematical puzzles, many of which can be expressed in the form of computer programs. In Chapter XII, Hofstadter mentions a wonderful problem that is well within the scope of the control statements from Chapter 4. The problem can be expressed as follows:

Pick some positive integer and call it n.
If n is even, divide it by two.
If n is odd, multiply it by three and add one. Continue this process until n is equal to one.

On page 401 of the Vintage edition, Hofstadter illustrates this process with the following example, starting with the number 15:
Continue reading “cs106a – Assignment #2 – Task #6”

FacebooktwitterredditpinterestlinkedintumblrmailFacebooktwitterredditpinterestlinkedintumblrmail

cs106a – Assignment #2 – Task #5

Write a ConsoleProgram that reads in a list of integers, one per line, until a sentinel value of 0 (which you should be able to change easily to some other value). When the sentinel is read, your program should display the smallest and largest values in the list, as illustrated in this sample run:

cs106a – assignment #2 – task #5

Your program should handle the following special cases:

  • If the user enters only one value before the sentinel, the program should report that value as both the largest and smallest.
  • If the user enters the sentinel on the very first input line, then no values have been entered, and your program should display a message to that effect.

Start by printing the program description. Read the fist value, if it is equal to the sentinel print an adequate message, otherwise set two helper variables representing the current smallest and largest numbers equal to the first value. Inside the following infinite loop, read the next number and stop processing if it is equal to the sentinel. Adjust the two helper variables if necessary. And finally print the result:
Continue reading “cs106a – Assignment #2 – Task #5”

FacebooktwitterredditpinterestlinkedintumblrmailFacebooktwitterredditpinterestlinkedintumblrmail