Hey,
I have made a program that basically takes 2 parents blood groups (genotypes) and finds all the combinations that the baby's genotype could be, stores it in an NSArray and then selects a random one for the baby.
i.e. if the genotypes were OA for the father and BO for the mother, the baby's possible genotypes would be: OB, OO, AB, AO.
The problem however is that the array initially seems to store all the right values, but when it saves it returns the array later it seems to save an array of only AO (it saves this 4 times) and so the baby no matter what seems to have AO always.
Here is the code:
Human.h
Human.m
BloodType.h
BloodType.m
ArrayCategory.h
ArrayCategory.m
main.m
And that's it, I'm not sure if I have outlined the problem enough, but please if you could help, please do.
I have made a program that basically takes 2 parents blood groups (genotypes) and finds all the combinations that the baby's genotype could be, stores it in an NSArray and then selects a random one for the baby.
i.e. if the genotypes were OA for the father and BO for the mother, the baby's possible genotypes would be: OB, OO, AB, AO.
The problem however is that the array initially seems to store all the right values, but when it saves it returns the array later it seems to save an array of only AO (it saves this 4 times) and so the baby no matter what seems to have AO always.
Here is the code:
Human.h
Code:
#import <Cocoa/Cocoa.h>
#import "ArrayCategory.h"
#import "BloodType.h"
@class BloodType;
@interface Human : NSObject
{
BloodType * blood;
}
-(Human *) makeBabyWith: (Human *) other;
-(id) init;
-(void) dealloc;
@property (nonatomic, retain) BloodType * blood;
@end
Human.m
Code:
#import "Human.h"
@implementation Human
-(Human *) makeBabyWith: (Human *) other
{
Human * baby = [[Human alloc]init];
NSArray * arr = [NSArray arrayWithArray:[self.blood getAllCombosWith: other.blood]];
baby.blood = [arr tpRandomObject];
[arr release];
//baby.blood = [[self.blood getAllCombosWith: other.blood] tpRandomObject];
return baby;
}
-(id) init
{
if (self =[super init])
{
blood = [[BloodType alloc] init ];
nos = 3;
}
return (self);
}
-(void) dealloc
{
[blood release];
[super dealloc];
}
-(NSString *) description
{
return [self.blood description];
}
@synthesize blood, nos;
@end
Code:
#import <Cocoa/Cocoa.h>
#import "ArrayCategory.h"
typedef enum {O = 1, A, B} bloodType;
@interface BloodType : NSObject
{
bloodType allele1;
bloodType allele2;
int number;
}
-(NSString *) alleleAsString;
-(NSMutableArray *) getAllCombosWith: (BloodType *) otherBlood;
@property bloodType allele1;
@property bloodType allele2;
@property int number;
@end
BloodType.m
Code:
#import "BloodType.h"
@implementation BloodType
-(NSArray *) getAllCombosWith: (BloodType *) otherBlood
{
NSMutableArray * array = [NSMutableArray arrayWithCapacity:4];
BloodType * tempBlood = [[BloodType alloc]init];
tempBlood.allele1 = self.allele1;
tempBlood.allele2 = otherBlood.allele1; //OB
[array addObject:tempBlood];
tempBlood.allele1 = self.allele1;
tempBlood.allele2 = otherBlood.allele2;
[array addObject:tempBlood]; //OO
tempBlood.allele1 = self.allele2;
tempBlood.allele2 = otherBlood.allele1;
[array addObject:tempBlood]; //AB
tempBlood.allele1 = self.allele2;
tempBlood.allele2 = otherBlood.allele2;
[array addObject:tempBlood]; //AO
[tempBlood release];
return array;
}
-(NSString *) alleleAsString
{
//NSDictionary * dict = [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithInt: 0],@"O", [NSNumber numberWithInt: 1], @"A",[NSNumber numberWithInt: 2], @"B", nil];
NSMutableString * a1 = [[NSMutableString alloc]init];
NSMutableString * a2 = [[NSMutableString alloc]init];
switch (self.allele1) {
case O:
[a1 appendString: @"O"];
break;
case A:
[a1 appendString: @"A"];
break;
case B:
[a1 appendString: @"B"];
break;
default:[a1 appendString: @"***There was an error!***"];
break;
}
//NSLog(@"this is %@", a1);
switch (self.allele2) {
case O:
[a2 appendString: @"O"];
break;
case A:
[a2 appendString: @"A"];
break;
case B:
[a2 appendString: @"B"];
break;
default:[a2 appendString: @"***There was an error!***"];
break;
}
//NSLog(@"this is %@", a2);
[a1 autorelease];
[a2 autorelease];
NSString * retString = [NSString stringWithFormat: @"blood genotype is %@ %@", a1 , a2];
[retString autorelease];
return (retString);
//return (@"My blood genotype is %@ %@", [dict objectForKey: [NSNumber numberWithInt: self.allele1]],
// [dict objectForKey: [NSNumber numberWithInt: self.allele2]]); //Check how to fix
}
Code:
#import <Cocoa/Cocoa.h>
@interface NSArray (ArrayCategory)
-(id) tpRandomObject;
@end
Code:
#import "ArrayCategory.h"
@implementation NSArray (ArrayCategory)
-(id) tpRandomObject
{
long int x = arc4random();
int y = x % 4;
return [self objectAtIndex: y];
}
@end
Code:
#import <Foundation/Foundation.h>
#import "Human.h"
#import "BloodType.h"
#import "ArrayCategory.h"
int main (int argc, const char * argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
Human * Joe = [[Human alloc]init];
Human * Jenny = [[Human alloc]init];
Human * baby = [[Human alloc]init];
Joe.blood.allele1 = O;
Joe.blood.allele2 = A;
//NSLog(@"%@", Joe);
NSLog(@"Joe's %@ ", [Joe.blood description]);
Jenny.blood.allele1 = B;
Jenny.blood.allele2 = O;
NSLog(@"Jenny says \"My %@\"", Jenny);
[Joe.blood setNumber: 5];
[Joe setNos:6];
baby = [Joe makeBabyWith: Jenny];
NSLog(@"The baby's %@", baby);
// The baby's blood genotype is A O
NSLog(@"%i", [Joe retainCount] ); //1
NSLog(@"%i", [Jenny retainCount] ); //1
NSLog(@"%i", [baby retainCount] ); //1
NSLog(@"%i", [Joe.blood retainCount] ); //1
[Joe release];
[Jenny release];
[baby release];
[pool drain];
return 0;
}
Last edited by a moderator: