100 likes | 238 Views
Object References. Arrows and names and values oh my!. TV. int channel int volume boolean isOn. «constructor» void TV () «update» void turnSetOn () void turnSetOff () void pressO () void press1 () void press2 () void press3 () void press4 ()
E N D
Object References Arrows and names and values oh my!
TV intchannel intvolume booleanisOn «constructor» void TV() «update» void turnSetOn() void turnSetOff() void pressO() void press1() void press2() void press3() void press4() void press5() void press6() void press7() void press8() void press9() void pressEnter() void setToAverageVolume() void raiseVolume1db() void lowerVolume1db() off on channel selection 1 2 3 4 5 6 7 8 9 Enter 0 volume - Avg + Consider a TV Create UML Class Diagram
TV intchannel intvolume booleanisOn «constructor» void TV() «update» void turnSetOn() void turnSetOff() void pressO() void press1() void press2() void press3() void press4() void press5() void press6() void press7() void press8() void press9() void pressEnter() void setToAverageVolume() void raiseVolume1db() void lowerVolume1db() A Program // TV Program Example public class Example { private TV joesTV, suesTV, maesTV; public Example() { joesTV = new TV(); joesTV.turnSetOn(); joesTV.press8(); joesTV.pressEnter(); joesTV.setToAverageVolume(); } }
Variables and Objects • Prior to running a program (at compile time) • The programmer defines classes • The programmer defines what will happen • When the program is run (at run time) • Objects are constructed (instantiated) • They are values that occupy memory • A variable is the nameof an object • At run time can be bound to no more than one object • Each variable is unbound (null) initially • A variable is also known as an object reference
Object Diagrams • Object diagrams are a graphical means of representing the state of computation at some point in a program. • The state of an object is the value (binding) of all instance variables. • Object diagrams are a snapshot in time. • Class diagrams don’t change with time.
Object Diagrams • Similar to ‘class diagrams’ – but different! • A box is an object (not a class) • A box has two ‘slots’ • Name of class in top slot • Attributes and values in bottom slot • An arrow drawn between a name and an object represents a binding
Object Diagram example public class SomeClass { public static void main(String[] args) { TV joesTV= new TV(); joesTV.turnSetOn(); joesTV.press8(); joesTV.pressEnter(); joesTV.setToAverageVolume(); joesTV.turnSetOff(); } } joesTV TV channel == 8 volume == 10 isOperating == true public class TV { private int channel; private int volume; private booleanisOperating; … }
Object Trace • An object diagram reflects the current state of computation. • Programmers often use object diagrams to ‘think like the computer’ when ‘debugging’ code • A sequence of object diagrams is a simulation of execution. joesTV = new TV(); joesTV.turnSetOn(); joesTV.press1(); joesTV.press9(); joesTV.pressEnter(); joesTV.setToAverageVolume(); joesTV.turnSetOff(); maesTV = new TV(); maesTV.turnSetOn(); maesTV.setToAverageVolume(); maesTV.raiseVolume1db(); suesTV = maesTV; suesTV.press3(); suesTV.pressEnter(); maesTV = joesTV; joesTV = null;
OOOOPS… • NullPointerException • Occurs when an object reference is null but an attempt is made to invoke a behavior or access data joesTV = null; joesTV.turnSetOn(); joesTV = new TV(); joesTV = null; maesTV = newTV(); suesTV = maesTV; suesTV = joesTV; maesTV = new TV(); • Orphan • Occurs when an object has no references. Not necessarily an error.
Tracing using Object Diagrams Example joesTV = new TV(); joesTV = null; maesTV = newTV(); suesTV = maesTV; suesTV = joesTV; maesTV = new TV();