1 / 10

Object References

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 ()

mirari
Download Presentation

Object References

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Object References Arrows and names and values oh my!

  2. 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

  3. 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(); } }

  4. 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

  5. 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.

  6. 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

  7. 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; … }

  8. 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;

  9. 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.

  10. Tracing using Object Diagrams Example joesTV = new TV(); joesTV = null; maesTV = newTV(); suesTV = maesTV; suesTV = joesTV; maesTV = new TV();

More Related