Question

In: Computer Science

For this assignment, you will write a program that consists of four function definitions, including one...

For this assignment, you will write a program that consists of four function definitions, including one main() function. The main function gets inputs from the user, and the other functions show output to the user. When the program is executed, it asks the user to enter a small amount of information, and then shows output based on the user's info.

Specification:

Here are some specific requirements for this program:

  • The program should consist of four function definitions, written one after the other (not one inside the body of another), followed by a call to the main() function.
    • The main() function gets inputs from the user and calls the other functions to show output. It should have no parameters.
    • The show_circle_info() function has ONE parameter, the radius of a circle in cm. It prints one sentence with the following form:
      A circle with a radius of __ cm has a diameter of __ cm, 
      a circumference of __ cm and an area of __ cm^2.
    • The show_sphere_info() function has TWO parameters, the radius of a solid sphere in cm and the density of the sphere in g/cm3. It prints one sentence with the following form:
      A solid sphere with a radius of __ cm and a density of __ g/cm^3 
      has a surface area of __ cm^2, a volume of __ cm^3 and a mass of __ g.
    • The show_cylinder_info() function has THREE parameters, the radius of a solid cylinder in cm, the length of the cylinder in cm and the density of the cylinder in g/cm3. It prints one sentence with the following form:
      A solid cylinder with a radius of __ cm, a length of __ cm 
      and a density of __ g/cm^3 has a surface area of __ cm^2, 
      a volume of __ cm^3 and a mass of __ g.
  • Almost no code should appear outside of the function definitions except for the call to main(). In fact, there are just two things in this category: the density of gold and the value of pi.
    • Use a "named constant" for the density of gold. The value is 19.3 g/cm3.
    • For the value of pi, you have two choices: 1) you may use a "named constant" to assign 3.14159 for the value of pi; 2) if you prefer, you may use the "math" module to get the value of pi, in which case there needs to be an appropriate "import" statement at the beginning of your program. Either of these approaches is fine for this assignment (the "math" module is preferred in general, but we have not explicitly covered that yet).
    • No global variables other than the named constants described here may be used.
  • Only the main() function may call input(). The other functions get the information they need from parameters (except for the two pieces of "constant" information described above).
  • Here is an example of a complete program interaction showing the output that should be produced by the main() function (underlining is used to show values entered by the user, but they do not actually appear underlined in the shell window).
    >>> 
    Enter the desired radius (in cm): 2.1
    Enter the desired length (in cm): 3.75
    Enter the desired density (in g/cm^3): 5
    
    
    Here are results for your specified characteristics:
    
    A circle with a radius of 2.10 cm has a diameter of 4.20 cm, 
    a circumference of 13.19 cm and an area of 13.9 cm^2.
    
    A solid sphere with a radius of 2.10 cm and a density of 5.0 g/cm^3 
    has a surface area of 55.4 cm^2, a volume of 38.8 cm^3 and a mass of 194 g.
    
    A solid cylinder with a radius of 2.10 cm, a length of 3.75 cm 
    and a density of 5.0 g/cm^3 has a surface area of 77.2 cm^2, 
    a volume of 52.0 cm^3 and a mass of 260 g.
    
    
    Here are results when the radius is doubled:
    
    A circle with a radius of 4.20 cm has a diameter of 8.40 cm, 
    a circumference of 26.39 cm and an area of 55.4 cm^2.
    
    A solid sphere with a radius of 4.20 cm and a density of 5.0 g/cm^3 
    has a surface area of 221.7 cm^2, a volume of 310.3 cm^3 and a mass of 1552 g.
    
    A solid cylinder with a radius of 4.20 cm, a length of 3.75 cm 
    and a density of 5.0 g/cm^3 has a surface area of 209.8 cm^2, 
    a volume of 207.8 cm^3 and a mass of 1039 g.
    
    
    If the sphere and cylinder you specified were 
    made of solid gold, these would be the results:
    
    A solid sphere with a radius of 2.10 cm and a density of 19.3 g/cm^3 
    has a surface area of 55.4 cm^2, a volume of 38.8 cm^3 and a mass of 749 g.
    
    A solid cylinder with a radius of 2.10 cm, a length of 3.75 cm 
    and a density of 19.3 g/cm^3 has a surface area of 77.2 cm^2, 
    a volume of 52.0 cm^3 and a mass of 1003 g.
  • Notice the following in the sample above:
    • Each of the "show" functions is called more than one time with different parameter values.
    • All numbers in the output are formatted is specific ways. All linear dimensions (radius, diameter, length, and circumference) are formatted with two digits after the decimal point. Areas, volumes and density values are formatted with one digit after the decimal point. All mass values are formatted with zero digits after the decimal point. All this formatting should be done in the "output" section of each function.
    • Line wrap and the use of blank lines are deliberate; try to make your output match the sample as closely as possible.

