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

                234
Chapter 6
Object-Oriented Programming
45
40
35
30
25
20
15
10
5 0
              Figure 6.21
(dashed)
Step-with-midpoint simulated solution with time increment = 1 (solid) compared to exact solution
0 5 10 15
Time in weeks
more than twice as good. For example, at 4 weeks the error is now only 1.5 grams, instead of the previous 10 grams.
Summary
• An object is a group of relateAdpdata wghoich idPentDifiFes theEcunrrehntacondcitioenror state of the object plus the methods that describe the behavior of that object.
• Objects are instances of the classes which define them. A class definition specifies the instance vari- ables an object of that class contains, and it defines the methods an object of that class may call. Each object contains its own copy of the instance variables its class defines, and a given instance variable generally has different values in different objects.
• Use the private access modifier to specify that a particular variable is encapsulated or hidden. Use the public access modifier to make methods accessible to the outside world.
• To make a class as general as possible, drive it from a main method in a separate “driver” class. In the driver’s main method, declare a reference variable of the driven class’s type. Then, use Java’s keyword new to instantiate an object of the driven class, and initialize the reference variable with the object ref- erence returned by new.
• Use Java’s keyword this to refer to the calling object from within one of that object’s methods. Use this to distinguish an instance variable from a same-named parameter or local variable.
• When you trace an object-oriented program, you need to keep track of which class you’re in, which method you’re in, which object called that method, parameter and local variable names, and the names of all instance variables in each object.
• A UML class diagram has separate boxes for the class name, a description of the class’s variables, and headings for the class’s methods. Use a “+” prefix for public and a “-” prefix for private. Specify variable and method return types and non-default initial values.
• Instance variable default values are zero for numbers, false for boolean values, and null for ref- erences. Instance variable values persist for the life of their object. Local variable default values are undefined garbage. Local variables and parameters persist for as long as their method is being executed, and after that, their values are undefined.
 Weight in grams







































































   266   267   268   269   270