Write a brand new driver program that does the following: Creates a circle object circleOne at (0,0):5. Creates a pointer c1Ptr to circleOne Creates a circle object circleTwo at (-2,-2): 10 Creates a pointer c2Ptr to circleTwo Use greaterThan to check if circleTwo is bigger than circleOne and display results Declare an array of pointers to circles- circlePointerArray Call a function with signature inputData(circlePointerArray, string filename) that reads data from a file called dataLab4.txt into the array The following h-k are done in this function 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. Read the coordinates for the center and the radius from instream to create the circles 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 Display all the circles in this array of pointers using the toString method Display the sum of the areas of all the circles in the array Switch the position of the second and fourth circles by swapping pointers to the circles Display this changed sequence of circles using toString method

EBK JAVA PROGRAMMING
9th Edition
ISBN:9781337671385
Author:FARRELL
Publisher:FARRELL
Chapter8: Arrays
Section: Chapter Questions
Problem 15RQ
icon
Related questions
Question

m05B

C++

Please extend my code to do the following:

  1. Write a brand new driver program that does the following:
  2. Creates a circle object circleOne at (0,0):5.
  3. Creates a pointer c1Ptr to circleOne
  4. Creates a circle object circleTwo at (-2,-2): 10
  5. Creates a pointer c2Ptr to circleTwo
  6. Use greaterThan to check if circleTwo is bigger than circleOne and display results
  7. Declare an array of pointers to circles- circlePointerArray
  8. Call a function with signature inputData(circlePointerArray, string filename) that reads data from a file called dataLab4.txt into the array The following h-k are done in this function
  9. 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.
  10. Read the coordinates for the center and the radius from instream to create the circles
  11. 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
  12. Display all the circles in this array of pointers using the toString method
  13. Display the sum of the areas of all the circles in the array
  14. Switch the position of the second and fourth circles by swapping pointers to the circles
  15. Display this changed sequence of circles using toString method

 

Here is a starter code:

int main(){
// declare two pointers to circles and create them using "new " memory
from the heap
Circle *One = new Circle(0,0,0);
Circle *Two = new Circle(1,1,1);
if (One->greaterThan(Two))
cout<<" Circle One is bigger "<<endl;
else
cout<<" Circle Two is bigger "<<endl;
// create an array of pointers to circles
Circle *circlePointerArray[SIZE];
//populate the pointer array with circles from the data file and return
the number of circles created
int count = inputData(circlePointerArray, "dataLab4.txt");
//rest of the code
}
**************** Static Function in Driver ************************
//pointer to pointer works
//Circle * circlePointer[] of course works
int inputData(Circle **circlePointerArray, string filename) // function
header for inputting data from data file into Circle array
*****************Instance Method in Circle Class*********************
bool Circle::greaterThan(Circle * other){ // method
that compares two circles using radius as key and returns true if
return this->radius > other->getRadius(); // "this
circle" is greater than the "other circle". Note the use of ->
}

My code:

Main.cpp:

in attachment

Circle.cpp:

in attachment

Circle.h:

in attachment

dataLab4.txt:

0 0 4
0 0 12
-2 -9 11
4 5 7
7 8 9
2 -5 11

 

Here is the required output:

 

