Create a program that has a class named Sphere that: Takes a numeric value (float or…
- User
Create a program that has a class named Sphere that:
Takes a numeric value (float or int) representing the radius when it is declared. Sphere should have at least three methods get_surface_area, which returns the surface area of the sphere get_volume, which returns the volume of the sphere set_radius, which changes the radius of the sphere. It should result in new values being returned from the other two methods. Feel free to set init or other methods as necessary to accomplish the task. To ensure you match the test cases exactly, use 3.14 as your approximation for π. The surface area of a sphere is 4πr^2; the volume of a sphere is 4/3πr^3. The program will accept multiple command line arguments. When the program runs, it calculates the surface area and volume for the provided numeric value (radius). Then the program adds 1 to the numeric value (radius), sets the new radius, and computes the surface area and volume for the new radius value. The program prints out the radius, surface area, and volume for the initial radius and the new radius.
- Assistant
import math import sys
class Sphere: def init(self, radius): self.radius = radius
def get_surface_area(self): return 4 * math.pi * self.radius ** 2 def get_volume(self): return 4/3 * math.pi * self.radius ** 3 def set_radius(self, new_radius): self.radius = new_radiusif len(sys.argv) > 1: radius = float(sys.argv[1]) sphere = Sphere(radius)
initial_surface_area = sphere.get_surface_area() initial_volume = sphere.get_volume() print("Initial Radius: {:.2f}".format(radius)) print("Initial Surface Area: {:.2f}".format(initial_surface_area)) print("Initial Volume: {:.2f}".format(initial_volume)) new_radius = radius + 1 sphere.set_radius(new_radius) new_surface_area = sphere.get_surface_area() new_volume = sphere.get_volume() print("New Radius: {:.2f}".format(new_radius)) print("New Surface Area: {:.2f}".format(new_surface_area)) print("New Volume: {:.2f}".format(new_volume))else: print("Please provide a radius for the sphere.")