Created
May 15, 2012 14:10
-
-
Save egrouse/2702059 to your computer and use it in GitHub Desktop.
Completed exercises from 'Programming in Objective-C' - because why not?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// main.m | |
// ch3q7 | |
// | |
// Created by Ellis Grouse on 15/05/2012. | |
// Copyright (c) 2012 __MyCompanyName__. All rights reserved. | |
// Book exercise: Chapter 3 Q7 | |
// Make a class called XYPoint that holds 2 coordinates x,y and can set/get them separately | |
// Write functionality to output the information | |
// | |
#import <Foundation/Foundation.h> | |
// ----- @interface section ----- | |
@interface XYPoint: NSObject | |
// Instance methods | |
-(void) print; | |
-(void) setX: (int) inputX; | |
-(void) setY: (int) inputY; | |
-(int) x; | |
-(int) y; | |
@end | |
// ----- @implementation section ----- | |
@implementation XYPoint | |
{ | |
int x, y; | |
} | |
// -- SETTERS | |
-(void) setX:(int)inputX | |
{ | |
// Set x value to input | |
x = inputX; | |
} | |
-(void) setY:(int)inputY | |
{ | |
// Set y value to input | |
y = inputY; | |
} | |
// -- GETTERS | |
-(int) x | |
{ | |
return x; | |
} | |
-(int) y | |
{ | |
return y; | |
} | |
// -- OUTPUT | |
-(void) print | |
{ | |
NSLog(@"The coordinates are: %i,%i", x, y); | |
} | |
@end | |
// ----- program run section | |
int main (int argc, const char * argv[]) | |
{ | |
@autoreleasepool { | |
// Allocate and initialise object | |
XYPoint *coord = [[XYPoint alloc] init]; | |
// Set coordinate values | |
[coord setX: 3]; | |
[coord setY: 9]; | |
// Output | |
[coord print]; | |
// And again, with getters | |
NSLog(@"It is: %i,%i", [coord x], [coord y]); | |
} | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// main.m | |
// ch4q2 | |
// | |
// Created by Ellis Grouse on 15/05/2012. | |
// Copyright (c) 2012 __MyCompanyName__. All rights reserved. | |
// Book exercise: Chapter 4 Q2 | |
// Write a program that converts 27°F to °C using the following formula: | |
// C = ( F - 32 ) / 1.8 | |
// | |
#import <Foundation/Foundation.h> | |
// ==== PROGRAM | |
int main (int argc, const char * argv[]) | |
{ | |
@autoreleasepool | |
{ | |
// Run the program | |
float result; | |
int fahrenheit = 27; | |
result = ( fahrenheit - 32 ) / 1.8; | |
NSLog(@"Result: %g", result); | |
} | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// main.m | |
// ch4q7 | |
// | |
// Created by Ellis Grouse on 15/05/2012. | |
// Copyright (c) 2012 __MyCompanyName__. All rights reserved. | |
// Book exercise: Chapter 4 Q 7 | |
// Create an object to hold info on a rectangle | |
// With height, width, and functions to calculate area and perimeter | |
// | |
#import <Foundation/Foundation.h> | |
// ==== INTERFACE | |
@interface Rectangle : NSObject { | |
@private | |
} | |
// Setters | |
-(void) setWidth: (int) w; | |
-(void) setHeight: (int) h; | |
// Getters | |
-(int) width; | |
-(int) height; | |
-(int) area; | |
-(int) perimeter; | |
@end | |
// ==== IMPLEMENTATION | |
@implementation Rectangle | |
{ | |
int width, height; | |
} | |
// Setters | |
-(void) setWidth:(int)w | |
{ | |
width = w; | |
} | |
-(void) setHeight:(int)h | |
{ | |
height = h; | |
} | |
// Getters | |
-(int) width | |
{ | |
return width; | |
} | |
-(int) height | |
{ | |
return height; | |
} | |
-(int) area; | |
{ | |
return width * height; | |
} | |
-(int) perimeter | |
{ | |
return height * 2 + width * 2; | |
} | |
@end | |
// ==== PROGRAM CODE | |
int main (int argc, const char * argv[]) | |
{ | |
@autoreleasepool | |
{ | |
// Create a new Rectangle object | |
Rectangle *rect = [[Rectangle alloc] init]; | |
// Assign values to our rectangle | |
[ rect setWidth: 5 ]; | |
[ rect setHeight: 9 ]; | |
// Create some output | |
NSLog(@"The rectangle has the following parameters.\n Width: %i Height: %i\n Area: %i Perimeter: %i", | |
[ rect width ], [ rect height ], [ rect area ], [ rect perimeter] ); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// main.m | |
// ch5q1 | |
// | |
// Created by Ellis Grouse on 15/05/2012. | |
// Copyright (c) 2012 __MyCompanyName__. All rights reserved. | |
// Book exercise: Chapter 5 Q 1 | |
// Write a program to generate and display a table of n and n2, for integer values of n ranging from 1 through 10. Be sure to print the appropriate column headings. | |
// | |
#import <Foundation/Foundation.h> | |
int main (int argc, const char * argv[]) | |
{ | |
@autoreleasepool | |
{ | |
// Variables | |
int n; | |
// Output headings | |
NSLog(@" n n^2 "); | |
NSLog(@"--------"); | |
// Counter | |
for (n = 1; n <= 10; n++) { | |
NSLog(@"%2i %3i", n, n * n); | |
} | |
} | |
// Return 0 | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// main.m | |
// ch6q1 | |
// | |
// Created by Ellis Grouse on 15/05/2012. | |
// Copyright (c) 2012 __MyCompanyName__. All rights reserved. | |
// Chapter 6 | |
// 1. Write a program that asks the user to type in two integer values. Test these two numbers to determine whether the first is evenly divisible by the second and then display an appropriate message at the terminal. | |
// | |
#import <Foundation/Foundation.h> | |
// Program coding | |
int main( int argc, const char * argv[] ) | |
{ | |
@autoreleasepool | |
{ | |
// Variables | |
int first, second; | |
bool isEven; | |
// Take user input | |
NSLog( @"Please type in two integer variables, separated by a space." ); | |
scanf( "%i %i", &first, &second ); | |
// Divide | |
int division = first / second; | |
// Check if division is an even number | |
if( division % 2 == 0 ) | |
isEven = YES; | |
// Output | |
NSLog( @"You entered %i and %i.", first, second ); | |
NSLog( @"%i / %i is: %i", first, second, division ); | |
if( isEven ) | |
NSLog( @"%i is evenly divisible by %i", first, second ); | |
else | |
NSLog( @"%i is not evenly divisible by %i", first, second ); | |
} | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// main.m | |
// ch6q4 | |
// | |
// Created by Ellis Grouse on 15/05/2012. | |
// Copyright (c) 2012 __MyCompanyName__. All rights reserved. | |
// Chapter 6 | |
/* 4. Write a program that acts as a simple printing calculator. The program should allow the user to type in expressions of the following form: | |
number operator | |
The program should recognize the following operators: | |
+ - * / S E */ | |
#import <Foundation/Foundation.h> | |
int main (int argc, const char * argv[]) | |
{ | |
@autoreleasepool | |
{ | |
// Variable declarations | |
float accumulator, inputValue; | |
char inputOperator; | |
bool isRunning = YES; | |
// Begin execution | |
NSLog( @"Beginning calculator operation..." ); | |
do | |
{ | |
// Take user input | |
scanf( "%f %c", &inputValue, &inputOperator ); | |
// Switch between the different operators the user may have input | |
switch( inputOperator ) | |
{ | |
// Set accumulator to input value | |
case 'S': case 's': | |
accumulator = inputValue; | |
break; | |
// End operation | |
case 'E': case 'e': | |
isRunning = NO; | |
break; | |
// Addition | |
case '+': | |
accumulator += inputValue; | |
break; | |
// Subtraction | |
case '-': | |
accumulator -= inputValue; | |
break; | |
// Multiplication | |
case '*': | |
accumulator *= inputValue; | |
break; | |
// Division | |
case '/': | |
accumulator /= inputValue; | |
break; | |
// Default operation - fallback for invalid | |
default: | |
NSLog( @"Invalid operator. Valid operators: + - * / S E" ); | |
} | |
// Output the current value of the accumulator | |
NSLog( @"= %f", accumulator ); | |
} | |
while( isRunning == YES ); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment