CS 139 Algorithm Development
Lab05B: Introduction to Methods
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.
Watch the video about methods posted to Canvas and/or read chapter 5 about methods.
void
".return
statement.
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.
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.
1.
printGreeting
method will simply print 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. NOTE: This method takes in no parameters and returns nothing so there should be no @param or @return tags in the documentation.
2. enterRadius
- create a method header that looks like this:
public double enterRadius()
. This header says the method ispublic
, returns a value ofdouble
type, is namedenterRadius
, 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 adouble
value, is namedcalculateArea
and has a single parameter, adouble
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
.
CirclePlay.jav
a program. 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.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.java CircleDriver
. What output do you see? Does it make any sense?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>
java CircleDriver 1
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. (This application is using the command line arguments (args) which are the parameter for the main method.
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.
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.
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.
format2
, that accepts a double value as a parameter and returns a String which is that number formatted to 2 decimal places.