C++ Problem #1: Polygon vs. circle (You can do it!) Design a class named Circle
ID: 3590214 • Letter: C
Question
C++ Problem #1: Polygon vs. circle (You can do it!)
Design a class named Circle to represent a circle and a class named Polygon to represent a regular polygon.
Implement the classes (header and implementation files) using the following UML diagrams:
a.
Circle
-r: double
The radius, r, of the circle (default: 0)
+Circle()
+Circle(r: double)
+getArea(): double
+getPerimeter(): double
+getRadius(): double
+setRadius(r: double) void
Constructs a default circle object
Constructs a circle with specified radius
Returns the area of this circle
Returns the perimeter of this circle
Returns the radius of this circle
Sets a new radius, r, to this circle
Polygon
-R: double
-n: int
The circumradius, R, of the polygon (default:0)
The number of sides, n, of the polygon (default: 3)
+Polygon()
+getArea(): double const
+getPerimeter(): double const
+getApothem: double const
+getSideNumber: int const
+getSideLength: double const
+getInteriorAngle(): double const
+getExteriorAngle(): double const
+setSideNumber(sideNumber: double) void
+setCircumRadius(radius: double) void
Constructs a default polygon object
Returns the area of this polygon
Returns the perimeter of this polygon
Returns the apothem of this polygon
Returns the number of sides of this polygon
Returns the length side of this polygon
Returns the interior angle of this polygon
Returns the exterior angle of this polygon
Sets a new number of sides to this polygon
Sets a new circumradius to this polygon
b. Write a test program that
i.Randomly generate a double value ranging 1…300 for X.
ii.Create one Circle object and assign radius of X
iii.Create 100 regular polygon objects, and assign circumradius of X and set the number of sides starting at 3 and increment by 1.
iv.Find the average area and perimeter of the 100 regular polygons.
v.Print a table of information of those 100 regular polygons.
Regular n-polygon where
n - number of sides
a - apothem
r - innerradius
R - circumradius
A - areas
P - perimeter
x - interior angle
y - exterior angle
Example output:
vi. Print the area and perimeter of the circle and the average area and perimeter of the regular polygons, and their differences.
Example output:
The program output has to look exactly like the pictures above (numbers are the only exception as they have to be randomly generated!)
Thank you in advance!
Circle
-r: double
The radius, r, of the circle (default: 0)
+Circle()
+Circle(r: double)
+getArea(): double
+getPerimeter(): double
+getRadius(): double
+setRadius(r: double) void
Constructs a default circle object
Constructs a circle with specified radius
Returns the area of this circle
Returns the perimeter of this circle
Returns the radius of this circle
Sets a new radius, r, to this circle
egular n-polygon where numbeF Of sidles - apothem 1nnerradius R circumr aeas perimeter - interior angle - exterior angle 12.09 20.940 6.045 189.878 62.821 60.000 120.000 12.09 17.098 8.549 292.336 68.391 90.000 90.000 12.09 14.213 9.781 347.535 71.063 198.000 72.000 12.09 12.090 10.470 379.756 72.540 120.000 60.000 12.09 10.491 10.893 399.976 73.439 128.571 51.429 12.0909.253 11.170 413.42674.026 135.000 45.000 12.090 8.270 11.361 422.797 74.430 140.000 40.000 12.0907.47211.498 429.577 74.720 144.000 36.000 12.09 6.812 11.600 434.63474.935 147.273 32.727 12.09 6.258 11.678 438.504 75.099 150.000 30.000 12.0905.787 11.739 441.530 75.226 152.308 27.692 12.09 5.381 11.787 443.939 75.328 154.286 25.714 4Explanation / Answer
/******************************************************************************
Online C++ Compiler.
Code, Compile, Run and Debug C++ program online.
Write your code in this editor and press "Run" button to compile and execute it.
*******************************************************************************/
#include <iostream>
#include <math.h>
using namespace std;
class Circle{
private:
int r;
public:
Circle(){
r = 0;
}
Circle(double radius){
r = radius;
}
double getArea(){
return (M_PI)*r*r; // Area of circle is pi*r*r
}
double getPerimeter(){
return 2*(M_PI)*r; // Perimeter of circle is 2*pi*r
}
double getRadius(){
return r;
}
// Sets a new radius, r, to this circle
void setRadius(double radius){
r = radius;
}
};
class Polygon{
private:
int R;
int n;
// Convert defrees to radians
double degreeToRadian(double d) {
return (d / 180.0) * ((double) M_PI);
}
public:
Polygon(){
R=0;
n=3;
}
double getArea(){
return R*R*n*sin(degreeToRadian(360/n))/2;
}
double getPerimeter(){
return 2*n*R*sin(degreeToRadian(M_PI/n));
}
double getApothem(){
return R*cos(degreeToRadian(180/n));
}
int getSideNumber(){
return n;
}
double getSideLength(){
return 2*R*sin(degreeToRadian(M_PI/n));
}
double getInteriorAngle(){
double sumOfInteriorAngles = (n-2)*180;
return sumOfInteriorAngles/n;
}
double getExteriorAngle(){
return 180 - getInteriorAngle();
}
double getCircumRadius(){
return R;
}
void setSideNumber(int nS){
n = nS;
}
void setCircumRadius(int radius){
R = radius;
}
};
int main()
{
double x= 1+ (rand()*(300-1) / RAND_MAX); // min + (rand() * (int)(max - min) / RAND_MAX)
Circle c(x);
Polygon p[100];
cout<<"n a R A P x y";
int i, s;
for (i= 0, s=3;i<100;i++,s++)
{
x= 1+ (rand()*(300-1) / RAND_MAX);
p[i].setCircumRadius(x);
p[i].setSideNumber(s);
cout<<p[i].getSideNumber()<<" "<<p[i].getApothem()<<" "<<p[i].getCircumRadius()<<" "<<p[i].getArea()<<" "<<p[i].getPerimeter()<<" "<<p[i].getInteriorAngle()<<" "<<p[i].getExteriorAngle();
}
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.