1 / 33

State

State. Instance Variables Procedures Properties Print Statements Println Vs Print Overloading J++. What-if BMI Calculations. State: Data Remembered by an Object between computations. BMI Spreadsheet. accesses. accesses. Belong to a single method .

ashby
Download Presentation

State

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. State • Instance Variables • Procedures • Properties • Print Statements • Println Vs Print • Overloading • J++

  2. What-if BMI Calculations

  3. State: Data Remembered by an Object between computations BMI Spreadsheet

  4. accesses accesses Belong to a single method Belong to all methods of an instance Instance Variables ABMICalculator Instance ABMISpreadsheet Instance calculateBMI getBMI Body Body Instance Variables Parameters local variable global variable

  5. Identical Instances Different Instances State-less Vs State-ful Objects ~ car radios without presets ~ car radios with presets

  6. Missing Code No Parameters Declaring Instance Variables • publicclass ABMISpreadsheet { • double height; • ... • double weight; • ... • public double getBMI() { • return weight/(height*height); • } • … • } Instance Variables

  7. Outside Access to a Class • publicclass ABMISpreadsheet { • double height; • ... • double weight; • ... • public double getBMI() { • return weight/(height*height); • } • … • } outside access Variables should not be public ObjectEditor But ObjectEditor needs their values

  8. reads reads reads writes writes getHeight() setHeight() getBMI() new Height height weight calls calls calls calls Accessing Instance Variables Via Public Methods ABMISpreadsheet Instance weight height getWeight() setWeight() new Weight ObjectEditor

  9. reads reads writes writes getHeight() setHeight() new Height height weight calls calls calls calls Coding the Methods ABMISpreadsheet Instance weight height getWeight() setWeight() new Weight ObjectEditor

  10. reads writes weight calls calls Coding the Methods ABMISpreadsheet Instance weight getWeight() setWeight() new Weight ObjectEditor

  11. reads writes function procedure weight calls calls Coding Getter and Setter Methods ABMISpreadsheet Instance publicdouble getWeight() { return weight; } weight publicvoid setWeight(double newWeight) { weight = newWeight; } getWeight() setWeight() returns nothing new Weight ObjectEditor

  12. functions procedures Getter and Setter Methods publicclass ABMISpreadsheet { double height; publicdouble getHeight() { return height; } publicvoid setHeight(double newHeight) { height = newHeight; } double weight; publicdouble getWeight() { return weight; } publicvoid setWeight(double newWeight) { weight = newWeight; } publicdouble getBMI() { return weight/(height*height); } } return nothing

  13. Procedure Function Function Vs Procedure

  14. Function Vs Procedure Function Procedure

  15. LHS RHS newHeight 0 1.77 weight 1.77 1.75*weight Assignment Statement publicvoid setHeight(double newHeight) { height = newHeight; } setHeight(1.77) code that yields a value <variable> = <expression> variables memory height 0 weight 0.0

  16. Properties publicclass ABMISpreadsheet { double height; publicdouble getHeight() { return height; } publicvoid setHeight(double newHeight) { height = newHeight; } double weight; publicdouble getWeight() { return weight; } publicvoid setWeight(double newWeight) { weight = newWeight; } publicdouble getBMI() { return weight/(height*height); } } Height Weight BMI

  17. Name: P publicclass C { Type: T Editable publicvoid setP(T newValue) { ... } Getter method Setter method } newP Violates Bean Conventions obtainP Read-Only and Editable Properties Typed, Named Unit of Exported Object State Bean public T getP() { ... } Readonly • Conventions for • humans • tools

  18. Read-Only Editable Independent Editable Independent Read-only Dependent Properties Classification publicclass ABMISpreadsheet { double height; publicdouble getHeight() { return height; } publicvoid setHeight(double newHeight) { height = newHeight; } double weight; publicdouble getWeight() { return weight; } publicvoid setWeight(double newWeight) { weight = newWeight; } publicdouble getBMI() { return weight/(height*height); } } Height Weight BMI

  19. Properties Classification publicclass ABMICalculator { publicdouble calculateBMI (double weight, double height) { return weight/(height*height); } }

  20. Calling Getter and Setter Methods publicclass ABMISpreadsheet { double height; publicdouble getHeight() { return height; } publicvoid setHeight(double newHeight) { height = newHeight; } double weight; publicdouble getWeight() { return weight; } publicvoid setWeight(double newWeight) { weight = newWeight; } publicdouble getBMI() { return weight/(height*height); } }

  21. publicclass ABMISpreadsheet { double height; publicdouble getHeight() { System.out.println (“getHeight Called”); return height; } publicvoid setHeight(double newHeight) { System.out.println (“setWeight Called”); height = newHeight; } double weight; publicdouble getWeight() { System.out.println (“getWeight Called”); return weight; } publicvoid setWeight(double newWeight) { System.out.println (“setWeight Called”); weight = newWeight; } publicdouble getBMI() { System.out.println (“getBMI Called”); return weight/(height*height); } } Tracing Method Calls

  22. Actual Trace

  23. Target Object Method Name Actual Parameter Print Line programmed call System.out.println(“setWeight called”); print statement method invocation/call interactive call

  24. Actual Trace

  25. System.out.println(“setWeight called”); System.out.println(newWeight); Printing Weight

  26. Two different words with same name Look at the airplane fly. The fly is bothering me. Two different operations with same name String Operation Definitions double publicvoid println (String val) {…} publicvoid println (double val) {…} Overloading Context of Actual Parameters System.out.println(“setWeight called”); System.out.println(newWeight);

  27. Time flies like an arrow. Operation Definitions publicvoid println (String val) {…} System.out.println(“setWeight called”); publicvoid println (String val) {…} Ambiguous Context Fruit flies like an orange.

  28. Operator Overloading System.out.print(“setWeight called: ”); System.out.println(newWeight); System.out.println("setWeight called: " + newWeight); Printing Multiple Values on One Line 5 + 6

  29. Cannot use + instead of print() Print Vs + • publicvoid setWeight(double newWeight) { • System.out.print (“weight = “ + weight); • weight = newVal; • System.out.println(“weight = “ + weight); • }

  30. Undefined variable Multiply defined variable publicclass ABMISpreadsheet { double height; publicdouble getHeight() { return height; } publicvoid setHeight(double newHeight) { height = newHeight; } double weight; publicdouble getWeight() { return weight; } publicvoid setWeight(double newWeight) { weight = newWeight; } publicdouble getBMI() { return weight/(height*height); } } Variable Declaration Errors double weight;

  31. Declarations Vs Statement Order • Order of variable and method declarations in a class does not matter in Java. • Order of statements in a method body matters. • Statements executed sequentially.

  32. Expression: Piece of code yielding value 5 “setWeight called” newHeight x*x weight/(height*height) Statement: computer instruction executed autonomously System.out.println(“seWeight called”); return x*x bmi = weight/(height*height); Expressions Vs Statements Expression always evaluated as part of some statement.

  33. ABMICalculator Instance ABMISpreadsheet Instance calculateBMI getWeight Body Body accesses accesses weight weight height Pure Vs Impure Functions setWeight(77) getWeight() 77 calculateBMI(77,1.77) 24.57 setWeight(71) ... ... calculateBMI(77,1.77) 24.57 getWeight() 71

More Related