Amazon.com Widgets chapter 6 exercise 3
Welcome, Guest. Please login or register.
Did you miss your activation email?
April 16, 2014, 03:48:06 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
|-+ Programming in Objective-C, 4th edition
| |-+ Exercises
| | |-+ Chapter 6
| | | |-+ chapter 6 exercise 3
Pages: [1] Go Down
Print
Author Topic: chapter 6 exercise 3 (Read 2117 times)
stan4ev
Newbie
*
Posts: 7


Email




on: February 15, 2012, 04:39:44 PM

This is my solution of the exercise! i hope it helps someone! thanks!


#import <Foundation/Foundation.h>

@interface Fraction : NSObject

-(void) print;
-(void) setNumerator: (int) n;
-(void) setDenominator: (int) d;
-(int) numerator;
-(int) denominator;
-(double) convertToNum;

@end

@implementation Fraction
{
    int numerator;
    int denominator;
}

-(void) print
{
    if (denominator == 1)
        NSLog(@"%i", numerator);
   
    else if (numerator == 0)
        NSLog(@"%i", numerator);
   
    else
        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;
}

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

@end

int main (int argc, char * argv[])
{
    @autoreleasepool {
       
        Fraction *aFraction = [[Fraction alloc]init];
       
        int n, d;
       
        NSLog(@"Type in your numerator:");
        scanf("%i",&n);
       
        NSLog(@"Type in your denominator:");
        scanf("%i",&d);
       
        [aFraction setNumerator:n];
        [aFraction setDenominator:d];
       
        [aFraction print];
       
        NSLog(@" =");
        NSLog(@"%g", [aFraction convertToNum]);
       
               
       
    }
    return 0;
}
Logged
elkie13
Newbie
*
Posts: 17






Reply #1 on: February 21, 2012, 02:07:41 PM

Mine was the same but I managed to skip an if statement in the print method

-(void) print
{
    if (denominator == 1 || numerator == 0)
        NSLog(@"%i", numerator);
    else
    NSLog(@"%i/%i",numerator,denominator);
}
Logged
rzs
Newbie
*
Posts: 5






Reply #2 on: February 25, 2012, 10:03:56 AM

I don't understand the need to modify the fraction class with regards to making it display 5/1 as 5. When I type the fraction 5/1 or any fraction that divides evenly it already returns a whole number. 5 in this case? The convertToNum method does this already! What am I missing?
Logged
Antoniogra7
Newbie
*
Posts: 6



Email




Reply #3 on: February 26, 2012, 12:06:50 PM

I think you are not doing it right. The first exercise (display the whole numbers as such) means that if the result of the fraction is a whole number, it display it instead of the fraction (2 instead of 4/2 or 5 instead 5/1).

This would be the print method implementation:
Code: (Objective-C)
-(void) print
{
    if (numerator == 0) {
        NSLog(@"0");
    } else if (numerator % denominator == 0) {
        NSLog(@"%i", numerator / denominator);
    } else {
        NSLog(@"%i/%i", numerator, denominator);
    }
}
Logged
rzs
Newbie
*
Posts: 5






Reply #4 on: February 27, 2012, 10:31:30 AM

My program displays both the faction and the result. So the point is to show only one of those depending upon wether it divides evenly or not! Cheers:)
Logged
clouded
Full Member
***
Posts: 123






Reply #5 on: May 11, 2012, 10:46:27 AM

Here's what I have:

Code: (Objective-C)
// Chapter 6 Exercise 3.
// Modify the print method from the Fraction class so that
// whole numbers are displayed as such (so the fraction 5/1
// should display as simply 5). Also modify the method to
// display fractions with a numerator of 0 as simply zero.

#import <Foundation/Foundation.h>

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

@implementation Fraction
{
    int numerator;
    int denominator;
}
-(void) print
{
    if (denominator == 0 && numerator == 0) {
        NSLog (@" %i ", numerator);
    }
    else if (denominator == 0 && numerator != 0) {
        NSLog (@"div by 0 undefined");
    }
    else if (denominator == 1) {
        NSLog (@" %i ", numerator);
    }
    else {
        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;
}
-(double) convertToNum
{
    if (denominator != 0)
        return (double) numerator / denominator;
    else
        return NAN;
}
@end

int main (int argc, char * argv[])
{
    @autoreleasepool {
        Fraction *aFraction = [[Fraction alloc] init];
        Fraction *bFraction = [[Fraction alloc] init];
    
        [aFraction setNumerator: 2]; // set input here
        [aFraction setDenominator: 4]; // set input here
        [aFraction print];
        NSLog (@" =");
        NSLog (@"%g", [aFraction convertToNum]);
        [bFraction print]; // never assigned a value NSLog (@" =");
        NSLog (@"%g", [bFraction convertToNum]);
    }
    return 0;
}
Output:

Input: 2 4
 2/4
 =
0.5
 0
nan

Input: 0 0
 0
 =
nan
 0
nan

Input: 0 1
 0
 =
0
 0
nan

Input: 2 1
 2
 =
2
 0
nan

Input: 5 0
div by 0 undefined
 =
nan
 0
nan
Last Edit: May 11, 2012, 05:26:05 PM by clouded Logged
stuartjj
Newbie
*
Posts: 11






Reply #6 on: June 05, 2012, 11:16:15 AM

I don't understand the need to modify the fraction class with regards to making it display 5/1 as 5. When I type the fraction 5/1 or any fraction that divides evenly it already returns a whole number. 5 in this case? The convertToNum method does this already! What am I missing?

The same for me.  Program 6.2 in the book already contains the solution, without having to modify the program.
Logged
mo7ionsickness
Jr. Member
**
Posts: 50






Reply #7 on: July 24, 2012, 02:09:31 PM

the instruction states 'whole numbers'
using convertToNum on 1/2 will result to .5 which isn't a 'whole number'

here's my take:

Code: (Objective-C)
-(void) print
{
    if (numerator == 0)
        NSLog(@"%i", 0);
    else
        if (denominator == 0)
            NSLog(@"Invalid denominator");
        else
            if (numerator % denominator == 0)
                NSLog(@"%i", numerator/denominator);
            else
                NSLog(@"%i / %i", numerator, denominator);
}

Code: (Objective-C)
        [myFraction setNumerator:0];
        [myFraction setDenominator:1];

output:

2012-07-24 21:58:48.830 prog6[5081:403] 0

Code: (Objective-C)
        [myFraction setNumerator:1];
        [myFraction setDenominator:0];

output:

2012-07-24 22:05:11.804 prog6[5089:403] Invalid denominator

Code: (Objective-C)
        [myFraction setNumerator:6];
        [myFraction setDenominator:1];

output:

2012-07-24 22:06:11.527 prog6[5096:403] 6

Code: (Objective-C)
        [myFraction setNumerator:6];
        [myFraction setDenominator:3];

output:

2012-07-24 22:07:03.207 prog6[5104:403] 2

Code: (Objective-C)
        [myFraction setNumerator:6];
        [myFraction setDenominator:5];

output:

2012-07-24 22:07:56.283 prog6[5111:403] 6 / 5
Logged
MacD
Newbie
*
Posts: 20






Reply #8 on: January 02, 2013, 05:51:36 PM

Here is my take on it with sample output.

Code: (Objective-C)
/*
 Programming in Objective-C, 4th edition
 Chapter 6 Exercise 6.3
 Modification of example program 6.2
 */

#import <Foundation/Foundation.h>

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

@implementation Fraction
{
int numerator;
int denominator;
}
-(void) print
{
if ((numerator > denominator) && denominator != 0 && denominator != 1)
{
NSLog (@" %i %i/%i ", (numerator / denominator), (numerator % denominator), denominator);
    }
   
    else if (denominator == 1)
    {
        NSLog (@" %i ", numerator);
    }
   
    else if (denominator == 0)
    {
        NSLog (@" %i ", denominator);
    }

else
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;
}

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

int main (int argc, char * argv[])
{
@autoreleasepool {
Fraction *aFraction = [[Fraction alloc] init];
Fraction *bFraction = [[Fraction alloc] init];
[aFraction setNumerator: 1]; // 1st fraction is 1/4
[aFraction setDenominator: 4];
[aFraction print];
NSLog (@" =");
NSLog (@"%g", [aFraction convertToNum]);
[bFraction print]; // never assigned a value
NSLog (@" =");
NSLog (@"%g", [bFraction convertToNum]);
        [aFraction setNumerator: 5]; // 2nd fraction is 5/0
[aFraction setDenominator: 0];
[aFraction print];
NSLog (@" =");
NSLog (@"%g", [aFraction convertToNum]);
        [aFraction setNumerator: 7]; // 3rd fraction is 7/3
[aFraction setDenominator: 3];
[aFraction print];
NSLog (@" =");
NSLog (@"%g", [aFraction convertToNum]);
        [aFraction setNumerator: 7]; // 4th fraction is 7/1
[aFraction setDenominator: 1];
[aFraction print];
NSLog (@" =");
NSLog (@"%g", [aFraction convertToNum]);
}
return 0;
}

Output:
  1/4
  =
 0.25
  0
  =
 nan
  0
  =
 nan
  2 1/3
  =
 2.33333
  7
  =
 7
Logged
Rfields
Newbie
*
Posts: 5






Reply #9 on: January 08, 2013, 12:20:10 PM

Shouldn't line 35 and 37 say numerator?
Logged
JeroenJK
Newbie
*
Posts: 40







Reply #10 on: January 27, 2013, 09:29:11 AM

My exercise 6.3.

Code: (Objective-C)
//---interface section---
 @interface Fraction : NSObject
 
 -(void) setNumerator;
 -(void) setDenominator;
 
 -(int) numerator;
 -(int) denominator;
 
 -(void) print;
 
 -(double) number;
 
 @end
 //---implementation section---
 @implementation Fraction
 {
 int numerator;
 int denominator;
 double number;
 int n;
 int d;
 }
 
 -(void) setNumerator
 {
 NSLog(@"Please enter the numerator. (Only use integers!)");
 scanf(" %i", &n);
 numerator = n;
 }
 
 -(void) setDenominator
 {
     NSLog(@"Please enter the denominator. (Only use integers!)");
     scanf(" %i", &d);
    
     while ( d == 0 ) {
         NSLog(@"Please don't enter a 0!\nEnter you denominator again...");
         scanf(" %i", &d);
     }
 denominator = d;
 }
 
 -(int) numerator
 {
 return numerator;
 }
 
 -(int) denominator
 {
     if ( denominator == 0)
         return NAN;
     else
         return denominator;
 }
 
 -(void) print
 {
     if ( numerator == 0 && denominator != 0 )
         NSLog(@"0");
     else if ( denominator == 0 )
         NSLog(@"The denominator cannot be 0!");
     else
         NSLog(@"The fraction is %i/%i", numerator, denominator);
 }
 
 -(double) number
 {
     if ( (denominator != 0) && (denominator % numerator == 0) && (numerator >= denominator) )
         number = numerator / denominator;
     else if ( numerator == 0 && denominator != 0 )
         number = 0;
     else if ( denominator== 0 )
         return NAN;
     else
         number = (double) numerator / denominator;
    
     return number;
 }
 
 @end
 //---program---
 int main(int argc, char * argv[]) {
     @autoreleasepool {
 Fraction *aFraction = [[Fraction alloc] init];
 
 [aFraction setNumerator];
 [aFraction setDenominator];
 
 NSLog(@"The numerator is now %i", [aFraction numerator]);
 NSLog(@"The denominator is now %i", [aFraction denominator]);
 [aFraction print];
 NSLog(@"The real number of the fraction is %.2g", [aFraction number]);
    }
    
     return 0;
 }

But what is wrong in line 69?
When the numerator is 0 and the denominator is 1 or higher I get this in my output (for example):
Quote
2013-01-27 17:29:47.281 ObjC[3755:303] Please enter the numerator. (Only use integers!)
0
2013-01-27 17:29:48.363 ObjC[3755:303] Please enter the denominator. (Only use integers!)
4
2013-01-27 17:29:48.836 ObjC[3755:303] The numerator is now 0
2013-01-27 17:29:48.838 ObjC[3755:303] The denominator is now 4
2013-01-27 17:29:48.838 ObjC[3755:303] 0
(lldb)

And the error looks like this:
Last Edit: January 27, 2013, 09:31:47 AM by JeroenJK Logged

Just started learning Objective-C (fourth edition), so don't expect too much from me. Wink
Spark Plug
Newbie
*
Posts: 1






Reply #11 on: February 02, 2013, 07:42:05 AM

@JeroenJK

Hey mate, on line 69 you are doing denominator % numerator which means that you are dividing the denominator by the numerator. This would cause a division by 0 when the numerator is set as 0, which is exactly why you are having a runtime error. I think what you actually intended to do was (numerator % denominator ==0)  Wink
Logged
Pages: [1] 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.