Question

In: Computer Science

17.11 Worksheet 9A Point - x:double - y:double +Point() +Point(x:double, y:double) +setPoint(x: double y:double):void +setX(x:double):void +setY(y:double):void...

17.11 Worksheet 9A

Point
- x:double
- y:double
+Point()
+Point(x:double, y:double)

+setPoint(x: double y:double):void
+setX(x:double):void
+setY(y:double):void

+getX():double
+getY():double

+findDistance(Point point):double
+print():void
  • Create a class called Point as described below
  • Constructor with no arguments
    • Sets x to 0 and y to 0
  • Constructor with arguments
    • Sets the x and y to the values passed in
  • Get methods (accessors) return the field that the get refers to
  • Set methods (mutators) set the field to the new values passed in to it
  • The print method prints the following for the point (3.54, 5.43).
           The point is (3.5, 5.4).

Below is main.cpp - follow the instructions in the comments

#include <iostream>
#include <string>
#include <cmath>
#include <fstream>
#include "Point.h"
using namespace std;

void loadArray(Point points[], int& numE, string filename);


int main() {

    Point point1 = Point();
    Point point2 = Point(1, 0);
    Point points[50];
    string filename = "points1.txt";
    int numE = 0;
    double totalPoint1 = 0;
    double totalPoint2 = 0;

    loadArray(points, numE, filename);

    totalPoint1 = findTotalDistance(points, point1, numE);
    totalPoint2 = findTotalDistance(points, point2, numE);

    cout.setf(ios::fixed);
    cout.precision(1);
    cout << "The total distance from point 1 to the points in the array is " << totalPoint1 << "." << endl;
    cout << "The total distance from point 2 to the points in the array is " << totalPoint2 << "." << endl;
}

void loadArray(Point points[], int& numE, string filename) {
    ifstream fin;
    double point1;
    double point2;
    numE = 0;

    fin.open(filename);
    if(fin.fail()) {
        cerr << "File failed to open" << endl;
        exit(1);
    }

    fin >> point1;

    while(!fin.eof() && numE < 50) {
        fin >> point2;
        points[numE].setPoint(point1, point2);
        numE++;
        fin >> point1;
    }
}

// Write a method called find total distance that finds the distance from a point to every other point in the array. 
// It should return the total of those distances.

Solutions

Expert Solution

Note: Could you plz go through this code and let me know if u need any changes in this.Thank You
_________________

// points1.txt

10 2
3 -4
5 6
7 8
9 -3
4 6
6 8

______________________

// Point.h

#ifndef POINT_H
#define POINT_H

class Point
{
public:
Point();
Point(double x,double y);
void setPoint(double x,double y);
void setX(double x);
void setY(double y);
double getX();
double getY();
  
  
private:
// Declaring variables
double x;
double y;
};
#endif
_________________________

// Point.cpp

#include <iostream>
using namespace std;
#include "Point.h"


Point::Point()
{
this->x=0;
this->y=0;

}
Point::Point(double x,double y)
{
this->x=x;
this->y=y;   
}
void Point::setPoint(double x,double y)
{
this->x=x;
this->y=y;
}
void Point::setX(double x)
{
this->x=x;
}
void Point::setY(double y)
{
this->y=y;
}
double Point::getX()
{
return x;
}
double Point::getY()
{
return y;
}
  
__________________________

// main.cpp

#include <iostream>
#include <string>
#include <cmath>
#include <cstdlib>
#include <fstream>
#include "Point.h"
using namespace std;

void loadArray(Point points[], int& numE, string filename);
double findTotalDistance(Point points[],Point point1,int numE);

int main() {

Point point1 = Point();
Point point2 = Point(1, 0);
Point points[50];
string filename = "points1.txt";
int numE = 0;
double totalPoint1 = 0;
double totalPoint2 = 0;

loadArray(points, numE, filename);

totalPoint1 = findTotalDistance(points, point1, numE);
totalPoint2 = findTotalDistance(points, point2, numE);

cout.setf(ios::fixed);
cout.precision(1);
cout << "The total distance from point 1 to the points in the array is " << totalPoint1 << "." << endl;
cout << "The total distance from point 2 to the points in the array is " << totalPoint2 << "." << endl;
}

void loadArray(Point points[], int& numE, string filename) {
ifstream fin;
double point1;
double point2;
numE = 0;

fin.open(filename.c_str());
if(fin.fail()) {
cerr << "File failed to open" << endl;
exit(0);
}

fin >> point1;

while(!fin.eof() && numE < 50) {
fin >> point2;
points[numE].setPoint(point1, point2);
numE++;
fin >> point1;
}
}

// Write a method called find total distance that finds the distance from a point to every other point in the array.
// It should return the total of those distances.
double findTotalDistance(Point points[],Point point1,int numE)
{
double totDis=0;
for(int i=0;i<numE;i++)
{
totDis+=sqrt(pow((points[i].getX() - point1.getX()), 2) + pow((points[i].getY() - point1.getY()), 2));
}
return totDis;
}

______________________________

Output:


_______________Could you plz rate me well.Thank You


Related Solutions

