Skip to content

Instantly share code, notes, and snippets.

@daegren
Created May 2, 2017 19:56
Show Gist options
  • Save daegren/a0ca0bbd83326a832fea5611915d9f89 to your computer and use it in GitHub Desktop.
Save daegren/a0ca0bbd83326a832fea5611915d9f89 to your computer and use it in GitHub Desktop.
W1D2 breakout demo
//
// main.m
// BreakoutW1D2
//
// Created by David Mills on 2017-05-02.
// Copyright © 2017 David Mills. All rights reserved.
//
#import <Foundation/Foundation.h>
#import "Rectangle.h"
#import "Square.h"
int main(int argc, const char * argv[]) {
@autoreleasepool {
NSMutableArray *rectangles = [[NSMutableArray alloc] init];
// insert code here...
Rectangle *rect = [[Rectangle alloc] initWithWidth:25.0 andHeight:15.0];
Rectangle *rect2 = [[Rectangle alloc] initWithWidth:7.0 andHeight:23.3];
Rectangle *rect3 = [[Rectangle alloc] initWithWidth:42.0 andHeight:5.0];
Square *square = [[Square alloc] initWithSideLength:10.0];
// [rectangles addObject:rect];
// [rectangles addObject:rect2];
// [rectangles addObject:rect3];
[rectangles addObjectsFromArray:@[rect, rect2, rect3, square]];
// rect.width = 25.0;
// rect.height = 15.0;
for (Rectangle *rectangleInArray in rectangles) {
NSLog(@"%0.4f", [rectangleInArray area]);
}
// NSLog(@"%0.2f", [rect area]);
// NSLog(@"%0.2f", [rect2 area]);
// NSLog(@"%0.2f", [rect3 area]);
// NSLog(@"%0.2f", rect.width * rect.height);
}
return 0;
}
//
// Rectangle.h
// BreakoutW1D2
//
// Created by David Mills on 2017-05-02.
// Copyright © 2017 David Mills. All rights reserved.
//
#import <Foundation/Foundation.h>
@interface Rectangle : NSObject
@property (nonatomic) double width;
@property (nonatomic) double height;
- (instancetype) initWithWidth:(double)width andHeight:(double)height;
- (double) area;
@end
//
// Rectangle.m
// BreakoutW1D2
//
// Created by David Mills on 2017-05-02.
// Copyright © 2017 David Mills. All rights reserved.
//
#import "Rectangle.h"
@implementation Rectangle
- (instancetype)initWithWidth:(double)width andHeight:(double)height {
if (self = [super init]) {
// Custom initialization
_width = width;
_height = height;
}
return self;
}
- (double)area {
return self.width * self.height;
}
@end
//
// Square.h
// BreakoutW1D2
//
// Created by David Mills on 2017-05-02.
// Copyright © 2017 David Mills. All rights reserved.
//
#import "Rectangle.h"
@interface Square : Rectangle
- (instancetype) initWithSideLength:(double)length;
@end
//
// Square.m
// BreakoutW1D2
//
// Created by David Mills on 2017-05-02.
// Copyright © 2017 David Mills. All rights reserved.
//
#import "Square.h"
@implementation Square
- (instancetype)initWithSideLength:(double)length {
if (self = [super initWithWidth:length andHeight:length]) {
// Custom Initializtion
// Nothing really to do here.
}
return self;
}
- (double)area {
return self.width * self.width;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment