Home arrow Java arrow Introducing Classes and More in Java
JAVA

Introducing Classes and More in Java


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).

Author Info:
By: Sams Publishing
Rating: 4 stars4 stars4 stars4 stars4 stars / 6
May 11, 2006
TABLE OF CONTENTS:
  1. · Introducing Classes and More in Java
  2. · Running the Program
  3. · Organizing Classes and Class Behavior
  4. · Creating a Class Hierarchy
  5. · Inheritance in Action
  6. · Summary
  7. · Questions and Answers

print this article
SEARCH DEVARTICLES

Introducing Classes and More in Java
(Page 1 of 7 )

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 24:

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 611:

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 1317:

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.


blog comments powered by Disqus
JAVA ARTICLES

- Java Too Insecure, Says Microsoft Researcher
- Google Beats Oracle in Java Ruling
- Deploying Multiple Java Applets as One
- Deploying Java Applets
- Understanding Deployment Frameworks
- Database Programming in Java Using JDBC
- Extension Interfaces and SAX
- Entities, Handlers and SAX
- Advanced SAX
- Conversions and Java Print Streams
- Formatters and Java Print Streams
- Java Print Streams
- Wildcards, Arrays, and Generics in Java
- Wildcards and Generic Methods in Java
- Finishing the Project: Java Web Development ...

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 

Developer Shed Affiliates

 




© 2003-2017 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials