Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:

AhmadLang / Java, How To Program, 2004

.pdf
Скачиваний:
588
Добавлен:
31.05.2015
Размер:
51.82 Mб
Скачать

calculates the squares and cubes of the numbers from 0 to 10 and prints the resulting values in table format, as shown below. [Note: This program does not require any input from the user.]

number

square

cube

0

0

0

1

1

1

2

4

8

3

9

27

4

16

64

5

25

125

6

36

216

7

49

343

8

64

512

9

81

729

10

100

1000

 

 

 

2.32Write a program that inputs five numbers and determines and prints the number of negative numbers input, the number of positive numbers input and the number of zeros input.

[Page 81]

Chapter 3. Introduction to Classes and Objects

You will see something new. Two things. And I call them Thing One and Thing Two.

Dr. Theodor Seuss Geisel

Nothing can have value without being an object of utility.

Karl Marx

Your public servants serve you right.

Adlai E. Stevenson

Knowing how to answer one who speaks, To reply to one who sends a message.

Amenemope

OBJECTIVES

In this chapter you will learn:

What classes, objects, methods and instance variables are.

How to declare a class and use it to create an object.

How to declare methods in a class to implement the class's behaviors.

How to declare instance variables in a class to implement the class's attributes.

How to call an object's method to make that method perform its task.

The differences between instance variables of a class and local variables of a method.

How to use a constructor to ensure that an object's data is initialized when the object is created.

The differences between primitive and reference types.

[Page 82]

Outline

3.1 Introduction

3.2 Classes, Objects, Methods and Instance Variables

3.3 Declaring a Class with a Method and Instantiating an Object of a Class

3.4 Declaring a Method with a Parameter

3.5 Instance Variables, set Methods and get Methods

3.6 Primitive Types vs. Reference Types

3.7 Initializing Objects with Constructors

3.8 Floating-Point Numbers and Type double

3.9 (Optional) GUI and Graphics Case Study: Using Dialog Boxes

3.10 (Optional) Software Engineering Case Study: Identifying the Classes in a Requirements Document

3.11 Wrap-Up

Summary

Terminology

Self-Review Exercises

Answers to Self-Review Exercises

Exercises

[Page 82 (continued)]

3.1. Introduction

We introduced the basic terminology and concepts of object-oriented programming in Section 1.16. In Chapter 2, you began to use those concepts to create simple applications that displayed messages to the user, obtained information from the user, performed calculations and made decisions. One common feature of every application in Chapter 2 was that all the statements that performed tasks were located in method main. Typically, the applications you develop in this book will consist of two or more classes, each containing one or more methods. If you become part of a development team in industry, you might work on applications that contain hundreds, or even thousands, of classes. In this chapter, we present a simple framework for organizing object-oriented applications in Java.

First, we motivate the notion of classes with a real-world example. Then we present five complete working applications to demonstrate creating and using your own classes. The first four of these examples begin our case study on developing a grade-book class that instructors can use to maintain student test scores. This case study is enhanced over the next several chapters, culminating with the version presented in Chapter 7, Arrays. The last example in the chapter introduces floating-point numbersthat is, numbers containing decimal points, such as 0.0345, 7.23 and 100.7in the context of a bank account class that maintains a customer's balance.

[Page 82 (continued)]

3.2. Classes, Objects, Methods and Instance Variables

Let's begin with a simple analogy to help you understand classes and their contents. Suppose you want to drive a car and make it go faster by pressing down on its accelerator pedal. What must happen before you can do this? Well, before you can drive a car, someone has to design the car. A car typically begins as engineering drawings, similar to the blueprints used to design a house. These engineering drawings include the design for an accelerator pedal to make the car go faster. The pedal "hides" the complex mechanisms that actually make the car go faster, just as the brake pedal "hides" the mechanisms that slow the car and the steering wheel "hides" the mechanisms that turn the car. This enables people with little or no knowledge of how engines work to drive a car easily.

[Page 83]

