vakho

Prog. Langs (Objective C) 5

May 7th, 2016
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #import <Foundation/Foundation.h>
  2.  
  3. @interface Time : NSObject
  4. {
  5.     int ticks;
  6. }
  7. -(id) init:(int) h:(int) m: (int) s;
  8. -(int) hour;
  9. -(int) min;
  10. -(int) sec;
  11. -(void) setHour:(int) h;
  12. -(void) setMin:(int) m;
  13. -(void) setSec:(int) s;
  14. @end
  15.  
  16. @protocol IGround
  17.  
  18. -(double) area;
  19. -(double) perimeter;
  20.    
  21. @end
  22.  
  23. @interface Triangle : NSObject <IGround>
  24. {
  25.     double a, b, c;
  26. }
  27. -(void) initTriangle:(double) _a: (double) _b: (double) _c;
  28. -(double) area;
  29. -(double) perimeter;
  30. @end
  31.  
  32. @interface Prism : NSObject
  33. {
  34.     double height;
  35.     id<IGround> ground; // Is an implementator of IGround
  36. }
  37. -(double) area; // - for instance method and + for statis
  38. -(double) volume;
  39. -(void) initPrism:(double) h: (id<IGround>) g;
  40. @end
  41.  
  42. @interface Circle : NSObject
  43. {
  44.     double radius;
  45. }
  46. -(double) area;
  47. -(double) perimeter;
  48. -(void) initR:(double)r;
  49.  
  50. @end
  51.  
  52. @interface Cylinder : Circle
  53. {
  54.     @public
  55.     double height;
  56. }
  57. -(double) area;
  58. -(void) initRH:(double) r: (double) h;
  59. -(double) volume;
  60.  
  61. @end
  62.  
  63. int main (int argc, const char * argv[])
  64. {
  65.    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
  66.  
  67.     Time* tm = [[Time alloc] init:12:15:57];
  68.     tm.hour = 4;
  69.     tm.min = 10;
  70.     tm.sec = 36;
  71.    
  72.     printf("%i.%i.%i\n", tm.hour, tm.min, tm.sec);
  73.  
  74.     Circle* c;
  75.     c = [[Circle alloc] init];
  76.     [c initR:9];
  77.    
  78.     double a;
  79.     a = [c area]; // Calling method
  80.    
  81.    
  82.     double _p;
  83.     _p = c.perimeter; // Other way of calling (nonparametrised) method
  84.    
  85.     printf("%f, %f\n", a, _p);
  86.    
  87.     Cylinder* cyl;
  88.     cyl = [[Cylinder alloc] init];
  89.     [cyl initRH: 2 : 3];
  90.    
  91.     printf("%f, %f\n", [cyl area], [cyl volume]);
  92.    
  93.     // Polymorphism
  94.     c = cyl;
  95.     printf("%f\n", [c area]);
  96.    
  97.     Prism* p = [[Prism alloc] init];
  98.     Triangle* t = [[Triangle alloc] init];
  99.     [t initTriangle: 3: 4: 5];
  100.     [p initPrism: 7: t];
  101.    
  102.     printf("%f, %f", [p area], [p volume]);
  103.    
  104.    NSLog (@"hello world");
  105.    [pool drain];
  106.    return 0;
  107. }
  108.  
  109. @implementation Circle
  110. -(double)area
  111. {
  112.     return 3.14159 * self->radius * radius;
  113. }
  114.  
  115. -(double)perimeter
  116. {
  117.     return 3.14159 * 2 * self->radius;
  118. }
  119.  
  120. -(void)initR: (double) r
  121. {
  122.     self->radius = r;
  123. }
  124.  
  125. @end
  126.  
  127. @implementation Cylinder
  128.  
  129. -(double) area
  130. {
  131.     return 2 * [super area] + self.perimeter * self->height;
  132. }
  133.  
  134. -(double) volume
  135. {
  136.     return [super area] * self->height;
  137. }
  138.  
  139. -(void) initRH: (double) r : (double) h
  140. {
  141.     self->radius = r;
  142.     self->height = h;
  143. }
  144.  
  145. @end
  146.  
  147. @implementation Prism
  148. -(double) volume {
  149.     return [self->ground area] * self->height;
  150. }
  151.  
  152. -(double) area {
  153.     return 2 * [self->ground area] + self->height * [self->ground perimeter];
  154. }
  155.  
  156. -(void) initPrism: (double) h: (id<IGround>) g
  157. {
  158.     self->height = h;
  159.     self->ground = g;
  160. }
  161.  
  162. @end
  163.  
  164. @implementation Triangle
  165.  
  166. -(void) initTriangle:(double) _a: (double) _b: (double) _c
  167. {
  168.     self->a = _a;
  169.     self->b = _b;
  170.     self->c = _c;
  171. }
  172.  
  173. -(double) area
  174. {
  175.     return self->a * self->b / 2;
  176. }
  177.  
  178. -(double) perimeter
  179. {
  180.     return self->a * self->b * self->c;
  181. }
  182.  
  183. @end
  184.  
  185. @implementation Time
  186. -(id) init:(int) h:(int) m: (int) s
  187. {
  188.     self = [super init];
  189.     if (self == 0) {
  190.         return 0;
  191.     }
  192.     self->ticks = (h*60 + m)*60 + s;
  193.     return self;
  194. }
  195. -(int) hour {
  196.     return self->ticks / 3600;
  197. }
  198. -(int) min {
  199.     return self->ticks / 60 % 60;
  200. }
  201. -(int) sec {
  202.     return self->ticks % 60;
  203. }
  204. -(void) setHour:(int) h {
  205.     self->ticks = self->ticks % 3600 + h * 3600;
  206. }
  207. -(void) setMin:(int) m {
  208.     self->ticks = self->ticks + (m - self->ticks / 60 % 60)* 60;
  209. }
  210. -(void) setSec:(int) s {
  211.     self->ticks = self->ticks - self->ticks % 60 + s;
  212. }
  213. @end
Add Comment
Please, Sign In to add comment