Question: Grid Writer excrise : In this part of the assignment you will not need to write any code. Instead you will submit your answers to

Grid Writer excrise :

In this part of the assignment you will not need to write any code. Instead you will submit your answers to written questions regarding the given code.

The project uses a class named GridWriter to display circles and rectangles in a text grid. The GridWriter class is a collection type class similar to an IntCollection. It stores Circle and Rectangle objects that inherit from a base class named Shape.

The GridWriter class has a member function named display that accepts a row count and column count as arguments, and prints out a text grid that shows the shapes that it stores.

Consider the main function below. It creates a GridWriter, adds some shapes, and then displays a grid:

int main() {

GridWriter gw;

// Add some circles...

gw.add(new Circle(10, 10, 9));

gw.add(new Circle(25, 20, 12));

gw.add(new Circle(25, 20, 5));

// Add a rectangle...

gw.add(new Rectangle(40, 0, 10, 10));

// Display the grid with 40 rows and 50 columns

gw.display(40, 50);

}

The console output will look like this:

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . 1 . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . 1 1 1 1 1 1 1 1 1 . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . 1 1 1 1 1 1 1 1 1 1 1 1 1 . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . 1 1 1 1 1 1 1 1 1 1 2 1 1 1 1 1 1 1 1 1 1 . . . . . . . . . . . . . .

. . . . . . . . . . . . . . 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 1 1 1 1 1 1 1 1 . . . . . . . . . . . . .

. . . . . . . . . . . . . . 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 1 1 1 1 1 1 1 . . . . . . . . . . . . .

. . . . . . . . . . . . . . 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 1 1 1 1 1 1 1 . . . . . . . . . . . . .

. . . . . . . . . . . . . . 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 1 1 1 1 1 1 1 . . . . . . . . . . . . .

. . . . . . . . . . . . . 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 2 1 1 1 1 1 1 1 . . . . . . . . . . . .

. . . . . . . . . . 1 . . . 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 1 1 1 1 1 1 1 . . . . . . . . . . . . .

. . . . . . 1 1 1 1 1 1 1 1 2 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 1 1 1 1 1 1 1 . . . . . . . . . . . . .

. . . . . 1 1 1 1 1 1 1 1 1 2 2 1 1 1 1 1 2 2 2 2 2 2 2 2 2 1 1 1 1 1 1 1 . . . . . . . . . . . . .

. . . . 1 1 1 1 1 1 1 1 1 1 2 2 2 1 1 1 1 1 2 2 2 2 2 2 2 1 1 1 1 1 1 1 1 . . . . . . . . . . . . .

. . . 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 1 1 1 1 1 1 1 2 1 1 1 1 1 1 1 1 1 1 . . . . . . . . . . . . . .

. . 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 . . . . . . . . . . . . . .

. . 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 . . . . . . . . . . . . . . .

. . 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 . . . . . . . . . . . . . . . .

. . 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 1 1 1 1 1 1 1 1 1 1 1 1 1 1 . . . . . . . . . . . . . . . . .

. 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 1 1 1 1 1 1 1 1 1 1 1 1 . . . . . . . . 1 1 1 1 1 1 1 1 1 1

. . 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 . . 1 1 1 1 1 1 1 1 1 . . . . . . . . . . 1 1 1 1 1 1 1 1 1 1

. . 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 . . . . . . 1 . . . . . . . . . . . . . . 1 1 1 1 1 1 1 1 1 1

. . 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 . . . . . . . . . . . . . . . . . . . . . 1 1 1 1 1 1 1 1 1 1

. . 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 . . . . . . . . . . . . . . . . . . . . . 1 1 1 1 1 1 1 1 1 1

. . . 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 . . . . . . . . . . . . . . . . . . . . . . 1 1 1 1 1 1 1 1 1 1

. . . . 1 1 1 1 1 1 1 1 1 1 1 1 1 . . . . . . . . . . . . . . . . . . . . . . . 1 1 1 1 1 1 1 1 1 1

. . . . . 1 1 1 1 1 1 1 1 1 1 1 . . . . . . . . . . . . . . . . . . . . . . . . 1 1 1 1 1 1 1 1 1 1

