Write the code for a program that tries to assemble a jigsaw puzzle by choosing
ID: 3627268 • Letter: W
Question
Write the code for a program that tries to assemble a jigsaw puzzle by choosing random pieces and seeing if they fit. This project aims to increase your comprehension of abstract classes, inheritance, polymorphism, and references.You must submit the following files, zipped together, in an archive named
lastname-firstname-puzzle.zip:
1. makefile or Makefile
2. assembler.cpp (the 'main' or 'driver' program for this project)
3. shapes.h, which contains the class denitions for the following classes:
Shape
Rectangle
Square
Circle
Triangle
(regular) Pentagon
(regular) Hexagon
4. shapes.cpp, which contains the implementations for the above classes.
Class Information (pseudoUML)
You must define the following classes and methods, adhering to the same inheritance hierarchy:
<<abstract>> class Shape:
+<<pure virtual>> float getArea(void)
class Rectangle(inherits Shape):
-float width
-float height
+<<constructor>> Rectangle(float width, float height)
+float getArea(void)
class Square(inherits Rectangle):
+<<constructor>> Square(float dimension)
class Circle(inherits Shape):
-float radius
+<<constructor>> Circle(float radius)
+float getArea(void)
class Triangle(inherits Shape):
-float width
-float height
+<<constructor>> Triangle(float width, float height)
+float getArea(void)
class Pentagon(inherits Shape):
-float edgeLength
+<<constructor>> Pentagon(float edgeLength)
+float getArea(void)
class Hexagon(inherits Shape):
-float edgeLength
+<<constructor>> Hexagon(float edgeLength)
+float getArea(void)
Runtime requirements
When run, your program should instantiate a random Shape object, called
Canvas, to be used as the canvas for the puzzle. This Canvas variable should be
a random Shape object instantiated with random dimension(s). Your program
must also declare a Shape* array called Pieces of size 6, and make every element
in the array point to a Shape object instantiated with random dimension(s). One
of each kind of Shape object should be represented in your array (e.g. Pieces[0]
could point to a Rectangle, Pieces[1] to a Pentagon, etc.). This Pieces array
represents the kinds of puzzle pieces you can use to ll your Canvas.
Then your program should pick a random piece from Pieces, subtract that
Shape's area from Canvas, and repeat until none of the Pieces will fit into the
remaining space in the Canvas.
Output
Your program should produce output just like below:
$ ./myproject
Pieces[0] -> Square(15.00)
Pieces[1] -> Rectangle(101.24, 2.00)
...etc...
Pieces[5] -> Hexagon(341.38)
Canvas: Circle of area 478.47 meters^2
Inserted Rectangle piece of area 202.24 meters^2.
Remaining Canvas area: 276.23 meters^2
Inserted Square piece of area 225.00 meters^2.
Remaining Canvas area: 51.23 meters^2
...output abbreviated...
Inserted Circle piece of area 24.57 meters^2.
Remaining Canvas area: 3.02 meters^2
No more room for any Pieces. Exiting.
Explanation / Answer
//shape.h
abstract class Shape
{
//pure virtual function
pure virtual float getArea(void);
//implementation not here
}
class Rectangle :public Shape
{
float width;
float height;
public Rectanglle(float w,float h)
{
width=w;
height=h;
}
public float getArea(void)
{
return width*height;
}
}
class Square:public Rectangle
{
float dimension;
public Square(float dim)
{
dimension=dim;
}
public float getArea(void)
{
return dimension*dimension;
}
}
class Circle:public Shape
{
float radius;
const PI=3.14;
public Circle(float r)
{
radius=r;
}
public float getArea(void)
{
return (PI*redius*radius );
}
}
class Triangle:public Shape
{
float width;
float height;
public Triangle(float w,float h)
{
width=w;
height=h;
}
public float getArea(void )
{
return (0.5*width*height);
}
}
class Pentagon:public Shape
{
float edgeLength;
public Pentagon(float edLen)
{
edgelength=edLen;
}
public float getArea(void)
{
return (edLen*edLen*1.7);
}
}
class Hexagon:public Shape
{
float edgeLength;
public Hexagon(float edLen)
{
edLen=edgeLength;
}
public float getArea(void)
{
return (edLen*edLen*2.6)
}
}
//Driver program
#include<iostream.h>
#include<conio.h>
using namespace std;
int main(void )
{
Shape canvas[6];
Square s=Square(15.00);
Rectangle r=Rectangle(101.24,24,2.0);
Circle c=Circl(10.00);
Pentagon p=Pntagon(11.00);
Hexagon h=Hexagon(11.00);
if(r.getArea()<s.getArea())
{float r;
r=s.getArea()-r.getArea();
cout<<"Remainaing
area"<<r;
}
//remiang code as foloows
return 0;
}
Hope this would help you
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.