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!
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!
And then the implementation:
And then for main:
As a starting point, you should set up your
.h
to something more like this:This will set up a
setter
andgetter
forradius
as well asgetters
forarea
,diameter
, andcircumference
. It also sets up aninit
andfactory
method for yourcircle
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 yourmain
file in order to make this work. As a note, at a minimum we'll override thegetters
for the 3readonly
properties. This will prevent the compiler from creatingivars
(instance variables) for these properties (because we can just calculate and return the number we calculation when we call it).In your
.m
:In
main
, you use thisCircle
class in just the same way you use any other object in Objective-C (think aboutNSString
,NSArray
, etc).