Assistance needed writing simple Objective-C progr

2019-09-21 05:44发布

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!

2条回答
做个烂人
2楼-- · 2019-09-21 06:07

There are of course many ways to set this up. I can remember being confused when starting out, below is an alternative example to give you something else to look at.

It is not intended to be fancy but just bare-bones so that you can see a minimal setup of the class with an initializer.

Note that the only value initialized is the const pi, of course, the radius can be initialized there as well, as nhgrif's example shows quite nicely.

Hope this helps!

//  Circle.h

#import <Foundation/Foundation.h>

@interface Circle : NSObject
{
    double radius;
    double pi;
}

@property double radius, pi;

-(double) getArea;
-(double) getDiameter;
-(double) getCircumference;

@end

And then the implementation:

//  Circle.m

#import "Circle.h"

@implementation Circle

@synthesize radius, pi;

// Initialize with const pi:
- (id)init {
    self = [super init];
    if (self) {
        pi = 3.14159;
        NSLog(@"Circle created.");
    }
    return self;
}

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

-(double) getDiameter {
    return 2*radius;
}

-(double) getCircumference {
    return 2*pi*radius;
}

@end

And then for main:

//  main.m

#import <Foundation/Foundation.h>
#import "Circle.h"

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

        Circle *aCircle = [[Circle alloc] init];

        // Use an arbitrary value:            
        [aCircle setRadius:2];

        NSLog(@"Area = %f",[aCircle getArea]);
        NSLog(@"Circumference = %f",[aCircle getCircumference]);
        NSLog(@"Diameter = %f",[aCircle getDiameter]);
        NSLog(@"Check pi = %f",[aCircle pi]);

    }
    return 0;
}
查看更多
等我变得足够好
3楼-- · 2019-09-21 06:30

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;
}
查看更多
登录 后发表回答