Introducing Classes and More in Java
(Page 1 of 7 )
This article introduces you to the Java programming language. The second of two parts, it 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).
Creating a Class
To see classes, objects, attributes, and behavior in action, you develop a VolcanoRobot class, create objects from that class, and work with them in a running program.
Note - The main purpose of this project is to explore object-oriented programming. You'll learn more about Java programming syntax during Day 2, "The ABCs of Programming."
To begin creating a class, open the text editor you're using to create Java programs and create a new file. Enter the text of Listing 1.1, and save the file as VolcanoRobot.java in a folder you are using to work on programs from this book.
Listing 1.1 The Full Text of VolcanoRobot.java
1: class VolcanoRobot {
2: String status;
3: int speed;
4: float temperature;
5:
6: void checkTemperature() {
7: if (temperature > 660) {
8: status = "returning home";
9: speed = 5;
10: }
11: }
12:
13: void showAttributes() {
14: System.out.println("Status: " + status);
15: System.out.println("Speed: " + speed);
16: System.out.println("Temperature: " +
temperature);
17: }
18: }
The class statement in line 1 of Listing 1.1 defines and names the VolcanoRobot class. Everything contained between the opening brace ("{") on line 1 and the closing brace ("}") on line 18 is part of this class.
The VolcanoRobot class contains three instance variables and two instance methods.
The instance variables are defined in lines 2–4:
String status;
int speed;
float temperature;
The variables are named status, speed, and temperature. Each is used to store a different type of information:
status holds a String object, a group of letters, numbers, punctuation, and other characters.
speed holds an int, an integer value.
temperature holds a float, a floating-point number.
String objects are created from the String class, which is part of the Java class library and can be used in any Java program.
Tip - As you might have noticed from the use of String in this program, a class can use objects as instance variables.
The first instance method in the VolcanoRobot class is defined in lines 6–11:
void checkTemperature() {
if (temperature > 660) {
status = "returning home";
speed = 5;
}
}
Methods are defined in a manner similar to a class. They begin with a statement that names the method, the kind of information the method produces, and other things.
The checkTemperature() method is contained within the brackets on lines 6 and 11 of Listing 1.1. This method can be called on a VolcanoRobot object to make sure the robot hasn't become overheated.
This method checks to see whether the object's temperature instance variable has a value greater than 660. If it does, two other instance variables are changed:
The status is changed to the text "returning home", indicating that the temperature is too hot and the robot is heading back to its base.
The speed is changed to 5. (Presumably, this is as fast as the robot can travel.)
The second instance method, showAttributes(), is defined in lines 13–17:
void showAttributes() {
System.out.println("Status: " + status);
System.out.println("Speed: " + speed);
System.out.println("Temperature: " + temperature);
}
This method uses System.out.println() to display the values of three instance variables along with some text explaining what each value represents.
Next: Running the Program >>
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.
|
|