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()...
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 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...
In this assignment you will write a program that encrypts a text file.  You will use the...
In this assignment you will write a program that encrypts a text file.  You will use the following encryption scheme. Ask the user for the name of the original file. Ask the user for the name of the output file. Ask the user for the encryption key, n. Read n2 characters from the file into the n rows and n columns of a 2-dimensional array. Transpose the array.  (Exchange the rows and columns.) Write the characters from the array to an output...
For Python: In this assignment you are asked to write a Python program to determine the...
For Python: In this assignment you are asked to write a Python program to determine the Academic Standing of a studentbased on their CGPA. The program should do the following: Prompt the user to enter his name. Prompt the user to enter his major. Prompt the user to enter grades for 3 subjects (A, B, C, D, F). Calculate the CGPA of the student. To calculate CGPA use the formula: CGPA = (quality points * credit hours) / credit hours...
In C++ For this assignment, you will write a program to count the number of times...
In C++ For this assignment, you will write a program to count the number of times the words in an input text file occur. The WordCount Structure Define a C++ struct called WordCount that contains the following data members: An array of 31 characters named word An integer named count Functions Write the following functions: int main(int argc, char* argv[]) This function should declare an array of 200 WordCount objects and an integer numWords to track the number of array...
For this computer assignment, you are to write a C++ program to implement a class for...
For this computer assignment, you are to write a C++ program to implement a class for binary trees. To deal with variety of data types, implement this class as a template. Most of the public member functions of the BinaryTree class call private member functions of the class (with the same name). These private member functions can be implemented as either recursive or non-recursive, but clearly, recursive versions of these functions are preferable because of their short and simple implementations...
For this computer assignment, you are to write a C++ program to implement a class for...
For this computer assignment, you are to write a C++ program to implement a class for binary trees. To deal with variety of data types, implement this class as a template. Most of the public member functions of the BinaryTree class call private member functions of the class (with the same name). These private member functions can be implemented as either recursive or non-recursive, but clearly, recursive versions of these functions are preferable because of their short and simple implementations...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT