COMP 141 Project 4: Geometry
You will write a program that allows the user to compute measurements of various
geometric shapes using a menu-driven interface.
Changes to help you with longer programs
Now that we are beginning to write longer programs, there are two issues that
will be important for the remainder of this class (and for your entire computer science
career) at least when programming.
The first is to be sure to use
functions appropriately, as they divide your program into separate pieces that can
be tested individually. When first thinking about a program, try to envision it as
a set of components that all fit together like a puzzle, where each component handles
a separate, distinct task. Consider making separate tasks into separate functions.
Now this idea can be taken to the extreme, which is not helpful, as using too many functions,
especially if these functions all need access to the same variables, ends up being
very messy because you have to write functions that take, for instance, ten arguments.
Try to find a balance between too many functions and too few.
The second important issue is commenting your code. It is important to get into good
commenting habits, and so I'm asking you use the new guidelines below for commenting.
Guidelines for comments
These guidelines apply to this program and all future programs.
- You should place a comment section at the top of your program that contains:
- Your name, the date, and the class.
- The honor pledge saying you have neither given nor received any help you weren't
supposed to.
- A description of the program (a few sentences).
- A description of the input to the program (what the user types), if any.
- A description of the output of the program (what the user sees).
Here's an example for the darts program:
# Name: Phil Kirlin
# Date: 9/18/2013
# Class: CS141
# Pledge: I have neither given nor received unauthorized aid on this program.
# Description: This program computes where a dart lands on a dartboard and awards
# the user a certain number of points. Red areas are worth 5 points, yellow is
# 10 points, and green is 15 points.
# Input: x and y coordinates of the dart's location.
# Output: How many points the user wins for their dart.
- For every function you write (except main), you must include a comment immediately before the
function definition line that contains:
- A description of what the function does.
- Any parameters the function takes (if any), along with their meanings.
- The return value of the function, if there is one, and what it means.
Here's an example you'd use for a function that computes the circumference of a circle:
# This function computes the circumference of a circle.
# Parameters: r, the radius of the circle.
# Returns: the circumference of the circle.
def circumference_of_circle(r):
return 2 * 3.14 * r
About the pledge
As we being to write more complicated programs, it is not uncommon to get stuck.
However, as the syllabus states, all homework assignments you complete outside of class must be
entirely your own work. You should not discuss the programs with other students,
look at anyone else's code, nor share your code with anyone else. The only exceptions,
of course, are the official course tutors and the instructor.
I'm asking everyone to please include the honor pledge in the comments at the top
of your programs indicating you have conformed to the request to work individually.
(See the example above for the text to include.)
Note that I'm not saying you can't talk to any other students about anything, just
don't discuss the homework. Feel free to talk to each other with
notes, examples from class, and general concepts.
OK, enough administration stuff. On to the actual program!
What you need to do
First, write five functions that compute and return the following:
- Area of a circle. This function takes one parameter, the radius.
- Area of a rectangle. This function takes two parameters, the length and width.
- Area of a square. This function takes one parameter, the length of a side.
However, because a square is just a special type of rectangle (where the sides
have the same length), have this function call your area of a rectangle function
to compute 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.
This idea, while probably taken a little too far in this case, is normally good programming practice:
We are using an already-solved problem to solve a new problem. Here, a square
is just a special case of a rectangle, so use the solution for a rectangle we've
already written rather than writing a completely new solution.
- Area of a triangle. This function takes two parameters, the base and height.
- The surface area of a rectangular prism. This function takes three parameters,
the length, width, and height.
Note that the surface area of this shape
can be computed as the sum of the areas of the six faces of the prism. Because each
face is a rectangle, call your area of a rectangle function to write this function.
You will need to call it three times.
Again, I know I'm taking the idea of reusing functions to the extreme, but I want
you to practice calling functions and capturing return values.
If you write these functions correctly, none of them should contain input statements
or print statements. Each one interacts with outside functions only through parameters
and return values.
Second, write your main() function to do this:
- The user is first asked what kind of
shape they want to calculate the area (or surface area) of.
- The user will type in the name of the shape
as a string (e.g., circle, rectangle, square, triangle, or prism).
- 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 a side). If the user types in an invalid shape name, then
print an appropriate error message.
- Display the area of the shape, followed
by a message asking the user if they want to calculate the area of another shape. If they
answer yes, then run the program again. You should use a while loop for this part, similar
to those in section 5.2 (see page 160).
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.
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.
Sample interaction
Note that this is only a sample. Your program should work with any order of shapes the
user wants.
What shape do you want? triangle
What is the base? 3
What is the height? 4
The area of the triangle is 6.0
Do you want to calculate another area? yes
What shape do you want? square
What is the side length? 7
The area of the square is 49
Do you want to calculate another area? yes
What shape do you want? rectangle
What is the length? 8
What is the width? 5
The area of the rectangle is 40
Do you want to calculate another area? yes
What shape do you want? prism
What is the length? 6
What is the width? 5
What is the height? 4
The surface area of the prism is 148
Do you want to calculate another area? yes
What shape do you want? Hexagon
I don't know that shape.
Do you want to calculate another area? yes
What shape do you want? square
What is the side length? 5
The area of the square is 25
Do you want to calculate another area? yes
What shape do you want? Triangle
What is the base? 9
What is the height? 8
The area of the triangle is 36.0
Do you want to calculate another area? yes
What shape do you want? Circle
What is the radius? 6
The area of the circle is 113.03999999999999
Do you want to calculate another area? no
What to turn in
Through Moodle, turn in your code as a file called geometry_yourLastName_yourFirstName.py.
Hints
- Write your five geometric functions first. Test them using the Python shell before you write the main function.
- Write your main function first without a while loop (so it will only run once). Make sure it works
fine for a single shape, then put the while loop in.
Challenge Problems
Do not work on these until you have completely solved the basic assignment.
- Add a menu to the program so the user can type in a number for their shape rather
than the word. For example, you could print a menu showing "(1) Circle (2) Rectangle...
(6) Quit." Change the while loop so the user can use the menu to quit, rather than
having to ask the question about whether they want to calculate another shape.
- 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.
- 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.