Unfortunately, you cannot drive the engineering drawings of a car. Before you can drive a car, the car must be built from the engineering drawings that describe it. A completed car will have an actual accelerator pedal to make the car go faster, but even that's not enoughthe car will not accelerate on its own, so the driver must press the accelerator pedal.

Now let's use our car example to introduce the key programming concepts of this section. Performing a task in a program requires a method. The method describes the mechanisms that actually perform its tasks. The method hides from its user the complex tasks that it performs, just as the accelerator pedal of a car hides from the driver the complex mechanisms of making the car go faster. In Java, we begin by creating a program unit called a class to house a method, just as a car's engineering drawings house the design of an accelerator pedal. In a class, you provide one or more methods that are designed to perform the class's tasks. For example, a class that represents a bank account might contain one method to deposit money to an account, another to withdraw money from an account and a third to inquire what the current balance is.

Just as you cannot drive an engineering drawing of a car, you cannot "drive" a class. Just as someone has to build a car from its engineering drawings before you can actually drive a car, you must build an object of a class before you can get a program to perform the tasks the class describes how to do. That is one reason Java is known as an object-oriented programming language.

When you drive a car, pressing its gas pedal sends a message to the car to perform a taskthat is, make the car go faster. Similarly, you send messages to an objecteach message is known as a method call and tells a method of the object to perform its task.

Thus far, we have used the car analogy to introduce classes, objects and methods. In addition to the capabilities a car provides, it also has many attributes, such as its color, the number of doors, the amount of gas in its tank, its current speed and its total miles driven (i.e., its odometer reading). Like the car's capabilities, these attributes are represented as part of a car's design in its engineering diagrams. As you drive a car, these attributes are always associated with the car. Every car maintains its own attributes. For example, each car knows how much gas is in its own gas tank, but not how much is in the tanks of other cars. Similarly, an object has attributes that are carried with the object as it is used in a program. These attributes are specified as part of the object's class. For example, a bank account object has a balance attribute that represents the amount of money in the account. Each bank account object knows the balance in the account it represents, but not the balances of the other accounts in the bank. Attributes are specified by the class's instance variables.

The remainder of this chapter presents examples that demonstrate the concepts we introduced in the context of the car analogy. The first four examples, summarized below, incrementally build a GradeBook class to demonstrate these concepts:

1.The first example presents a GradeBook class with one method that simply displays a welcome message when it is called. We then show how to create an object of that class and call the method so that it displays the welcome message.

2.The second example modifies the first by allowing the method to receive a course name as an argument and by displaying the name as part of the welcome message.

3.The third example shows how to store the course name in a GradeBook object. For this version of the class, we also show how to use methods to set the course name and obtain the course

name.

[Page 84]

4.The fourth example demonstrates how the data in a GradeBook object can be initialized when the object is createdthe initialization is performed by the class's constructor.

The last example in the chapter presents an Account class that reinforces the concepts presented in the first four examples and introduces floating-point numbersnumbers containing decimal points, such as 0.0345, 7.23 and 100.7. For this purpose, we present an Account class that represents a bank account and maintains its balance as a floating-point number. The class contains two methodsone that credits a deposit to the account, thus increasing the balance, and another that retrieves the balance. The class's constructor allows the balance of each Account object to be initialized as the object is created. We create two Account objects and make deposits into each to show that each object maintains its own balance. The example also demonstrates how to input and display floating-point numbers.

[Page 84 (continued)]

3.3. Declaring a Class with a Method and Instantiating an Object of a Class

We begin with an example that consists of classes GradeBook (Fig. 3.1) and GradeBookTest (Fig. 3.2). Class GradeBook (declared in file GradeBook.java) will be used to display a message on the screen (Fig. 3.2) welcoming the instructor to the grade-book application. Class GradeBookTest (declared in file GradeBookTest.java) is an application class in which the main method will use class GradeBook. Each class declaration that begins with keyword public must be stored in a file that has the same name as the class and ends with the .java file-name extension. Thus, classes GradeBook and GradeBookTest must be declared in separate files, because each class is declared public.

Figure 3.1. Class declaration with one method.

