-2

ここ数週間、オンラインの Objective-C クラスで苦労しています。私の最近の課題は、ユーザーに円の半径を尋ね、Circle オブジェクトを作成し、円の面積、直径、および円周を報告することによって、Circle という名前のクラスを示すプログラムを作成することです。次のメンバー変数が必要です。

radius:  a double
pi: a double initialized to 3.14159

および次のメンバー関数:

setRadius - a mutator function for the radius variable
getRadius - an accessor function for the radius variable
getArea - returns the area of the circle, which is calculated as:    area = pi * radius * radius
getDiameter - returns the diameter of the circle, which is calculated as:   diameter = radius * 2
getCircumference - returns the circumference of the circle, which is calculated as:   circumference = 2 * pi * radius

クラスのメンバー変数はプライベートとして設定する必要があります。

これまでの私のプログラムは次のとおりです。

主要:

int main(int argc, const char * argv[])
{

    @autoreleasepool {

        int radius;

        NSLog(@"Enter the circles radius:");
        scanf ("%d", &radius);





    }
    return 0;
}

インターフェース:

#import <Foundation/Foundation.h>

//circle class

@interface circle : NSObject

{ @private

-(double)      radius;
-(double)      pi;

}

@property int setRadius, getRadius;

-(double)      getArea;
-(double)      getDiameter;
-(double)      getCircumcerence;


@end

実装:

#import "circle.h"

@implementation circle

@synthesize setRadius, getRadius;


-(double) pi
{
    pi = 3.14159;
}

-(double) getArea
{
    pi * radius * radius;
}

-(double) getDiameter
{
    radius * 2;
}

-(double) getCircumcerence
{
    2 * pi * radius;
}
@end

ご覧のとおり、あまり進んでいません。私は自分の方法をメインで単純に利用する方法について混乱しており、すでに間違いを犯していると確信しています.

どんなアドバイスでも大歓迎です!私は本当に助けが必要で、時間がありません。また、これはとてつもないことかもしれませんが、スカイプで助けてくれる人がいたら教えてください。ありがとう!

4

2 に答える 2

2

.h出発点として、次のようなものに設定する必要があります。

@interface Circle : NSObject

@property double radius;
@property (readonly) double area;
@property (readonly) double diameter;
@property (readonly) double circumference;
@property (readonly) double pi;

-(id)initWithRadius:(double)r;

+(instancetype)circleWithRadius:(double)r;

@end

これにより、 、、およびだけでなく、setterおよびも設定されgetterます。また、半径に double を取るandメソッドも設定します。radiusgettersareadiametercircumferenceinitfactorycircle

.m私は戻ってきて、これを機能させるためにあなたとあなたのmainファイルに加える必要があるいくつかの変更を編集します. 注意として、少なくともgetters3 つのreadonlyプロパティをオーバーライドします。これにより、コンパイラはivarsこれらのプロパティの (インスタンス変数) を作成できなくなります (呼び出したときに計算した数値を計算して返すことができるため)。

あなたの.m

#import Circle.h

@implementation Circle

-(id)initWithRadius:(double)r
{
    self = [super init];
    if(self) {
        self.radius = r;
    }
    return self;
}

+(instancetype)circleWithRadius:(double)r
{
    return [[Circle alloc] initWithRadius:r];
}

-(void)setRadius:(double)r  //This method is automatically created by @property
{  //include any verification logic (make sure r>0 etc), then...
    self.radius = r;
}

//we don't really need to override the radius getter

-(double)pi
{
    return 3.14159;  //or however much accuracy you want
}

-(double)area
{
    return (self.pi * self.radius * self.radius);
}

-(double)diameter
{
    return (2.0 * self.radius);
}

-(double)circumference
{
    return (self.diameter * self.pi);
}

では、 Objective-C で他のオブジェクトを使用するのとまったく同じ方法でmainこのクラスを使用します ( 、などについて考えてください)。CircleNSStringNSArray

int main(int argc, const char * argv[])
{

    @autoreleasepool {

        double radius;

        NSLog(@"Enter the circles radius:");
        scanf ("%lf", &radius);

        Circle *myCircle = [Circle circleWithRadius:radius]; //the factory method we set up

        NSLog(@"myCircle radius: %lf", myCircle.radius);
        NSLog(@"myCircle area: %lf", myCircle.area);
        NSLog(@"myCircle diameter: %lf", myCircle.diameter);
        NSLog(@"myCircle circumference: %lf", myCircle.circumference);

    }
    return 0;
}
于 2013-11-04T20:13:10.030 に答える