Presentation is loading. Please wait.

Presentation is loading. Please wait.

State Instance Variables Procedures Properties Print Statements Println Vs Print Overloading J++

Similar presentations


Presentation on theme: "State Instance Variables Procedures Properties Print Statements Println Vs Print Overloading J++"— Presentation transcript:

1 State Instance Variables Procedures Properties Print Statements Println Vs Print Overloading J++

2 What-if BMI Calculations

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

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

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

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

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

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

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

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

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

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

13 Function Vs Procedure Function Procedure

14 Function Vs Procedure Function Procedure

15 Assignment Statement public void setHeight(double newHeight) { height = newHeight; } newHeight 0 height 0 variablesmemory setHeight(1.77) 1.77 LHS RHS = code that yields a value weight 1.75*weight 0.0 weight

16 Properties public class ABMISpreadsheet { double height; public double getHeight() { return height; } public void setHeight(double newHeight) { height = newHeight; } double weight; public double getWeight() { return weight; } public void setWeight(double newWeight) { weight = newWeight; } public double getBMI() { return weight/(height*height); } Height Weight BMI

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

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

19 Properties Classification public class ABMICalculator { public double calculateBMI (double weight, double height) { return weight/(height*height); }

20 Calling Getter and Setter Methods public class ABMISpreadsheet { double height; public double getHeight() { return height; } public void setHeight(double newHeight) { height = newHeight; } double weight; public double getWeight() { return weight; } public void setWeight(double newWeight) { weight = newWeight; } public double getBMI() { return weight/(height*height); }

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

22 Actual Trace

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

24 Actual Trace

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

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

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

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

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

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

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 Expressions Vs Statements Expression: Piece of code yielding value 5 “setWeight called” newHeight x*x weight/(height*height) Statement: computer instruction executed autonomously System.out.println(“seW eight called”); return x*x bmi = weight/(height*height); Expression always evaluated as part of some statement.

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


Download ppt "State Instance Variables Procedures Properties Print Statements Println Vs Print Overloading J++"

Similar presentations


Ads by Google