. . . . . . 1 1 1 1 1 1 1 1 1 . . . . . . . . . . . . . . . . . . . . . . . . . 1 1 1 1 1 1 1 1 1 1

. . . . . . . . . . 1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 1 1 1 1 1 1 1 1 1

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 1 1 1 1 1 1 1 1 1

The code in the main function adds three Circle objects and one Rectangle object to the GridWriter. See if you can find the shapes in the output.

The Shape class defines x and y. Both the circle and the rectangle class inherit these instance variables. Similarly, the Shape class defines the getArea and containsPoint member functions, and the circle and rectangle inherit them. The circle and rectangle classes extend the Shape class by adding radius or height and width.

Download the GridWriter.zip from Canvasand examine the classes. Carefully read through the code and note any statements that you do not understand. When you are comfortable with the code, answer the following questions. Submit your answers in a text file.

1. The two argument constructor of the Shape class contains this code:

this->x = x;

this->y = y;

Why is it necessary to put this-> in front of x and y? If you are not sure then remove these lines and see if it changes the output, which may provide you a hint why it is necessary. Note: the answer to this question is not what happens if you remove "this->" but why do you need it.

2. In the containsPoint member function of Circle, why are this->x and this->y in scope, even though they are not defined as member variables in Circle.h?

3. What is the purpose of the keyword virtual in front of Shape::containsPoint and Shape::getArea? What is the purpose of the = 0?

4. In Circle.cpp the three argument Circle constructor is defined like this:

Circle:: Circle(int x, int y, int radius) : Shape(x, y) {

this->radius = radius;

}

What is the purpose of the : Shape(x, y) (shown in bold in the code above). What happens if you remove it?

5. List an example of polymorphism that you can find in the code base. That is to say, list a statement where the datatype of a reference or pointer is not an exact match with the datatype of the object that it refers to.

What to submit?

Grid Writer

Submit your answers to the GridWriter questions in a plain text file, e.g. GridWriter.txt.

Given code:

file name : Circle.cpp

//

// Circle.cpp

// GridWriter

//

#include

#include "Circle.h"

Circle::Circle(int x, int y, int radius) : Shape(x, y) {

this->radius = radius;

}

bool Circle::containsPoint(int x, int y) {

int dx = this->x - x;

int dy = this->y - y;

return pow(radius, 2) >= (pow(dx, 2) + pow(dy, 2));

}

double Circle::getArea() {

return pow(radius, 2) * M_PI;

}

_______________________________________________

file name : Circle.h

//

// Circle.h

// GridWriter

//

#ifndef GridWriter__Circle__H

#define GridWriter__Circle__H

#include "Shape.h"

class Circle : public Shape

{

private:

int radius;

public:

Circle(int x, int y, int radius);

void setRadius(int value ) {radius = value;}

int getRadius() {return radius;}

void setX(int value) {x = value;}

int getX() {return x;}

void setY(int value) {y = value;}

int getY() {return y;}

bool containsPoint(int x, int y);

double getArea();

};

#endif

_________________________________________________________

file name : GridWriter.cpp

//

// GridWriter.cpp

// GridWriter

//

#include "GridWriter.h"

#include "Shape.h"

const int INITIAL_CAPACITY = 4;

GridWriter::GridWriter(){

size = 0;

capacity = INITIAL_CAPACITY;

shapes = new Shape*[capacity];

}

GridWriter::GridWriter(const GridWriter &g) {

size = g.size;

capacity = g.capacity;

shapes = new Shape*[capacity];

// copy the shape pointers

for (int i = 0; i < g.size; i++) {

shapes[i] = g.shapes[i];

}

}

GridWriter::~GridWriter(){

delete [] shapes;

}

GridWriter& GridWriter::add(Shape *s){

// Make sure that we have room

if (size >= capacity) {

doubleArrayCapacity();

}

// save the pointer into the array of Shape pointers

shapes[size] = s;

size++;

// Return this to allow for chaining

return *this;

}

Shape& GridWriter::get(int index) const{

return *shapes[index];

}

//

void GridWriter::display(int rowCount, int columnCount) {

for (int r = rowCount; r >= 0; r--) {

for (int c = 0; c < columnCount; c++) {

int count = numberOfShapesCoveringPoint(c, r);

if (count == 0) {

std::cout << ". ";

} else {

std::cout << count << ' ';

}

}

std::cout << std::endl;

}

}

//

// Private Helpers

//

void GridWriter::doubleArrayCapacity() {

// int the capacity

capacity *= 2;

// allocate a new array

Shape** newArray = new Shape*[capacity];

// copy the shape pointers

for (int i = 0; i < size; i++)

newArray[i] = shapes[i];

// delete the old array

delete [] shapes;

// save the address of the new array into shapes

shapes = newArray;

}

int GridWriter::numberOfShapesCoveringPoint(int x, int y) {

int count = 0;

for (int i = 0; i < size; i++) {

if (shapes[i]->containsPoint(x, y)) {

count++;

}

}

return count;

}

_________________________________________________________

file name: GridWriter.h

//

// GridWriter.h

// GridWriter

//

// Created by Jason Schatz on 5/3/13.

// Copyright (c) 2013 Jason Schatz. All rights reserved.

//

#ifndef GridWriter__GridWriter__H

#define GridWriter__GridWriter__H

#include

#include "Shape.h"

class GridWriter

{

private:

Shape** shapes;

int size;

int capacity;

void doubleArrayCapacity();

int numberOfShapesCoveringPoint(int x, int y);

public:

GridWriter();

GridWriter(const GridWriter &g);

~GridWriter();

GridWriter& add(Shape *s);

Shape& get(int index) const;

int getSize() const {return size;}

int getCapacity() const {return capacity;};

void display(int rowCount, int columnCount);

};

#endif

____________________________________________________

file name: main.cpp

//

// main.cpp

// GridWriter

//

// Created by Jason Schatz on 5/3/13.

// Copyright (c) 2013 Jason Schatz. All rights reserved.

//

#include

#include "GridWriter.h"

#include "Shape.h"

#include "Circle.h"

#include "Rectangle.h"

int main() {

GridWriter gw;

// Add some circles...

gw.add(new Circle(10, 10, 9));

gw.add(new Circle(25, 20, 12));

gw.add(new Circle(25, 20, 5));

// Add a rectangle...

gw.add(new Rectangle(40, 0, 10, 10));

// Display the grid with 40 rows and 50 columns

gw.display(40, 50);

}

_____________________________________________________________

file name: Rectangle.cpp

//

// Rectangle.cpp

// GridWriter

//

// Created by Jason Schatz on 5/3/13.

// Copyright (c) 2013 Jason Schatz. All rights reserved.

//

#include "Rectangle.h"

Rectangle:: Rectangle(int x, int y, int width, int height) : Shape(x, y) {

this->width = width;

this->height = height;

}

bool Rectangle::containsPoint(int x, int y) {

return x >= this->x && x <= (this->x + width) &&

y >= this->y && y <= (this->y + height);

}

double Rectangle::getArea() {

return height * width;

}

_______________________________________________________

file name: Rectangle.h

//

// Rectangle.h

// GridWriter

//

#ifndef GridWriter__Rectangle__H

#define GridWriter__Rectangle__H

#include "Shape.h"

class Rectangle : public Shape

{

private:

int width;

int height;

public:

Rectangle(int x, int y, int width, int height);

bool containsPoint(int x, int y);

double getArea();

};

#endif

_________________________________________________

file name: Shape.h

//

// Shape.h

// GridWriter

//

#ifndef GridWriter__Shape__H

#define GridWriter__Shape__H

#include

class Shape

{

protected:

int x;

int y;

public:

Shape(int x, int y) {

this->x = x;

this->y = y;

}

void setX(int value) {x = value;}

int getX() {return x;}

void setY(int value) {y = value;}

int getY() {return y;}

virtual bool containsPoint(int x, int y) = 0;

virtual double getArea() = 0;

};

#endif

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!