Download - State Instance Variables Procedures Properties Print Statements Println Vs Print Overloading J++
![Page 1: State Instance Variables Procedures Properties Print Statements Println Vs Print Overloading J++](https://reader035.vdocuments.net/reader035/viewer/2022062314/56649d7d5503460f94a5f9b0/html5/thumbnails/1.jpg)
State
• Instance Variables
• Procedures
• Properties
• Print Statements
• Println Vs Print
• Overloading
• J++
![Page 2: State Instance Variables Procedures Properties Print Statements Println Vs Print Overloading J++](https://reader035.vdocuments.net/reader035/viewer/2022062314/56649d7d5503460f94a5f9b0/html5/thumbnails/2.jpg)
What-if BMI Calculations
![Page 3: State Instance Variables Procedures Properties Print Statements Println Vs Print Overloading J++](https://reader035.vdocuments.net/reader035/viewer/2022062314/56649d7d5503460f94a5f9b0/html5/thumbnails/3.jpg)
BMI Spreadsheet
State: Data Remembered by an Object between computations
![Page 4: State Instance Variables Procedures Properties Print Statements Println Vs Print Overloading J++](https://reader035.vdocuments.net/reader035/viewer/2022062314/56649d7d5503460f94a5f9b0/html5/thumbnails/4.jpg)
Instance VariablesABMICalculator Instance
calculateBMI
Parameters
Body
accesses
ABMISpreadsheet Instance
getBMI
InstanceVariables
Body accesses
Belong to a single method
Belong to all methods of an instance
local variable global variable
![Page 5: State Instance Variables Procedures Properties Print Statements Println Vs Print Overloading J++](https://reader035.vdocuments.net/reader035/viewer/2022062314/56649d7d5503460f94a5f9b0/html5/thumbnails/5.jpg)
State-less Vs State-ful Objects
~ car radios without presets
~ car radios with presets
Identical Instances
Different Instances
![Page 6: State Instance Variables Procedures Properties Print Statements Println Vs Print Overloading J++](https://reader035.vdocuments.net/reader035/viewer/2022062314/56649d7d5503460f94a5f9b0/html5/thumbnails/6.jpg)
Declaring Instance Variables
public class ABMISpreadsheet {double height;...double weight; ...public double getBMI() {
return weight/(height*height);}
…}
Instance Variables
Missing Code
No Parameters
![Page 7: State Instance Variables Procedures Properties Print Statements Println Vs Print Overloading J++](https://reader035.vdocuments.net/reader035/viewer/2022062314/56649d7d5503460f94a5f9b0/html5/thumbnails/7.jpg)
Outside Access to a Class
public class ABMISpreadsheet {double height;...double weight; ...public double getBMI() {
return weight/(height*height);}
…}
ObjectEditor
Variables should not be publicBut ObjectEditor needs their values
outside access
![Page 8: State Instance Variables Procedures Properties Print Statements Println Vs Print Overloading J++](https://reader035.vdocuments.net/reader035/viewer/2022062314/56649d7d5503460f94a5f9b0/html5/thumbnails/8.jpg)
Accessing Instance Variables Via Public Methods
ObjectEditor
ABMISpreadsheet Instance
weight height
getBMI()
reads
setWeight()
new Weight
calls
writes
setHeight()
new Height
calls
writes
height
getHeight()
calls
reads
getWeight()
reads
weight calls
![Page 9: State Instance Variables Procedures Properties Print Statements Println Vs Print Overloading J++](https://reader035.vdocuments.net/reader035/viewer/2022062314/56649d7d5503460f94a5f9b0/html5/thumbnails/9.jpg)
Coding the Methods
ObjectEditor
ABMISpreadsheet Instance
weight height
setWeight()
new Weight
calls
writes
setHeight()
new Height
calls
writes
height
getHeight()
calls
reads
getWeight()
reads
weight calls
![Page 10: State Instance Variables Procedures Properties Print Statements Println Vs Print Overloading J++](https://reader035.vdocuments.net/reader035/viewer/2022062314/56649d7d5503460f94a5f9b0/html5/thumbnails/10.jpg)
Coding the Methods
ObjectEditor
ABMISpreadsheet Instance
weight
setWeight()
new Weight
calls
writes
getWeight()
reads
weight calls
![Page 11: State Instance Variables Procedures Properties Print Statements Println Vs Print Overloading J++](https://reader035.vdocuments.net/reader035/viewer/2022062314/56649d7d5503460f94a5f9b0/html5/thumbnails/11.jpg)
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
![Page 12: State Instance Variables Procedures Properties Print Statements Println Vs Print Overloading J++](https://reader035.vdocuments.net/reader035/viewer/2022062314/56649d7d5503460f94a5f9b0/html5/thumbnails/12.jpg)
Getter and Setter Methodspublic 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
![Page 13: State Instance Variables Procedures Properties Print Statements Println Vs Print Overloading J++](https://reader035.vdocuments.net/reader035/viewer/2022062314/56649d7d5503460f94a5f9b0/html5/thumbnails/13.jpg)
Function Vs Procedure
Function Procedure
![Page 14: State Instance Variables Procedures Properties Print Statements Println Vs Print Overloading J++](https://reader035.vdocuments.net/reader035/viewer/2022062314/56649d7d5503460f94a5f9b0/html5/thumbnails/14.jpg)
Function Vs Procedure
Function Procedure
![Page 15: State Instance Variables Procedures Properties Print Statements Println Vs Print Overloading J++](https://reader035.vdocuments.net/reader035/viewer/2022062314/56649d7d5503460f94a5f9b0/html5/thumbnails/15.jpg)
Assignment Statementpublic void setHeight(double newHeight) {
height = newHeight;}
newHeight 0
height 0
variables memory
setHeight(1.77)
1.77
1.77
<expression>
LHS RHS
<variable> = code that yields a value
weight
1.75*weight 0.0weight
![Page 16: State Instance Variables Procedures Properties Print Statements Println Vs Print Overloading J++](https://reader035.vdocuments.net/reader035/viewer/2022062314/56649d7d5503460f94a5f9b0/html5/thumbnails/16.jpg)
Propertiespublic 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
![Page 17: State Instance Variables Procedures Properties Print Statements Println Vs Print Overloading J++](https://reader035.vdocuments.net/reader035/viewer/2022062314/56649d7d5503460f94a5f9b0/html5/thumbnails/17.jpg)
public class C {
}
Read-Only and Editable Properties
public T getP() { ...}
Typed, Named Unit of Exported Object State
Name: P
Type: T
Readonlypublic void setP(T newValue) { ...}
Editable
newP
obtainPViolates Bean Conventions
Bean
Conventions for
•humans
•tools
Getter methodSetter method
![Page 18: State Instance Variables Procedures Properties Print Statements Println Vs Print Overloading J++](https://reader035.vdocuments.net/reader035/viewer/2022062314/56649d7d5503460f94a5f9b0/html5/thumbnails/18.jpg)
Properties Classificationpublic 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
Editable
Read-only
Independent
Independent
Dependent
Read-Only
![Page 19: State Instance Variables Procedures Properties Print Statements Println Vs Print Overloading J++](https://reader035.vdocuments.net/reader035/viewer/2022062314/56649d7d5503460f94a5f9b0/html5/thumbnails/19.jpg)
Properties Classification
public class ABMICalculator {
public double calculateBMI (double weight, double height) {
return weight/(height*height);
}
}
![Page 20: State Instance Variables Procedures Properties Print Statements Println Vs Print Overloading J++](https://reader035.vdocuments.net/reader035/viewer/2022062314/56649d7d5503460f94a5f9b0/html5/thumbnails/20.jpg)
Calling Getter and Setter Methodspublic 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);}
}
![Page 21: State Instance Variables Procedures Properties Print Statements Println Vs Print Overloading J++](https://reader035.vdocuments.net/reader035/viewer/2022062314/56649d7d5503460f94a5f9b0/html5/thumbnails/21.jpg)
Tracing Method Callspublic 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);
}}
![Page 22: State Instance Variables Procedures Properties Print Statements Println Vs Print Overloading J++](https://reader035.vdocuments.net/reader035/viewer/2022062314/56649d7d5503460f94a5f9b0/html5/thumbnails/22.jpg)
Actual Trace
![Page 23: State Instance Variables Procedures Properties Print Statements Println Vs Print Overloading J++](https://reader035.vdocuments.net/reader035/viewer/2022062314/56649d7d5503460f94a5f9b0/html5/thumbnails/23.jpg)
Print Line
System.out.println(“setWeight called”); print statement
Actual Parameter
method invocation/callMethod
Name
interactive call
Target Object
programmed call
![Page 24: State Instance Variables Procedures Properties Print Statements Println Vs Print Overloading J++](https://reader035.vdocuments.net/reader035/viewer/2022062314/56649d7d5503460f94a5f9b0/html5/thumbnails/24.jpg)
Actual Trace
![Page 25: State Instance Variables Procedures Properties Print Statements Println Vs Print Overloading J++](https://reader035.vdocuments.net/reader035/viewer/2022062314/56649d7d5503460f94a5f9b0/html5/thumbnails/25.jpg)
Printing Weight
System.out.println(“setWeight called”);
System.out.println(newWeight);
![Page 26: State Instance Variables Procedures Properties Print Statements Println Vs Print Overloading J++](https://reader035.vdocuments.net/reader035/viewer/2022062314/56649d7d5503460f94a5f9b0/html5/thumbnails/26.jpg)
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
![Page 27: State Instance Variables Procedures Properties Print Statements Println Vs Print Overloading J++](https://reader035.vdocuments.net/reader035/viewer/2022062314/56649d7d5503460f94a5f9b0/html5/thumbnails/27.jpg)
Ambiguous Context
System.out.println(“setWeight called”);
Time flies like an arrow.
public void println (String val) {…}
public void println (String val) {…}
Operation Definitions
Fruit flies like an orange.
![Page 28: State Instance Variables Procedures Properties Print Statements Println Vs Print Overloading J++](https://reader035.vdocuments.net/reader035/viewer/2022062314/56649d7d5503460f94a5f9b0/html5/thumbnails/28.jpg)
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
![Page 29: State Instance Variables Procedures Properties Print Statements Println Vs Print Overloading J++](https://reader035.vdocuments.net/reader035/viewer/2022062314/56649d7d5503460f94a5f9b0/html5/thumbnails/29.jpg)
Print Vs +
public void setWeight(double newWeight) {System.out.print (“weight = “ + weight);weight = newVal;System.out.println(“weight = “ + weight);
}
Cannot use + instead of print()
![Page 30: State Instance Variables Procedures Properties Print Statements Println Vs Print Overloading J++](https://reader035.vdocuments.net/reader035/viewer/2022062314/56649d7d5503460f94a5f9b0/html5/thumbnails/30.jpg)
Variable Declaration Errorspublic 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
![Page 31: State Instance Variables Procedures Properties Print Statements Println Vs Print Overloading J++](https://reader035.vdocuments.net/reader035/viewer/2022062314/56649d7d5503460f94a5f9b0/html5/thumbnails/31.jpg)
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.
![Page 32: State Instance Variables Procedures Properties Print Statements Println Vs Print Overloading J++](https://reader035.vdocuments.net/reader035/viewer/2022062314/56649d7d5503460f94a5f9b0/html5/thumbnails/32.jpg)
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(“seWeight called”);
• return x*x• bmi =
weight/(height*height);
Expression always evaluated as part of some statement.
![Page 33: State Instance Variables Procedures Properties Print Statements Println Vs Print Overloading J++](https://reader035.vdocuments.net/reader035/viewer/2022062314/56649d7d5503460f94a5f9b0/html5/thumbnails/33.jpg)
Pure Vs Impure FunctionsABMISpreadsheet Instance
getWeight
weight
Body accesses
calculateBMI(77,1.77)
calculateBMI(77,1.77) 24.57
24.57 getWeight()
getWeight()
setWeight(77)
77
71
......setWeight(71)
ABMICalculator Instance
calculateBMI
weight
Body
accesses
height