Write a separate class to use to solve a problem. This is essentially an extension of A02, only this time defining the spherical cone as its own class with instance methods.
Concepts: Objects, instance methods
Textbook: 4.4
You are going to write a SphericalCone
class, test that it is correct, and then use that class to solve a problem involving ice cream cones. Do this in the following order.
SphericalCone.java
Write a public class named SphericalCone
. Put this in its own file. It should contain the following three instance methods:
Your methods must much these signatures exactly (though you can change the variable names r
and c
, if you like). Note that each method takes the same two parameters: the radius of the cone base and the cone height, in that order.
The necessary formulas were given in A02. Ideally, the calculation of each detail will occur in only one place in your code. Remember that you can call one method from another. This would be handy for getting the slant height when computing the volume or surface area.
Your methods should return the correct values. They should not print anything to the screen.
The primary reason for writing methods is so that you can easily reuse chunks of code. It reduces errors and makes code easier to maintain if you can avoid duplicating code.
But the separation of code into small, independent modules has additional benefits. It can make the code more readable overall. And you can then unit test each of these small pieces to know they are correct. This helps greatly when debugging, especially for larger programs.
Here is a sample unit test for your cone: SphericalConeTester.java.
Download this file and put it in the same directory as your SphericalCone.java
file. You should then be able to compile and run SphericalConeTester
to ensure that all your methods have the correct signatures and return the correct values.
UsernameA09.java
)Now that you know your SphericalCone is correct, you could reuse it in a number of different programs. Here is one such program:
An ice cream company makes three different sizes of pre-packaged ice cream cone: small, medium, and large. Since these are pre-packaged, there is not quite as much ice cream as on a regular cone; therefore, they are actually shaped as spherical cones.
The dimensions are as follows:
The company is interested in knowing how much packaging (surface area) and ice cream (volume) goes into each of their cones.
Write a program that clearly prints the surface area and volume for each of these three spherical cone sizes. Create a single instance of your SphericalCone
class and call its methods to calculate this. Then also print the total volume consumed if one ate all three ice cream cones.
There is no user input for this program.
SMALL cone: Volume = 68.48657702033661 cm^3 Surface area = 100.88951136341649 cm^2 MEDIUM cone: Volume = 118.34480509114118 cm^3 Surface area = 145.28089636331964 cm^2 LARGE cone: Volume = 353.95277230445 cm^3 Surface area = 285.8849314766712 cm^2 Total volume: 540.7841544159278 cm^3
You are going to submit only your UsernameA09
and SphericalCone
classes.
Normally, each Java class should be in its own file. However, Tamarin only takes a single file per submission. So you are going to move your SphericalCone
class into your UsernameA09.java
file. Paste the entire SphericalCone
class after the last } of the UsernameA09
class. (Please make sure you don't put one class within another!)
Java will allow you to have only 1 public class per file though, so you also need to remove the word public
from the SphericalCone
class. (Leave the public modifier on all your methods, though.)
So, your UsernameA09.java
file should now be structured like this:
public class UsernameA09 { //... your main method in here ... } class SphericalCone { //... your three methods in here ... }
You may want to try deleting all your .class files, and then compiling and running your UsernameA09.java one more time before you submit it, just to make everything still works correctly.
Then, upload your complete UsernameA09.java
file to Tamarin.
SphericalCone
UsernameA09