Use pen/paper or a design tool such as Visio to design the application described
ID: 3850318 • Letter: U
Question
Use pen/paper or a design tool such as Visio to design the application described below. After you design, go ahead and implement it. Your submission should include the entire project (zip the entire project and submit), and the design document (as a PDF file. If you want to put some explanation, you can get a screenshot and paste it as an image into a Word document, then Save As PDF). The design document should be the fully defined UML Class Diagram (with 3 compartments, fields and methods).
Application:
Write an application that receives and reads from a text file. Text file should be formatted as: BEGIN Square 2.0 Circle 1.5 Rectangle 3.0 4.0 Triangle 5.0 3.0 END
Once the application sees BEGIN, it should read the lines till it sees END. The purpose is to get the first word, determine which object to create, use the provided parameters to create the object, calculate its area (assume centimeters) and display the information in the console. So if we provide the text file shown above, then the output by calling each object’s toString() method should be: The area of the square is 4.00 cm2 The area of the circle is 7.06 cm2 The area of the rectangle is 12.00 cm2 The area of the triangle is 7.50 cm2
The area formula for the objects: Square = side x side Circle = x r2 Rectangle = side1 x side2 Triangle = height x base / 2
You should create a Shape class as the parent of all and define common behavior. Children should override toString() method and implement their unique ways of calculating the area. Parent Shape class should be abstract and define area as an abstract method. There should be two constructors for each object: default constructor, which sets everything to 0 and a fully defined constructor which assigns fields as you pass the information as parameters. Use the file I provided for testing purposes.
Explanation / Answer
#include<iostream>
#include<cstring>
#include<fstream>
using namespace std;
class Shape
{
public:
virtual float getarea() =0 ;
};
class Circle: public Shape
{
float radius;
public:
Circle(float radius)
{
radius = 0;
}
float getarea() ;
};
class Rectangle: public Shape
{
public:
Rectangle(float width, float height);
float getarea() ;
};
class Square: public Rectangle
{
public:
Square(float size);
float getarea() ;
};
Square::Square (float size) : Rectangle(size, size) { }
Rectangle::Rectangle(float width, float height)
{
this->width = width; this->height = height;
}
Circle::Circle (float radius)
{
this->width = this->height = radius * 2.0;
}
float Circle::getarea()
{
return width * width * 3.14159265 / 4.0;
}
float Rectangle::getarea()
{
return width * height ;
}
int main ()
{
ifstream file;
cout<<"Enter file name:"<<endl;
cin>>fname;
file.open(fname);
if(!file)
{
cout<<"Error in opening file..!!"<<endl;
exit(0);
}
while(ifile.eof()==0)
{
ifile>>s;
cout<<s<<" ";
}
Shape * papers[5];
Circle michael(15.0);
Rectangle andrew(34.0,2.0);
Circle ann(2.3);
Square tim(3.33);
Rectangle jonathan(5.61,7.92);
papers[0] = &michael; papers[1] = &andrew; papers[2] = &ann;
papers[3] = &tim; papers[4] = &jonathan;
for (int k=0; k<5; k++) {
cout << "Area is " << papers[k]->getarea() << endl;
}
return 1;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.