CSU Banner

CS 150, Fall 2017

Programming Assignment - P10

Geometry Methods

Due - November 28th, 2017 at 6:00pm

Late - November 29th, 2017 at 8:00am

Objectives of this Assignment

  1. Use methods to calculate five different geometric dimensions,
  2. understand the purpose behind different method signatures, and
  3. review printf() and file output.


Create a Java program called P10 with a class named P10. Please complete the code required from this week's lab first if you have not already done so. All of the Java code you write should be contained in the P10 class. To complete the assignment, fill in the required code for the remaining three methods, as well as add more code to main.

    Part One
  1. Start by writing the code necessary in calcCircleArea().
  2. The incoming variable r represents the radius of the circle. Calculate the area and return the result.
  3. This completes the code for calcCircleArea(); however you will need more code in main before you can test this method.
  4. Next, finish the calcPrismVolume() method. Start by creating a local Scanner object to read from the keyboard.
  5. Declare five variables of type double to represent the following: trapezoid base one, trapezoid base two, trapezoid height, prism length and prism volume. Initialize all to zero.
  6. Now you should print out the following prompt:
     Base One Length? 
    Make sure a space follows the question mark.
  7. Using your Scanner, assign the appropriate local variable.
  8. Using the pattern in steps 6 and 7 print three more prompts, each followed by an assignment statement.
     Base Two Length?
     Prism Length? 
  9. Next, calculate the prism volume using the equation: volume = (baseOne + baseTwo)/2.0 * height * length
  10. Close the local Scanner if you wish, then return the volume.

  11. Part Two
  12. Now move to calcCylinderSurfaceArea(). Notice this method has an incoming array and does not return anything. What is different about arrays in this situation?
  13. Declare three variables of type double to represent the following: area of cylinder base, circumference, and surface area. Initialize all to zero.
  14. Calculate the area of the cylinder base using the equation for the area of a circle or by calling calcCircleArea().
  15. The value of the radius you need is the first element of the specs array.
  16. Next, calculate the circumference of the cylinder, which is just the circumference of a circle in this situation. Again use the first element of the specs array as the radius.
  17. Calculate the surface area of the cylinder with the equation: (2 * baseArea) + (circumference * length)
  18. The cylinder length is the second element in the specs array.
  19. Assign the result of the calculation to the surface area variable.
  20. Then, assign the surface area to the third element of the specs array.
  21. This will complete the calcPrismVolume() method.
  22. Also, notice how many lines were saved from main by keeping all of the user input code in this method.

  23. Part Three
  24. The last part of this assignment requires you to write several more lines of code main.
  25. Following the code where your PrintWriter wrote the triangle area result to the file, start by assigning radius the value stored in args[1]. Remember, program arguments are Strings, so you will need to parse the value in order to assign a number to radius. Use the following code to do so: Double.parseDouble(args[1])
  26. Next, return to P10's program arguments and add 5.0. If you have implemented the code in calcCircleArea() correctly, once you run your program you should see the associated output value below.
  27. After this, call calcCircleArea() and assign the result of the call to circleArea.
  28. Then, print the area of the circle to the output file with three numbers following the decimal point after the following prompt:
     Circle Area: 
    Make sure there is a space after the colon and include a new line.
  29. Next, call calcPrismVolume() and assign the call to prismVolume.
  30. Print the volume of the prism to the output file with three numbers following the decimal point after the prompt:
     Prism Volume: 
    Make sure there is a space after the colon and include a new line.
  31. Next, call calcCylinderSurfaceArea(). Note the result of the method calculation will be in element three of the cylinderSpecs array.
  32. Print the surface area of the cylinder to the output file with five numbers following the decimal point after the prompt:
     Cylinder Surface Area: 
    Make sure there is a space after the colon and include a new line.
  33. You may test your program using the input values shown below but try other values as well and compare to hand-calculated results.

Console Contents

Base One Length? 2.2
Base Two Length? 3.3
Height? 4.4
Prism Length? 5.5

Output File Contents

Rectangle Area: 10
Triangle Area: 14
Circle Area: 78.540
Prism Volume: 66.550
Cylinder Surface Area: 202.39774


Grading Criteria

Submit P10.java to Checkin.

CS Banner
CS Building