Amazon.com Widgets Exercise 5, 6
Welcome, Guest. Please login or register.
Did you miss your activation email?
September 16, 2014, 08:24:25 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 13
| | | |-+ Exercise 5, 6
Pages: [1] Go Down
Print
Author Topic: Exercise 5, 6 (Read 1558 times)
alexsom
Jr. Member
**
Posts: 63



Email




on: February 03, 2012, 08:24:20 PM

13.5
Not quite sure if this is the requirement.

Code: (Objective-C)
#import <Foundation/Foundation.h>

typedef struct date {  
                     int day;  
                     int month;  
                     int year;  
                    } Date;  

int main (int argc, const char * argv[])
{

    @autoreleasepool {
        
      
        Date todaysDate = {02,01, 2012};
        
        NSLog(@"Today is %i/%i/%i", todaysDate.day, todaysDate.month, todaysDate.year);
    }
    return 0;
}

13.6
Unsophisticated.

Code: (Objective-C)

#import <Foundation/Foundation.h>

@interface Date : NSObject

@property int month, day, year;

-(void) setMonth:(int) m andDay: (int) d andYear: (int) y;

-(void) print;

-(Date *) dateUpdate;

@end


Code: (Objective-C)
#import "Date.h"

@implementation Date

@synthesize month, day, year;

-(void) setMonth:(int) m andDay: (int) d andYear: (int) y;
{
    month = m;
    day = d;
    year = y;
}

-(void) print;
{
    NSLog(@"%i/%i/%i",month,day,year);
}

-(Date *) dateUpdate;

{
    Date *Update = [[Date alloc] init];
    
    
    if (month ==1 && day==31){
        Update.day = 1;
        Update.month = ++month;
        Update.year = year;}
    
    else if (month ==2 && day==28){
        Update.day = 1;
        Update.month = ++month;
        Update.year = year;}
    
    else if (month ==2 && day==29){
        Update.day = 1;
        Update.month = ++month;
        Update.year = year;}
    
    else if (month ==3 && day==31){
        Update.day = 1;
        Update.month = ++month;
        Update.year = year;}
    
     else if (month ==4 && day==30){
        Update.day = 1;
        Update.month = ++month;
        Update.year = year;}
    
    else if (month ==5 && day==31){
        Update.day = 1;
        Update.month = ++month;
        Update.year = year;}
    
    else if (month ==6 && day==30){
        Update.day = 1;
        Update.month = ++month;
        Update.year = year;}
    
     else if (month ==7 && day==31){
        Update.day = 1;
        Update.month = ++month;
        Update.year = year;}
    
    else if (month ==8 && day==31){
        Update.day = 1;
        Update.month = ++month;
        Update.year = year;}
    
    else if (month ==9 && day==30){
        Update.day = 1;
        Update.month = ++month;
        Update.year = year;}
    
    else if (month ==10 && day==31){
        Update.day = 1;
        Update.month = ++month;
        Update.year = year;}
    
    else if (month ==11 && day==30){
        Update.day = 1;
        Update.month = ++month;
        Update.year = year;}
    
    else if (month ==12 && day==31){
        Update.day = 1;
        Update.month = 1;
        Update.year = ++year;}
  
    else {
    Update.day = ++day;
    Update.month = month;
    Update.year = year;}
    
    return Update;
}

@end


Code: (Objective-C)
#import <Foundation/Foundation.h>
#import "Date.h"
int main (int argc, const char * argv[])
{

    @autoreleasepool
    
    {
      
        Date *today = [[Date alloc] init];
        Date *tomorrow = [[Date alloc] init];
        
        [today setMonth:12 andDay:31 andYear:2012];
        [today print];
        
         tomorrow = [today dateUpdate];
         [tomorrow print];
        
    }
    return 0;
}

Last Edit: February 04, 2012, 01:34:32 PM by alexsom Logged
skochan
Administrator
Hero Member
*****
Posts: 3114







Reply #1 on: February 03, 2012, 08:51:35 PM

How about using an array of 12 integers, where each entry in the array contains the number of days in the month corresponding to the element number.  Then you simply use the month number as an index into the array to find out how many days it has.

Cheers,

Steve
Logged
alexsom
Jr. Member
**
Posts: 63



Email




Reply #2 on: February 04, 2012, 12:33:35 PM

