© 2006 Pearson Addison-Wesley. All rights reserved 2.3.1 123 456 789 0 Enter onoff channel...

9
© 2006 Pearson Addison-Wesley. All rights reserved 2.3.1 1 2 3 4 5 6 7 8 9 0 Ente r on off channel selecti on volum e Ave - + TV int channel int volume boolean isOperating «constructor» 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()

Transcript of © 2006 Pearson Addison-Wesley. All rights reserved 2.3.1 123 456 789 0 Enter onoff channel...

Page 1: © 2006 Pearson Addison-Wesley. All rights reserved 2.3.1 123 456 789 0 Enter onoff channel selection volume Ave-+ TV int channel int volume boolean isOperating.

© 2006 Pearson Addison-Wesley. All rights reserved 2.3.1

1 2 3

4 5 6

7 8 9

0 Enter

on off

channel selection

volume

Ave- +

TVint channelint volumeboolean isOperating

«constructor» 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()

Page 2: © 2006 Pearson Addison-Wesley. All rights reserved 2.3.1 123 456 789 0 Enter onoff channel selection volume Ave-+ TV int channel int volume boolean isOperating.

© 2006 Pearson Addison-Wesley. All rights reserved 2.3.2

// TV Program Example

public class Driver { private TV joesTV, suesTV, emasTV;

public Driver() { joesTV = new TV(); joesTV.turnSetOn(); joesTV.press8(); joesTV.pressEnter(); joesTV.setToAverageVolume(); }}

// TV Program Example

public class Driver { private TV joesTV, suesTV, emasTV;

public Driver() { joesTV = new TV(); joesTV.turnSetOn(); joesTV.press8(); joesTV.pressEnter(); joesTV.setToAverageVolume(); }}

TVint channelint volumeboolean isOperating

«constructor» 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()

Page 3: © 2006 Pearson Addison-Wesley. All rights reserved 2.3.1 123 456 789 0 Enter onoff channel selection volume Ave-+ TV int channel int volume boolean isOperating.

© 2006 Pearson Addison-Wesley. All rights reserved 2.3.3

A variable serves to name an object. Each variable is can be bound to no more than one object. Each variable is unbound (________) initially.

Object Diagram

At run time instances of each class are constructed (instantiated). These instances are called objects, and they occupy computer memory.

An object diagram is a picture of object bindings and valuesat some moment (run-time location).

Example

joesTV = new TV();joesTV.turnSetOn();joesTV.press8();joesTV.pressEnter();joesTV.setToAverageVolume();joesTV.turnSetOff();

joesTV

joesTV : TV

channel = = 8volume = = 10isOperating = = true

run-timelocation

Page 4: © 2006 Pearson Addison-Wesley. All rights reserved 2.3.1 123 456 789 0 Enter onoff channel selection volume Ave-+ TV int channel int volume boolean isOperating.

© 2006 Pearson Addison-Wesley. All rights reserved 2.3.4

Programmers often trace their code by simulating its execution and observing state changes..

ExamplejoesTV = new TV();joesTV.turnSetOn();joesTV.press1();joesTV.press9();joesTV.pressEnter();joesTV.setToAverageVolume();joesTV.turnSetOff();emasTV = new TV();emasTV.turnSetOn();emasTV.setToAverageVolume();emasTV.raiseVolume1db();suesTV = emasTV;suesTV.press3();suesTV.pressEnter();emasTV = joesTV;joesTV = null;

joesTV

suesTV

emasTV

Page 5: © 2006 Pearson Addison-Wesley. All rights reserved 2.3.1 123 456 789 0 Enter onoff channel selection volume Ave-+ TV int channel int volume boolean isOperating.

© 2006 Pearson Addison-Wesley. All rights reserved 2.3.5

ExamplejoesTV = new TV();joesTV.turnSetOn();joesTV.press1();joesTV.press9();joesTV.pressEnter();joesTV.setToAverageVolume();joesTV.turnSetOff();emasTV = new TV();emasTV.turnSetOn();emasTV.setToAverageVolume();emasTV.raiseVolume1db();suesTV = emasTV;suesTV.press3();suesTV.pressEnter();emasTV = joesTV;joesTV = null;

run-timelocation

joesTVemasTV : TV

channel = = 19volume = = 10isOperating = = false

suesTV

emasTVsuesTV : TV

channel = = 3volume = = 11isOperating = = true

( null )

Page 6: © 2006 Pearson Addison-Wesley. All rights reserved 2.3.1 123 456 789 0 Enter onoff channel selection volume Ave-+ TV int channel int volume boolean isOperating.

© 2006 Pearson Addison-Wesley. All rights reserved 2.3.6

When a variable is null, it cannot be used as a reference to an object (e.g., in a method call).

Null Pointer Exception

Failure to remember the rule above results in a null pointer exception, the most common of all run-time errors.

Example

joesTV = null;joesTV.turnSetOn();

When an object has no remaining program reference, it is said to be an orphan.

Orphans

Orphans don’t cause run-time errors in Java. However, the programmer should be aware that assigning a value to a variable may orphan the object previously bound to the variable. Orphans become inaccessible.

Example

joesTV = new TV();joesTV = emasTV;

Page 7: © 2006 Pearson Addison-Wesley. All rights reserved 2.3.1 123 456 789 0 Enter onoff channel selection volume Ave-+ TV int channel int volume boolean isOperating.

© 2006 Pearson Addison-Wesley. All rights reserved 2.3.7

Example

joesTV = new TV();joesTV = null;emasTV = new TV();suesTV = emasTV;suesTV = joesTV;emasTV = new TV();

joesTV

suesTV

emasTV

Page 8: © 2006 Pearson Addison-Wesley. All rights reserved 2.3.1 123 456 789 0 Enter onoff channel selection volume Ave-+ TV int channel int volume boolean isOperating.

© 2006 Pearson Addison-Wesley. All rights reserved 2.3.8

Example

joesTV = new TV();joesTV = null;emasTV = new TV();suesTV = emasTV;suesTV = joesTV;emasTV = new TV();run-time

location

joesTV

suesTV

emasTV

( null )

( null )

emasTV : TV

channel = = ?volume = = ?isOperating = = ?

: TV

channel = = ?volume = = ?isOperating = = ?

: TV

channel = = ?volume = = ?isOperating = = ?

Page 9: © 2006 Pearson Addison-Wesley. All rights reserved 2.3.1 123 456 789 0 Enter onoff channel selection volume Ave-+ TV int channel int volume boolean isOperating.

A Few Style Guidelines

2. Indent the bodies/clauses from their enclosing brackets/braces. (e.g. instance variable declarations and Driver code.)

3. An indentation distance of three or four characters is good.

1. Matching items should be aligned on the left. (e.g. public class … } )

4. Place assertions (class invariants, pre- and post-conditions) immediately before their class or method. Use /** convention for pre/postconditions.

5. Pick identifiers that are meaningful (to you and others).

6. Variable names should be nouns or noun phrases.

7. Capitalize the first letter of each word in an identifier, excepting possibly the first.

8. Capitalize the first letter of the name of a class. Names of instance variables and methods should begin with a small letter.

9. Blank lines can be included to separate code, but should be used sparingly.

10. Assertions are a good use of comments. Minimize other comments.