CS 139 Algorithm Development
Lab05B: Introduction to Methods

Background

Yesterday you looked at designing programs with methods and some of the reasons why we might break a problem down into individual methods. Today we will implement some methods and experience "separate compilation". You will also use command line arguments for the first time in a program.

Objectives

The students will:

New Terms

void method
A method that returns nothing. It's return type is "void".
value returning method
A method that returns some value (like a function). It's return type is the data type of the value being returned.
return value
The value that the method returns through the return statement.
return type
The data type that the method returns. Return type is found in the method header
stub
A method that has a method header, method documentation, and a return statement of the appropriate type. It is used for designing a class and then testing individual class components.

Instructions

 


Part 1: Creating "Stubs" and command line compilation

We are going  to create a small class to carry out some geometry operations. It will include a couple of utility methods to make input and printing easier as well as calculation of the area of a circle and the volume of a circular sphere. NOTE: Normally, you would design a class to do the geometry and another one to deal with the utility methods, but in this case we are putting them together in one class for lab purposes.

Before you build the entire class, you are going to build a shell that will include "stubbed" versions of the code.

  1. Create a new java file called CirclePlay.java using the editor of your choice. Create the class documentation including your name and assignment date. Describe the class as a class to do some circle geometry.
  2. Create the class header and block (open and close curly braces).
  3. Create each of the following method "stubs":

    1. printGreeting method will simply print the some lines to greet the user upon accessing the application. Create a method "stub" by creating the following header and a method block. No code needs to be contained in the block at this time.

    public void printGreeting(). This header says that the method is public, returns nothing (void), is named printGreeting, and accepts no parameters.

    Create method documentation. In this case you only need to describe what this method will do.

    2. enterRadius - create a method header that looks like this:

    public double enterRadius(). This header says the method is public, returns a value of double type, is named enterRadius, and accepts no parameters.

    Add method documentation for this method as follows (Note the use of the @return label):

    /**
      *  enterRadius - This method displays a prompt to the user, then reads in an integer value
      *  that the user has entered.
      *
      *  @return An integer value representing a radius
    */

    Create the method block, and add in a single return statement, return -999; This is a "stubbed" return statement that returns a value of the correct type, but is not the value we will eventually need.

    3. calculateArea - This method will calculate the area of a circle with a given radius.

    Create a method header that is public, returns a double value, is named calculateArea and has a single parameter, a double value representing a radius.

    Create the method block with a return statement to return the value -888. (These are meaningless values, but if we call the method and its not done, we'll know that by the return value.)

    Add your documentation including an @param tag (See book Chapter 5.2 for the @param tag)

    4. calculateVolume - This method will calculate the volume of a sphere with a given radius.

    Create a method header, method block, return statement, and documentation as for calculateArea.

  4. Save your work.
  5. Open a terminal window.
  6. Navigate to the directory containing your CirclePlay.java program.
  7. Compile your work using the javac command (javac CirclePlay.java). If it compiles correctly, you will receive the input prompt on the following line. If not, you will see the normal Java error messages. Correct your errors using your editor.
  8. Compile CircleDriver.java. You should not have any compiler errors. If you do, check to make sure that each method in CirclePlay is created exactly as described. Correct the errors in CirclePlay.java and repeat steps 7 and 8 until they compile correctly.
  9. Run CircleDriver from the command line using the command java CircleDriver. What output do you see? Does it make any sense?
  10. Demo your CircleDriver program from the command line for the lab instructors.

Part 2: Creating a Void Method With No Parameters

Now we will begin to fill in our stubbed methods.

Begin with printGreeting. printGreeting should print the following <blank line> just means that we should print a blank line here:

Welcome to the Circle Calculator
<blank line>   
This application will calculate the area of a circle and/or volume of a sphere.
<blank line>
  1. Create the code to print those four lines.
  2. Compile your code.
  3. Test your code again in the terminal window. This time, you are testing the 1st method, so pass the argument, 1, to the java command:

java CircleDriver 1

  1. Does your greeting print correctly? How was this execution of the application different than step 9? Make any corrections that you need to before moving on.

Explanation: The CircleDriver is designed to let you test each of your methods. In this case, you have built a single method, so you are testing just that method. The method stubs enable you to compile the entire program, but then just fill in the parts that you are ready to complete and test. By testing individual methods, you can focus on getting that method written correctly before moving on to the next.

 


Part 3: Creating a Value Returning Method With No Parameters

You'll next fill in the enterRadius method. This method displays a standard prompt, "Enter the radius: " and then reads in the radius from the keyboard. You will need to create a Scanner object as part of this method.

  1. You will need at least two variables, a Scanner, and a double. The double will hold the radius that the user enters.
  2. Instantiate the Scanner.
  3. Output the prompt "Enter the radius: ". The cursor should remain on the command line one space to the right of the colon.
  4. Read the value that the user enters.
  5. Change the return statement to return the value that the user enters.
  6. Test your program on the command line. This time enter the number 2 as the command line argument. What happens?

Part 4: Creating Value Returning Methods With Parameters

Finally, you will fill in each of the calculation methods. You are going to calculate the area of a circle and the volume of a sphere. Stop now and create some examples to test.

The area of a circle is defined as A = ∏ r 2. The volume of a sphere is V = 4/3 ∏ r 3. Use these formulas to come up with 3 examples for each.

  1. Write down your examples on a piece of paper.
  2. Begine with the calculateArea method. You may find that the Math class that is provided in the java.lang package is helpful for this. First, there is a constant defined in that class called PI. PI is as you might expect, the value of PI. I can use Math.PI anywhere that I want that value just like I can use a constant. However, in this case, since the constant is in another class, we must use the qualified name Math.PI to identify that we want the PI that is in the Math class. Other helpful Math methods are found in Appendix G of your book as well as in Chapter 2.
  3. Define the variables that you will need.
  4. Create the calculation using Math.PI for ∏.
  5. Instead of the return value in your return statement, return the area that you calculate.
  6. Test your work passing the number 3 in for the command line argument.
  7. Check your area values against the ones that you calculated. Work with your method until it works correctly.
  8. Finally, create the calculateVolume method and test as before using command line argument 4 to test your volume method.
  9. Finally, test the entire application running the application with no arguments. It should run each method in turn.

Upload your completed CirclePlay.java file to the Blackboard assignment.


OPTIONAL - Part 5: Formatting Your Output

You created methods to calculate the area and volume of a circle, but these methods did nothing to format the output. You could format the output in the driver program, but then you would have to format each one. It is reasonable to build a method in your CirclePlay class to format values to 2 decimal points.

  1. Create a method, format2, that accepts a double value as a parameter and returns a String which is that number formatted to 2 decimal places.
  2. Activate the commented code in the driver for Test 5. This code formats the input radius to 2 decimal points. In testing enter values that have three or more decimals and insure that the output is correct.
  3. Use 5 as the command line argument to test.
  4. When it is working correctly, change the output statement in the area and volume tests to use this format method to format the output. (This is challenging as the return values are not stored in a variable.)