COMP 141 Project 3: Geometry
You will write
a program that allows the user to compute the areas of various geometric shapes, including
circles, rectangles, squares, and triangles. This will be done using functions and
if-else statements.
What you need to do
First, write four functions that compute the area of a circle, a rectangle, a square,
and a triangle, respectively. All of these functions will return the
areas of their shapes back to the place where they were called. None of the functions
will directly print the areas; all of the printing will be handled by the main function.
- The area of a circle function should take a single argument, the radius.
- The area of a rectangle function should take two arguments, the width and height.
- The area of a square function should take one argument, the length of a side of the
square. However, because a square is just a special type of rectangle (where the sides
have the same length), the area of a square function should call your area of a rectangle function to get the answer. In other
words, do not directly compute the area of a square inside this function, instead, call your area of a rectangle function with appropriate arguments and return the value that
comes back.
- The area of a triangle function should take two arguments, the length of the base
and the height of the triangle.
Second, write your main() function so that the user is first asked what kind of
shape they want to calculate the area of. The user will type in the name of the shape
as a string (e.g., circle, rectangle, square, or triangle). Use if-elif-else statements
to figure out what shape they wanted, then ask the user to type in the appropriate
attributes of the shape (e.g., for circle you would ask for the area, for square you
would ask for the length of side). Then display the area of the shape and end the program.
You may assume the user will type in the name of the shape either in all lowercase,
or with a capital first letter (i.e., your program should work correctly for both
"triangle" and "Triangle"). You do not have to handle any other type of capitalization.
If the user types in an invalid shape name (like "TrIaNgLe" or "trapezoid"), your program
should print an appropriate error message before ending.
You do not need to use any loops in this program.
Testing your program
You should test your program thoroughly to make sure all of your shape functions work.
You may assume the user will never give "bad" input --- the user will always type in
integers for the numbers, and never negative numbers or zero.
What to turn in
Through Moodle, turn in your code as a file called geometry_yourLastName_yourFirstName.py.
Challenge Problems
From time to time, I will offer "challenge problems" on assignments. These problems
are designed to have little (but some) impact on your grade whether you do them or not.
You should think of these problems as opportunities to work on something interesting and optional,
rather than a way to raise your grade through "extra credit."
Policy on challenge problems:
-
Challenge problems will typically allow you to get 2-5% of additional credit on an assignment, yet they
will typically be much more difficult than this credit amount suggests.
-
You should not attempt a challenge problem until you have finished the rest of an assignment.
- The tutors will not provide help on challenge problems. The instructor
will provide minimal assistance only, as these problems are optional and are designed
to encourage independent thought.
- Challenge problems may be less carefully specified and less carefully calibrated for how difficult or time-consuming they are.
Challenge problems for this assignment:
- Enhance your program using the graphics library. Make it so the user will not
specify the shapes in terms of lengths or widths or radii, but rather by the (x, y)
endpoints of the shapes. Once the user picks a shape and endpoints, the shape
will be drawn on the screen, as well as the area calculated. You'll need to add
additional functions to calculate the area of a shape based on endpoints, rather than
side lengths.
- Make it so the endpoints of the shapes can be specified by clicking the mouse
on the canvas, rather than typing them in at the keyboard.