Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
197 views
in Technique[技术] by (71.8m points)

class - Assistance needed writing simple Objective-C program

I have been struggling along with an online objective-c class for a few weeks now. I'm feeling very stupid.. My latest assignment is to write a program that demonstrates a class named Circle by asking the user for the circle's radius, creating a Circle object, and then reporting the circle's area, diameter, and circumference. We should have the following member variables:

radius:  a double
pi: a double initialized to 3.14159

and the following member functions:

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

The member variables of the class should be set as private.

Here is my program so far:

Main:

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

    @autoreleasepool {

        int radius;

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





    }
    return 0;
}

Interface:

#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

Implementation:

#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

As you can see, I haven't gotten very far. I am confused as how to simply utilize my methods in my main, and am sure I have already made mistakes.

Any advice is appreciated! I really need help, and am short on time. Also, this may be far-fetched but if anyone could maybe skype with me and help me through it? Thanks!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

As a starting point, you should set up your .h to something more like this:

@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

This will set up a setter and getter for radius as well as getters for area, diameter, and circumference. It also sets up an init and factory method for your circle which takes a double for the radius.

I will come back and edit in some modifications you need to make to your .m as well as your main file in order to make this work. As a note, at a minimum we'll override the getters for the 3 readonly properties. This will prevent the compiler from creating ivars (instance variables) for these properties (because we can just calculate and return the number we calculation when we call it).

In your .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);
}

In main, you use this Circle class in just the same way you use any other object in Objective-C (think about NSString, NSArray, etc).

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;
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...