What is your weight on another planet? |
In this program you ask a user his weight. Then you ask him to choose a planet in the solar system. Finally, your program calculates what he would weigh on that planet, and displays that result. This is a revised version of the program "What is Your Weight on Another Planet?" This version makes use of two classes, an array to store the planets, and loops to access each of the planets in the array.
Planet.java
This file creates a class for an individual planet. Look at the
private instance variables that have been declared. The other class,
PlanetWeight_ArrayVersion.java, needs to access these private instance
variables. But it cannot do so directly: one class cannot directly access
the instance variables of another class. So the Planet.java class has to
provide methods that allow the other class to access and change its instance
variables. These methods are essentially one-line methods, and completing
them should be almost self-explanatory.
PlanetWeight_ArrayVersion.java
This file creates a solar system with 9 Planet objects, and it makes use of
an array 'planet' to store them. Information on how to complete each
method is given in the Java file rather than here. You may notice that,
since you have already written this program, most of the tedious work like
entering the sizes of planets into your program has been done for you.
Here is Javadoc documentation that describes the two classes that create this program. Note in the two .java files what has been done before the Javadoc utility could be used. A description of each method is given above each method, following the symbol /**. Each parameter is described with a @param tag. What each method returns is described with a @return tag. Once this information is added to the file, you simply run the Javadoc program from the DOS simulator window and it creates the Javadoc documentation you see here.
TP discovered that if she moved the bottom edge of the output window up so that the planets disappeared, and then moved it back down, the planets went from a solid color to having bands of color. This is an example.
You can download PlanetWeight_ArrayVersion.java and
Planet.java to use as starting points in writing your
program. You will need to create a project that holds both .java files.