CSU Banner

CS 150, Fall 2017

Lab 12 - Introduction to Methods With Geometry Equations and the Start of P10

Monday - November 13th, 2017


Objectives of this Lab

  1. Review the structure and implementation of methods, then
  2. write code both in the main method and supporting methods, in order
  3. to calculate the area of a rectangle and a triangle, and
  4. write the results to an output file, which
  5. will complete the first two methods of P10.

Getting Started

Create a new Java Project named P10, and make a class named P10. Once complete, P10 will have five methods that interact with code in main to calculate results for five different common geometry equations. These equations include calculating the following: area of a rectangle, area of a triangle, area of a circle, volume of a trapezoidal prism, and the surface area of a right circular cylinder. In today's lab, after your TA reviews the structure of the method signature and program flow with methods, you will be assisted over the course of the lab with the rectangle area and triangle area methods. Additionally, your results will need to be printed to an output file called Geometry.txt.

Today's Assignment

    Part One
  1. Create a new Java Project in Eclipse called P10. Make a class also called P10 and check the box to have Eclipse add the main method for you.
  2. It is recommended you read through the rest of the instructions first before beginning to write any code. Once you understand what you are expected to do in this assignment, you can better organize your thoughts and make a more efficient plan of action.
  3. Copy the following code and paste it into your main method:
     
    try {
        // Declare and initialize program variables.
        int length = 5, width = 2, rectangleArea = 0, base = 4, height = 7, triangleArea = 0; 
        double radius = 0, circleArea = 0, prismVolume = 0, cylinderSurfaceArea = 0;
        double[] cylinderSpecs = {2.66, 9.45, 0.0}; 
        // cylinderSpecs = {radius, height, surfaceArea} - DO NOT MODIFY FIRST TWO ELEMENTS
    			
        // Call calcRectangleArea() and calcTriangleArea() and write results to Geometry.txt.
    					
        // Assign radius the value located at args[1],
        // call calcCircleArea(), and write result to Geometry.txt.
    			
        // Call calcPrismVolume() and write result to Geometry.txt.
    			
        // Call calcCylinderSurfaceArea() and write result to Geometry.txt.
    			
        // Close PrintWriter.	
    
        } catch (Exception e) {
            e.printStackTrace();
        }
    
  4. Next, copy the following code and paste it after your main method but before the closing brace of P10:
    // calcRectangleArea method
    public static int calcRectangleArea(int l, int w) {
        // YOUR CODE HERE
    		
    } // end calcRectangleArea
    	
    // calcTriangleArea method
    public static int calcTriangleArea(int b, int h) {
        // YOUR CODE HERE
    		
    } // end calcTriangleArea
    	
    // calcCircleArea method
    public static double calcCircleArea(double r) {
        // YOUR CODE HERE
    		
    } // end calcCircleArea
    	
    // calcPrismVolume method
    public static double calcPrismVolume() {
        // YOUR CODE HERE
    		
    } // end calcPrismVolume
    	
    // calcCylinderSurfaceArea method
    public static void calcCylinderSurfaceArea(double[] specs) {
        // YOUR CODE HERE
    		
    } // end calcCylinderSurfaceArea
    

  5. Part Two
  6. First, in the main method, declare and initialize a PrintWriter object to write to the file name located at args[0].
  7. Set up your P10 program arguments with Geometry.txt as the file name. For this project, you will only work with an output file.
  8. After finishing the code necessary for setup, start by finishing the code in calcRectangleArea(). The code required for this method will be 1-3 lines depending upon your implementation.
  9. Start by creating a variable to hold the result.
  10. Then, use the incoming variables to calculate the area and assign to your result variable.
  11. Last, in this method, return the result.
  12. Next, finish the code needed in calcTriangleArea(). The code for this method will also be 1-3 lines.
  13. Again, create a variable to hold the result.
  14. Use the incoming variables to calculate the area and assign to your result variable.
  15. Then, return the result.

  16. Part Three
  17. To finish the lab, return to main and call calcRectangleArea() and store the return value in rectangleArea.
  18. Then write rectangleArea to the output file after following prompt:
     Rectangle Area: 
    Include a space after the colon and a new line character.
  19. Next, call calcTriangleArea() and store the return value in triangleArea.
  20. Then write triangleArea to the output file after the following prompt:
     Triangle Area: 
    Include a space after the colon and a new line character.
  21. Lastly, close your PrintWriter.

Output File Contents

Rectangle Area: 10
Triangle Area: 14



CS Banner
CS Building