Official Forum for Programming in Objective-C (the iPhone Programming Language) - Stephen Kochan
June 21, 2018, 08:37:08 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
  Print  
Author Topic: Program 3.3 vs Program 3.4  (Read 1195 times)
barrettlikespizza
Newbie
*
Posts: 17


The secret of getting ahead is getting started.


WWW
« on: February 05, 2015, 02:47:42 AM »

Why does Program 3.4 bother keeping the Print method, when the program section doesn't even use it?

The way I understand it, Print was necessary in Program 3.3, because Print was a dumbed-down form of a "Get" method.

But the whole aim of Program 3.4 is to teach us how to write and use "Get" methods, right? (such as "numerator" and "denominator").
So Print doesn't even make it past the Implementation section. It's just there, staring back at me with no purpose.

Code: (Objective-C)
// Program to access instance variables  cont'd 

#import < Foundation/ Foundation.h >

//---- @interface section ----

@interface Fraction: NSObject
-( void) print;
-( void) setNumerator: (int) n;
-( void) setDenominator: (int) d;
-( int) numerator;
-( int) denominator;

@end

//---- @implementation section ----

@implementation Fraction
{
int numerator;
int denominator;
}

-( void) print
{
NSLog (@"% i/% i", numerator, denominator);
}
-( void) setNumerator: (int) n
{
numerator = n;
}
-( void) setDenominator: (int) d
{
denominator = d;
}
-( int) numerator
{
return numerator;
}
-( int) denominator
{
return denominator;
} @end

//---- program section ----

int main (int argc, char * argv[])
{
@autoreleasepool { Fraction *myFraction = [[ Fraction alloc] init];

// Set fraction to 1/ 3

[myFraction setNumerator: 1];
[myFraction setDenominator: 3];

// Display the fraction using our two new methods

NSLog (@" The value of myFraction is: %i/% i", [myFraction numerator], [myFraction denominator]);
}
return 0;
}
« Last Edit: February 05, 2015, 02:49:24 AM by barrettlikespizza » Logged
BrianLawson
Sr. Member
****
Posts: 262


Email
« Reply #1 on: February 05, 2015, 07:01:34 AM »

Rather than the NSLog statement at the end of your program, you could have used the print method to do exactly the same thing.
Code: (Objective-C)
[myFraction print];
Logged
Pages: [1]   Go Up
  Print  
 
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 classroomM.com. All rights reserved.