1

//

Fig. 3.1: GradeBook.java

2

//

Class declaration with one method.

3

 

 

4public class GradeBook

5{

6// display a welcome message to the GradeBook user

7public void displayMessage()

8{

9System.out.println( "Welcome to the Grade Book!" );

10} // end method displayMessage

11

12 } // end class GradeBook

Figure 3.2. Creating an object of class GradeBook and calling its displayMessage method.

(This item is displayed on page 86 in the print version)

1

//

Fig. 3

.2: GradeBookTest.java

2

//

Create

a GradeBook object and call its displayMessage method.

3

 

 

 

4public class GradeBookTest

5{

6// main method begins program execution

7public static void main( String args[] )

8{

9// create a GradeBook object and assign it to myGradeBook

10GradeBook myGradeBook = new GradeBook();

11

12// call myGradeBook's displayMessage method

13myGradeBook.displayMessage();

14} // end main

15

16 } // end class GradeBookTest

Welcome to the Grade Book!

Common Programming Error 3.1

Declaring more than one public class in the same file is a compilation error.

Class GradeBook

The GradeBook class declaration (Fig. 3.1) contains a displayMessage method (lines 710) that displays a message on the screen. Line 9 of the class performs the work of displaying the message. Recall that a class is like a blueprintwe'll need to make an object of this class and call its method to get line 9 to execute and display its message.

[Page 85]

The class declaration begins at line 4. The keyword public is an access modifier. For now, we will simply declare every class public. Every class declaration contains keyword class followed immediately by the class's name. Every class's body is enclosed in a pair of left and right braces ({ and }), as in lines 5 and 12 of class GradeBook.

In Chapter 2, each class we declared had one method named main. Class GradeBook also has one methoddisplayMessage (lines 710). Recall that main is a special method that is always called automatically by the Java Virtual Machine (JVM) when you execute an application. Most methods do not get called automatically. As you will soon see, you must call method displayMessage to tell it to perform its task.

The method declaration begins with keyword public to indicate that the method is "available to the public"that is, it can be called from outside the class declaration's body by methods of other classes. Keyword void indicates that this method will perform a task but will not return (i.e., give back) any information to its calling method when it completes its task. You have already used methods that return informationfor example, in Chapter 2 you used Scanner method nextInt to input an integer typed by the user at the keyboard. When nextInt inputs a value, it returns that value for use in the program.

The name of the method, displayMessage, follows the return type. By convention, method names begin with a lowercase first letter and all subsequent words in the name begin with a capital letter. The parentheses after the method name indicate that this is a method. An empty set of parentheses, as shown in line 7, indicates that this method does not require additional information to perform its task. Line 7 is commonly referred to as the method header. Every method's body is delimited by left and right braces ({ and }), as in lines 8 and 10.

The body of a method contains statement(s) that perform the method's task. In this case, the method contains one statement (line 9) that displays the message "Welcome to the Grade Book!" followed by a newline in the command window. After this statement executes, the method has completed its task.

Next, we'd like to use class GradeBook in an application. As you learned in Chapter 2, method main begins the execution of every application. A class that contains method main is a Java application. Such a class is special because the JVM can use main to begin execution. Class GradeBook is not an application because it does not contain main. Therefore, if you try to execute GradeBook by typing java GradeBook in the command window, you will get the error message:

Exception in thread "main" java.lang.NoSuchMethodError: main

This was not a problem in Chapter 2, because every class you declared had a main method. To fix this problem for the GradeBook, we must either declare a separate class that contains a main method or

place a main method in class GradeBook. To help you prepare for the larger programs you will encounter later in this book and in industry, we use a separate class (GradeBookTest in this example) containing method main to test each new class we create in this chapter.

Class GradeBookTest

The GradeBookTest class declaration (Fig. 3.2) contains the main method that will control our application's execution. Any class that contains main declared as shown on line 7 can be used to execute an application. This class declaration begins at line 4 and ends at line 16. The class contains only a main method, which is typical of many classes that begin an application's execution.