Given a program as shown below: #include <stdio.h> void function1(void); void function2 (int, double x); void...
Given a program as shown below: #include <stdio.h> void function1(void); void function2 (int, double x); void main (void) { int m; double y; m=15; y=308.24; printf ("The value of m in main is m=%d\n\n",m); function1(); function2(m,y); printf ("The value of m is main still m = %d\n",m); } void function1(void) { printf("function1 is a void function that does not receive\n\\r values from main.\n\n"); } void function2(int n, double x) { int k,m; double z; k=2*n+2; m=5*n+37; z=4.0*x-58.4; printf ("function2 is...
Given a program as shown below: #include <stdio.h> void function1(void); void function2 (int, double x); void...
Given a program as shown below: #include <stdio.h> void function1(void); void function2 (int, double x); void main (void) { int m; double y; m=15; y=308.24; printf ("The value of m in main is m=%d\n\n",m); function1(); function2(m,y); printf ("The value of m is main still m = %d\n",m); } void function1(void) { printf("function1 is a void function that does not receive\n\\r values from main.\n\n"); } void function2(int n, double x) { int k,m; double z; k=2*n+2; m=5*n+37; z=4.0*x-58.4; printf ("function2 is...
class Point3d { public: Point3d(double x, double y, double z); Point3d(const point3d& p); void setX(double x);...
class Point3d { public: Point3d(double x, double y, double z); Point3d(const point3d& p); void setX(double x); void setY(double y); void setZ(double z); double getX() const; double getY() const; double getZ() const; point3d& operator=(const point3d& rhs); private: double x; double y; double z; }; Given the Point class above, complete the following: 1. Write just the signature for the overloaded addition operator that would add the respective x,y, and z from two point3d objects. 2. What is the output of the...
Study the following class definition: class Car { public: Car(double speed); void start(); void accelerate(double speed);...
Study the following class definition: class Car { public: Car(double speed); void start(); void accelerate(double speed); void stop(); double get_speed() const; private: double speed; }; Which of the following options would make logical sense in the definition of the void accelerate(double speed)function? Group of answer choices this->speed = this->speed; this->speed = speed; this.speed = speed; speed1 = this->speed; Flag this Question Question 131 pts The Point class has a public function called display_point(). What is the correct way of calling...
Write  the following functions using the following structure for a point. struct Point { double x, y;...
Write  the following functions using the following structure for a point. struct Point { double x, y; }; (a) A function that passes a point and returns an integer (1, 2, 3, or 4) to indicate in which quadrant the point is located int findQuadrant(struct Point testpoint) { } (b) A function that passes two points and returns the distance between them. float distance(struct Point point1, struct Point point2) { } (c) A function that passes two points and generate the...
Circle Radius: double Circle() Circle(newRadius: double) getArea(): double setRadius(newRadius: double): void getRadius(): double After creating the...
Circle Radius: double Circle() Circle(newRadius: double) getArea(): double setRadius(newRadius: double): void getRadius(): double After creating the Circle class, you should test this class from the main() method by passing objects of this class to a method “ public static void printAreas(Circle c, int times)” You should display the area of the circle object 5 times with different radii. 2 java.util.Random +Random() +Random(seed: long) +nextInt(): int +nextInt(n: int): int +nextLong(): long +nextDouble(): double +nextFloat(): float +nextBoolean(): boolean Constructs a Random object...
Make a function definition in C for the following: void insert (double *b, int c, double...
Make a function definition in C for the following: void insert (double *b, int c, double s, int pos); //Insert value s at position pos in array. //needs: // c > 0, pos >= 0, and pos <= c-1. Elements b[0]...b[c-1] exist. //this will do: //Elements from indexes pos up to c-2 have been moved up to indexes pos+1 up to c-1. The value s has been copied into b[pos]. //Note: the data that was in b[c-1] when the function...
#include<iostream> using namespace std; class point{ private: int x; int y; public: void print()const; void setf(int,...
#include<iostream> using namespace std; class point{ private: int x; int y; public: void print()const; void setf(int, int); }; class line{ private: point ps; point pe; public: void print()const; void setf(int, int, int, int); }; class rectangle{ private: line length[2]; line breadth[2]; public: void print()const; void setf(int, int, int, int, int, int, int, int); }; int main(){ rectangle r1; r1.setf(3,4,5,6, 7, 8, 9, 10); r1.print(); system("pause"); return 0; } a. Write function implementation of rectangle, line and point. b. What is...
Determine the IEEE single and double floating point representation of the following numbers: a) (15/2) x...
Determine the IEEE single and double floating point representation of the following numbers: a) (15/2) x 2^50 b) - (15/2) x 2^-50 c) 1/5
Given the definition for a Point class that holds the coordinates of the point as double...
Given the definition for a Point class that holds the coordinates of the point as double values x and y, write a function called pt_dist that takes two points and returns the straight-line distance between them (as a double). Use two ways, pt_dist function version and the pt_dist method version. In main, include two if else tests for each, If passed "TEST PASSED, DIST IS " else "Test Failed, dist is ". Hint: Rhymes with Bythagorean Beorem. #include <iostream> #include...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT