4A
You have all probably seen a snow globe - a glass ball containing a winter scene and filled with a liquid and white particles. When turned upside down, the white particles float to the bottom. When turned right-side up again, the particles drift down looking like snow.
Write a class SnowGlobe
to model this behavior. There is no starter file but there is a tester
20% of the volume of the object will be filled with white particles while 50% will be filled with a liquid. The other 30% is the things in the scene.
The snow costs $1.25 per cubic centimeter
Provide 2 constructors. Both constructors have the same name, SnowGlobe
. The compiler tells them apart because they have different parameter lists. This is called overloading):
It has methods
setRadius
Sets a new radius. It is a good practice for the constructors to call setRadius rather than setting the radius directly.getRadius
Gets the radiusgetVolume
Gets the volume of the sphere in cubic centimetergetSnowVolume
Gets the volume of the snow in cubic centimeter. Do not calculate the volume of sphere in this method. Reuse the getVolume
method so you only calculate the volume once.getSnowCost
Gets the cost of the snow needed in this globe. Do not calculate the volume of snow in this method. Reuse the getSnowVolume
method so you only calculate the snow volume once.Define and use constants for the percent and the cost per cubic centimeter of the snow. That means you should not hard code .20 or 1.25 in the methods. you should use constants. Define them as class constants at the top of the class with the instance variables.
Provide Javadoc for the class, the constructor and methods
Formulas
Volume of a Sphere: v = 4/3 π r3 Use the constant from the Math class for pi. (Caution: remember about integer division. What is 4/3?)
For the draft , provide the instance variable, the no-argument constructor, the setRadius(), and getRadius() methods. Remember that instance variables should be private. Provide the Javadoc.
4B
Complete the application InputApplication
.
Create a Scanner
object and do the following..
System.out.print("Enter an integer: ");
I recommend copy/pasting it so you have it exactly right.Scanner's
nextInt
method to get the integerSystem.out.print("Enter another integer: ");
Scanner's
nextInt
method to get the integerSystem.out.print("Enter a double: ");
Scanner's
nextDouble
method to get the doubleOnly create one Scanner.
For the draft, create the Scanner and do the first three steps.
4C
Write a class StringStuff
which creates a Scanner
and then does the following:
System.out.print("Enter your full name: ");
This may be several wordsTo get just the first name, you need to know the index of the first space. You can find the index of the first space with the indexOf method. str.indexOf(" ")
will return the index of the first space or -1 if there are no spaces in str. You can assume there is at least one space so you do not need to worry about -1 for this problem.
There is no starter file
For the draft, do the first three steps.