ここ数週間、オンラインの 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
ご覧のとおり、あまり進んでいません。私は自分の方法をメインで単純に利用する方法について混乱しており、すでに間違いを犯していると確信しています.
どんなアドバイスでも大歓迎です!私は本当に助けが必要で、時間がありません。また、これはとてつもないことかもしれませんが、スカイプで助けてくれる人がいたら教えてください。ありがとう!