Chapter 5 Ball W orlds In our in tuitiv e description of ob ject-orien ted programming presen ted in Chapter 1, w e describ ed an ob ject-orien ted program as a univ erse of in teracting agen ts. Ho w ev er, in our �rst example Ja v a program, in Chapter 4, w e did not actually create an y new ob jects, but only used the static pro cedure named main in the program class. Our second program is sligh tly more complex in structure, although hardly more com- plicated in functionalit y . It places a graphical windo w on the user's screen, dra ws a ball that b ounces around the windo w for a few momen ts, and then halts. Our second example program, or paradigm, is constructed out of t w o classes. The �rst of these app ears in Figure 5.1. Again, w e ha v e added line n um b ers for the purp oses of 75
76 CHAPTER 5. BALL W ORLDS 1 reference, ho w ev er these are not part of the actual program. The reader should compare this program to the example program describ ed in the previous c hapter, noting b oth the similarities and di�erences. Lik e the previous program, this program imp orts (on line 1) information from the Ja v a library . Lik e the earlier program, execution will b egin in the pro cedure named main , (lines 3 through 6), whic h is declared as static , void and public . Lik e all main programs, this pro cedure m ust tak e as argumen t an arra y of string v alues, whic h are, in this case, b eing ignored. Ho w ev er, this program also incorp orates a n um b er of new features. These are summa- rized b y the follo wing list, and will b e the sub ject of more detailed discussion in subsequen t sections. � The class de�nes a n um b er of priv ate in ternal v ariable �elds, some of whic h are con- stan t, some of whic h are initialized but not constan t, and some of whic h are not initialized. These data �elds will b e describ ed in detail in Section 5.1. � The main program creates an instance of the class BallW o rld . This ob ject is initialized b y means of a c onstructor . A constructor is a function that automatically ties together the actions of ob ject cr e ation and ob ject initialization . Constructors will b e in tro duced in Section 5.2. � The class is declared as an extension of an existing Ja v a class named F rame . This tec hnique is called inheritanc e , and is the principal means in ob ject-orien ted languages for constructing new soft w are abstractions that are v ariations on existing data t yp es. Inheritance will b e in tro duced in Section 5.3, and will b e more extensiv ely studied b eginning in Chapter 8. � The output displa y ed in a windo w b y this program is created using some of the graph- ics primitiv es pro vided b y the Ja v a run-time library . These graphics op erators are explained in Section 5.4. 5.1 Data Fields W e ha v e seen in the previous c hapter (Section 4.4) ho w data �elds can b e declared within a class and ho w they can b e initialized. The example program here includes features w e ha v e not seen in our previous programs in the four data �elds declared on lines 7 to 10: public static final int FrameWidth = 600; // 7 public static final int FrameHeight = 400; // 8 private Ball aBall; // 9 1 In order to dra w more atten tion to the Ja v a co de itself, the programs presen ted in this text ha v e purp osely b een written using v ery few commen ts. In practice commen ts w ould usually b e used to describ e eac h function in a class.
5.1. D A T A FIELDS 77 java.awt. � ; 1 import // f 2 public class BallWorld extends Frame // f 3 public static void main (String [ ] args) // BallWorld world = new BallWorld (Color.red); // 4 world.show (); // 5 g // 6 public static final int FrameWidth = 600; // 7 public static final int FrameHeight = 400; // 8 private Ball aBall; // 9 private int counter = 0; // 10 private BallWorld (Color ballColor) f // constructor for new windo w 11 // resize our frame, initialize title 12 setSize (FrameWidth, FrameHeight); // 13 setTitle ("Ball World"); // 14 // initialize aBall data �eld 15 aBall = new Ball (10, 15, 5); // 16 aBall.setColor (ballColor); // 17 aBall.setMotion (3.0, 6.0); // 18 g // 19 f 20 public void paint (Graphics g) // �rst, dra w the ball 21 // 22 aBall.paint (g); // then mo v e it sligh tly 23 // aBall.move(); // 24 if ((aBall.x() < 0) jj (aBall.x() > FrameWidth)) // 25 aBall.setMotion (-aBall.xMotion() , aBall.yMotion() ); // 26 if ((aBall.y() < 0) jj (aBall.y() > FrameHeight)) // 27 aBall.setMotion (aBall.xMotion(), -aBall.yMotion() ); // 28 // �nally , redra w the frame 29 counter = counter + 1; // 30 if (counter < 2000) repaint(); // 31 else System.exit(0); // 32 g // 33 g // 34 Figure 5.1: Class Description for Ball W orld
78 CHAPTER 5. BALL W ORLDS 10 private int counter = 0; // Recall that the k eyw ord public means that the v ariables b eing declared can b e accessed (that is, used directly) an ywhere in a Ja v a program, while those that are declared as p rivate can b e used only within the b ounds of the class description in whic h the declaration app ears. Recall also that the k eyw ord static means that there is one instance of the data �eld, shared b y all instances of the class. The mo di�er k eyw ord �nal generally means that this is the last time when an ob ject is c hanged. It is here applied to a v ariable declaration; w e will in later c hapters see ho w the mo di�er can also b e applied to a function de�nition. When used with a v ariable declaration, the declaration m ust also include an initialization, as sho wn here. V ariables that are static and �nal are used to create sym b olic names for constan ts, as they are v ariables that are guaran teed to exist only in one place, and not c hange v alues. Because they cannot b e mo di�ed, there is less reason to encapsulate a static �nal v ariable b y declaring it p rivate . Th us, suc h v alues are often made public , as sho wn here. The particular sym b olic v alues b eing de�ned in this program represen t the heigh t and width of the windo w in whic h the application will ev en tually pro duce its output. Sym b olic constan ts are useful in programs for a n um b er of di�eren t reasons: � By b eing de�ned in only one place, they mak e it easy to subsequen tly c hange, should circumstances require. F or example, c hanging the heigh t and or width of the windo w merely requires editing the �le to c hange the v alues b eing used to initialize these sym b olic constan ts, rather than h un ting do wn all lo cations in the co de where the quan tities are used. � When subsequen tly used elsewhere in the program, the sym b olic name helps do cumen t the purp ose of the constan t v alues. The counter data �eld is an in teger v alue, initialized to zero: private int counter = 0; // 10 Because the �eld is declared p rivate w e kno w it can b e used only within the b ounds of the class de�nition. Because it w as not declared static , w e kno w that eac h instance of the class will hold its o wn di�eren t v alue. Because it w as not declared as �nal w e kno w that the v alue b eing assigned is simply the initial v alue the v ariable will hold, but that it subsequen tly could b e reassigned. W e will see ho w this v ariable is used when w e discuss the graphical asp ects of the curren t program. private Ball aBall; // 9 The �nal data �eld is declared as an instance of class Ball , whic h is the second class used in the creation of our example program. A ball is an abstraction that represen ts a b ouncing ball. It is represen ted b y a colored circle that can mo v e around the displa y surface. The
Recommend
More recommend