Official Forum for Programming in Objective-C (the iPhone Programming Language) - Stephen Kochan
December 10, 2018, 03:36:10 PM *
Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
   Home   Help Search Login Register Chat  
Pages: [1]   Go Down
Author Topic: Chapter 3 - Exercises (1 until 7)  (Read 2885 times)
Posts: 2

« on: August 18, 2013, 07:58:09 AM »


Answers for Chapter 3 Exercises:
1)The invalid names are:
6_05 - Variable Numbers can't start with numbers!
A$ - "$" is a invalid character.
_calloc is ok, but remember: calloc is a reserved word!
Int starts with "I", uppercase letter, so, it's not a reserved word.
@interface Mac : NSObject

//Really Long Code


@implementation Mac
//A billion variables
//Really Long Code times 3
int main()
Mac * myMacBook = [[myMacBook alloc] init];
[myMacBook turnOn];
myMacBook connectToInternet];
[myMacBook turnOff];
[myMacBook printBatteryLevel];
[myMacBook publishToFacebook: @"Really Long Day..];

3) Haha, already did it Tongue

[myBoat startEngine];
[myBoat getDiesel:100]; //Add 100 gallons of sea diesel
[myBoat turNightLightsOn];
[myBoat checkRadar: 1000]; //Check 1000 miles area with a radar
[myBoat setSpeed: 40] //Set speed to 40 miles per hour
[myBoat stopEngine];
[myMotocycle service];
[myMotocycle getGas:30]; //Add 30 gallons of Gas.
[myMotocycle startEngine];
[myMotocycle turnNightLightsOn];
[myMotocycle setSpeed: 20] /*Set speed to 20MPH*/
[myMotocycle stopEngine];

5) Yes, because you don't need to write the same(or atleast really similar) code three or more times.

6)I see advantages on object-oriented programming, because it's simply creating a new instance.

//Cartesian Plans by Gabriel Tomitsuka. No Rights Reserved. You are allowed to modify, republish and
//do whatever you want. Made for Chapter 3 Exercise 7, page 50 of the book "Programming in Objective-C
//4th Edition by Stephan G. Kochan

@interface XYPoint : NSObject

-(void) printCoordinates;
-(void) setX : (int) xcoord;
-(void) setY : (int) ycoord;


@implementation XYPoint
    int x;
    int y;

-(void) printCoordinates
    printf("X: %i \nY: %i", x, y);

-(void) setX : (int) xcoord
    x = xcoord;
-(void) setY : (int) ycoord
    y = ycoord;


int main()
    @autoreleasepool {

        NSLog(@"\n\n              ---\n\nWelcome to Universe Positioning System (UPS)\n \n Your coordinates are: \n\n");
        XYPoint * Instance1 = [[XYPoint alloc] init];
        [Instance1 setX: 123];
        [Instance1 setY: 345];
        [Instance1 printCoordinates];

« Last Edit: August 18, 2013, 01:21:36 PM by gstomi » Logged
Pages: [1]   Go Up
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.11 | SMF © 2006-2009, Simple Machines LLC Valid XHTML 1.0! Valid CSS!
Entire forum contents ゥ 2009 All rights reserved.