/** * CS139 - Programming Fundamentals * Department of Computer Science * James Madison University * @version Spring 2016 */
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.
public static
.return
statement.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.)
CirclePlay.java
.
Create the class documentation including your name and today's date.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.
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 CS139 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 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.
new
keyword).return
statement to return the value that the user enters.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.
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.return
statement, return the area that you calculate.calculateVolume
method and test as before using command line argument 4 to test your volume method.Upload your completed CirclePlay.java program to Web-CAT. To receive full credit for this assignment, all 4 methods must work correctly. Note that Web-CAT 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.
If you find you're writing the same code over and over again, it's a good idea to put that code into a method. We're going to have a little competition to illustrate this point. Think of a typical song from popular culture: it often repeats the same phrases over and over again.
Using Happy.java as a starting point, see if you can rewrite the program using as few System.out.println
statements as possible by breaking down the song into multiple methods. You may NOT change or combine any of the existing println statements, but you may delete as many as you like. In the end your program's output must match Happy.txt exactly (use diff
or meld
to compare your results).
If you're up for this challenge then submit your completed Happy.java file along with your CirclePlay.java file. Be sure to replace the ??
's in the documentation comment with your final results.