Sir,
Believe it or not I'm staring at this code since yesterday and I can't figure it out why it doesn't work properly, without the leap year involved, only in this simple form. What can possibly be wrong with it?

Code: (Objective-C)
-(Date *) dateUpdate;

{
    Date *Update = [[Date alloc] init];
    
    Update.month = month;
    Update.day = day;
    Update.year = year;
    
    static int months[]={1,2,3,4,5,6,7,8,9,10,11,12};
    
    if (day==31 && months[11])
         {  
            Update.day = 1;
            Update.month = 1;
            Update.year = ++year;
         }
    
    else if (day == 31 && (months[0] || months[2] || months[4] || months[6] || months[7] || months[9]))
       {
        Update.day = 1;
        Update.month = ++month;
        Update.year = year;
       }
    
    else if (day == 30 && (months[3] || months[5] || months[8] || months[10]))
        
       {
        Update.day = 1;
        Update.month = ++month;
        Update.year = year;
       }
      
    
    else if (day == 28 && months[1] )
       {
        Update.day =1;
        Update.month = ++month;
        Update.year = year;
        }
    else
    {
        Update.day = ++day;
        Update.month = month;
        Update.year = year;
    }
    
    
    return Update;
    
}


Last Edit: February 04, 2012, 01:35:28 PM by alexsom Logged
skochan
Administrator
Hero Member
*****
Posts: 3114







Reply #3 on: February 04, 2012, 01:09:22 PM

First, the logic in your if statements are not correctly formed.     Rather than addressing that, you need to step back and think about how to use the array. What you want to do is compare the value of months[month] against day.    If they're equal, you're at the end of the month (of course, feb of a leap year is still a special case).   Just be careful with your month number.   If it ranges from 1-12, then you need to subtract 1 before doing the lookup in the months array, whose index values go from 0 to 11.

Cheers,

Steve
Last Edit: February 04, 2012, 01:12:59 PM by skochan Logged
alexsom
Jr. Member
**
Posts: 63



Email




Reply #4 on: February 04, 2012, 01:37:42 PM

I've changed the values of the array (dahh...that was big Cry), I need to work on the logic of if statements now. Thank you, alex
Logged
alexsom
Jr. Member
**
Posts: 63



Email




Reply #5 on: February 04, 2012, 04:57:38 PM

Interesting discovery in here:

Function works well if I write (month == 1,2,3..and so on..) but not if I replace it with the values months from my array (static int months[] = {1,2,3,4,5,6,7,8,9,10,11,12}).  Huh
 
Both displayed below;

Code: (Objective-C)
-(Date *) dateUpdate;

{
    Date *Update = [[Date alloc] init];
    
    Update.day = day;
    Update.month = month;
    Update.year = year;
    
    
    if (day == 31 && month==12)
        {      
          
            Update.month = 1;
            Update.day = 1;
            Update.year = ++year;
        }
        else if (day == 31 && (month==1 || month==3 || month==5 || month==7 || month==8 || month==10 || month==12))
        {
            Update.day = 1;
            Update.month = ++month;
            Update.year = year;
        }
        
    
       else if ( day == 30 && (month==4 || month==6 || month==9 || month==11))
    
       {
        Update.day = 1;
        Update.month = ++month;
        Update.year = year;
       }
    
       else if (day == 28 && month == 2)
       {
        Update.day = 1;
        Update.month = ++month;
        Update.year = year;
       }  
    else
        Update.day= ++day;
    
    return Update;
    
}
@end

Code: (Objective-C)
-(Date *) dateUpdate;

{
    Date *Update = [[Date alloc] init];
    
    
    Update.day = day;
    Update.month = month;
    Update.year = year;
    
    static int months[] = {1,2,3,4,5,6,7,8,9,10,11,12};
    
    if (day == 31 && months[11])
        {      
            Update.day = 1;
            Update.month = 1;
            Update.year = ++year;
        }
        
        
    else if(day == 31 && (months[0] || months[2] || months[4] || months[6] || months[7] || months[9] || months[11]))
    {
       Update.day = 1;
       Update.month = ++month;
       Update.year = year;
        
    }  
    else if ( day == 30 && (months[3] || months[5] || months[8] || months[10]))
    {
        Update.day = 1;
        Update.month = ++month;
        Update.year = year;
    }
    else if (day == 28 && months[1])
    {
        Update.day = 1;
        Update.month = ++month;
        Update.year = year;
    }  
    else
        Update.day= ++day;
    
    
    
    return Update;
    
}
@end


