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

C++ Task: You are to write a class called Sphere, using filenames Sphere.h and S

ID: 3748816 • Letter: C

Question

C++

Task: You are to write a class called Sphere, using filenames Sphere.h and Sphere.cpp. This class will allow creation and handling of Sphere objects. The Sphere class is described below. Class Details 1. The Sphere class has two attributes called radius and color as member data. Radius will be a double value indicating the radius of the Sphere object, whereas color is a character used to store the color of the sphere. The class will need to provide internal storage for the member data that must be kept track of.

2. The valid values for color are B (blue), R (red), P (purple), Y (yellow), G (green), L (black), and M (maroon).

3. The Sphere class should have a default constructor to create a Sphere object of radius 1.0 and a random color (use a random number generator for this).

4. The Sphere class should have a constructor with two parameters: a double (required) which is the radius of the sphere and a char (optional) which is the color of the sphere. If the radius provided is smaller or equal to 0.0, set the radius to 1.0. If the color is not specified or is an invalid value, a random color should be assigned as the color of the sphere (use a random number generator for this). When entering the color, both uppercase and lowercase inputs should be accepted.

5. The Sphere class should have at least the following member functions: o getRadius, getColor, getDiameter, getSurfaceArea, and getVolume, which will return the Sphere’s radius, its color, its diameter, its surface area, and its volume, respectively. Note that for some of these functions you will need to compute those values as they are not member data. The formulas are (r is the radius, = 3.14159):

diameter = 2 pi

volume = 4/3 *pi*r^3

surfaceArea= 4pi*r^2

setRadius, setColor - these mutator functions will update the values of radius and color, respectively. setRadius should receive a double as parameter and update the sphere’s radius to be that value if the value is larger than 0.0. No change should be made if the radius provided as parameter is smaller or equal to 0.0. setColor should receive a single char parameter. This function should update the sphere’s color accordingly if the character provided is a valid character color. No change should be made if the char provided is not a valid color. None of these functions have a return statement.

grow and shrink, which will increase or decrease, respectively, the radius of the Sphere by the given double value, unless this would cause the size to be less or equal to 0.0. If applying grow or shrink leads to a radius less than or equal to 0.0 no change should be made.

randomizeColor, this function should randomly assign a new color for the sphere (use a random number generator for this). This is a void function that receives no parameters. o printSummary, which receives an optional integer precision as parameter and displays all the information about a sphere – one item per line: its radius, color, diameter, surface area, and volume. When displaying the data, always show exactly the number of decimal places specified by the precision parameter. If no precision is specified, or the precision is larger than 5 or smaller than 1, the output should be shown to two decimal places. The summary should print the color of the sphere as the full word for that color (e.g., Red, Green) and not only the char representation stored in the object. Your output should be in the exact same format shown in the example below.

Example: Given a blue Sphere with radius 1.0, a call to printSummary with a precision parameter of 2 would produce the following output:

Radius: 1.00 Color: Blue Diameter: 2.00 Surface Area: 12.57 Volume: 4.19

General Requirements 1. Make sure that your program works with g++ on linprog before you hand it in.

2. Include a comment header with your NAME and SECTION at the top of your submitted files.

3. No global variables, other than constants.

4. No goto statements.

5. No system calls.

6. All member data of the class must be private.

7. You may use the iostream, cstdlib, ctime, iomanip, and iostream libraries. You CANNOT use the cmath library.

8. Do not use language or library features that are C++11-only.

9. When you write source code, it should be readable and well-documented. o Refer to the notes on style guidelines posted on Canvas.

10. Your Sphere.h file should contain the class declaration only. The Sphere.cpp file should contain the member function definitions.

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

***********SAMPLE OUTPUT***********

Print summaries

Summary Sphere 1

Radius: 1.00000

Color: Purple

Diameter: 2.00000

Surface Area: 12.56636

Volume: 4.18879

Summary Sphere 2

Radius: 2.50000

Color: Purple

Diameter: 5.00000

Surface Area: 78.53975

Volume: 65.44979

Summary Sphere 3

Radius: 1.54000

Color: Black

Diameter: 3.08000

Surface Area: 29.80238

Volume: 15.29855

Summary Sphere 4

Radius: 5.00000

Color: Yellow

Diameter: 10.00000

Surface Area: 314.15900

Volume: 523.59833

Summary Sphere 5

Radius: 32.32000

Color: Maroon

Diameter: 64.64000

Surface Area: 13126.59849

Volume: 141417.22104

--> Growing Spheres by 2.50000

Print summaries

Summary Sphere 1

Radius: 3.50

Color: Purple

Diameter: 7.00

Surface Area: 153.94

Volume: 179.59

Summary Sphere 2

Radius: 5.00

Color: Purple

Diameter: 10.00

Surface Area: 314.16

Volume: 523.60

Summary Sphere 3

Radius: 4.04

Color: Black

Diameter: 8.08

Surface Area: 205.10

Volume: 276.21

Summary Sphere 4

Radius: 7.50

Color: Yellow

Diameter: 15.00

Surface Area: 706.86

Volume: 1767.14

Summary Sphere 5

Radius: 34.82

Color: Maroon

Diameter: 69.64

Surface Area: 15235.86

Volume: 176837.57

--> Shrinking Spheres by 2.00

Print summaries

Summary Sphere 1

Radius: 1.50

Color: Purple

Diameter: 3.00

Surface Area: 28.27

Volume: 14.14

Summary Sphere 2

Radius: 3.00

Color: Purple

Diameter: 6.00

Surface Area: 113.10

Volume: 113.10

Summary Sphere 3

Radius: 2.04

Color: Black

Diameter: 4.08

Surface Area: 52.30

Volume: 35.56

Summary Sphere 4

Radius: 5.50

Color: Yellow

Diameter: 11.00

Surface Area: 380.13

Volume: 696.91

Summary Sphere 5

Radius: 32.82

Color: Maroon

Diameter: 65.64

Surface Area: 13535.88

Volume: 148082.58

Explanation / Answer

//File Name: Sphere.h
#ifndef Sphere_H
#define Sphere_H

// Defines class Sphere
class Sphere
{
private:
// Data member to store data
double Radius;
char color;
public:
// Prototype of member functions
Sphere();
Sphere(double, char);
double getRadius();
char getColor();
void setRadius(double);
void setColor(char);
double getDiameter();
double getSurfaceArea();
double getVolume();
void grow(double);
void shrink(double);
void printSummary();
};// End of class

#endif

-------------------------------------------------------------------------

//File Name: Sphere.cpp
#include "Sphere.h"
#include <iostream>
#include <iomanip>
#include <time.h>
#include <stdlib.h>
#define PI 3.14159 // Defines PI value
using namespace std;

// Function to return color character
char getColor()
{
// Use current time as seed for random generator
srand(time(0));
// Generates a random number between 1 and 7
int no = rand() % 7 + 1;

// Check the random color number and returns appropriate color character
switch(no)
{
case 1:
return 'B';
case 2:
return 'R';
case 3:
return 'P';
case 4:
return 'Y';
case 5:
return 'G';
case 6:
return 'L';
case 7:
return 'M';
}// End of switch - case
}// End of function

// Function to return true if color character is valid otherwise returns false
bool validColor(char c)
{
// Checks the lower and capital color character
if(c == 'R' || c == 'r' || c == 'P' || c == 'p' || c == 'Y' || c == 'y' || c == 'G' || c == 'g' || c == 'L' || c == 'l' || c == 'M' || c == 'm')
return true;
else
return false;
}// End of function

// Default constructor definition
Sphere::Sphere()
{
// Sets the default value
Radius = 1.0;
// Calls the function to generate random color
color = getColor();
}// End of default constructor

// Parameterized constructor to initialize parameter values to data member
Sphere::Sphere(double r, char c)
{
// Checks if radios parameter value is less than or equals to zero
if(r <= 0.0)
// Set the radios value to 1.0
Radius = 1.0;
// Otherwise assigns parameter radius to data member radius
else
Radius = r;

// Calls the function to check the color character is valid or not
if(validColor(c))
// If valid assigns parameter color to data member color
color = c;
// Otherwise calls the function to generate random color
else
color = getColor();
}// End of parameterized constructor

// Function to return radius
double Sphere::getRadius()
{
return Radius;
}// End of function

// Function to return color character
char Sphere::getColor()
{
return color;
}// End of function

// Function to set radius
void Sphere::setRadius(double r)
{
// Checks if radius is greater than 0.0 then set the parameter radius to data member radius
if(r > 0.0)
Radius = r;
}// End of function

// Function to set the color character
void Sphere::setColor(char c)
{
// Calls the function to validates the color character
// If valid then assigns the parameter color character to data member color
if(validColor(c))
color = c;
}// End of function

// Function to calculate and returns diameter
double Sphere::getDiameter()
{
return 2 * Radius;
}// End of function

// Function to calculate and return surface area
double Sphere::getSurfaceArea()
{
return (4 * PI * Radius * Radius);
}// End of function

// Function to calculate and return volume
double Sphere::getVolume()
{
return (4.0 / 3.0 * PI * Radius * Radius * Radius);
}// End of function

// Function to increase the radius by the parameter value
void Sphere::grow(double r)
{
Radius += r;
}// End of function

// Function to decrease the radius by the parameter value
void Sphere::shrink(double r)
{
// Checks if after deduction radios is greater than zero then make the deduction
if((Radius - r) > 0.0)
Radius -= r;
}// End of function

// Function to display the diameter, surface area and volume
void Sphere::printSummary()
{
// Displays radius
cout<<" Radius: "<<fixed<<setprecision(5)<<Radius;
// Checks the color character and displays the color
switch(color)
{
case 'B':
case 'b':
cout<<" Color: Blue";
break;
case 'R':
case 'r':
cout<<" Color: Red";
break;
case 'P':
case 'p':
cout<<" Color: Purple";
break;
case 'Y':
case 'y':
cout<<" Color: Yellow";
break;
case 'G':
case 'g':
cout<<" Color: Green";
break;
case 'L':
case 'l':
cout<<" Color: Black";
break;
case 'M':
case 'm':
cout<<" Color: Maroon";
break;
}// End of switch - case

// Displays diameter, surface area and volume
cout<<" Diameter: "<<fixed<<setprecision(5)<<getDiameter();
cout<<" Surface Area: "<<fixed<<setprecision(5)<<getSurfaceArea();
cout<<" Volume: "<<fixed<<setprecision(5)<<getVolume();
}// End of function

-----------------------------------------------------------------------------------

//File Name: SphereMain.cpp
#include "Sphere.cpp"
#include <iostream>
using namespace std;

// main function definition
int main()
{
// Creates an array for radius
double radius[] = {1.0, 2.5, 1.54, 5.0, 32.32};
// Creates an array for color
char color[] = {'p', 'p', 'l', 'y', 'm'};
// Declares an array of Sphere class pointer objects
Sphere *sp[5];

// Loops 5 times
for(int x = 0; x < 5; x++)
// Dynamically creates the object using parameterized constructor
sp[x] = new Sphere(radius[x], color[x]);

cout<" Print summaries";
// Loops 5 times
for(int x = 0; x < 5; x++)
{
cout<<" Summary Sphere "<<(x + 1);
// Calls the function to display the result
sp[x]->printSummary();
}// End of for loop

cout<<" --> Growing Spheres by 2.50000";
// Loops 5 times
for(int x = 0; x < 5; x++)
// Calls the function to increase the radius
sp[x]->grow(2.5);
cout<" Print summaries";
// Loops 5 times
for(int x = 0; x < 5; x++)
{
cout<<" Summary Sphere "<<(x + 1);
// Calls the function to display the result
sp[x]->printSummary();
}// End of for loop


cout<<" --> Shrinking Spheres by 2.00";
// Loops 5 times
for(int x = 0; x < 5; x++)
// Calls the function to reduce the radius
sp[x]->shrink(2.0);
cout<" Print summaries";
// Loops 5 times
for(int x = 0; x < 5; x++)
{
cout<<" Summary Sphere "<<(x + 1);
// Calls the function to display the result
sp[x]->printSummary();
}// End of for loop
}// End of main function

Sample Output:

Summary Sphere 1
Radius: 1.00000
Color: Purple
Diameter: 2.00000
Surface Area: 12.56636
Volume: 4.18879

Summary Sphere 2
Radius: 2.50000
Color: Purple
Diameter: 5.00000
Surface Area: 78.53975
Volume: 65.44979

Summary Sphere 3
Radius: 1.54000
Color: Black
Diameter: 3.08000
Surface Area: 29.80238
Volume: 15.29855

Summary Sphere 4
Radius: 5.00000
Color: Yellow
Diameter: 10.00000
Surface Area: 314.15900
Volume: 523.59833

Summary Sphere 5
Radius: 32.32000
Color: Maroon
Diameter: 64.64000
Surface Area: 13126.59849
Volume: 141417.22104
--> Growing Spheres by 2.50000

Summary Sphere 1
Radius: 3.50000
Color: Purple
Diameter: 7.00000
Surface Area: 153.93791
Volume: 179.59423

Summary Sphere 2
Radius: 5.00000
Color: Purple
Diameter: 10.00000
Surface Area: 314.15900
Volume: 523.59833

Summary Sphere 3
Radius: 4.04000
Color: Black
Diameter: 8.08000
Surface Area: 205.10310
Volume: 276.20551

Summary Sphere 4
Radius: 7.50000
Color: Yellow
Diameter: 15.00000
Surface Area: 706.85775
Volume: 1767.14437

Summary Sphere 5
Radius: 34.82000
Color: Maroon
Diameter: 69.64000
Surface Area: 15235.86201
Volume: 176837.57178
--> Shrinking Spheres by 2.00

Summary Sphere 1
Radius: 1.50000
Color: Purple
Diameter: 3.00000
Surface Area: 28.27431
Volume: 14.13715

Summary Sphere 2
Radius: 3.00000
Color: Purple
Diameter: 6.00000
Surface Area: 113.09724
Volume: 113.09724

Summary Sphere 3
Radius: 2.04000
Color: Black
Diameter: 4.08000
Surface Area: 52.29616
Volume: 35.56139

Summary Sphere 4
Radius: 5.50000
Color: Yellow
Diameter: 11.00000
Surface Area: 380.13239
Volume: 696.90938

Summary Sphere 5
Radius: 32.82000
Color: Maroon
Diameter: 65.64000
Surface Area: 13535.88483
Volume: 148082.58008

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