state instance variables procedures properties print statements println vs print overloading j++

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

Post on 22-Dec-2015

235 views

Category:

Documents


0 download

TRANSCRIPT

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

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++

What-if BMI Calculations

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

BMI Spreadsheet

State: Data Remembered by an Object between computations

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

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++

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++

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++

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++

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++

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++

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++

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++

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++

Function Vs Procedure

Function Procedure

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

Function Vs Procedure

Function Procedure

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

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++

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++

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++

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++

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++

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++

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++

Actual Trace

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

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++

Actual Trace

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

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++

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++

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++

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++

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++

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++

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++

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++

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