diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000..96bd9f5 Binary files /dev/null and b/.DS_Store differ diff --git a/TestAssessment/Person.h b/TestAssessment/Person.h index d331757..42160bf 100644 --- a/TestAssessment/Person.h +++ b/TestAssessment/Person.h @@ -12,6 +12,8 @@ @interface Person : NSObject + + - (void)setName:(NSString *)name; - (NSString *)name; diff --git a/TestAssessment/TestViewController.m b/TestAssessment/TestViewController.m index 013428d..15ec7e6 100644 --- a/TestAssessment/TestViewController.m +++ b/TestAssessment/TestViewController.m @@ -18,30 +18,58 @@ @implementation TestViewController This method should return any positive NSInteger (hint: cannot be 0) */ -- (void)shouldReturnAPositiveNSInteger { +- (NSInteger)shouldReturnAPositiveNSInteger{ +// if (i > 0) { +// return i; +// } +// i = i * -1; +// return i; +//} + + return 4; } /* This method should return any negative CGFloat (hint: cannot be 0) */ -- (void)shouldReturnANegativeCGFloat { +- (CGFloat)shouldReturnANegativeCGFloat{ + +// if (i < 0) { +// return i; +// } +// i = -i; +// +// return i; + return -8.0; } /* This method should return a falsy boolean Falsey: Something which evaluates to FALSE. */ -- (void)shouldReturnAFalseyBool { +- (BOOL)shouldReturnAFalseyBool { + + return NO; } /* This method should return a single char from a - z */ -- (void)shouldReturnACharAtoZ { +- (char)shouldReturnACharAtoZ{ + +// char aLetter; +//// char anArray[26] = "abcdefghijklmnopqrstuvwxyz"; +//// +//// while (<#condition#>) { +//// <#statements#> +//// } +// +// return aLetter; + return 'c'; } /* @@ -49,7 +77,13 @@ - (void)shouldReturnACharAtoZ { 0 - 99 using a loop (. 1 + 2 + 3 + ... + 98 + 99) */ - (NSInteger)shouldReturnSumOf0To100 { - return 0; + + int sum = 0; + for (int i = 0; i < 100; i++) { + sum = sum + i; + } + + return sum; } /* @@ -57,7 +91,12 @@ Given a c array (int[]) and a count, return the sum of the numbers within the ar (eg. arr[0] + arr[1] ...) */ - (NSInteger)shouldReturnSumOfArrayValues:(int *)arr withSize:(int)count { - return 0; + + int sum = 0; + for (int i = 0; i < count; i++) { + sum = sum + arr[i]; + } + return sum; } /* @@ -67,14 +106,40 @@ Provided a C string (array of chars), return the character (hint: while loop) */ - (char)shouldReturnCharBeforeQ:(char *)str { - return '\0'; + +// int i = 0; +// +// while (str[i] != 'q') { +// +// if (str[i] == 'q') { +// return str[i-1]; +// } +// +// i++; +// } +// //I know this is incorrect +// return str[i]; + + + int i = 0; + + while (str[i] != 'q') { + //the loop will continue until it finds a 'q'. when it does it will break out of the loop. No need for an if statement. + i++; + } + + return str [i - 1]; + } /* This method should return the sum of aNumber + bNumber */ - (NSInteger)sumOfAnInteger:(NSInteger)aNumber andAnotherInteger:(NSInteger)bNumber { - return 0; + + NSInteger sum = aNumber + bNumber; + + return sum; } @@ -82,14 +147,27 @@ - (NSInteger)sumOfAnInteger:(NSInteger)aNumber andAnotherInteger:(NSInteger)bNum This method should return a YES if aNumber is odd */ - (BOOL)isOdd:(NSInteger)aNumber { - return NO; + + if (aNumber %2 == 0) { + return NO; + } + + return YES; } /* This method should return YES if aNumber is a multiple of 5 */ - (BOOL)isMultipleOfFive:(NSInteger)aNumber { - return NO; + //the below works! +// if (aNumber %5 == 0) { +// return YES; +// } +// return NO; + + //This would also work: + return aNumber %5 == 0; + //same for the above } /* @@ -97,6 +175,11 @@ - (BOOL)isMultipleOfFive:(NSInteger)aNumber { */ - (BOOL)returnYesIfThisNumberIsOdd:(NSInteger)aNumber andThisNumberIsEven:(NSInteger)bNumber { + + if (aNumber %2 != 0 && bNumber %2 == 0) { + return YES; + } + return NO; } @@ -105,14 +188,15 @@ - (BOOL)returnYesIfThisNumberIsOdd:(NSInteger)aNumber parameter (hint: command + click on class name to jump to the interface. */ - (NSString *)shouldReturnPersonsName:(Person *)person { - return @""; + + return [person name]; } /* This method should change the person name to "Ada Lovelace" */ -- (void)changePersonsNameToAdaLovelace:(Person *)person { - +- (void) changePersonsNameToAdaLovelace:(Person *)person { + [person setName:@"Ada Lovelace"]; } /* @@ -122,7 +206,11 @@ - (void)changePersonsNameToAdaLovelace:(Person *)person { 3) Set the person's age to 1823 */ - (Person *)createAndReturnPersonWithSomeProperties { - return [[Person alloc] init]; + Person *anotherPerson = [[Person alloc] init]; + [anotherPerson setName:@"Santa Clause"]; + [anotherPerson setAge:1823]; + + return anotherPerson; } /* @@ -133,7 +221,8 @@ - (Person *)createAndReturnPersonWithSomeProperties { */ - (void)makePersonSitInChair:(Chair *)chair { - + Person *person = [[Person alloc] init]; + [person sitInChair:chair]; } /* @@ -141,6 +230,7 @@ - (void)makePersonSitInChair:(Chair *)chair { Send a message to this Person object telling it to stand up */ - (void)makePersonStandUp:(Person *)person { + [person standUp]; } @@ -149,7 +239,23 @@ - (void)makePersonStandUp:(Person *)person { https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSArray_Class/ */ - (NSArray *)createAndReturnNSArray { - return [[NSArray alloc] init]; + + + NSArray *arr = [[NSArray alloc] init]; + NSString *object1 = @"one"; + NSString *object2 = @"two"; + NSString *object3 = @"three"; + NSString *object4 = @"four"; + NSString *object5 = @"five"; + NSString *object6 = @"six"; + + arr = [NSArray arrayWithObjects:object1, object2, object3, object4, object5, object6, nil]; + + return arr; + + //also, the following would work: + //return [NSArray arrayWithObjects: @"one", @"two", @"three", @"four", @"five", @"six", nil]; + //Because "arrayWithObjects" does the alloc init for you. } // BONUS @@ -161,6 +267,11 @@ - (NSArray *)createAndReturnNSArray { - (void)changeValueOfIndexFourInArray:(NSMutableArray *)arr toPersonsName:(Person *)person { + [arr replaceObjectAtIndex:4 withObject:[person name]]; + + //this also would work, but it might not give you "bounce checking", i.e. give you an error msg, it might just silently fail + //arr[4] = [person name]; + } // BONUS @@ -172,7 +283,14 @@ - (void)changeValueOfIndexFourInArray:(NSMutableArray *)arr - (NSString *)repeatString:(NSString *)str numberOfTimes:(NSInteger)x { - return @""; + + //from review in class + NSMutableString *finalString = [[NSMutableString alloc] init]; + for (int i = 0; i < x; i++) { + [finalString appendString:str]; + } + + return finalString; } // BONUS @@ -184,7 +302,28 @@ - (NSString *)repeatString:(NSString *)str (ex: [200, 500, 100, 400] returns 800) */ - (int)returnSumWhileSumIsLessThan1050:(int *)arr { - return 0; + + //from in-class review +// int i = 0; +// int sum = 0; +// +// while (sum <= 1050) { +// sum = sum + arr[i]; +// i++; +// } +// +// return sum - arr [i - 1 ]; + + //Caleb's solution + int i = 0; + int sum = 0; + + while (sum + arr[i] <= 1050) { + sum = sum + arr[i]; + i++; + } + + return sum; } @end