Amazon.com Widgets About Program 7.5
Welcome, Guest. Please login or register.
Did you miss your activation email?
October 23, 2014, 06:45:18 AM
Home Help Search chat Login Register   
News: Read this please.The Great Kangaroo Escape Looking for reviews of the 4th ed on Amazon!   Twitter:  @skochan
                     

+  Official Forum for Programming in Objective-C (the iPhone Programming Language) - Stephen Kochan
|-+  Old Stuff
| |-+  Program Examples
| | |-+  Chapter 7
| | | |-+  About Program 7.5
Pages: [1] 2   Go Down
Print
Author Topic: About Program 7.5  (Read 5921 times)
willc2
Newbie
*
Posts: 9







« on: February 17, 2009, 09:04:20 AM »

I notice the filename is "main.m"

Do I rename "FractionTest.m" from Program 7.4 then edit or do I create a brand new file.

If I create a new file, won't both "main.m" and "FractionTest.m" both execute at runtime?
Logged
skochan
Administrator
Hero Member
*****
Posts: 3114







« Reply #1 on: February 17, 2009, 01:08:53 PM »

The filename is not important.  Execution will begin wherever main is located, whether it's in a file called FractionTest.m or main.m.  What you can't have are two functions called main.

Cheers,

Steve
Logged
willc2
Newbie
*
Posts: 9







« Reply #2 on: February 18, 2009, 12:38:36 AM »

For the purposes of using this book I have found that a student should just edit the 7.4 version of the file "FractionTest.m"

Don't create a new file named "main.m" to match the subhead titled Program 7.5 Test File main.m
Logged
skochan
Administrator
Hero Member
*****
Posts: 3114







« Reply #3 on: February 18, 2009, 06:27:18 AM »

Good advice!   Wink

Steve
Logged
i-ash
Newbie
*
Posts: 10







« Reply #4 on: March 08, 2009, 05:39:25 PM »

For some reason I can't run this example, and I can't figure out why...  Angry

It has an issue with the 'reduce' method, with the debugger highlighting the "numerator /= u;" line.

In the console I get:

[Session started at 2009-03-09 11:36:06 +1100.]
2009-03-09 11:36:06.397 FractionTest[1287:10b] 1/4
2009-03-09 11:36:06.398 FractionTest[1287:10b] +
2009-03-09 11:36:06.399 FractionTest[1287:10b] 1/2
2009-03-09 11:36:06.400 FractionTest[1287:10b] =

