COMP 141 Fall 2019

Program 4: Geometry Volumes

Assigned:Friday, September 20
Due: Sunday, September 29, 2019 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 "sphere" and "Sphere"). You do not have to handle any other type of capitalization. If the user types in an invalid shape name (like "SpHeRe" 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? sphere
What is the radius? 5
The volume of the sphere is 523.5987755982989
Would you like to calculate another volume?(yes or no) yes

What shape do you want? cube
What is the side length? 4
The volume of the cube is 64.0
Would you like to calculate another volume?(yes or no) yes

What shape do you want? ice cream
What is the radius? 3
What is the height? 7
The volume of the ice cream cone is 122.52211349000193
Would you like to calculate another volume?(yes or no) yes

What shape do you want? Prism
What is the length? 3
What is the width? 4
What is the height? 5
The volume of the rectangular prism is 60.0
Would you like to calculate another volume?(yes or no) yes

What shape do you want? Triangle
I don't know that shape.
Would you like to calculate another volume?(yes or no) yes

What shape do you want? Cone
What is the radius? 6
What is the height? 8
The volume of the cone is 301.59289474462014
Would you like to calculate another volume?(yes or no) yes

What shape do you want? hexagon
I don't know that shape.
Would you like to calculate another volume?(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 volume of each shape they input.
  • Correctly use a while loop to allow the user to keep calculating shape volumes 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.