Introducing Classes and More in Java - Running the Program
(Page 2 of 7 )
If you have compiled the VolcanoRobot class, you can't actually use it to simulate the actions of these exploratory robots. The class you have created defines what a VolcanoRobot object would be like if it were used in a program, but it doesn't use one of these objects yet.
There are two ways to put this VolcanoRobot class to use:
Create a separate Java program that uses this class.
Add a special class method called main() to the VolcanoRobot class so that it can be run as an application, and then use VolcanoRobot objects in that method.
The first option is chosen for this exercise. Listing 1.2 contains the source code for VolcanoApplication, a Java class that creates a VolcanoRobot object and uses its instance variables and methods.
Listing 1.2 The Full Text of VolcanoApplication.java
1: class VolcanoApplication {
2: public static void main(String[] arguments) {
3: VolcanoRobot dante = new VolcanoRobot();
4: dante.status = "exploring";
5: dante.speed = 2;
6: dante.temperature = 510;
7:
8: dante.showAttributes();
9: System.out.println("Increasing speed to
3.");
10: dante.speed = 3;
11: dante.showAttributes();
12: System.out.println("Changing temperature to
670.");
13: dante.temperature = 670;
14: dante.showAttributes();
15: System.out.println("Checking the
temperature.");
16: dante.checkTemperature();
17: dante.showAttributes();
18: }
19: }
Save the file as VolcanoApplication.java and compile the program.
If you are using the Software Development Kit, you can do the following to compile the program: Go to a command line or open a command-line window, open the folder where VolcanoApplication.java was saved, and then compile the program by typing the following command at the command line:
javac VolcanoApplication.java
The Java compiler also compiles the VolcanoRobot.java file if necessary because the VolcanoRobot class class is being used in this application.
Tip - If you encounter problems compiling or running any program in this book with SDK 1.5, you can find a copy of the source file and other related files on the book's official Web site at http://www.java21days.com.
After you have compiled the application, run the program.
Using the SDK, you can run the VolcanoApplication program by opening the folder containing the VolcanoRobot.class and VolcanoApplication.class files at a command line and using this command:
java VolcanoApplication
When you run the VolcanoApplication class, the output should be the following:
Status: exploring
Speed: 2
Temperature: 510.0
Increasing speed to 3.
Status: exploring
Speed: 3
Temperature: 510.0
Changing temperature to 670.
Status: exploring
Speed: 3
Temperature: 670.0
Checking the temperature.
Status: returning home
Speed: 5
Temperature: 670.0
Using Listing 1.2 as a guide, the following things take place in the main() class method:
Line 2—The main() method is created and named. All main() methods take this format, and you'll learn more about them during Day 5, "Creating Classes and Methods." For now, the most important thing to note is the static keyword. This indicates that the method is a class method.
Line 3—A new VolcanoRobot object is created using that class as a template. The object is given the name dante.
Lines 4–6—Three instance variables of the dante object are given values: status is set to the text "exploring", speed is set to 2, and temperature is set to 510.
Line 8—On this line and several that follow, the showAttributes() method of the dante object is called. This method displays the current values of the instance variables status, speed, and temperature.
Line 9—On this line and others that follow, a System.out.println() statement is used to display the text within the parentheses.
Line 10—The speed instance variable is set to the value 3.
Line 13—The temperature instance variable is set to the value 670.
Line 16—The checkTemperature() method of the dante object is called. This method checks to see whether the temperature instance variable is greater than 660. If it is, status and speed are assigned new values.
Next: Organizing Classes and Class Behavior >>
More Java Articles
More By Sams Publishing
|
This article is excerpted from chapter one of Sams Teach Yourself Java 2 in 21 Days, 4th Edition, written by Rogers Cadenhead and Laura Lemay (Sams; ISBN: 0672326280). Buy this book now.
|
|