Question

Implement the Point class (code for this up to the isHigher method was discussed in the

lecture). The class has the following instance variables:

• x coordinate (an int)

• y coordinate (an int)

and the following methods:

• Constructor that sets the x and y coordinates

• Get and set methods

• Method equals if this point is equal to another point object (if the x and y coordinates are the

same).

• Method isHigher if this point is higher than another point object (Note that we assume that the top

left corner is the coordinate 0,0).

• Method findDistance that calculates the distance between this point and another point. (Formula

for the distance between two points (x1,y1) and (x2,y2) is square root of ((x2-x1)2 + (y2-y1)2)

Test the class. In the demo program, construct four points p1, p2, p3 and p4 using user-defined values.

Determine

• Which point is the highest. (If more than one point is at the same height, you may report any one

point).

• Whether the distance p1à p2 is more than the distance p3à p4, or p3à p4 is more than p1àp2,

or whether they are the same.

FULL QUESTION IS IN IMAGES BELOW

Implement the class for this up to the isHigh lect

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include <bits/stdc++.h>
using namespace std;

class Point
{
  
public:
   int x;int y;
   Point(int a,int b)
   {
       x=a;
       y=b;
   }

   void setX(int a)
   {
       x=a;
   }

   void setY(int b)
   {
       y=b;
   }

   int getX()
   {
       return x;
   }

   int getY()
   {
       return y;
   }

   float findDistance(Point p)
   {
       return sqrt( (p.x-x)*(p.x-x)+(p.y-y)*(p.y-y));
   }

   int isHigher(Point p)
   {
       if(p.y<y)
           return 1;
       return 0;
   }

};

int main(int argc, char const *argv[])
{
   int x;int y;
   cout<<"Enter the x and y coordinates of point1:";
   cin>>x>>y;
   Point p1(x,y);
   cout<<"Enter the x and y coordinates of point2:";
   cin>>x>>y;
   Point p2(x,y);
   cout<<"Enter the x and y coordinates of point3:";
   cin>>x>>y;
   Point p3(x,y);
   cout<<"Enter the x and y coordinates of point4:";
   cin>>x>>y;
   Point p4(x,y);

   cout<<"[2,1] is highest point\n";
   float d1=p1.findDistance(p2);
   float d2=p3.findDistance(p4);

   cout<<"The distance between ["<<p1.x<<","<<p1.y<<" and ["<<p2.x<<","<<p2.y<<"] is "<<d1<<endl;
   cout<<"The distance between ["<<p3.x<<","<<p3.y<<" and ["<<p4.x<<","<<p4.y<<"] is "<<d2<<endl;
   if(d2>d1)
   {
               cout<<"["<<p1.x<<","<<p1.y<<"] ---- "<<"["<<p2.x<<","<<p2.y<<"] is shorter than "<<"["<<p3.x<<","<<p3.y<<"]--------["<<p4.x<<","<<p4.y<<"]";
   }
   else
   {
               cout<<"["<<p1.x<<","<<p1.y<<"] ---- ["<<"["<<p2.x<<","<<p2.y<<"] is longer than "<<"["<<p3.x<<","<<p3.y<<"]--------["<<p4.x<<","<<p4.y<<"]";

   }

   return 0;
}

========================

Output:

akshay@akshay-Inspiron-3537:~/Chegg$ ./a.out
Enter the x and y coordinates of point1:8 9
Enter the x and y coordinates of point2:4 3
Enter the x and y coordinates of point3:2 1
Enter the x and y coordinates of point4:5 6
[2,1] is highest point
The distance between [8,9 and [4,3] is 7.2111
The distance between [2,1 and [5,6] is 5.83095
[8,9] ---- [[4,3] is longer than [2,1]--------[5,6]

Add a comment
Answer #2

// C++ code
#include <iostream>
#include <string>
#include <fstream>
#include <stdlib.h>
#include <math.h>

using namespace std;

class Point
{
  
public:
int x;int y;
Point(int a,int b)
{
x=a;
y=b;
}

void setX(int a)
{
x=a;
}

void setY(int b)
{
y=b;
}

int getX()
{
return x;
}

int getY()
{
return y;
}

float findDistance(Point p)
{
return sqrt( (p.x-x)*(p.x-x)+(p.y-y)*(p.y-y));
}

int isEqual(Point p)
{
if(p.x == x && p.y == y)
return 1;
else
return 0;
}

int isHigher(Point p)
{
if(p.y > y)
return 1;
return 0;
}

};

int main(int argc, char const *argv[])
{
int x;int y;
cout<<"Enter the x and y coordinates of point1: ";
cin>>x>>y;
Point p1(x,y);
cout<<"Enter the x and y coordinates of point2: ";
cin>>x>>y;
Point p2(x,y);
cout<<"Enter the x and y coordinates of point3: ";
cin>>x>>y;
Point p3(x,y);
cout<<"Enter the x and y coordinates of point4: ";
cin>>x>>y;
Point p4(x,y);

if(p1.isHigher(p2)==1 && p1.isHigher(p3)==1 && p1.isHigher(p4)==1)
cout << "[" << p1.x << "," <<p1.y << "] is the highest point" << endl;
else if(p2.isHigher(p1)==1 && p2.isHigher(p3)==1 && p2.isHigher(p4)==1)
cout << "[" << p2.x << "," <<p2.y << "] is the highest point" << endl;
else if(p3.isHigher(p2)==1 && p3.isHigher(p1)==1 && p3.isHigher(p4)==1)
cout << "[" << p3.x << "," <<p3.y << "] is the highest point" << endl;
else
cout << "[" << p4.x << "," <<p4.y << "] is the highest point" << endl;

float d1=p1.findDistance(p2);
float d2=p3.findDistance(p4);

cout<<"The distance between ["<<p1.x<<","<<p1.y<<"] and ["<<p2.x<<","<<p2.y<<"] is "<<d1<<endl;
cout<<"The distance between ["<<p3.x<<","<<p3.y<<"] and ["<<p4.x<<","<<p4.y<<"] is "<<d2<<endl;
if(d2>d1)
{
cout<<"["<<p1.x<<","<<p1.y<<"] ----> "<<"["<<p2.x<<","<<p2.y<<"] is shorter than "<<"["<<p3.x<<","<<p3.y<<"]--------> ["<<p4.x<<","<<p4.y<<"]\n";
}
else
{
cout<<"["<<p1.x<<","<<p1.y<<"] ----> ["<<"["<<p2.x<<","<<p2.y<<"] is longer than "<<"["<<p3.x<<","<<p3.y<<"]--------> ["<<p4.x<<","<<p4.y<<"]\n";

}

return 0;
}

/*
output:

Enter the x and y coordinates of point1: 8 9
Enter the x and y coordinates of point2: 4 3
Enter the x and y coordinates of point3: 2 1
Enter the x and y coordinates of point4: 5 6
[2,1] is the highest point
The distance between [8,9] and [4,3] is 7.2111
The distance between [2,1] and [5,6] is 5.83095
[8,9] ----> [[4,3] is longer than [2,1]--------> [5,6]
*/

Add a comment
Answer #3

// C++ code
#include <iostream>
#include <string>
#include <fstream>
#include <stdlib.h>
#include <math.h>

using namespace std;

class Point
{
  
public:
int x;int y;
Point(int a,int b)
{
x=a;
y=b;
}

void setX(int a)
{
x=a;
}

void setY(int b)
{
y=b;
}

int getX()
{
return x;
}

int getY()
{
return y;
}

float findDistance(Point p)
{
return sqrt( (p.x-x)*(p.x-x)+(p.y-y)*(p.y-y));
}

int isEqual(Point p)
{
if(p.x == x && p.y == y)
return 1;
else
return 0;
}

int isHigher(Point p)
{
if(p.y > y)
return 1;
return 0;
}

};

int main(int argc, char const *argv[])
{
int x;int y;
cout<<"Enter the x and y coordinates of point1: ";
cin>>x>>y;
Point p1(x,y);
cout<<"Enter the x and y coordinates of point2: ";
cin>>x>>y;
Point p2(x,y);
cout<<"Enter the x and y coordinates of point3: ";
cin>>x>>y;
Point p3(x,y);
cout<<"Enter the x and y coordinates of point4: ";
cin>>x>>y;
Point p4(x,y);

if(p1.isHigher(p2)==1 && p1.isHigher(p3)==1 && p1.isHigher(p4)==1)
cout << "[" << p1.x << "," <<p1.y << "] is the highest point" << endl;
else if(p2.isHigher(p1)==1 && p2.isHigher(p3)==1 && p2.isHigher(p4)==1)
cout << "[" << p2.x << "," <<p2.y << "] is the highest point" << endl;
else if(p3.isHigher(p2)==1 && p3.isHigher(p1)==1 && p3.isHigher(p4)==1)
cout << "[" << p3.x << "," <<p3.y << "] is the highest point" << endl;
else
cout << "[" << p4.x << "," <<p4.y << "] is the highest point" << endl;

float d1=p1.findDistance(p2);
float d2=p3.findDistance(p4);

cout<<"The distance between ["<<p1.x<<","<<p1.y<<"] and ["<<p2.x<<","<<p2.y<<"] is "<<d1<<endl;
cout<<"The distance between ["<<p3.x<<","<<p3.y<<"] and ["<<p4.x<<","<<p4.y<<"] is "<<d2<<endl;
if(d2>d1)
{
cout<<"["<<p1.x<<","<<p1.y<<"] ----> "<<"["<<p2.x<<","<<p2.y<<"] is shorter than "<<"["<<p3.x<<","<<p3.y<<"]--------> ["<<p4.x<<","<<p4.y<<"]\n";
}
else
{
cout<<"["<<p1.x<<","<<p1.y<<"] ----> ["<<"["<<p2.x<<","<<p2.y<<"] is longer than "<<"["<<p3.x<<","<<p3.y<<"]--------> ["<<p4.x<<","<<p4.y<<"]\n";

}

return 0;
}

/*
output:

Enter the x and y coordinates of point1: 8 9
Enter the x and y coordinates of point2: 4 3
Enter the x and y coordinates of point3: 2 1
Enter the x and y coordinates of point4: 5 6
[2,1] is the highest point
The distance between [8,9] and [4,3] is 7.2111
The distance between [2,1] and [5,6] is 5.83095
[8,9] ----> [[4,3] is longer than [2,1]--------> [5,6]
*/

Add a comment
Know the answer?
Add Answer to:
Implement the Point class (code for this up to the isHigher method was discussed in the...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Create a MyPoint class to model a point in a two-dimensional space. The MyPoint class contains:...

    Create a MyPoint class to model a point in a two-dimensional space. The MyPoint class contains: • Two data fields x and y that represent the coordinates. • A no-arg constructor that creates a point (0,0). • A constructor that constructs a point with specified coordinates. • Two get functions for data fields x and y, respectively. • Two set functions for data fields x and y, respectively. • A function named distance that returns the distance from this point...

  • INSTRUCTIONS: I NEED TO CREATE A PointApp PROGRAM THAT USES THE FOLLOWING API DOCUMENTATION. Below the...

    INSTRUCTIONS: I NEED TO CREATE A PointApp PROGRAM THAT USES THE FOLLOWING API DOCUMENTATION. Below the API Documentation is the code I submitted. However, the output is different for what they are asking. I am looking for someone to fix the code to print out the correct output and to add comments so I can have an idea in how the code works. PLEASE AND THANK YOU. API DOCUMENTATION: public class Point extends java.lang.Object The Point class is used to...

  • Java Help 2. Task: Create a client for the Point class. Be very thorough with your...

    Java Help 2. Task: Create a client for the Point class. Be very thorough with your testing (including invalid input) and have output similar to the sample output below: ---After declaration, constructors invoked--- Using toString(): First point is (0, 0) Second point is (7, 13) Third point is (7, 15) Second point (7, 13) lines up vertically with third point (7, 15) Second point (7, 13) doesn't line up horizontally with third point (7, 15) Enter the x-coordinate for first...

  • NEEDS TO BE IN PYTHON: (The Point class) Design a class named Point to represent a...

    NEEDS TO BE IN PYTHON: (The Point class) Design a class named Point to represent a point with x- and y-coordinates. The class contains: - Two private data fields x and y that represent the coordinates with get methods. - A constructor that constructs a point with specified coordinates, with default point (0, 0). - A method named distance that returns the distance from this point to another point of the Point type. - A method named isNearBy(p1) that returns...

  • In Java please! Problem You will be using the point class discussed in class to create...

    In Java please! Problem You will be using the point class discussed in class to create a Rectangle class. You also need to have a driver class that tests your rectangle. Requirements You must implement the 3 classes in the class diagram below and use the same naming and data types. Following is a description of what each method does. Point Rectangle RectangleDriver - x: int - top Left: Point + main(args: String[) - y: int - width: int +...

  • This is my code that i need to finish. In BoxRegion.java I have no idea how...

    This is my code that i need to finish. In BoxRegion.java I have no idea how to create the constructor. I tried to use super(x,y) but It is hard to apply. And Also In BoxRegionHashTable, I don't know how to create displayAnnotation BoxRegion.java ------------------------------------------------ public final class BoxRegion { final Point2D p1; final Point2D p2; /** * Create a new 3D point with given x, y and z values * * @param x1, y1 are the x,y coordinates for point...

  • Create a new class called Point that represents a point in the Cartesian plane with two...

    Create a new class called Point that represents a point in the Cartesian plane with two coordinates. Each instance of Point should have to coordinates called x and y. The constructor for Point should take two arguments x and y that represent the two coordinates of the newly created point. Your class Point should override the __str__(self) method so that it returns a string showing the x- and y-coordinates of the Point enclosed in parentheses and separated by a comma....

  • create a java class with the name Brick class: For this part of the assignment, you...

    create a java class with the name Brick class: For this part of the assignment, you will create a class that allows you to specify a colored brick that is capable of drawing itself given a specified Graphics object. Note that this is a regular Java class and does not extend the JApplet class. Your class should a constructor with the following signature: Identifier: Brick(int xPosition, int yPosition, int width, int height, Color color) Parameters: xPosition – an int representing...

  • Java code for the following inheritance hierarchy figure.. 1. Create class Point, with two private instance...

    Java code for the following inheritance hierarchy figure.. 1. Create class Point, with two private instance variables x and y that represented for the coordinates for a point. Provide constructor for initialising two instance variables. Provide set and get methods for each instance variable, Provide toString method to return formatted string for a point coordinates. 2. Create class Circle, its inheritance from Point. Provide a integer private radius instance variable. Provide constructor to initialise the center coordinates and radius for...

  • Inheritance Problem: (C++) (The MyPoint class) Design a class named MyPoint to represent a point with...

    Inheritance Problem: (C++) (The MyPoint class) Design a class named MyPoint to represent a point with x-and y-coordinates. The class contains: Two data fields x and y that represent the coordinates. A no-arg constructor that creates a point (0, 0). A constructor that constructs a point with specified coordinates using passed in arguments. Two get functions for data fields x and y, respectively. A method named distance that returns the distance from this point to another point of the MyPoint...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT