Page 230 - Introduction to Programming with Java: A Problem Solving Approach
P. 230

                196 Chapter 6 Object-Oriented Programming 6.1 Introduction
As discussed in the Preface, we’ve written the book with some built-in flexibility in terms of content order- ing. Readers who want an early introduction to object-oriented programming (OOP) have the option of read- ing Sections 6.1 through 6.8 after completing Chapter 3.
Chapter 5 served as a bridge from basic programming language constructs (variables, assignments, operators, if statements, loops, etc.) to OOP concepts. We focused primarily on one important as- pect of OOP—learning how to use pre-built methods. You used methods associated with an object, like substring and indexOf for string objects, and you used methods associated with a class, like abs and pow from the Math class. In this chapter, you’ll learn how to do more than just use pre-built classes and methods; you’ll learn how to write your own classes and methods.
As you’ll come to see, OOP makes large programs easier to work with. And making large programs eas- ier to work with is very important because today’s computers use lots of very large programs! The tension in learning OOP is that the first OOP programs a student can understand are necessarily small, and they can’t show the power of OOP very well. But hang in there. Think of your study of this chapter and most of the next chapter as an investment. By the end of the next chapter, you’ll be getting some return on that investment.
In this chapter, we start with an overview of basic OOP terms and concepts. We then step through the
design and implementation of a simple OOP program. Typically, OOP design starts with a simple Unified
Modeling Language (UML) class diagram, which provides a high-level, pictorial description of what you
want the program to model. Then OOP design proceeds to the program’s details. We’ll show you how to
adapt the previously described tracing technique to an OOP environment. We’ll show you how to specify
 method details. In the previous chapter you looked at methods from the outside—with a user or client view. Apago PDF Enhancer
Now you’ll be looking at methods from the inside—with an implementation or server view.
We end the chapter with an optional problem-solving section that introduces you to an important com- puter application—computer simulation. Computer simulation allows humans to solve problems that are difficult or impossible to solve by hand. We describe a special strategy which enables you to substantially
improve both the accuracy and efficiency of computer simulations.
6.2 Object-Oriented Programming Overview
Readers who want a very early OOP overview have the option of reading this section after completing Chapter 1, Section 1.3 (Program Development).
Before OOP, the standard programming technique was procedural programming. Procedural program- ming is so named because the emphasis is on the procedures or tasks that make up a problem solution. You think first about what you want to do—your procedures. In contrast, the OOP programming paradigm in- vites you to think about what you want the program to represent. You typically respond to this invitation by identifying some things in the world that you want your program to model. Those things might be physical entities or conceptual entities. Once you have identified the things you want to model, you identify their basic properties/attributes. Then you determine what the things can do (their behaviors) or what the things can have done to them. You group each thing’s properties and behaviors together into a coherent structure called an object. In writing an OOP program, you define objects, create them, and have them interact with each other.
Objects
An object is:
a set of related data which identifies the current state of the object 􏰀 a set of behaviors.
 















































































   228   229   230   231   232