In: Computer Science
In object C
Define a class called XYPoint that will hold a Cartesian coordinate (x, y), where x and y are integers. Define methods to individually set the x and y coordinates of your new point and retrieve their values. Write an Objective-C program to implement your new class and test it (main section of the file). Your test program needs to create two instances of your class. Make sure your program test prints out the values that you have stored for x and y in an acceptable format for each instance.
Please add comments explaing sections of code, Thanks!
Code:
#import <Foundation/Foundation.h>
@interface XYPoint : NSObject
{
int x, y;
}
- (void) setX: (int) xValue;
- (void) setY: (int) yValue;
- (int) getX;
- (int) getY;
@end
@implementation XYPoint
// set x point
- (void) setX: (int) xValue
{
x = xValue;
}
// set y point
- (void) setY: (int) yValue
{
y = yValue;
}
- (int) getX
{
return x;
}
- (int) getY
{
return y;
}
@end
int main (int argc, const char * argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
//point 1
XYPoint *p1 = [[XYPoint alloc] init];
[p1 setX:10];
[p1 setY:100];
NSLog(@"Point 1: X = %i, Y = %i", [p1 getX], [p1 getY]);
//point 2
XYPoint *p2 = [[XYPoint alloc] init];
[p2 setX:20];
[p2 setY:200];
NSLog(@"Point 1: X = %i, Y = %i", [p2 getX], [p2 getY]);
[pool drain];
return 0;
}
Output: