Java Reference
In-Depth Information
To report a change to another object
n
To tell the other object to change something about itself
n
To ask another object to do something
n
1
For example, two volcano robots could use methods to report their locations to each
other and avoid collisions, and one robot could tell another to stop so that it could
pass by.
Just as there are instance and class variables, there also are instance and class methods.
Instance methods , which are usually just called methods , are used when you are working
with an object of the class. If a method makes a change to an individual object, it must
be an instance method. Class methods apply to a class itself.
Creating a Class
To see classes, objects, attributes, and behavior in action, you will 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 pro-
gramming. 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 topic.
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:
 
Search WWH ::




Custom Search