72
COMP 110 STATE Instructor: Jason Carter

Comp 110 State

  • Upload
    mira

  • View
    34

  • Download
    0

Embed Size (px)

DESCRIPTION

Comp 110 State. Instructor: Jason Carter. Outline. Instance Variables Procedures Properties Print Statements Println vs. Print Overloading. “What if” BMI Calculations With General Purpose Calculator. Must re-enter height each time!. - PowerPoint PPT Presentation

Citation preview

Page 1: Comp 110 State

COMP 110STATE

Instructor: Jason Carter

Page 2: Comp 110 State

2

OUTLINE Instance Variables Procedures Properties Print Statements Println vs. Print Overloading

Page 3: Comp 110 State

3

“WHAT IF” BMI CALCULATIONS WITH GENERAL PURPOSE CALCULATOR

Must re-enter height each time!

Page 4: Comp 110 State

4

“WHAT IF” BMI CALCULATIONS WITH SPECIALIZED CALCULATOR

public double calculateMyBMI(double weight) { final double MY_HEIGHT = 1.77; return (new ABMICalculator).calculateBMI(weight, MY_HEIGHT);}

Must only enter the weightBut the height is hardwired! Must create a

separate class for each user!

General purpose solution that does not require re-entry of height each time?

Page 5: Comp 110 State

5

Caculate two BMIs using one instance of ABMISpreadsheet and changing only the weight

BMI SPREADSHEET

State: Data remembered by an object between

computations

Page 6: Comp 110 State

6

INSTANCE VARIABLESABMICalculator Instance

calculateBMI

Parameters

Body

accesses

ABMISpreadsheet Instance

getBMI

InstanceVariables

Body accesses

Belong to a single method

Local variable

Belong to all methods of an instance

Global variable

Page 7: Comp 110 State

7

STATE-LESS VS. STATE-FULL OBJECTS

Identical Instances ~ car radios with no presets

Different Instances ~ car radios with presets

Page 8: Comp 110 State

8

DECLARING INSTANCE VARIABLES

public class ABMISpreadsheet {double height;...double weight; ...public double getBMI() {

return weight/(height*height);}

…}

Missing Code

No Parameters

Instance Variables

Page 9: Comp 110 State

9

OBJECT ACCESS TO A CLASS

public class ABMISpreadsheet {double height;...double weight; ...public double getBMI() {

return weight/(height*height);}

…}

ObjectEditorOutside Access:

Variables should not be public

But ObjectEditor needs their values

Page 10: Comp 110 State

10

ACCESSING INSTANCE VARIABLES VIA PUBLIC METHODS

ABMISpreadsheet Instance

weight height

getWeight()

setWeight()

getHeight()

setHeight() getBMI()

ObjectEditor

calls

writes

weight

calls

reads

height

calls

reads writes

callsnew

weight

new height

reads

Page 11: Comp 110 State

11

CODING GETTER AND SETTER METHODS

ABMISpreadsheet Instance

weight

getWeight()

setWeight()

ObjectEditor

calls

writes

weight

calls

reads

new weight

Page 12: Comp 110 State

12

FUNCTION VS. PROCEDURE

procedure: deposit

function: withdraw

Page 13: Comp 110 State

13

CODING GETTER AND SETTER METHODS

ABMISpreadsheet Instance

weight

getWeight()

setWeight()

ObjectEditor

calls

writes

weight

calls

reads

new weight

public double getWeight(){ return weight;}

public void setWeight(double newWeight){ weight = newWeight;}

procedure – returns nothing

function

Page 14: Comp 110 State

14

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);}

}

procedure – returns nothing

function

Page 15: Comp 110 State

15

ASSIGNMENT STATEMENT

public void setHeight(double newHeight) { height = newHeight;}

variables memory

heightweight 0.0

0.0

setHeight(1.77)

newheight 0.01.77

1.77

=<variable> <expression>

Code that yields a value

height1.75*height

RHSLHS

Page 16: Comp 110 State

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);}

}

Page 17: Comp 110 State

17

READ-ONLY AND EDITABLE PROPERTIES

public class C{

}

public T getP() { ...}

public void setP(T newValue) { ...}

Typed, Named Unit of Exported Object State

Name P

Type T

Read-only

Editable

Getter methodSetter method

newPobtainP Violates Bean convention

Bean

Bean convention:For humans

and tools

Page 18: Comp 110 State

18

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

Page 19: Comp 110 State

19

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

EditableIndependent

EditableIndependent

Read-onlyDependent

Page 20: Comp 110 State

20

PROPERTIES CLASSIFICATION

public class ABMICalculator { public double calculateBMI (double weight,

double height) {return weight/ (height * height);

}}

No Properties

Page 21: Comp 110 State

21

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); }}

Page 22: Comp 110 State

22

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(“setHeight 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 23: Comp 110 State

23

ACTUAL TRACE

Extra getWeight() call made by the undo-redo mechanism in ObjectEditor

Load

Change weight

Page 24: Comp 110 State

24

PRINT LINE

System.out.println(“setWeight Called”);

Target Object Method Name Actual Parameters

Programmed Call

Interactive Call

Page 25: Comp 110 State

25

ACTUAL TRACE

Page 26: Comp 110 State

26

PRINTING WEIGHT

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

Page 27: Comp 110 State

27

PRINTING WEIGHT

System.out.print(“setWeight called: ”);System.out.println(newWeight);

Page 28: Comp 110 State

28

PRINTING WEIGHT

System.out.println(“setWeight called: ” + newWeight);

Page 29: Comp 110 State

29

PRINT VS. + (EDIT)

public void setWeight(double newWeight) {System.out.print (“old weight = “ +

weight);weight = newWeight;System.out.println(“ new weight = “ +

weight);}

Cannot use + instead of print()

public void setWeight(double newWeight) {System.out.println( “old weight = “ + weight + “ new

weight = “ + newWeight );weight = newWeight;

}

Page 30: Comp 110 State

30

PRINT VS. +

public void setWeight(double newWeight) {System.out.print (“old weight = “ +

weight);weight = newVal;System.out.println(“ new weight = “ +

weight);}

Cannot use + instead of print()

public void setWeight(double newWeight) {System.out.println( “old weight = “ + weight +

“ new weight = “ + newWeight);weight = newVal;

}

We will later see examples when you cannot use +

Page 31: Comp 110 State

31

RECAPPING THE STORY: CLASS DECLARATIONClass

ABMISpreadsheet Class XyZ must be stored in XyZ.java

File Name:

<class header>

<class body>

public class ABMISpreadsheetSpelling and use of upper and

lower case letters is important!

Class Header Info:Is this a class?Is it accessible to other

classes?What is the name of the

class?

Page 32: Comp 110 State

32

RECAPPING THE STORY: CLASS BODYClass

ABMISpreadsheet<class header>

<class body>

public class ABMISpreadsheet

Class Body Info:How can the class be used?

(i.e. methods)Instance variables

Curly braces after class header declare start of class

body

Curly braces at the very end declare end of class body

{

double height; ... double weight; ... public double setBMI() { … }

}

Page 33: Comp 110 State

33

RECAPPING THE STORY: DECLARATIONSdouble 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);}

<class body> Declarations:Declaring instance variables

Declaring methodsDeclaration order:Does not matter:- e.g. declare a method before or after any instance variables it accesses- e.g. can declare height and weight instance variables at the bottom of the class- e.g. declare methods in any orderDeclaration terminator:

Instance variables: “;”Methods: end of method body

Page 34: Comp 110 State

34

RECAPPING THE STORY: VARIABLESdouble 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);}

<class body>

Variable declaration:Must have variable type and

variable name

Kinds of variables:Formal parameter

Internal method variableNamed constantInstance variable

Usefulness of each kind of variable?

Page 35: Comp 110 State

35

RECAPPING THE STORY: METHODSdouble 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);}

<class body>

Method header info:Is it accessible to other

objects?Return type

NameParameter list

Kinds of methods:Functions

Procedures

Parameter list:Enclosed in brackets

Comma separated list of formal parameters

Page 36: Comp 110 State

36

RECAPPING THE STORY: METHOD BODY

double weight;…public double getWeight() { return weight;}…public void setWeight(double newWeight) { System.out.print ( “old weight = “ + weight); weight = newVal; System.out.println( “ new weight = “ + weight);}

<class body> Method body:Sequence of one or more “;”

terminated statements

Curly braces after class header declare start of class

body

Curly braces at the very end declare end of class body

Page 37: Comp 110 State

37

RECAPPING THE STORY: STATEMENTS

double weight;…public double getWeight() { return weight;}…public void setWeight(double newWeight) { System.out.print ( “old weight = “ + weight); weight = newVal; System.out.println( “ new weight = “ + weight);}

<class body> Statements order:Order matters:

Statements executed in sequence

Kinds of statements :Return statements

Assignment statementsPrint statements

Statements is a:

An instruction that the computer must execute

Page 38: Comp 110 State

38

RECAPPING THE STORY: STATEMENTS

double weight;…public double getWeight() { return weight;}…public void setWeight(double newWeight) { System.out.print ( “old weight = “ + weight); weight = newVal; System.out.println( “ new weight = “ + weight);}

<class body> Return statement:return <expression>;

Assignment statement :

<variable> = <expression>;

Print statement :System.out.println( <expres

sion>);

Page 39: Comp 110 State

39

RECAPPING THE STORY: 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(“

setWeight called”); return x*x bmi =

weight/(height*height);

Expression always evaluated as part of some statement.

Page 40: Comp 110 State

40

RECAPPING THE STORY: EVALUATING EXPRESSIONS

X = (a+b) + (c*(d+e)/(f+g))Mathematical expression

Look for a closing brace and evaluate the sub-expression

(a+b) + (f+g)/(d+e)c *

Page 41: Comp 110 State

41calculateBMI( , ) toKgs

(new ABMICalculator )

RECAPPING THE STORY: EVALUATING EXPRESSIONS

public class APoundInchBMICalculator {

public double calculateBMI( double weightInLbs, double heightInInches) {

return (new ABMICalculator()).calculateBMI( toKgs(weightInLbs), toMetres(heightInInches)); }

public double toMetres(double heightInInches) { … } public double toKgs(double weightInLbs) { … }}

APoundInchBMICalculator

( )

(weightInLbs) toMetres (heightInInches

)

Page 42: Comp 110 State

42

RECAPPING THE STORY: INTERNAL VS. EXTERNAL METHOD CALLS

public class APoundInchBMICalculator {

public double calculateBMI( double weightInLbs, double heightInInches) {

return (new ABMICalculator()).calculateBMI( toKgs(weightInLbs), toMetres(heightInInches)); }

public double toMetres(double heightInInches) { … } public double toKgs(double weightInLbs) { … }}

APoundInchBMICalculator

ExternalCaller and callee

methods are in different objectsMust specify target object

InternalCaller and callee methods

are in the same objectTarget object is implicit

(this)

Actual parameters:

<expression>

Page 43: Comp 110 State

43

OVERLOADING

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

public void println(String val) {…}public void println(double val) {…}

Operation Definitions

Context of actual parameters

Two different operations with the same name

Stringdoubl

e

Two different words with the

same nameLook at that plane fly.

The fly is bothering me.

Page 44: Comp 110 State

44

AMBIGUOUS CONTEXT

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

public void println(String val) {…}public void println(String val) {…}

Time flies like an arrow.

Fruit flies like an orange.

Operation Definitions

Java cannot use context to disambiguate

Defining two versions of a method ? Why is overloading useful?

Page 45: Comp 110 State

45

PRINTING MULTIPLE VALUES ON ONE LINE

System.out.println(“setWeight called”);

System.out.println(newWeight);

System.out.println(“setWeight called” + newWeight);5 + 6

Operator Overloading

Page 46: Comp 110 State

46

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); }}

Page 47: Comp 110 State

47

VARIABLE DECLARATION ERRORSpublic class ABMISpreadsheet { 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

Page 48: Comp 110 State

48

VARIABLE DECLARATION ERRORSpublic class ABMISpreadsheet { double weight; 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); }}

Multiply defined variable

Page 49: Comp 110 State

49

PURE VS. IMPURE FUNCTIONSABMICalculator Instance

weight

Body

accesses

ABMISpreadsheet Instance

getWeight

weight

Body accesses

height

calculateBMI

calculateBMI(77,1.77)

calculateBMI(77,1.77)24.57

24.57...

getWeight()

getWeight()

setWeight(77)77

71

...setWeight(71)

Page 50: Comp 110 State

50

STATE-LESS OBJECTEDITOR

Page 51: Comp 110 State

51

STATE-FULL OBJECTEDITOR

Page 52: Comp 110 State

52

STATE-FULL OBJECTEDITOR

Page 53: Comp 110 State

53

STATE-FULL OBJECTEDITOR

Page 54: Comp 110 State

54

GETTING ALL CLASSES IN CURRENT DIRECTORY

Page 55: Comp 110 State

55

INCONSISTENT BMI STATE

(new ABMISpreadsheet());

Page 56: Comp 110 State

56

FIXING INCONSISTENT BMI STATEABMISpreadsheet aBMISpreadsheet = new ABMISpreadsheet();aBMISpreadsheet.setHeight(1.77);aBMISpreadsheet.setWeight(75.0);

Page 57: Comp 110 State

57

CONSISTENT BMI STATEABMISpreadsheet aBMISpreadsheet = new ABMISpreadsheet( 1.77, 75.0);

Page 58: Comp 110 State

58

CONSTRUCTORpublic class ABMISpreadsheet { double height, weight; public ABMISpreadsheet( double theInitialHeight, double theInitialWeight) { setHeight(theInitialHeight); setWeight(theInitialWeight); }

public double getHeight() { return height; } public void setHeight(double newHeight) { height = newHeight; } public double getWeight() { return weight; } public void setWeight(double newWeight) { weight = newWeight; } public double getBMI() { return weight/(height*height); }}

Calling setter methods instead

of modifying variable directly

makes debugging easier

Page 59: Comp 110 State

59

CONSTRUCTORpublic class ABMISpreadsheet { double height, weight; public ABMISpreadsheet( double theInitialHeight, double theInitialWeight) { setHeight(theInitialHeight); setWeight(theInitialWeight); }

public double getHeight() { return height; } public void setHeight(double newHeight) { height = newHeight; } public double getWeight() { return weight; } public void setWeight(double newWeight) { weight = newWeight; } public double getBMI() { return weight/(height*height); }}

Constructor name must be the name

of the class

Constructor name is also the type of object returned

Page 60: Comp 110 State

60

INSTANTIATING A CLASS WITH A CONSTRUCTOR

Page 61: Comp 110 State

61

SETTING THE CONSTRUCTOR PARAMETERS

Page 62: Comp 110 State

62

INITIALIZED OBJECT STATE

Page 63: Comp 110 State

63

EVERY CLASS HAS A CONSTRUCTOR

public class ABMISpreadsheet { double height, weight;

public double getHeight() { return height; } public void setHeight(double newHeight) { height = newHeight; } public double getWeight() { return weight; } public void setWeight(double newWeight) { weight = newWeight; } public double getBMI() { return weight/(height*height); }}

Page 64: Comp 110 State

64

EQUIVALENT CLASS CREATED BY JAVA

public class ABMISpreadsheet { double height, weight;

public ABMISpreadsheet() { }

public double getHeight() { return height; } public void setHeight(double newHeight) { height = newHeight; } public double getWeight() { return weight; } public void setWeight(double newWeight) { weight = newWeight; } public double getBMI() { return weight/(height*height); }}

If Programmer Specifies no

Constructor, Java inserts a null constructor

Inserted In Object Code not in Source Code

Page 65: Comp 110 State

65

OVERLOADING THE CONSTRUCTOR

public class ABMISpreadsheet { double height, weight;

public ABMISpreadsheet() { }

public ABMISpreadsheet( double theInitialHeight, double theInitialWeight) { setHeight(theInitialHeight); setWeight(theInitialWeight); }

…}

Null constructor has been

manually added

Page 66: Comp 110 State

66

INSTANTIATING A CLASS WITH MULTIPLE CONSTRUCTORS

Page 67: Comp 110 State

67

INSTANTIATING A CLASS WITH MULTIPLE CONSTRUCTORS

Page 68: Comp 110 State

68

INSTANTIATING A CLASS WITH MULTIPLE CONSTRUCTORS

Page 69: Comp 110 State

69

INSTANTIATING A CLASS WITH MULTIPLE CONSTRUCTORS

Page 70: Comp 110 State

70

INSTANTIATING A CLASS WITH MULTIPLE CONSTRUCTORS

Page 71: Comp 110 State

71

INSTANTIATING A CLASS WITH MULTIPLE CONSTRUCTORS

Page 72: Comp 110 State

72

INSTANTIATING A CLASS WITH MULTIPLE CONSTRUCTORS