1. Create a program that ask for a radius of a circle and has the ability to return both the Area and Circumference – so methods call getArea() and getCircumferance() 2.Create a program that will ask for both gallon used and mileage travelled; calculate the MPG.
Program 1: Calculation of Area and Circumference of a Circle
In order to create a program that calculates the area and circumference of a circle based on a given radius, we need to define two methods: getArea() and getCircumference(). These methods will take the radius as an input and return the respective values.
Firstly, let’s define the mathematical formulas for area and circumference:
1. The formula for calculating the area of a circle is given by:
Area = π * radius^2
2. The formula for calculating the circumference of a circle is given by:
Circumference = 2 * π * radius
Using these formulas, we can define our program as follows:
def __init__(self, radius):
self.radius = radius
return math.pi * self.radius**2
return 2 * math.pi * self.radius
# Prompt the user for the radius of the circle
radius = float(input(“Enter the radius of the circle: “))
# Create an instance of the Circle class
circle = Circle(radius)
# Print the area and circumference of the circle
print(“Area of the circle:”, circle.getArea())
print(“Circumference of the circle:”, circle.getCircumference())
In this program, we define a Circle class with a constructor that takes the radius as an argument. We then define the getArea() and getCircumference() methods, which use the radius to calculate the area and circumference using the formulas mentioned earlier.
After prompting the user for the radius, we create an instance of the Circle class with the given radius. Finally, we call the getArea() and getCircumference() methods on the circle object to obtain the calculated values and print them to the console.
Program 2: Calculation of Mileage Per Gallon (MPG)
To create a program that calculates the mileage per gallon (MPG), we need to ask the user for the number of gallons used and the mileage traveled. The MPG can then be calculated by dividing the mileage traveled by the gallons used.
Here is an example program that performs these calculations:
# Prompt the user for the number of gallons used and the mileage traveled
gallons_used = float(input(“Enter the number of gallons used: “))
mileage_traveled = float(input(“Enter the mileage traveled: “))
# Calculate the MPG
mpg = mileage_traveled / gallons_used
# Print the MPG to the console
print(“Miles per gallon (MPG):”, mpg)
This program simply prompts the user to enter the number of gallons used and the mileage traveled. It then calculates the MPG by dividing the mileage traveled by the gallons used and finally prints the result to the console.
The post 1. Create a program that ask for a radius of a circle and ha… appeared first on My Perfect Tutors.