Presentation is loading. Please wait.

Presentation is loading. Please wait.

C OMP 110 S TATE Instructor: Jason Carter. 2 O UTLINE Instance Variables Procedures Properties Print Statements Println vs. Print Overloading.

Similar presentations


Presentation on theme: "C OMP 110 S TATE Instructor: Jason Carter. 2 O UTLINE Instance Variables Procedures Properties Print Statements Println vs. Print Overloading."— Presentation transcript:

1 C OMP 110 S TATE Instructor: Jason Carter

2 2 O UTLINE Instance Variables Procedures Properties Print Statements Println vs. Print Overloading

3 3 “W HAT IF ” BMI C ALCULATIONS W ITH G ENERAL P URPOSE C ALCULATOR Must re-enter height each time!

4 4 “W HAT IF ” BMI C ALCULATIONS W ITH S PECIALIZED C ALCULATOR public double calculateMyBMI( double weight) { final double MY_HEIGHT = 1.77; return ( new ABMICalculator).calculateBMI(weight, MY_HEIGHT); } Must only enter the weight But 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?

5 5 Caculate two BMIs using one instance of ABMISpreadsheet and changing only the weight BMI S PREADSHEET State: Data remembered by an object between computations

6 6 I NSTANCE V ARIABLES ABMICalculator Instance calculateBMI Parameters Body accesses ABMISpreadsheet Instance getBMI Instance Variables Instance Variables Body accesses Belong to a single method Local variable Belong to all methods of an instance Global variable

7 7 S TATE - LESS VS. S TATE - FULL O BJECTS Identical Instances ~ car radios with no presets Different Instances ~ car radios with presets

8 8 D ECLARING I NSTANCE V ARIABLES public class ABMISpreadsheet { double height;... double weight;... public double getBMI() { return weight/(height*height); } … } Missing Code No Parameters Instance Variables

9 9 O BJECT A CCESS TO A C LASS public class ABMISpreadsheet { double height;... double weight;... public double getBMI() { return weight/(height*height); } … } ObjectEditor Outside Access: Variables should not be public But ObjectEditor needs their values

10 10 A CCESSING I NSTANCE V ARIABLES VIA P UBLIC M ETHODS ABMISpreadsheet Instance weight height getWeight()setWeight()getHeight()setHeight()getBMI() ObjectEditor calls writes weight calls reads height calls readswrites calls new weight new height reads

11 11 C ODING G ETTER AND S ETTER M ETHODS ABMISpreadsheet Instance weight getWeight()setWeight() ObjectEditor calls writes weight calls reads new weight

12 12 F UNCTION VS. P ROCEDURE procedure: deposit function: withdraw

13 13 C ODING G ETTER AND S ETTER M ETHODS 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

14 14 G ETTER AND S ETTER M ETHODS 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); } procedure – returns nothing function

15 15 A SSIGNMENT S TATEMENT public void setHeight( double newHeight) { height = newHeight; } variablesmemory height weight0.0 setHeight(1.77) new height 0.01.77 = Code that yields a value height 1.75*height RHSLHS

16 16 P ROPERTIES 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); }

17 17 R EAD - ONLY AND E DITABLE P ROPERTIES public class C { } public T getP() {... } public void setP(T newValue) {... } Typed, Named Unit of Exported Object State Name P Type T Name P Type T Read-only Editable Getter method Setter method newP obtainP Violates Bean convention Bean Bean convention: For humans and tools

18 18 P ROPERTIES 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

19 19 P ROPERTIES C LASSIFICATION 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 Independent Editable Independent Read-only Dependent

20 20 P ROPERTIES C LASSIFICATION public class ABMICalculator { public double calculateBMI ( double weight, double height) { return weight/ (height * height); } No Properties

21 21 C ALLING G ETTER AND S ETTER M ETHODS 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); }

22 22 T RACING M ETHOD C ALLS public 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); }

23 23 A CTUAL T RACE Extra getWeight() call made by the undo-redo mechanism in ObjectEditor Load Change weight

24 24 P RINT L INE System.out.println(“setWeight Called”); Target ObjectMethod NameActual Parameters Programmed Call Interactive Call

25 25 A CTUAL T RACE

26 26 P RINTING W EIGHT System.out.println(“setWeight called”); System.out.println(newWeight);

27 27 P RINTING W EIGHT System.out.print(“setWeight called: ”); System.out.println(newWeight);

28 28 P RINTING W EIGHT System.out.println(“setWeight called: ” + newWeight);

29 29 PRINT VS. + (E DIT ) 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; }

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

31 31 R ECAPPING THE S TORY : C LASS D ECLARATION Class ABMISpreadsheet Class XyZ must be stored in XyZ.java File Name: public class ABMISpreadsheet Spelling 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?

32 32 R ECAPPING THE S TORY : C LASS B ODY Class ABMISpreadsheet 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() { … }

33 33 R ECAPPING THE S TORY : D ECLARATIONS 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); } Declarations: Declaring instance variables Declaring methods Declaration 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 order Declaration terminator: Instance variables: “;” Methods: end of method body

34 34 R ECAPPING THE S TORY : V ARIABLES 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); } Variable declaration: Must have variable type and variable name Kinds of variables: Formal parameter Internal method variable Named constant Instance variable Usefulness of each kind of variable?

35 35 R ECAPPING THE S TORY : M ETHODS 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); } Method header info: Is it accessible to other objects? Return type Name Parameter list Kinds of methods: Functions Procedures Parameter list: Enclosed in brackets Comma separated list of formal parameters

36 36 R ECAPPING THE S TORY : M ETHOD B ODY 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); } 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

37 37 R ECAPPING THE S TORY : S TATEMENTS 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); } Statements order: Order matters: Statements executed in sequence Kinds of statements : Return statements Assignment statements Print statements Statements is a: An instruction that the computer must execute

38 38 R ECAPPING THE S TORY : S TATEMENTS 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); } Return statement: return ; Assignment statement : = ; Print statement : System.out.println( );

39 39 R ECAPPING THE S TORY : E XPRESSIONS VS. S TATEMENTS 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.

40 40 R ECAPPING THE S TORY : E VALUATING E XPRESSIONS 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 *

41 41 calculateBMI(, ) toKgs ( new ABMICalculator ) R ECAPPING THE S TORY : E VALUATING E XPRESSIONS 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)

42 42 R ECAPPING THE S TORY : I NTERNAL VS. E XTERNAL M ETHOD C ALLS 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 External Caller and callee methods are in different objects Must specify target object Internal Caller and callee methods are in the same object Target object is implicit ( this ) Actual parameters:

43 43 O VERLOADING 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 String double Two different words with the same name Look at that plane fly. The fly is bothering me.

44 44 A MBIGUOUS C ONTEXT System.out.println(“setWeight called”); System.out.println(newWeight); 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?

45 45 P RINTING M ULTIPLE V ALUES ON O NE L INE System.out.println(“setWeight called”); System.out.println(newWeight); System.out.println(“setWeight called” + newWeight); 5 + 6 Operator Overloading

46 46 V ARIABLE D ECLARATION E RRORS 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); }

47 47 V ARIABLE D ECLARATION E RRORS public 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

48 48 V ARIABLE D ECLARATION E RRORS public 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

49 49 P URE VS. I MPURE F UNCTIONS ABMICalculator Instance weight Body accesses ABMISpreadsheet Instance getWeight weight Body accesses height calculateBMI calculateBMI(77,1.77) 24.57... getWeight() setWeight(77) 77 71...setWeight(71)

50 50 S TATE - LESS O BJECT E DITOR

51 51 S TATE - FULL O BJECT E DITOR

52 52 S TATE - FULL O BJECT E DITOR

53 53 S TATE - FULL O BJECT E DITOR

54 54 G ETTING ALL C LASSES IN C URRENT D IRECTORY

55 55 I NCONSISTENT BMI S TATE (new ABMISpreadsheet());

56 56 F IXING I NCONSISTENT BMI S TATE ABMISpreadsheet aBMISpreadsheet = new ABMISpreadsheet(); aBMISpreadsheet.setHeight(1.77); aBMISpreadsheet.setWeight(75.0);

57 57 C ONSISTENT BMI S TATE ABMISpreadsheet aBMISpreadsheet = new ABMISpreadsheet( 1.77, 75.0);

58 58 C ONSTRUCTOR public 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

59 59 C ONSTRUCTOR public 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

60 60 I NSTANTIATING A C LASS WITH A C ONSTRUCTOR

61 61 S ETTING THE C ONSTRUCTOR P ARAMETERS

62 62 I NITIALIZED O BJECT S TATE

63 63 E VERY C LASS HAS A C ONSTRUCTOR 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); }

64 64 E QUIVALENT C LASS C REATED BY J AVA 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

65 65 O VERLOADING THE C ONSTRUCTOR public class ABMISpreadsheet { double height, weight; public ABMISpreadsheet() { } public ABMISpreadsheet( double theInitialHeight, double theInitialWeight) { setHeight(theInitialHeight); setWeight(theInitialWeight); } … } Null constructor has been manually added

66 66 I NSTANTIATING A C LASS WITH M ULTIPLE C ONSTRUCTORS

67 67 I NSTANTIATING A C LASS WITH M ULTIPLE C ONSTRUCTORS

68 68 I NSTANTIATING A C LASS WITH M ULTIPLE C ONSTRUCTORS

69 69 I NSTANTIATING A C LASS WITH M ULTIPLE C ONSTRUCTORS

70 70 I NSTANTIATING A C LASS WITH M ULTIPLE C ONSTRUCTORS

71 71 I NSTANTIATING A C LASS WITH M ULTIPLE C ONSTRUCTORS

72 72 I NSTANTIATING A C LASS WITH M ULTIPLE C ONSTRUCTORS


Download ppt "C OMP 110 S TATE Instructor: Jason Carter. 2 O UTLINE Instance Variables Procedures Properties Print Statements Println vs. Print Overloading."

Similar presentations


Ads by Google