TIPS:

  • Here is a link to a document (Links to an external site.) that includes surface area and volume formulas for the sphere and cylinder.
  • The mass of any uniform solid shape is equal to the volume of the shape times its density.
  • Start by developing a program that shows results only for circles. Once you have that working, add code to handle spheres and get that working. Then finish up by adding code for cylinders.
  • The rectangle.py sample program (linked from the Week 3 sample programs page) is a good example to review before starting this project.

Documentation and Style:

  • Include a main comment block at the beginning of the file listing that has your name, date, class and section number, and a brief description (one or two lines) of the project.
  • Include a comment block before the function header to describe a) what the function does and b) how the function's parameters are used. Follow the style you see in the posted examples, and in particular the rectangle.py sample program (Links to an external site.), which demonstrates the difference between the comment block at the very beginning of a code file and the comment block located before a function header.
  • Include blank lines where appropriate to make the program easier to read.
  • Use descriptive names for all variable, including function parameters.
  • Include at least 2 documented test cases -- comments at the end of your program that describe at least 2 tests you performed (with all different input values), and the results you saw, and why you believe the results are correct.

Program Submission: Upload a single .py file using the tool on this page.

Solutions

Expert Solution

// python

// if you have any doubt let me know i will try to help you. Thank you.

import math
def show_circle_info(radius):
  diameter=2*radius
  pi=math.pi
  circum=2*pi*radius
  area=pi*radius*radius
  print('A circle with a radius of ',round(radius,2),' cm has a diameter of ',round(diameter,2),' cm,')
  print('a circumference of ',round(circum,2),' cm and an area of ',round(area,1),'cm^2.')
  print()


def show_sphere_info(radius,density):
  surface_area=4*math.pi*radius*radius
  volume=(4/3)*math.pi*math.pow(radius,3)
  mass=density*volume
  print('A solid sphere with a radius of ',round(radius,2),' cm and a density of ',round(density,2),' g/cm^3')

  print('has a surface area of ',round(surface_area,1),' cm^2, a volume of ',round(volume,1),' cm^3 and a mass of ',math.ceil(mass),'g.')
  print()

def show_cylinder_info(radius,length,density):
  pi=math.pi
  surface_area=(2*pi*radius*length) +(2*pi*radius*radius)
  volume=pi*radius*radius*length
  mass=density*volume

  print('A solid cylinder with a radius of ',round(radius,2),' cm, a length of ',round(length,2),'cm and a density of ',round(density,2),' g/cm^3 ')
  
  print('has a surface area of ',round(surface_area,1),' cm^2, a volume of ',round(volume,1),' cm^3 and a mass of ',math.ceil(mass),'g.')
  print()



def main():
  radius=float(input("Enter the desired radius (in cm): "))
  length=float(input("Enter the desired length (in cm): "))
  density=float(input("Enter the desired density (in g/cm^3): "))
  
  show_circle_info(radius)
  show_sphere_info(radius,density)
  show_cylinder_info(radius,length,density)

if __name__=="__main__":
  main()


// Sample output:-


Related Solutions

Write a complete C++ program that at least consists of the main() function and at least...
Write a complete C++ program that at least consists of the main() function and at least two recursive functions. The first function has no return value and can be named printPrime(). It prints first n prime numbers with proper prompt. Note that number 1 is not regarded as a prime number. We assume the first prime number is 2. The printout should start from 2. The prototype of the recursive function should be void printPrime(int n); The algorithm of printPrime()...
C++ for Mac (Xcode) For this exercise, you will write a program that includes four function...
C++ for Mac (Xcode) For this exercise, you will write a program that includes four function definitions. You will also write a main() function that calls these four functions to demonstrate that they work as expected. The four functions are: 1. printExitMessage() : This function prints a message to the screen saying something like "Thanks for using this software. Goodbye." 2. getMin(): This function takes two float inputs and returns the lesser of the two float values. For example, if...
Ice Cream Program Assignment Write a program that uses a function to ask the user to...
Ice Cream Program Assignment Write a program that uses a function to ask the user to choose an ice cream flavor from a menu (see output below.) You must validate the users input for the flavor of ice cream accounting for both upper and lower-case letters. You must give them an appropriate error message and allow them to try again.   Once you have a valid flavor, your function will return the flavor back to the main() function.    Your main()...
Write a C++ program which consists of several functions besides the main() function. The main() function,...
Write a C++ program which consists of several functions besides the main() function. The main() function, which shall ask for input from the user (ProcessCommand() does this) to compute the following: SumProductDifference and Power. There should be a well designed user interface. A void function called SumProductDifference(int, int, int&, int&, int&), that computes the sum, product, and difference of it two input arguments, and passes the sum, product, and difference by-reference. A value-returning function called Power(int a, int b) that...
Write the following function and provide a program to test it (main and function in one...
Write the following function and provide a program to test it (main and function in one .py) def repeat(string, n, delim) that returns the string string repeated n times, separated by the string delim. For example repeat(“ho”, 3, “,”) returns “ho, ho, ho”. keep it simple.Python
Loop Introduction Assignment Please write a program in c# Using the conditions below, write one program...
Loop Introduction Assignment Please write a program in c# Using the conditions below, write one program that calculates a person’s BMI. Your main() function will call functions 1, 2, and 3. Your program will contain three functions: Function #1: Will ask the user for their weight in pounds and their height in inches.   Your function will convert the weight and height into Body Mass Index (BMI). The formula for converting weight into BMI is as follows: BMI = Weight *...
Write a program in c++, with at least four functions, including main, which must do the...
Write a program in c++, with at least four functions, including main, which must do the following: Ask user whether they want to encode or decode a message – if no, then terminate Take the input string from the user, store it in dynamic memory (use new) As appropriate, encode or decode the message using Rot13. Output the encoded/decoded message Delete the input string from dynamic memory (use delete) Input will be a string of no more than 25 characters....
Assignment Requirements Write a python application that consists of two .py files. One file is a...
Assignment Requirements Write a python application that consists of two .py files. One file is a module that contains functions used by the main program. NOTE: Please name your module file: asgn4_module.py The first function that is defined in the module is named is_field_blank and it receives a string and checks to see whether or not it is blank. If so, it returns True, if not it return false. The second function that is defined in the module is named...
For this week’s lab assignment, you will write a program called lab9.c. You will write a...
For this week’s lab assignment, you will write a program called lab9.c. You will write a program so that it contains two functions, one for each conversion. The program will work the same way and will produce the same exact output. The two prototypes should be the following: int btod(int size, char inputBin[size]); int dtob(int inputDec); The algorithm for the main() function should be the following: 1. Declare needed variables 2. Prompt user to enter a binary number 3. Use...
You have to write a program that computes the area of a triangle. Input consists of...
You have to write a program that computes the area of a triangle. Input consists of the three points that represent the vertices of the triangle. Points are represented as Cartesian units i.e. X,Y coordinates as in (3,5). Output must be the three points, the three distances between vertices, and the area of the triangle formed by these points. The program must read the coordinates of each point, compute the distances between each pair of them and print these values....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT