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

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)
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 !=...
If an object is at point x,y,z (Cartesian coordinates) and its symmetry mate is related by...
If an object is at point x,y,z (Cartesian coordinates) and its symmetry mate is related by a 21 screw axis, what is the location in Cartesian coordinates for the symmetry mate?
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 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...
Develop a C++ class called PrimeNumberGenerator. An object of this class will generate successive prime numbers...
Develop a C++ class called PrimeNumberGenerator. An object of this class will generate successive prime numbers on request. Think of the object as being similar to the digital tasbeeh counter. It has a reset button which makes the counter return to 0. There is also a button to get the next prime number. On the tasbeeh, pressing this button increments the current value of the counter. In your object, pressing this button would generate the next prime number for display....
Written in C++ Define a class for a type called Fraction. This class is used to...
Written in C++ Define a class for a type called Fraction. This class is used to represent a ratio of two integers. Include mutator functions that allow the user to set the numerator and denominator. Also include a member function that returns the values of the numerator divided by the denominator as a double. Include an additional member function that outputs the value of the fraction reduced to lowest terms. This will require finding the greatest common divisor for the...
Define a structure Point. A point has an x- and a y-coordinate. Write a function double...
Define a structure Point. A point has an x- and a y-coordinate. Write a function double distance(Point a, Point b) that computes the distance from a to b. Write a program that reads the coordinates of the points, calls your function, and displays the result. IN C++ if possible please give //comments
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...
C# programming Create a class called A with private integer field x, protected integer field y,...
C# programming Create a class called A with private integer field x, protected integer field y, public integer field z. Create a class B derived from class A with public integer field d and protected integer field e and private field f. Write a main (in a THIRD class called Program) that create an object B and assign all publicly accessible fields of the object with value of 1. Which fields will have a value of 1? Create a method...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT