Objectives
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.
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.)
/** * 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.
Now we will begin to fill in our stubbed methods.
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>
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.
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.
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.
Upload your completed CirclePlay.java 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.