Question

In: Computer Science

In C++                                          &n

In C++

                                                        Circle

-int x //x coord of the center

-int y // y coord of the center

-int radius

-static int count // static variable to keep count of number of circles created

+ Circle()                     //default constructor that sets origin to (0,0) and radius to 1

+Circle(int x, int y, int radius)   // regular constructor

+getX(): int

+getY(): int

+getRadius(): int

+setX(int newX: void

+setY(int newY): void

+setRadius(int newRadius):void

+getArea(): double // returns the area using formula pi*r^2

+getCircumference // returns the circumference using the formula 2*pi*r

+toString(): String   // return the circle as a string in the form (x,y) : radius

+getDistance(Circle other): double // * returns the distance between the center of this circle and the other circle

+moveTo(int newX,int newY):void // * move the center of the circle to the new coordinates

+intersects(Circle other): bool //* returns true if the center of the other circle lies inside this circle else returns false

+resize(double scale):void// * multiply the radius by the scale

+resize(int scale):Circle // * returns a new Circle with the same center as this circle but radius multiplied by scale

+getCount():int //returns the number of circles created

//note that the resize function is an overloaded function. The definitions have different signatures

  1. Extend the driver class to do the following:
    1. Declare a vector of circles
    2. Call a function with signature inputData(vector<Circle> &, string filename) that reads data from a file called dataLab4.txt into the vector. The following c-e are done in this function
    3. Use istringstream to create an input string stream called instream. Initialize it with each string that is read from the data file using the getline method.
    4. Read the coordinates for the center and the radius from instream to create the circles
    5. Include a try catch statement to take care of the exception that would occur if there was a file open error. Display the message “File Open Error” and exit if the exception occurs
    6. Display all the circles in this vector using the toString method
    7. Use an iterator to iterate through the vector to display these circles
    8. Display the count of all the circles in the vector using the getCount method
    9. Display the count of all the circles in the vector using the vector size method
    10. Clear the vector
    11. Display the current count of all the circles using the getCount method
    12. Display the current count of all the circles using the vector size method
    13. Use the cout statements and other helper statements given to you in the template in Zylabs

Create "Circle.h"

Create "Circle.cpp"

Complete Main.cpp

#include<iostream>
#include "Circle.h"
#include <fstream>
#include <vector>
#include<cstdlib>

#include <fstream>
#include <sstream>
void inputData(vector<Circle> &circleVector, string filename){
//create the input string stream called instream

//open file
//if there is a problem opening the file, throw an exception and exit - use a try catch statement
//otherwise
//use getline to read data line feed to the instream
//create a circle using the data in the instream
//add the circles to the vector
//keep doing this until all the data in the file is read

}

int main(){


cout<<"The number of circles, using getCount method is "<<//<<endl;
cout<<"The numher of circles, using vetor size method is "<<//<<endl;
//clear vector
cout<<"The number of circles, using getCount method is "<<//<endl;
cout<<"The number of circles remaining is ";

return 0;
}

file "dataLab4.txt" contains:

0 0 4
0 0 6
-2 -9 6
4 5 7
7 8 9

Solutions

Expert Solution

// Circle.h

#ifndef CIRCLE_H_

#define CIRCLE_H_

#include <string>

using namespace std;

class Circle

{

private:

               int x; //x coord of the center

               int y; //y coord of the center

               int radius;

               static int count; // static variable to keep count of number of circles created

public:

               Circle(); //default constructor that sets origin to (0,0) and radius to 1

               Circle(int x, int y, int radius); // regular constructor

               int getX();

               int getY();

               int getRadius();

               void setX(int newX);

               void setY(int newY);

               void setRadius(int newRadius);

               double getArea(); // returns the area using formula pi*r^2

               double getCircumference(); // returns the circumference using the formula 2*pi*r

               string toString(); // return the circle as a string in the form (x,y) : radius

               double getDistance(Circle other); // * returns the distance between the center of this circle and the other circle

               void moveTo(int newX, int newY); // * move the center of the circle to the new coordinates

               bool intersects(Circle other); //* returns true if the center of the other circle lies inside this circle else returns false

               void resize(double scale); // * multiply the radius by the scale

               Circle resize(int scale); // * returns a new Circle with the same center as this circle but radius multiplied by scale

               static int getCount(); //returns the number of circles created

};

#endif /* CIRCLE_H_ */

//end of Circle.h

// Circle.cpp

#include "Circle.h"

#include <cmath>

# define M_PI 3.14159265358979323846 // cmath doesn't contain the macro M_PI

int Circle::count = 0;

Circle::Circle()

{

               x = 0;

               y = 0;

               radius = 1;

               count++;

}

Circle::Circle(int xcoord,int ycoord, int r)

{

               x = xcoord;

               y= ycoord;

               radius = r;

               count++;

}

int Circle::getX()

{

               return x;

}

int Circle::getY()

{

               return y;

}

int Circle::getRadius()

{

               return radius;

}

void Circle::setX(int newX)

{

               x = newX;

}

void Circle::setY(int newY)

{

               y = newY;

}

void Circle::setRadius(int newRadius)

{

               radius = newRadius;

}

double Circle::getArea()

{

               return M_PI * (radius * radius);

}

double Circle:: getCircumference()

{

               return (2*M_PI) * (radius);

}

string Circle:: toString()

{

               string str_x = to_string(x);

               string str_y = to_string(y);

               string str_radius = to_string(radius);

               return "("+ str_x + "," + str_y + "):" + str_radius;

}

double Circle:: getDistance(Circle other)

{

               return sqrt(pow(x-other.getX(),2)+ pow(y-other.getY(),2) );

}

void Circle::moveTo (int newX, int newY)

{

               x = newX;

               y = newY;

}

// if distance between the circles <= radius, then center of the other circle lies inside this circle

bool Circle:: intersects(Circle other)

{

               double distance = getDistance(other);

               return(distance <= radius);

}

void Circle:: resize(double scale)

{

               radius *= scale;

}

Circle Circle:: resize(int scale)

{

               return Circle(x,y,radius*scale);

}

int Circle::getCount()

{

               return count;

}

//end of Circle.cpp

// Main.cpp

#include <iostream>

#include "Circle.h"

#include <fstream>

#include <sstream>

#include <vector>

#include <cstdlib>

// function declaration

void inputData(vector<Circle> &circleVector, string filename);

int main()

{

               vector<Circle> circles;

               inputData(circles,"dataLab4.txt");

               // Use an iterator to iterate through the vector to display these circles

               vector<Circle>::iterator itr = circles.begin();

               for(;itr != circles.end();itr++)

                              cout<<(*itr).toString()<<endl;

               cout<<"The number of circles,using getCount method is "<<Circle::getCount()<<endl;

               cout<<"The number of circles, using vector size method is "<<circles.size()<<endl;

               // clear the vector

               circles.clear();

               cout<<"The number of circles,using getCount method is "<<Circle::getCount()<<endl;

               cout<<"The number of circles, remaining "<<circles.size()<<endl;

               return 0;

}

// function to read Circles from file and insert into vector

void inputData(vector<Circle> &circleVector, string filename)

{

               ifstream fin;

               fin.exceptions(ifstream::failbit | ifstream::badbit);

               try{

                              fin.open(filename.c_str());

                              string line;

                              int x,y,radius;

                              // loop till the end of file

while(!fin.eof())

                              {

                                             getline(fin,line); // read a line from file

                                             istringstream ss(line);

                                             ss>>x>>y>>radius;

                                             circleVector.push_back(Circle(x,y,radius));

                              }

                              fin.close();

               }catch(ifstream::failure &e)

               {

                              cout<<"File Open Error"<<endl;

                              exit(1);

               }

}

//end of Main.cpp

Output:

Input file:

Output:


Related Solutions

Write a program in C or C++ that takes a number series of size n (n...
Write a program in C or C++ that takes a number series of size n (n integers) as input from the user, push all the numbers to the stack, and reverse the stack using recursion. Please note that this is not simply popping and printing the numbers, but the program should manipulate the stack to have the numbers stored in reverse order. In addition to the provided header file, the students can use the following function to print the content...
Find a primitive root for: (a) n = 18, (b) n = 50 (c) n =...
Find a primitive root for: (a) n = 18, (b) n = 50 (c) n = 27, (d) n = 625.
Blending of wastes to achieve an optimum C/N ratio. Leaves, with a C/N ratio of 50,...
Blending of wastes to achieve an optimum C/N ratio. Leaves, with a C/N ratio of 50, are to be blended with waste-activated sludge from a wastewater treatment plant, with a C/N ratio of 6.3. Determine the proportions of each component to achieve a blended C/N ratio of 25. Assume that the following conditions apply: moisture content of sludge = 75%; moisture content of leaves = 50%; nitrogen content of sludge = 5.6%; and nitrogen content of leaves = 0.7%.
Poof if a) A is nonsingular b) N(A) = {0} c) rank(A) = n
Poof if a) A is nonsingular b) N(A) = {0} c) rank(A) = n
Suppose C is a m × n matrix and A is a n × m matrix....
Suppose C is a m × n matrix and A is a n × m matrix. Assume CA = Im (Im is the m × m identity matrix). Consider the n × m system Ax = b. 1. Show that if this system is consistent then the solution is unique. 2. If C = [0 ?5 1 3 0 ?1] and A = [2 ?3   1 ?2    6 10] ,, find x (if it exists) when (a) b =[1...
Evaluate if the followings are Cauchy sequences or not. (a) an= (-1)n (b) an= (-1)n/n (c)...
Evaluate if the followings are Cauchy sequences or not. (a) an= (-1)n (b) an= (-1)n/n (c) an = n/(n+1) (d) an = (cos n)/n
In C++ Let n be a non-negative integer. The factorial of n, written n!, is defined...
In C++ Let n be a non-negative integer. The factorial of n, written n!, is defined by 0 ! 5 1, n! = 1·2·3· · ·n if n $ 1. For example, 4! = 1·2·3·4 = 24. Write a program that prompts the user to enter a nonnegative integer and outputs the factorial of the number. Allow the user to repeat the program. Example: If the user enters a 3 then your program should perform answer = 3 * 2...
Question 1 a) Determine whether the language {a n b m c n | n >...
Question 1 a) Determine whether the language {a n b m c n | n > 0} is regular or not using pumping Lemma. b) Prove that the language {(ai bn | i, n > 0, i = n or i = 2n} is not regular using the Pumping Lemma.
(8%) Write a C/C++ program that takes an input (array) from 1 to n (say n...
(8%) Write a C/C++ program that takes an input (array) from 1 to n (say n = 50) and displays the string representations of those numbers with following conditions If the current number is divisible by 2, then print CSU If the current number is divisible by 5, then print SB If the current number is divisible by both 2 and 5, then print CSUSB If the number is neither divisible by 2 nor 5, then print the number Example:...
What are alpha and Beta for a sampling plan A ( n=200 c=4) B( n=300 c=8...
What are alpha and Beta for a sampling plan A ( n=200 c=4) B( n=300 c=8 ) AQL =1% LTPD =4%
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT