C++ Code:
(4)
#include <iostream>
#include <vector>
using namespace std;
//Point class definition
class Point2D
{
//Private member variables
private:
double XCord, YCord;
//Public methods
public:
//Default
Constructor
Point2D()
{
XCord = 0;
YCord = 0;
}
//Constructor that
has 2 parameters(x coordinate , y coordinate)
Point2D(double x, double
y)
{
//Setting x,y values
XCord = x;
YCord = y;
}
//Function that sets
X Co-ordinate
void setXCord(double
x)
{
XCord = x;
}
//Function that sets
Y Co-ordinate
void setYCord(double
y)
{
YCord = y;
}
//Function that
returns X Co-ordinate
double getXCord()
{
return XCord;
}
//Function that
returns Y Co-ordinate
double getYCord()
{
return YCord;
}
//Function that
returns distance from point to origin
double distance()
{
double dist;
//Calculating distance d = sqrt( x^2 + y^2 )
dist = sqrt( (double)(XCord*XCord) + (double)(YCord*YCord) );
//Return distance
return dist;
}
//Overloading
<< operator
friend ostream
&operator<<( ostream &output, const Point2D
&D)
{
//Forming a result statement
output << "(" << D.XCord << ", " << D.YCord
<< ")";
return output;
}
};
//Overloading less than operator
bool operator<(Point2D p1, Point2D p2)
{
//Comparing distances
if(p1.distance() < p2.distance())
{
return true;
}
return false;
}
//Function midpoint
void midPoint(vector<Point2D> points)
{
int smallest;
//Initially assume that starting point is the
smallest point
smallest = 0;
//Iterating over vector
for(int i=0; i<points.size(); i++)
{
//Comparing points
if(points.at(i) <
points.at(smallest))
{
//Updating smallest point
smallest = i;
}
}
//Displaying smallest point
cout << "\n\n Smallest Point: " <<
points.at(smallest) << " \n\n";
}
//Main method
int main()
{
Point2D point1(6.2, 4.8);
Point2D point2(3.6, 5.9);
Point2D point3(8.6, 1.3);
Point2D point4(6.5, 2.4);
Point2D point5(10.2, 1.2);
//Creating a vector
vector<Point2D> points;
//Adding points
points.push_back(point1);
points.push_back(point2);
points.push_back(point3);
points.push_back(point4);
points.push_back(point5);
//Iterating over vector
for(int i=0; i<points.size(); i++)
{
cout << "\n\n
Distance of Point " << (i+1) << " " <<
points.at(i) << " from origin: " <<
(points.at(i)).distance();
}
cout << "\n\n\n Midpoint: ";
//Calling midpoint method
midPoint(points);
return 0;
}
____________________________________________________________________________________________
Sample Run:
f. Demonstration polymorphic behavior by declaring a Point2D pointer and separately assigning a Point2D object then...
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...
In Exercise 1, displayAnimal uses an Animal reference variable as its parameter. Change your code to make the displayAnimal function anim parameter as an object variable, not a reference variable. Run the code and examine the output. What has changed? Polymorphic behavior is not possible when an object is passed by value. Even though printClassName is declared virtual, static binding still takes place because anim is not a reference variable or a pointer. Alternatively we could have used an Animal...
The purpose of this is to use inheritance, polymorphism, object comparison, sorting, reading binary files, and writing binary files. In this application you will modify a previous project. The previous project created a hierarchy of classes modeling a company that produces and sells parts. Some of the parts were purchased and resold. These were modeled by the PurchasedPart class. Some of the parts were manufactured and sold. These were modeled by the ManufacturedPart class. In this you will add a...
: Implement the following given scenario in C++ a. Create an inheritance hierarchy by writing the source code, containing base class BankAccount and derived classes SavingsAccount and CheckingAccount that inherit from class BankAccount. b. Implement Polymorphism where required to achieve the polymorphic behavior. c. Multiple constructors be defined for initializing the account of a user. d. The class should provide four member functions. i. Member function Credit should add an amount to the current balance. ii. Member function Debit should...
I am having trouble understanding how this code is able to use the contents of the header file. Can someone please provide comments in the main code to describe what is happening? (especially on the bool isNumber) THE MAIN CODE: #include<bits/stdc++.h> #include "MyCartesianPoint.h" #include <math.h> #include <iostream> using namespace std; bool isNumber(string s) { if(!isdigit (s[0])) { if(s[0] != '-') return false; else if(s.length() == 1) return false;...
Hello I need help with this program. Should programmed in C! Program 2: Sorting with Pointers Sometimes we're given an array of data that we need to be able to view in sorted order while leaving the original order unchanged. In such cases we could sort the data set, but then we would lose the information contained in the original order. We need a better solution. One solution might be to create a duplicate of the data set, perhaps make...
please there are some specific instructions on the question so i would greatly appreciate if they are followed . thank you very much for your time This lab covers: arrays functions input exception handling Question 1 The purpose of this question is to write a python program (script) that manipulates arrays using vector arithmetic. You will compute the values of points The ellipse has a major axis whose length is a and a minor axis whose length is b. For...
I am having trouble understanding how this code is able to use the contents of the header file. Can someone please provide brief comments in the top code to show what is happening? THE CODE: #include<bits/stdc++.h> #include "MyCartesianPoint.h" #include <math.h> #include <iostream> using namespace std; bool isNumber(string s) { if(!isdigit (s[0])) { if(s[0] != '-') return false; else if(s.length() == 1) return false; } for...
I need to get this two last parts of my project done by tonight. If you see something wrong with the current code feel free to fix it. Thank you! Project 3 Description In this project, use project 1 and 2 as a starting point and complete the following tasks. Create a C++ project for a daycare. In this project, create a class called child which is defined as follows: private members: string First name string Last name integer Child...
Need some help with this C++ code. Please screenshot the code if possible. Purpose: Demonstrate the ability to create and manipulate classes, data members, and member functions. This assignment also aims at creating a C++ project to handle multiple files (one header file and two .cpp files) at the same time. Remember to follow documentation and variable name guidelines. Create a C++ project to implement a simplified banking system. Your bank is small, so it can have a maximum of...