[Session started at 2009-03-09 11:36:06 +1100.]
Loading program into debugger…
GNU gdb 6.3.50-20050815 (Apple version gdb-962) (Sat Jul 26 08:14:40 UTC 2008)
Copyright 2004 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and you are
welcome to change it and/or distribute copies of it under certain conditions.
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB.  Type "show warranty" for details.
This GDB was configured as "i386-apple-darwin".Program loaded.
sharedlibrary apply-load-rules all
Attaching to program: `/Users/ash/Documents/Education/Obj-C Progs/FractionTest/build/Debug/FractionTest', process 1287.
Current language:  auto; currently objective-c
(gdb)

Can someone please help me?

My code is:

Code: (Objective-C)
//  Fraction.m

#import "Fraction.h"


@implementation Fraction

@synthesize numerator, denominator;

- (void) print
{
NSLog(@"%i/%i", numerator, denominator);
}

- (double) convertToNum
{
if (denominator != 0)
return (double) numerator / denominator;
else
return 1.0;
}

- (void) setTo: (int) n over: (int) d
{
numerator = n;
denominator = d;
}

- (Fraction *) add: (Fraction *) f
{
Fraction *result = [[Fraction alloc] init];
int resultNum, resultDenom;

resultNum = numerator * f.denominator + denominator * f.numerator;
resultDenom = denominator * f.denominator;
[result reduce];

return result;
}

- (void) reduce
{
int u = numerator;
int v = denominator;
int temp;

while (v != 0) {
temp = u % v;
u = v;
v = temp;
}

numerator /= u;
denominator /= u;
}

@end

Code: (Objective-C)
// FractionTest.m

#import <Foundation/Foundation.h>
#import "Fraction.h"

int main (int argc, const char * argv[]) {
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

Fraction *aFraction = [[Fraction alloc] init];
Fraction *bFraction = [[Fraction alloc] init];

Fraction *resultFraction;

// Set two fractions to 1/4 and 1/2 and add them together.

[aFraction setTo: 1 over: 4];
[bFraction setTo: 1 over: 2];

// Print the results

[aFraction print];
NSLog(@"+");
[bFraction print];
NSLog(@"=");

resultFraction = [aFraction add: bFraction];
[resultFraction print];

[[aFraction add: bFraction] print];

[aFraction release];
[bFraction release];
[resultFraction release];

    [pool drain];
    return 0;
}
Logged

Ash of Melbourne town
skochan
Administrator
Hero Member
*****
Posts: 3114







« Reply #5 on: March 08, 2009, 05:51:37 PM »

You're not setting the numerator or denominator in your result fraction inside your add: method.

Cheers,

Steve Kochan
Logged
willc2
Newbie
*
Posts: 9







« Reply #6 on: March 08, 2009, 05:59:18 PM »

gaahh! Beat me by 30 sec.
 Grin

Logged
skochan
Administrator
Hero Member
*****
Posts: 3114







« Reply #7 on: March 08, 2009, 06:03:33 PM »

No, look at the post times....it's more like 8 minutes.   Grin
Logged
i-ash
Newbie
*
Posts: 10







« Reply #8 on: March 08, 2009, 06:05:18 PM »

Thanks Steve.

I'm starting to struggle with the book a little, in particular the mathematics involved with the algorithms & some key OO concepts...  Embarrassed
Logged

Ash of Melbourne town
skochan
Administrator
Hero Member
*****
Posts: 3114







« Reply #9 on: March 08, 2009, 06:06:35 PM »

Well, we're here to help!

Cheers,

Steve
Logged
kaha
Newbie
*
Posts: 8






« Reply #10 on: June 09, 2009, 10:38:13 PM »

im having a problem. when i run the code it tells me ..'may not repond to add...


#import "Fraction.h"

int main (int argc, char *argv[])
{
   NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
   
   Fraction *aFraction = [[Fraction alloc] init];
   Fraction *bFraction = [[Fraction alloc] init];
   
   Fraction *resultFraction;
   
   [aFraction setTo: 1 over: 4];   // set 1st fraction to 1/4
   [bFraction setTo: 1 over: 2];   // set 2nd fraction to 1/2
   
   [aFraction print];
   NSLog (@"+");
   [bFraction print];
   NSLog (@"=");
   
   resultFraction = [aFraction add: bFraction];
   [resultFraction print];
   
   // This time give the result directly to print
   // memory leakage here!
   
   [[aFraction add: bFraction] print];
   [aFraction release];
   [bFraction release];
   [resultFraction release];
   
   [pool drain];
   return 0;
}




thanks for any help.
Logged
skochan
Administrator
Hero Member
*****
Posts: 3114







« Reply #11 on: June 10, 2009, 01:39:33 AM »

The message typically indicates that the compiler can't find the indicated method defined in the class that uses the method.  Check your Fraction.h and Fraction.m files for the add: method.  Post those files here if you can't isolate the problem.

Cheers,

Steve Kochan
Logged
kaha
Newbie
*
Posts: 8






« Reply #12 on: June 10, 2009, 03:18:00 AM »

still cant isolate the problem... here is codesl

Fraction.h
#import <Foundation/Foundation.h>

// Define the Fraction class



@interface Fraction : NSObject
{
   int numerator;
   int denominator;
}

@property int numerator, denominator;

-(void)      print;
-(void)      setTo: (int) n over: (int) d;
-(double)   convertToNum;
@end



Fraction.m... which now also does not respond to reduce

#import "Fraction.h"


@implementation Fraction
// add a Fraction to the reciever

-(Fraction *) add: (Fraction *) f
{
   // To add two fractions:
   // a/b + c/d = ((a*d) + (b*c)) / (b * d)
   
   // result will store the result of the addition
   Fraction *result = [[Fraction alloc] init];
   int      resultNum, resultDenom;
   
   resultNum = numerator * f.denominator +
      denominator * f.numerator;
   resultDenom = denominator * f.denominator;
   [result setTo: resultNum over: resultDenom];
   [result reduce];
   
   return result;
   
}

@synthesize numerator, denominator;

-(void) print
{
   NSLog (@"%i/%i", numerator, denominator);
}

-(double) convertToNum
{
   if (denominator != 0)
      return (double) numerator / denominator;
   else
      return 1.0;
}

-(void) setTo: (int) n over: (int) d
{
   numerator = n;
   denominator - d;
}
@end



Thanks again so much
Logged
skochan
Administrator
Hero Member
*****
Posts: 3114







« Reply #13 on: June 10, 2009, 04:25:02 AM »

You need to declare the add: method in the interface section for your Fraction class.

Cheers,

Steve Kochan
Logged
kaha
Newbie
*
Posts: 8






« Reply #14 on: June 10, 2009, 07:24:30 AM »

I DID STILL NADA... is it correct?

#import <Foundation/Foundation.h>

// Define the Fraction class

@interface Fraction : NSObject
{
   int numerator;
   int denominator;
}

@property int numerator, denominator;

-(void)         print;
-(double)      convertToNum;
-(Fraction *)   add: (Fraction *) f;
-(void)         reduce;
@end
Logged
Pages: [1] 2   Go Up
Print
Jump to:  



Login with username, password and session length

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 (c) 2009 classroomM.com. All rights reserved.