Create a module named area having four functions named triangle, square, rectangle,and circle. All these functions takes arguments according to their shape; compute andreturns the area of the shape. Import the module in the main program and call therespective function depending on the user requirement and compute the area ofrespective shape
Question
Create a module named area having four functions named triangle, square, rectangle,and circle. All these functions takes arguments according to their shape; compute andreturns the area of the shape. Import the module in the main program and call therespective function depending on the user requirement and compute the area ofrespective shape
Solution
To create the module named "area" with four functions, follow these steps:
- Create a new file and save it as "area.py".
- Open the "area.py" file and define the four functions: triangle, square, rectangle, and circle.
- The triangle function should take two arguments: base and height. Inside the function, compute the area of the triangle using the formula: area = (base * height) / 2. Return the computed area.
- The square function should take one argument: side length. Inside the function, compute the area of the square using the formula: area = side length * side length. Return the computed area.
- The rectangle function should take two arguments: length and width. Inside the function, compute the area of the rectangle using the formula: area = length * width. Return the computed area.
- The circle function should take one argument: radius. Inside the function, compute the area of the circle using the formula: area = 3.14159 * radius * radius. Return the computed area.
- Save the "area.py" file.
To import the module in the main program and call the respective function depending on the user requirement, follow these steps:
- Create a new file and save it as "main.py".
- Open the "main.py" file and import the "area" module using the import statement: import area.
- Prompt the user to enter the shape for which they want to compute the area.
- Based on the user's input, call the respective function from the "area" module and pass the required arguments.
- Store the returned area value in a variable.
- Print the computed area to the console.
- Save the "main.py" file.
Now, when you run the "main.py" file, it will prompt the user to enter the shape and compute the area accordingly using the functions defined in the "area" module.
Similar Questions
Write a program to calculate the area of circle, triangle, rectangle and square using function overloading.
Create an abstract class named shape that contains two integers and an empty method named printarea().Provide three classes named rectangle, triangle and circle such that each one of the classes extends the class Shape. Each of the inherited class from shape class should provide the implementation for the method printarea(). Get the input and calculate the area of rectangle, circle and triangle.In the Main class, create the objects for the three inherited classes and invoke the methods and display the area values of the different shapes.2(length of rectangle)3(breadth)5(triangle breadth)6(Triangle height)4(circle radius)
Create an abstract class SHAPE. Define member functions get_data() and display_area(). Derive five sub classes namely square, triangle, rectangle, circle and ellipse. Compute the area of rectangle, square, triangle, circle and ellipse. Display the result using display_area().Override the display_area() in all the sub classes. Use suitable mechanism to avoid the duplication and link the function display_area() at run time.Note: Enter the option (1- Rectangle 2-Square 3-Triangle 4-Circle 5-Ellipse) and Side values for options 1,2 and 3, Radius for option 4, x-radius and y-radius for option 5. Input form:153Output form:15
Consider the base class "Shape" with methods draw() [prints message "Draw"]. Create three subclasses: Circle, Square, and Triangle. Override the draw() method in each subclass to draw the respective shape, and override the calculateArea() method to calculate and return the area of each shape. Implement the above scenario with suitable pseudocode and appropiate java application.User Choice may be "C/S/T". a.) C (Circle), then must read "Radius"b.) S (Square), then must read "Side"c.) T (Triange), then must read both "Height and Base".Identify the suitable data members for the above derived classes and ensure then all are private.
Write a function called triangle that takes in two parameters: b and h that represents the base and height respectively. The function calculates and returns the area of the triangle. You may assume that the arguments passed into the function are valid base and height values.
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.