Objective-C 程序设计(第六版)第八章习题答案

1.略了吧 步步高点读机

2.所有int类型改为float或double类型

3.略了吧 superclass和subclass的关系搞懂就OK!

4.

 1 - (void) translate: (XYPoint *) tp
 2 {
 3     origin.x = tp.x;
 4     origin.y = tp.y;
 5 }
 6 
 7 //main
 8         Rectangle *myRect = [[Rectangle alloc] init];
 9         XYPoint *myPoint = [[XYPoint alloc] init];
10         
11         [myPoint setX:100 andY:200];
12         [myRect setWidth:5 andHeight:8];
13         
14 //        myRect.origin = myPoint;
15         [myRect setOrigin:myPoint];
16         [myPoint setX:111 andY:222];
17         [myRect translate:myPoint];
18         
19         NSLog(@"Rectangle w = %.1f, h = %.1f", [myRect width], [myRect height]);
20         NSLog(@"Origin at (%.1f, %.1f)", myRect.origin.x , myRect.origin.y );
21         NSLog(@"Area = %.1f, perimeter = %.1f", [myRect area], [myRect perimeter]);
22         

5.  子类Rectangle类没帖上来 没什么必要

//GraphicObject类

#import <Foundation/Foundation.h>

@interface GraphicObject : NSObject

@property int fillColor, lineColor;
@property BOOL filled;

- (void) setFillColor: (int) f andLineColor: (int) l andFilled: (BOOL) yesOr;

@end


#import "GraphicObject.h"

@implementation GraphicObject
@synthesize fillColor, lineColor, filled;

- (void) setFillColor: (int) f andLineColor: (int) l andFilled: (BOOL) yesOr
{
    if (yesOr == NO) {
        fillColor = f;
        lineColor = l;
    }
}

@end

//Circle类

#import "GraphicObject.h"

@interface Circle : GraphicObject
@property double radius;

- (double) area;

- (double) perimeter;

@end


#import "Circle.h"

@implementation Circle
@synthesize radius;

- (double) area
{
    return  3.14 * radius * radius;
}

- (double) perimeter
{
    return 2 * 3.14 * radius;
}


@end


//Triangle类

#import "GraphicObject.h"

@interface Triangle : GraphicObject
@property double ab, bc, ac;

- (void) setAB: (double) a andBC: (double) b andAC: (double) c;

- (double) area;

- (double) perimeter;

@end


#import "Triangle.h"

@implementation Triangle
@synthesize ab, bc, ac;

- (void) setAB: (double) a andBC: (double) b andAC: (double) c
{
    ab = a;
    bc = b;
    ac = c;
}

- (double) area
{
    int s ;
    s = (ab + bc + ac) / 2;
    
    return sqrt(s * (s - ab) * (s - bc) * (s - ac));
}

- (double) perimeter
{
    return ab + ac +bc;
}

@end


// main

#import <Foundation/Foundation.h>
#import "GraphicObject.h"
#import "Circle.h"
#import "Triangle.h"
int main(int argc, const char * argv[]) {
    @autoreleasepool {
       
        //测试GraphicObject
        GraphicObject *newGraphic = [[GraphicObject alloc] init];
        
        [newGraphic setFillColor:2 andLineColor:3 andFilled:NO];
        
        NSLog(@"fillColor is %d, lineColor is %d",newGraphic.fillColor, newGraphic.lineColor);
        
        //测试Circle
        Circle *newCirle = [[Circle alloc] init];
        
        [newCirle setRadius:4.0];
        
        NSLog(@"Area  %.2f, Perimeter %.2f", [newCirle area], [newCirle perimeter]);
        
        //测试Triangle
        Triangle * newTriangle = [[Triangle alloc] init];
        
        [newTriangle setAB: 5 andBC: 6 andAC: 7];
        
        NSLog(@"Area %.2f, Perimeter %.2f", [newTriangle area], [newTriangle perimeter]);
        
        
    }
    return 0;
}

6.

1 - (BOOL) containsPoint: (XYPoint *) aPoint
2 {
3     if (aPoint.x >= origin.x && aPoint.x <= origin.x + width &&
4         aPoint.y >= origin.y && aPoint.y <= origin.y + height ) {
5         return YES;
6     }else
7         return NO;
8 }

7. 先不做了  重叠的情况太多  回头再做

8.我这个考虑的可能没那么全面

 1 - (void) darw
 2 {
 3     for (int i = 1; i <= width; i++) {
 4         printf("_");
 5     }
 6     printf("\n");
 7     for (int h = 1; h <= height; h++) {
 8         printf("|        |\n");
 9     }
10     for (int i = 1; i <= width; i++) {
11         printf("_");
12     }
13     printf("\n");
14 }

 

转载于:https://www.cnblogs.com/MingMing-King/p/4090947.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值