Circle Two is bigger
The total number of circles is 6
They are :
(0,0):4
(0,0):12
(-2,-9):11
(4,5):7
(7,8):9
(2,-5):11
The total sum of the areas is 1671.33
The modified array is
(0,0):4
(4,5):7
(-2,-9):11
(0,0):12
(7,8):9
(2,-5):11
Transcribed Image Text:Circle Two is bigger The total number of circles is 6 They are : (0,0):4 (0,0):12 (-2,-9):11 (4,5):7 (7,8):9 (2,-5):11 The total sum of the areas is 1671.33 The modified array is (0,0):4 (4,5):7 (-2,-9):11 (0,0):12 (7,8):9 (2,-5):11
Main.cpp:
#include "Circle.h"
#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
#include <vector>
using namespace std;
// Declare function prototype here
void inputData(vector<Circle> &circles, string fileName);
int main()
{
// 1. Declare a vector of circles
vector<Circle> circles;
// 2. Call the function
inputData(circles, "dataLab4.txt");
vector<Circle>::iterator circleIterator;
// 6. Display all the circles in this vector using toString method
cout << "The circles created are:" << endl;
// 7. Use an iterator to iterate through the vector to display these circles
for(circleIterator = circles.begin(); circleIterator < circles.end(); circleIterator++) {
cout << (*circleIterator).toString() << endl;
}
// 8. Display the count of all the circles in the vector using the getCount method
cout << "The number of circles, using getCount method is " << circles [0].getCount() << endl;
// 9. Display the count of all the circles in the vector using the vector size method
cout << "The number of circles, using vector size method is << circles.size() << endl;
//Erase all circles which have a radius greater than 8
for (circleIterator = circles.begin(); circleIterator < circles.end(); circleIterator++) {
if((*circleIterator).getRadius () > 8)
circles.erase (circleIterator- -);
}
//Display the number of circles remaining using vector size method
cout << "The number of circles remaining is " << circles.size() << endl;
//Display all the circles remaining using toString()
cout<<"The circles left are : "<<endl;
for (circleIterator = circles.begin(); circleIterator < circles.end(); circleIterator++) {
cout << (*circleIterator).toString() << endl;
//create Circle object circlel at (3,4) and radius 7
Circle circlel (3, 4, 7);
//make a Circle object circle2 at (-2,-4) and radius 4
Circle circle2(-2, -4, 4);
//insert these circles into the vector at positions 2 and 3 respectively
circles.insert(circles.begin() + 1, circle1);
circles.insert(circles.begin() + 2, circle2);
//Display the vector of circles
cout <<"The circles vector now has these circles : "<<endl;
for (circleIterator = circles.begin(); circleIterator < circles.end(); circleIterator++) {
cout << (*circleIterator).toString() << endl;
}
return 0;
}
// c-e read file and fill the vector of circles
void inputData(vector<Circle> &circles, string fileName) {
try{
// input file stream
ifstream inFile;
// Open input file
infile.open(fileName);
string str;
// 4. Read the coordinates for the center and the radius from instream to create the circles
4.1
/12.T:
Ln 240, Col 8
File Edit View
infile.open(fileName);
string str;
// 4. Read the coordinates for the center and the radius from instream to create the circles
while (!inFile.eof())
{
// read the entire line from file
getline (inFile, str);
// if content is empty exit the loop
if(str == ""){
break;
}
// Object class of istringstream
istringstream instream(str);
int x, y, radius;
instream >> x >> y ›› radius;
// Create the circle
Circle circle(x,y,radius);
circles.push_back(circle);
}
// close the file
inFile.close();
}catch(std::exception const& e) {
// error message
cout << "File open error" << e.what() << endl;
}
}
Circle.cpp:
#include "Circle.h"
// static variable to keep count of number of Circles created
int Circle::count = 0;
// default constructor that sets origin to (0,0) and radius to 1
Circle : Circle() {
this->x = 0;
this->y = 0;
this->radius = 1;
Circle::count += 1;
}
// regular constructor
Circle: Circle(int x, int y, int radius) {
this->x= x;
this->y = y;
this->radius = radius;
Circle::count += 1;
}
//Destructor
Circle :: ~Circle() {
cout << "Inside Destructor"<<endl;
}
// getter for x
int Circle :: getX(){
return x;
}
// getter for y
int Circle :: getY(){
return y;
}
// getter for radius
int Circle :: getRadius(){
return radius;
}
// setter for x
void Circle :: setX(int newX) {
this->x = newX;
}
// setter for x
void Circle :: setY(int newY) {
this->y = newY;
}
In 240 Col 8
this->y = newY;
}
// setter for x
void Circle :: setRadius(int newRadius) {
this->radius = newRadius;
}
// returns the area
double Circle :: getArea() {
return 3.14 * radius * radius;
}
// returns the circumference
double Circle :: getCircumference() {
return 2 * 3.14 * radius;
}
// returns the circle as a string in the form (x,y): radius
string Circle :: toString() {
return "(" + to_string(x) + ", ' + to_string(y) +")" +":"+to_string(radius);
}
// returns the distance between the center of this circle and the other circle
double Circle :: getDistance (Circle other) {
return sqrt(pow(other.getX() -this->x, 2) + pow(other.getY() - this->y, 2));
}
// move the center of the circle to the new coordinates
void Circle :: moveTo(int newX, int newY) {
this->x= newX;
this->y = newY;
}
// returns true if the center of the other circle lies inside this circle else returns false
bool Circle :: intersects (Circle other) {
return (this->getDistance (other)) < (this->radius + other.getRadius());
}
// multiply the radius by the scale
void Circle :: resize(double scale) {
this->radius *= scale;
}
// returns a new Circle with the same center as this circle but radius multiplied by scale
Circle Circle :: resize(int scale) {
return Circle(this->x, this->y, this->radius * scale);
}
//returns the number of circles created
int Circle :: getCount() {
return count;
}
Circle.h:
#ifndef CIRCLE_H
#define CIRCLE_H
#include <iostream>
#include <cmath>
using namespace std;
class Circle{
private:
int x; //
int y;
//
int radius;
static int count; // static variable to keep count of number of Circles created
x coord of the center
y coord of the center
public:
Circle(); // default constructor that sets origin to (0,0) and radius to 1
x, int y, int radius); // regular constructor
Circle(int
~Circle(); //Destructor
int getX();
int getY();
int getRadius();
void setX(int newX);
void setY(int newY);
void setRadius(int newRadius);
double getArea(); // returns the area
double getCircumference(); // returns the circumference
string toString(); // returns 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
void moveTo(int newX, int newY); // move the center of the circle to the new coordinates
..
Transcribed Image Text:Main.cpp: #include "Circle.h" #include <iostream> #include <string> #include <fstream> #include <sstream> #include <vector> using namespace std; // Declare function prototype here void inputData(vector<Circle> &circles, string fileName); int main() { // 1. Declare a vector of circles vector<Circle> circles; // 2. Call the function inputData(circles, "dataLab4.txt"); vector<Circle>::iterator circleIterator; // 6. Display all the circles in this vector using toString method cout << "The circles created are:" << endl; // 7. Use an iterator to iterate through the vector to display these circles for(circleIterator = circles.begin(); circleIterator < circles.end(); circleIterator++) { cout << (*circleIterator).toString() << endl; } // 8. Display the count of all the circles in the vector using the getCount method cout << "The number of circles, using getCount method is " << circles [0].getCount() << endl; // 9. Display the count of all the circles in the vector using the vector size method cout << "The number of circles, using vector size method is << circles.size() << endl; //Erase all circles which have a radius greater than 8 for (circleIterator = circles.begin(); circleIterator < circles.end(); circleIterator++) { if((*circleIterator).getRadius () > 8) circles.erase (circleIterator- -); } //Display the number of circles remaining using vector size method cout << "The number of circles remaining is " << circles.size() << endl; //Display all the circles remaining using toString() cout<<"The circles left are : "<<endl; for (circleIterator = circles.begin(); circleIterator < circles.end(); circleIterator++) { cout << (*circleIterator).toString() << endl; //create Circle object circlel at (3,4) and radius 7 Circle circlel (3, 4, 7); //make a Circle object circle2 at (-2,-4) and radius 4 Circle circle2(-2, -4, 4); //insert these circles into the vector at positions 2 and 3 respectively circles.insert(circles.begin() + 1, circle1); circles.insert(circles.begin() + 2, circle2); //Display the vector of circles cout <<"The circles vector now has these circles : "<<endl; for (circleIterator = circles.begin(); circleIterator < circles.end(); circleIterator++) { cout << (*circleIterator).toString() << endl; } return 0; } // c-e read file and fill the vector of circles void inputData(vector<Circle> &circles, string fileName) { try{ // input file stream ifstream inFile; // Open input file infile.open(fileName); string str; // 4. Read the coordinates for the center and the radius from instream to create the circles 4.1 /12.T: Ln 240, Col 8 File Edit View infile.open(fileName); string str; // 4. Read the coordinates for the center and the radius from instream to create the circles while (!inFile.eof()) { // read the entire line from file getline (inFile, str); // if content is empty exit the loop if(str == ""){ break; } // Object class of istringstream istringstream instream(str); int x, y, radius; instream >> x >> y ›› radius; // Create the circle Circle circle(x,y,radius); circles.push_back(circle); } // close the file inFile.close(); }catch(std::exception const& e) { // error message cout << "File open error" << e.what() << endl; } } Circle.cpp: #include "Circle.h" // static variable to keep count of number of Circles created int Circle::count = 0; // default constructor that sets origin to (0,0) and radius to 1 Circle : Circle() { this->x = 0; this->y = 0; this->radius = 1; Circle::count += 1; } // regular constructor Circle: Circle(int x, int y, int radius) { this->x= x; this->y = y; this->radius = radius; Circle::count += 1; } //Destructor Circle :: ~Circle() { cout << "Inside Destructor"<<endl; } // getter for x int Circle :: getX(){ return x; } // getter for y int Circle :: getY(){ return y; } // getter for radius int Circle :: getRadius(){ return radius; } // setter for x void Circle :: setX(int newX) { this->x = newX; } // setter for x void Circle :: setY(int newY) { this->y = newY; } In 240 Col 8 this->y = newY; } // setter for x void Circle :: setRadius(int newRadius) { this->radius = newRadius; } // returns the area double Circle :: getArea() { return 3.14 * radius * radius; } // returns the circumference double Circle :: getCircumference() { return 2 * 3.14 * radius; } // returns the circle as a string in the form (x,y): radius string Circle :: toString() { return "(" + to_string(x) + ", ' + to_string(y) +")" +":"+to_string(radius); } // returns the distance between the center of this circle and the other circle double Circle :: getDistance (Circle other) { return sqrt(pow(other.getX() -this->x, 2) + pow(other.getY() - this->y, 2)); } // move the center of the circle to the new coordinates void Circle :: moveTo(int newX, int newY) { this->x= newX; this->y = newY; } // returns true if the center of the other circle lies inside this circle else returns false bool Circle :: intersects (Circle other) { return (this->getDistance (other)) < (this->radius + other.getRadius()); } // multiply the radius by the scale void Circle :: resize(double scale) { this->radius *= scale; } // returns a new Circle with the same center as this circle but radius multiplied by scale Circle Circle :: resize(int scale) { return Circle(this->x, this->y, this->radius * scale); } //returns the number of circles created int Circle :: getCount() { return count; } Circle.h: #ifndef CIRCLE_H #define CIRCLE_H #include <iostream> #include <cmath> using namespace std; class Circle{ private: int x; // int y; // int radius; static int count; // static variable to keep count of number of Circles created x coord of the center y coord of the center public: Circle(); // default constructor that sets origin to (0,0) and radius to 1 x, int y, int radius); // regular constructor Circle(int ~Circle(); //Destructor int getX(); int getY(); int getRadius(); void setX(int newX); void setY(int newY); void setRadius(int newRadius); double getArea(); // returns the area double getCircumference(); // returns the circumference string toString(); // returns 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 void moveTo(int newX, int newY); // move the center of the circle to the new coordinates ..
Expert Solution
steps

Step by step

Solved in 2 steps

Blurred answer
Follow-up Questions
Read through expert solutions to related follow-up questions below.
Follow-up Question

The answer is incomplete, please provide full code with output. TYSM. 

Solution
Bartleby Expert
SEE SOLUTION
Knowledge Booster
User Defined DataType
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
EBK JAVA PROGRAMMING
EBK JAVA PROGRAMMING
Computer Science
ISBN:
9781337671385
Author:
FARRELL
Publisher:
CENGAGE LEARNING - CONSIGNMENT
Programming Logic & Design Comprehensive
Programming Logic & Design Comprehensive
Computer Science
ISBN:
9781337669405
Author:
FARRELL
Publisher:
Cengage