Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Extend the code from Lab5B. Use the same UML as below and make extensions as necessary

Extend the code from Lab5B. Use the same UML as below and make extensions as necessary

                                                        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. Write a brand new driver program that does the following:
  2. Creates a circle object circleOne at (0,0):5.
  3. Creates a shared pointer circleOnePtr to circleOne
  4. Creates a circle object circleTwo at (-2,-2): 10
  5. Creates a shared pointer circleTwoPtr to circleTwo
  6. Use greaterThan to check if circleTwo is bigger than circleOne and display results // **** you will need to change the parameter to shared pointer
  7. Declare a vector of pointers to circles- circlePointerVector
  8. Call a function with signature inputData(circlePointerVector &, string filename) that reads data from a file called dataLab4.txt into the array The following 1-7 are done in this function

 // **** note how the & operator is used in the parameter list

  1. 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.
  2. Read the coordinates for the center and the radius from instream to create the circles
  3. 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
  4. Display all the circles in this vector of shared pointers using the toString method
  5. Display the sum of the areas of all the circles in the array
  6. Switch the position of the second and fourth circles by swapping pointers to the circles
  7. Display this changed sequence of circles using toString method

Note that the template code is given to you. You need to go through it and learn how to use shared pointers to objects, create vectors of shared pointers to objects and pass these as parameters.

You need to write one line of code.  Declare circleTwoPtr. Watch for **** in comments.

Template:

//Circle.h

#include
#include
#include
using namespace std;
#ifndef CIRCLE_H// this is optional
#define CIRCLE_H // works with or without it
class Circle{  
//private
  static int count;
  int radius;
  int x,y;
public:
Circle();
Circle(int xcoord,int ycoord, int r);
int getY();
int getX();
int getRadius();
static int getCount();
double getArea();
double getCircumference();
double getDistance(Circle other);
bool intersects(Circle other);
Circle resize(int scale);//to copy self to other;
void resize(double scale);//for self

void setX(int xcoord);
void setY(int xcoord);
void setRadius(int r);
bool greaterThan(shared_ptr other);
string toString();
~Circle();
//regular functions
//void inputData(vector);
};

#endif

//end of Circle.h

//Circle.cpp

//#include
//#include // no need for this as is in header
#define _USE_MATH_DEFINES
#include "Circle.h"
#include
#include


Circle::Circle(){
 //if you have regular constructor you must
 //define default constructor
 //C++ will not auto create for you
 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;
  }
  int Circle::getCount(){
    return count;
  }
 void Circle::setX(int xcoord){
      x=xcoord;
 }
 void Circle::setY(int xcoord){
      x=xcoord;
 }
 void Circle::setRadius(int r){
      radius=r;
 }
 double Circle::getArea(){
   return M_PI*pow(radius,2);
 }
 double Circle::getCircumference(){
   return M_PI*radius/2;
 }
 double Circle::getDistance(Circle other){
      return sqrt(pow(x-other.getX(),2)+pow((y-other.getY()),2));
 }
 bool Circle::intersects(Circle other){
   return (this->getDistance(other)<=radius);
 }
 void Circle::resize(double scale){
   radius = scale*radius;
 }
 Circle Circle::resize(int scale){
   Circle c(x,y,radius*scale);
   return c;
 }
 string Circle::toString(){
   return "(" +to_string(x)+","+to_string(y)+"):"+to_string(radius);
 }
 // *****note the change to this parameter
 //  make suitable changes where need to other instance methods
 bool Circle::greaterThan(shared_ptr  other){
   return radius>other->getRadius();
 }
Circle::~Circle(){
 //std::cout<<"Inside Destructor "< //if you have dynamically allocated memory
 //you must release/delete it here

}

int Circle::count=0;

//end of Circle.cpp

//main.cpp

#include
#include "Circle.h"
#include
#include
#include
#include
#include
#include
using namespace std;
const int SIZE = 10;
// pay attention to this parameter list
int inputData(vector> &circlePointerArray, string filename)

{
 ifstream inputFile(filename);
 istringstream instream;
 string data;
 int count =0;
 int x,y,radius;
 try{
 if (inputFile){
   while (!inputFile.eof() && count      getline(inputFile,data);
      istringstream instream(data);
      instream>>x;
      instream>>y;
      instream>>radius;
      //create a new Circle object and push it into
      //the vector
      shared_ptr circle = make_shared(x,y,radius);
      circlePointerArray.push_back(circle);
 
      count++;    
 
   }

 }
 else throw string("File Not Found");
 }
 catch (string message){
   cout<   exit(0);
 }
return count;
}

int main(){
  shared_ptr circleOnePtr = make_shared(0,0,5);
  // create a new shared pointer to a Circle Object

  //*************** create another shared pointer called circleTwoPtr
  //
 

  if (circleOnePtr->greaterThan(circleTwoPtr))
     cout<<" Circle One is bigger "<  else
     cout<<" Circle Two is bigger "<

// declare a vector of shared pointers to Circle objects
 
  std::vector>circlePointerVector;
  int count = inputData(circlePointerVector, "dataLab4.txt");
  cout<<"The total number of circles is "<  cout<<"They are :"<  for (int i=0;i   cout<toString()<   double sumOfAreas=0;
   for (int i=0;i     sumOfAreas+=circlePointerVector[i]->getArea();
  cout<<"The total sum of the areas is "< shared_ptr tmpPtr=make_shared();
  circlePointerVector[1]=circlePointerVector[3];
  cout<<"The modified array is "<  for (int i=0;i   cout<toString()<  return 0;
 
}

//end of main.cpp

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Microeconomics An Intuitive Approach with Calculus

Authors: Thomas Nechyba

1st edition

538453257, 978-0538453257

More Books

Students also viewed these Databases questions

Question

What is float? How does it affect a firms cash?

Answered: 1 week ago