Lab 7 - Writing and Calling Methods


  • Create a class without a main method, but containing some other methods.
  • Stub out the class so that methods can be written and tested incrementally.
  • Become more familiar with the Math class in the Java Standard Library.

Key Terms

return type

The data type the method returns; it comes after public static.

return value

The value that the method returns via the return statement.

method stub

An empty or incomplete method that returns 0, "", false, etc.

Part 1: Creating Stubs

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 the calculation of the area of a circle and the volume of a sphere. (Normally, you would design one class to do the geometry and another one to deal with the utility methods, but for today's lab we are putting them together in one class.)

  1. Download and open the program. Do not be concerned with what it contains just yet.
  2. Create a new source file, Create the class documentation including your name and today's date and honor code statement.
  3. Define each of the following methods:
    • printGreeting will simply print some lines to greet the user upon running the application. Create a method stub using the following header:
      public static 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.
    • enterRadius - create a method header that looks like this:
      public static 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)
       * Displays a prompt to the user, then reads in a double value
       * that the user has entered.
       * @return a double value representing a radius

    • Create the method block, and add in a single return statement, return -999.0; This is a "stubbed" return statement that returns a value of the correct type, but is not the value we will eventually need.
    • calculateArea - This method will calculate the area of a circle with a given radius.
      Create a method header that is
      public, static, 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.0. (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.
    • calculateVolume - This method will calculate the volume of a sphere with a given radius.
      Create a method header, method block, return statement, and documentation. The stubbed return value should be -777.
  4. Save and compile your work at this point, before implementing any of the methods. You should not have any compiler errors. If you do, check to make sure that each method in CirclePlay is created exactly as described above.
  5. Run CircleDriver from jGRASP or using the command line. What output do you see? Does it make any sense?

Now we will begin to fill in our stubbed methods.

Part 2: Creating a Void Method With No Parameters

Begin with printGreeting, which should print the following. (Note that <blank line> just means that you should print a newline there.)

Welcome to the CS149 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 these four lines. Compile your code in jGRASP, but run it from the command line.
  2. You are currently testing the 1st method, so pass the argument 1 to the main method as follows:
  3. java CircleDriver 1
  4. Does your greeting print correctly? Make any corrections that you need before moving on.

Explanation: The CircleDriver is designed to let you test each of your methods separately. 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 one.

Part 3: Creating a Value Returning Method With No Parameters

Next you'll 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 need to declare at least two variables: a Scanner and a double. The double will hold the radius that the user enters.
  2. Instantiate the Scanner (using the new keyword).
  3. Output the prompt "Enter the radius: ". The cursor should remain on the same 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. 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 a few test cases for each method.

  1. Write down your examples on a piece of paper, so you can be sure your code works as expected.
  2. Begin with the calculateArea method. You may find that the Math class (provided in the java.lang package) is helpful for this step. First, there is a constant defined in that class called PI. Since the constant is in another class, we must use the qualified name Math.PI to access it.
  3. Define the variables that you will need.
  4. Create the calculation, using Math.PI for π.
  5. Instead of the stub 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 by hand. Work with your method until it runs correctly.
  8. 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 program to Autolab. To receive full credit for this assignment, all 4 methods must work correctly. Note that Autolab is configured to run Checkstyle on your submission. You will not receive full credit for this lab unless your code passes all of the automated formatting tests.

This lab was originally created by Nancy Harris and modified by Nathan Sprague.

Back to Top