state instance variables procedures properties print statements println vs print overloading j++
Post on 22-Dec-2015
226 views
TRANSCRIPT
State
• Instance Variables
• Procedures
• Properties
• Print Statements
• Println Vs Print
• Overloading
• J++
What-if BMI Calculations
BMI Spreadsheet
State: Data Remembered by an Object between computations
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
State-less Vs State-ful Objects
~ car radios without presets
~ car radios with presets
Identical Instances
Different Instances
Declaring Instance Variables
public class ABMISpreadsheet {double height;...double weight; ...public double getBMI() {
return weight/(height*height);}
…}
Instance Variables
Missing Code
No Parameters
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
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
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
Coding the Methods
ObjectEditor
ABMISpreadsheet Instance
weight
setWeight()
new Weight
calls
writes
getWeight()
reads
weight calls
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
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
Function Vs Procedure
Function Procedure
Function Vs Procedure
Function Procedure
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
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
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
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
Properties Classification
public class ABMICalculator {
public double calculateBMI (double weight, double height) {
return weight/(height*height);
}
}
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);}
}
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);
}}
Actual Trace
Print Line
System.out.println(“setWeight called”); print statement
Actual Parameter
method invocation/callMethod
Name
interactive call
Target Object
programmed call
Actual Trace
Printing Weight
System.out.println(“setWeight called”);
System.out.println(newWeight);
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
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.
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
Print Vs +
public void setWeight(double newWeight) {System.out.print (“weight = “ + weight);weight = newVal;System.out.println(“weight = “ + weight);
}
Cannot use + instead of print()
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
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.
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.
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