COMP 141 Fall 2016

Program 4: Geometry

Assigned:Friday, September 23
Due: Sunday, October 2, 2016 by 11:55pm

For this assignment, you will write a program that allows the user to compute measurements of various geometric shapes using a menu-driven interface. This assignment is meant to test your ability to define and call functions properly, and it will also incorporate conditional statements (if-elif-else) as well as a simple while loop.

About functions

Now that we are beginning to write longer programs, we will practice dividing our programs into functions. This is a critical habit to develop, because building a program out of functions that work together enables faster software development, because functions can be tested individually, then combined together into larger functions.

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.

What you need to do

First, write five functions that compute and return the following:

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:

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 or no) 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 or no) 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 or no) 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 or no) yes

What shape do you want? Hexagon
I don't know that shape.
Do you want to calculate another area? (yes or no) 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 or no) 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 or no) 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? (yes or no) no

Comments in your code

Because I'm requiring you to write functions in this assignment, I'm adding additional mandatory comments. It is good practice to always describe your functions in terms of what they do, the input they take in and the output they produce.

  • For every function you write (except main), you must include a comment immediately before the function definition line that contains:

    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 * math.pi * r
    	

    What to Do
  • Create a Python program named yourlastname_yourfirstname_prg4.py
  • Include the standard program header at the top of your Python file.
  • Follow the comment guide to correctly comment your code.
  • Submit your Python file on Moodle under Program 4.
  • Requirements

    A good program will do all of the following:

  • Program file is named correctly.
  • Include the standard program header as a comment at the top of your program.
  • Include six functions in total (main and five others).
  • Include all user prompts for information in the main function.
  • Output to the user the area of each shape they input.
  • Correctly use a while loop to allow the user to keep calculating shape areas for as long as they like.
  • Code is commented appropriately including function comments, is neatly and clearly formatted, and it includes proper use of 'white space'.
  • Hints

    Challenge Problems

    Do not work on these until you have completely solved the basic assignment.

    Grading

    Your program will be graded on correctness, as well as on coding style, which refers to choices you make when writing your code, such as good use of variable names, appropriate indentation, and comments (this is not an exhaustive list). See the syllabus for further grading details.

    You will receive one bonus point for every complete day your program is turned in early, up to a maximum of five points. For instance, if your program is due on September 20 at 11:59pm, if you turn in your code on Moodle any time on September 19 from 12:00am through 11:59pm, you will receive one extra point on your project. Programs submitted on September 18 from 12:00am through 11:59pm will receive two points. This pattern continues for up to five points.