Last Edit: February 04, 2012, 06:40:35 PM by alexsom Logged
alexsom
Jr. Member
**
Posts: 63



Email




Reply #6 on: February 04, 2012, 08:19:14 PM

I also tried with the switch case and a pointer to the array. It displays an output, but erroneous most of the time. The same function's layout with regular integers display very good results with only one exception: for 12/30/12, 5/30/12...and so on...for every month that should be 31 days, it displays a copy of date.......how is that possible?

Here are both displayed:
Code: (Objective-C)
-(Date *) dateUpdate;  


    Date *Update = [[Date alloc] init]; 
   
    Update.day = day; 
    Update.month = month; 
    Update.year = year; 
   
    static int months[] = {1,2,3,4,5,6,7,8,9,10,11,12};
    int *monthsPtr;
    monthsPtr = months;
   
    switch (day)
    {
        case (31):
            if(*(monthsPtr +11))
            {
                Update.day=1;
                Update.month = 1;
                Update.year = ++year;
            }
            else if (*(monthsPtr+0) || *(monthsPtr +2) || *(monthsPtr +4) || *(monthsPtr +6) || *(monthsPtr +7) || *(monthsPtr +9))
            {
                Update.day = 1;
                Update.month= ++month;
                Update.year = year;
            }
            break;
        case (30):
            if (*(monthsPtr+3) || *(monthsPtr +5) || *(monthsPtr +8) || *(monthsPtr +10))
            {
                Update.day = 1;
                Update.month= ++month;
                Update.year = year;
            }   
            break;
        case (28):
            if (*(monthsPtr+1))
            {
                Update.day = 1;
                Update.month= ++month;
                Update.year = year;
            }   
           
        default:
            Update.day= ++day;
           
    }
   
    return Update;
}   

@end 


Code: (Objective-C)
-(Date *) dateUpdate;  


    Date *Update = [[Date alloc] init]; 
   
    Update.day = day; 
    Update.month = month; 
    Update.year = year; 
   
   
    switch (day)
    {
        case (31):
            if(month==12)
            {
                Update.day=1;
                Update.month = 1;
                Update.year = ++year;
            }
            else if (month==1 || month==3 || month==5 || month==7 || month==8 || month==10)
            {
                Update.day = 1;
                Update.month= ++month;
                Update.year = year;
            }
            break;
        case (30):
            if (month==4 || month==6 || month==9 || month==11)
            {
                Update.day = 1;
                Update.month= ++month;
                Update.year = year;
            }   
            break;
        case (28):
            if (month==2)
            {
                Update.day = 1;
                Update.month= ++month;
                Update.year = year;
            }   
            break;
        default:
            Update.day= ++day;
    }
   
    return Update;
}   

@end 

Logged
kevinrivers
Newbie
*
Posts: 1


Email




Reply #7 on: February 08, 2012, 10:45:27 PM

alexsom,

For the cases of 12/30/12, 5/30/12, etc you have accounted for the else condition. In other words, if those months are not April, June, September or November what should happen?

In your case:

Code: (Objective-C)
++Update.day;

or

Code: (Objective-C)
Update.day = ++day;

Depending on what you decide.

You don't have an else condition that is invoking the increment, which is why nothing is happening for these days.

Lastly, you should abandon using pointers here. In the example here you don't have a test condition so assuming there is data in those memory locations (there will likely be even if it isn't your memory), they will always be true.

In fact, most of the logic there isn't even going to execute given the above fact. Stick with the plain old month == tests.

Some additional considerations:
  • overide init
  • Add an initWith... method
  • Disallow the setting of a non-valid date such as 11/31/2012. Returning a valid date would be a good way to handle this: 11/30/2012.
  • Don't forget leap years  Wink
Last Edit: February 08, 2012, 10:48:53 PM by kevinrivers Logged
alexsom
Jr. Member
**
Posts: 63



Email




Reply #8 on: February 18, 2012, 09:42:18 AM

thanks...imagine if I can't get properly done these simple exercises...do you expect me to write IOS applications?...man. I am crawling back under my rock.
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.