[Page 86]

Lines 714 declare method main. Recall from Chapter 2 that the main header must appear as shown in line 7; otherwise, the application will not execute. A key part of enabling the JVM to locate and call method main to begin the application's execution is the static keyword (line 7), which indicates that main is a static method. A static method is special because it can be called without first creating an object of the class in which the method is declared. We thoroughly explain static methods in Chapter 6, Methods: A Deeper Look.

In this application, we'd like to call class GradeBook's displayMessage method to display the welcome message in the command window. Typically, you cannot call a method that belongs to another class until you create an object of that class, as shown in line 10. We begin by declaring variable myGradeBook. Note that the variable's type is GradeBookthe class we declared in Fig. 3.1. Each new class you create becomes a new type in Java that can be used to declare variables and create objects. Programmers can declare new class types as needed; this is one reason why Java is known as an extensible language.

Variable myGradeBook is initialized with the result of the class instance creation expression new GradeBook(). Keyword new creates a new object of the class specified to the right of the keyword (i.e., GradeBook). The parentheses to the right of the GradeBook are required. As you will learn in Section 3.7, those parentheses in combination with a class name represent a call to a constructor, which is similar to a method, but is used only at the time an object is created to initialize the object's data. In that section you will see that data can be placed in parentheses to specify initial values for the object's data. For now, we simply leave the parentheses empty.

Just as we can use object System.out to call methods print, printf and println, we can now use myGradeBook to call method displayMessage. Line 13 calls the method displayMessage (declared at lines 710 of Fig. 3.1) using variable myGradeBook followed by a dot separator (.), the method name displayMessage and an empty set of parentheses. This call causes the displayMessage method to perform its task. This method call differs from the method calls in Chapter 2 that displayed information in a command windoweach of those method calls provided arguments that specified the data to display. At the beginning of line 13, "myGradeBook." indicates that main should use the GradeBook object that was created on line 10. Line 7 of Fig. 3.1 indicates that method displayMessage has an empty parameter listthat is, displayMessage does not require additional information to perform its task. For this reason, the method call (line 13 of Fig. 3.2) specifies an empty set of parentheses after the method name to indicate that no arguments are being passed to method displayMessage. When method displayMessage completes its task, method main continues executing at line 14. This is the end of method main, so the program terminates.

[Page 87]

Compiling an Application with Multiple Classes

You must compile the classes in Fig. 3.1 and Fig. 3.2 before you can execute the application. First, change to the directory that contains the application's source-code files. Next, type the command

javac GradeBook.java GradeBookTest.java

to compile both classes at once. If the directory containing the application includes only the files for this application, you can compile all the classes in the directory with the command

javac *.java

The asterisk (*) in *.java indicates that all files in the current directory that end with the file name extension ".java" should be compiled.

UML Class Diagram for Class GradeBook

Figure 3.3 presents a UML class diagram for class GradeBook of Fig. 3.1. Recall from Section 1.16 that the UML is a graphical language used by programmers to represent their object-oriented systems in a standardized manner. In the UML, each class is modeled in a class diagram as a rectangle with three compartments. The top compartment contains the name of the class centered horizontally in boldface type. The middle compartment contains the class's attributes, which correspond to instance variables in Java. In Fig. 3.3, the middle compartment is empty because the version of class GradeBook in Fig. 3.1 does not have any attributes. The bottom compartment contains the class's operations, which correspond to methods in Java. The UML models operations by listing the operation name followed by a set of parentheses. Class GradeBook has one method, displayMessage, so the bottom compartment of Fig. 3.3 lists one operation with this name. Method displayMessage does not require additional information to perform its tasks, so the parentheses following displayMessage in the class diagram are empty, just as they were in the method's declaration in line 7 of Fig. 3.1. The plus sign (+) in front of the operation name indicates that displayMessage is a public operation in the UML (i.e., a public method in Java). We will often use UML class diagrams to summarize a class's attributes and operations.

Figure 3.3. UML class diagram indicating that class GradeBook has a public displayMessage operation.

[View full size image]