In C++, A point class has the following definition:
class Point{
private:
int x;
int y;
public:
Point(int, int);
A vector has two points as a start point (x,y) and end point (x,y). Write the header file and implementation file of the Vector class containing constructor, other functions, and also a function returning length of Vector determined from start point and end point coordinates. Write also the client code.
----------------------Point.h------------------
#ifndef MY_POINT
#define MY_POINT
#include<iostream>
#include<cmath>
using namespace std;
class Point{
private:
int x;
int y;
public:
Point(int, int);
int getX();
int getY();
};
#endif
-------------------------point.cpp-------------------------
#include "Point.h"
Point::Point(int x, int y)
{
this->x = x;
this->y = y;
}
int Point::getX()
{
return this->x;
}
int Point::getY()
{
return this->y;
}
-----------------------Vector.h------------------------
#include "Point.cpp"
#include<cmath>
class Vector{
// point class object to store the information of two points
Point *p1, *p2;
public:
// constructor
Vector(int x1, int y1, int x2, int y2);
// getter methods
Point* getP1();
Point* getP2();
double calculateLengthOfvector();
};
-------------------------Vector.cpp-----------------------------
#include "Vector.h"
// constructor
Vector::Vector(int x1, int y1, int x2, int y2)
{
this->p1 = new Point( x1, y1 );
this->p2 = new Point( x2, y2 );
}
// return the pointer to first point
Point* Vector::getP1()
{
return this->p1;
}
// return the pointer to second point
Point* Vector::getP2()
{
return this->p2;
}
double Vector::calculateLengthOfvector()
{
// calculate distance between two points
double dist = sqrt( pow( this->p1->getX() - this->p2->getX() , 2 ) + pow( this->p1->getX() - this->p2->getY(), 2 ) );
}
---------------------main.cpp------------------
#include "Vector.cpp"
int main()
{
int x1, y1, x2, y2;
cout<<"Enter coordinates for first point : ";
cin>>x1>>y1;
cout<<"Enter coordinates for second point : ";
cin>>x2>>y2;
Vector v1(x1, y1, x2, y2);
cout<<"Distance of vector : "<<v1.calculateLengthOfvector();
return 0;
}
In C++, A point class has the following definition: class Point{ private: int x; int y;...
Examine the following class definition: public class Date private int year; private int month; private int day; public Date() { ...) public void set (int x, int y, int z) { ...) public int getYear() { ...) // returns year public int getMonth() { } // returns month public int get Day () { ...) // returns day //more methods here -- 1 Which of the following statements in a client program correctly prints out the day of the object...
Question 19 Given the following class: public class Swapper ( private int x; private String y public int z; public Swapper( int a, String b, int c) ( x-a; y b; zC; public String swap() ( int temp -x; x-z z temp; return y: public String tostring() ( if (x<z) return y: else return" +x+z What would the following code output? Swapper r new Suapper( 5, "no", 10); System.out.printin( r. swap ) ): no no510 510 e 15 Question 20...
public class Point f private int x; private int y; public Point(int x, int y) this.x X; this.y y; public int getX() return x; public int getY() return Y: public double distance (Point other) double dx this.x-other.x; double dy this.y-other.y double dist Math.sqrt(dx dx + dy dy); return dist;
What is wrong with the following code: class Point { private : int x, y; public : Point (int u, int v) : x(u), y(v) {} int getX () { return x; } int getY () { return y; } void setX (int newX ) const { x = newX ; } }; int main () { Point p(5, 3); p.setX (9001) ; cout << p. getX () << ’ ’ << p. getY (); return 0; }
4.a) 4.b> 4.c) C++ Programming Lab Exercise 09 Inheritance. Friend Functions, and Polymorphism (virtual functions) 4.a) Run the following code and observe the output. #include <iostream> #include <string> using namespace std; class Vehicle public: void print() { cout << "Print: I am a vehicle. \n"; } void display() { cout << "Display: I am a vehicle. \n"; } }; class Car: public Vehicle { public: void print() { cout << "Print: I am a car.\n"; } void display() { cout...
skeleton: import java.util.*; public class Point3D{ private int x,y,z; Point3D(int a, int b, int c){ x = a; y = b; z = c; } public int x(){ return x; } public int y(){ return y; } public int z(){ return z; } public String toString(){ return "("+x+","+y+","+z+")"; } } Question1Test.java import java.util.*; public class Question1Test { public static void main(String[] args){ MyHashList<Point3D> lst = new MyHashList<Point3D>(1000); } } a) A class Point3D is given. Re-write this class so that...
Consider the following class Point which represents a point in Cartesian coordinate system. The class has two instance variables x and y which represent its position along the x-axis and y-axis. The constructor of the class initializes the x and y of the point with the specific value init; the method move() moves the point as specified by its parameter amount. Using the implementation of the class Point information and write its specification. The specification of the class includes the...
Given the following class: class Q2 { private int a; private int b; private int c; public void setA(int a){this.a = a; } public void setB(int b){this.b = b;} public void setc(int c){this.c = c;} public int geta(){return a; } public int gets(){return b;} public int getc(){return c;} public int m1(int a, int b){ return a + b; public boolean m2 (int x, int y){ return m1(x, y) + x + y < 10; What is the output of the...
JAVA 1. Given the following class definition, what are the contents of the fields x and y of the object p ? class MyPoint { int x; int y; public MyPoint (int x, int y){ x = x; y = y; } --------------------------------------- MyPoint p = new MyPoint( 100, 88 ); 2. static and non-static members What would happen if you tried to compile and run the following code ? public class Driver {...
15. Define a class called Point. It has two private data members: x and y with int type; and three public member functions: a constructor, setPoint(int, int) and printPoint0. The constructor initializes the data members to 0. You need to use the operator?: to test whether x and y are positive. If not, assign them to 0. The function printPoint prints the message "(X, Y)" where X and Y are two integers. In the main program, first input the number...