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.

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 Spheres 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 spheres 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 spheres 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

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!