Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Is my program correct? How do I get the area diameter and circumference to outpu

ID: 3808297 • Letter: I

Question

Is my program correct?

How do I get the area diameter and circumference to output with only 2 decimal places and still define pi and radius as floats?

I am using visual studio 2013 and answers need to adhere to lateset c++ formatting and common use rules(no code from 2012).

This is the homework problem below.

Write a C++ program that will use a class called Circle that has the following member variables:

radius: a float

pi: a float initialized with the value 3.14159

The class should have the following member functions:

constructor - sets the radius value = 0.0.

setRadius - a mutator function for the radius variable.

getRadius - an accessor function for the radius variable.

getArea - returns the area of a circle, which is calculated as:

                    area = pi * radius * radius

getDiameter - returns the diameter of a circle, which is calculated as:

                    diameter = radius * 2

getCircumference - returns the circumference of the circle, which is calculated as

                    circumference = 2 * pi * radius

Write a program that demonstrates the Circle class by asking the user for the circle's radius, creating a Circle object, and then reporting the circle's area, diameter, and circumference with 2 decimal places each.

Here is what i have so far.

//Header files (Preprocessor directives)
#include


//Namespaces
using namespace std;

class Circle   //Class Declaration
{
private:   //Access Specifier
   float radius;   //Member Variables
   float pi = 3.14159;

public:
   Circle();
   Circle(double);
   void setRadius(double);
   double getRadius();
   double getArea();
   double getDiameter();
   double getCircumference();
   ~Circle();
};

   Circle::Circle()
   {
       radius = 0.0;
   }

   Circle::Circle(double r)
   {
       setRadius(r);
   }

   //Mutator Memeber Function
   void Circle::setRadius(double r)
   {
       radius = r;
   }

   //Accessor Functions
   double Circle::getRadius()
   {
       return radius;
   }

   double Circle::getArea()
   {
       double area = pi * radius * radius;
       return area;
   }

   double Circle::getDiameter()
   {
       double diameter = 2.0 * radius;
       return diameter;
   }

   double Circle::getCircumference()
   {
       double circumference = 2.0 * pi * radius;
       return circumference;
   }

   Circle::~Circle()
   {

   }

   void display(Circle circ);

   double getInput();

   void validate(double& n);


int main()
{
   double rad;

   Circle circle;

   rad = getInput();

   circle.setRadius(rad);

   display(circle);

   return 0;

}

double getInput()
{
   double n;

   cout << " Enter the radius of the circle ";

   cin >> n;

   validate(n);

   return n;
}

void validate(double& n)
{
   while (!cin || n <= 0)
   {
       cout << " Invalid input, please enter a number > 0 ";
       fseek(stdin, 0, SEEK_END);
       cin.clear();
       cin >> n;
   }
}

void display(Circle circ)
{

   cout << " The Area of the circle is " << circ.getArea();
   cout << " The diameter of the circle is " << circ.getDiameter();
   cout << " The circumference of the circle is " << circ.getCircumference();
   cout << " ";
}

Explanation / Answer

//Your Program is correct

//+ I added your corrections for two decimal points

//Header files (Preprocessor directives)
#include<iostream>
#include <cmath>
//Namespaces
using namespace std;
class Circle //Class Declaration
{
private: //Access Specifier
float radius; //Member Variables
float pi;
public:
Circle();
Circle(double);
void setRadius(double);
double getRadius();
double getArea();
double getDiameter();
double getCircumference();
~Circle();
};
Circle::Circle()
{
radius = 0.0;
   pi = 3.14159;
}
Circle::Circle(double r)
{
setRadius(r);
}
//Mutator Memeber Function
void Circle::setRadius(double r)
{
radius = r;
}
//Accessor Functions
double Circle::getRadius()
{
return radius;
}
double Circle::getArea()
{
double area = pi * radius * radius;
return floor(area*100)/100;
}
double Circle::getDiameter()
{
double diameter = 2.0 * radius;
return floor(diameter*100)/100;
}
double Circle::getCircumference()
{
double circumference = 2.0 * pi * radius;
return floor(circumference*100)/100;
}
Circle::~Circle()
{
}
void display(Circle circ);
double getInput();
void validate(double& n);

int main()
{
double rad;
Circle circle;
rad = getInput();
circle.setRadius(rad);
display(circle);
return 0;
}
double getInput()
{
double n;
cout << " Enter the radius of the circle ";
cin >> n;
validate(n);
return n;
}
void validate(double& n)
{
while (!cin || n <= 0)
{
cout << " Invalid input, please enter a number > 0 ";
fseek(stdin, 0, SEEK_END);
cin.clear();
cin >> n;
}
}
void display(Circle circ)
{
cout << " The Area of the circle is " << circ.getArea();
cout << " The diameter of the circle is " << circ.getDiameter();
cout << " The circumference of the circle is " << circ.getCircumference();
cout << " ";
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote