Well, there are two ways:
1. Without using the
print method. But to do this, you would have to add
numerator and
denominator methods to the fraction class.
NSLog(@"the 1st fraction is: %i/%i", [myFrac1 numerator], [myFrac1 denominator );
2. Using printf instead of NSLog. The printf is a C function, that is basicly the same as NSLog, but it doesn't produce a line break at the end. If you do this you would have to modify the program like this:
@interface Fraction : NSObject
{
int numerator;
int denominator;
}
//method of class
-(void) print;
-(void) setNumerator:(int) n;
-(void) setDenominator:(int) d;
@end
@implementation Fraction
-(void) print
{
printf("%d/%d \n", numerator,denominator);
}
-(void) setNumerator:(int) n
{
numerator = n;
}
-(void) setDenominator:(int) d
{
denominator = d;
}
@end
#import <Foundation/Foundation.h>
int main (int argc, const char * argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
//Fraction *myFraction;
//myFraction = [Fraction new];
Fraction *myFrac1 = [[ Fraction alloc] init];
Fraction *myFrac2 =[[ Fraction alloc] init];
// set fraction to 1/3
[myFrac1 setNumerator: 1];
[myFrac1 setDenominator: 3];
[myFrac2 setNumerator: 4];
[myFrac2 setDenominator: 8];
// display fraction
printf("the 1st fraction is:");
[myFrac1 print];
[myFrac1 release];
printf("the 2nd fraction is:");
[myFrac2 print];
[myFrac2 release];
[pool drain];
return 0;
}
Notice that there isn't a @ sign before the string anymore? That's because we're not dealing with Obj-C, but with C strings now.