Question

In: Computer Science

In object C Define a class called XYPoint that will hold a Cartesian coordinate (x, y),...

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!

Solutions

Expert Solution

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:


Related Solutions

C++ Define a base class called Person. The class should have two data members to hold...
C++ Define a base class called Person. The class should have two data members to hold the first name and last name of a person, both of type string. The Person class will have a default constructor to initialize both data members to empty strings, a constructor to accept two string parameters and use them to initialize the first and last name, and a copy constructor. Also include appropriate accessor and mutator member functions. Overload the operators == and !=...
in a cartesian coordinate space, a curved path is defined as y=sin(x).Find the vector that is...
in a cartesian coordinate space, a curved path is defined as y=sin(x).Find the vector that is normal to the path everywhere.(xy:no unit)
A Bookstore Application C++ Design the class Book. Each object of the class Book can hold...
A Bookstore Application C++ Design the class Book. Each object of the class Book can hold the following information about a book: title, authors, publisher, ISBN Include the member functions to perform the various operations on the objects of Book. For example, the typical operations that can be performed on the title are to show the title, set the title. Add similar operations for the publisher, ISBN , and authors. Add the appropriate constructors and a destructor (if one is...
In a two-dimensional Cartesian coordinate system the y-component of a given vector is equal to that...
In a two-dimensional Cartesian coordinate system the y-component of a given vector is equal to that vector's magnitude multiplied by which trigonometric function, with respect to the angle between vector and y-axis?
A Point Class Definition A two-dimensional point may be represented by an x- and y-coordinate. In...
A Point Class Definition A two-dimensional point may be represented by an x- and y-coordinate. In this problem, you will write a Point class which stores this information and provides useful methods to work with points. We have provided an __init__ definition for the Point class. You will note that this accepts two arguments, being the x- and y-coordinates of the point. We have also included a __repr__ method, which will provide a canonical string representation of the point. Make...
Define a class called Goals that has the following requirements in c++: a function called set...
Define a class called Goals that has the following requirements in c++: a function called set that takes 3 int parameters that are goals for "fame", "happiness" and "money". The function returns true and saves the values if they add up to exactly 60, and returns false otherwise (you may assume the parameters are not negative) a functions satisfies that takes 3 int parameters and returns true if each parameter is at least as large as the saved goal, false...
In C++ Define a base class called Person. The class should have two data members to...
In C++ Define a base class called Person. The class should have two data members to hold the first name and last name of a person, both of type string. The Person class will have a default constructor to initialize both data members to empty strings, a constructor to accept two string parameters and use them to initialize the first and last name, and a copy constructor. Also include appropriate accessor and mutator member functions. Overload the operators == and...
Define an interface called Shape that requires methods to get x and y, and a method...
Define an interface called Shape that requires methods to get x and y, and a method to calculate the area. Call these methods getX(), getY(), and getArea(). Write a class called MyRectangle that implements the Shape interface and contains: the double data fields x, y , and width with get and set methods a no-arg constructor that creates a default rectangle with 0 for both x and y and 1 for both length and width a constructor that creates a...
Create a C# Application. Create a class object called “Employee” which includes the following private variables:...
Create a C# Application. Create a class object called “Employee” which includes the following private variables: firstN lastN idNum wage: holds how much the person makes per hour weekHrsWkd: holds how many total hours the person worked each week regHrsAmt: initialize to a fixed amount of 40 using constructor. regPay otPay After going over the regular hours, the employee gets 1.5x the wage for each additional hour worked. Methods: constructor properties CalcPay(): Calculate the regular pay and overtime pay. Create...
Create a class named GameCharacter to define an object as follows: The class should contain class...
Create a class named GameCharacter to define an object as follows: The class should contain class variables for charName (a string to store the character's name), charType (a string to store the character's type), charHealth (an int to store the character's health rating), and charScore (an int to store the character's current score). Provide a constructor with parameters for the name, type, health and score and include code to assign the received values to the four class variables. Provide a...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT