Presentation is loading. Please wait.

Presentation is loading. Please wait.

Object-based Programming Intuitive explanation Using objects to read input Creating objects Style rules.

Similar presentations


Presentation on theme: "Object-based Programming Intuitive explanation Using objects to read input Creating objects Style rules."— Presentation transcript:

1 Object-based Programming Intuitive explanation Using objects to read input Creating objects Style rules

2 Manufactured ObjectsNatural Objects Program Components ~ Physical Objects ~ Program Objects

3 Program Objects ~ Manufactured Objects Program Object add subtract methods execute invoke call manufactured by accelerate brake operations perform Class instance of Program Object

4 Classification through Factories manufactured by

5 Classification through Classes BufferReader Instance BufferReader instance of BufferReader Instance ABMISpreadsheet Instance ABMISpreadsheet instance of ABMISpreadsheet Instance

6 Using vs. Creating Objects Driving a car –Using BufferReader for input Building a factory –Creating ABMISpreadsheet

7 String Input

8 package main; import java.io.BufferedReader; import java.io.InputStreamReader; public class AnInputPrinter { public static void main (String[] args) { System.out.println("Please enter the line to be printed"); System.out.println ("The input was: " + readString()); } static BufferedReader inputStream = new BufferedReader(new InputStreamReader(System.in)); public static String readString() { try { return inputStream.readLine(); } catch (Exception e) { System.out.println(e); return ""; } Classifying/ typing instances Invoking operation constructing new instance

9 static BufferedReader inputStream = new BufferedReader(new InputStreamReader(System.in)); public static String readString() { try { return inputStream.readLine(); } catch (Exception e) { System.out.println(e); return ""; } Reading a String Wait for the user to enter a string on the next line. In case the user terminates input before entring a string, return “” and print an error message. Objects constructed to allow reading of strings from System.in

10 Chained construction Order light fixtures Pass them to builder Construct InputStreamReader instance Pass it as a parameter to instance of BufferedReader Understand better when we implement our own class static BufferedReader inputStream = new BufferedReader(new InputStreamReader(System.in)); Byte input streamChar tokensLine tokens

11 try { return inputStream.readLine(); } catch (Exception e) { System.out.println(e); return 0; } Try-Catch Block Exception Object Program fragment that can cause exception Exception Handler

12 public class AnInputPrinter {... … } import java.io.BufferedReader; public class AnInputPrinter { …. …... } Importing a Package new BufferedReader(…) new java.io.BufferedReader(...) package java.io; public class BufferedReader { …. } short name full name Import Declaration Package declaration makes full class name long Import declaration allows use of short name

13 Import all vs. selective import import java.io.*; import java.util.*; import all classes in java.io convenient can accidentally import and use undesired classes must look at entire code to determine what is imported do not know package of imported class import java.io.BufferedStream; import java.io.InputStreamReader; import java.util.Vector; requires more typing, specially when a large number of classes are imported (e.g. toolkit) accident importation not possible class header documents imports know package of imported class Always do selective imports

14 Integer Input

15 package main; import java.io.BufferedReader; import java.io.InputStreamReader; public class AnInputSquarer { public static void main (String[] args) { System.out.println("Please enter the integer to be squared:"); int num = readInt(); System.out.println ("The square is: " + num*num); } static BufferedReader inputStream = new BufferedReader(new InputStreamReader(System.in)); public static int readInt() { try { return Integer.parseInt(inputStream.readLine()); } catch (Exception e) { System.out.println(e); return 0; }

16 public static int readInt() { try { return Integer.parseInt(inputStream.readLine()); } catch (Exception e) { System.out.println(e); return 0; } readInt() Wait for the user to enter a string (of digits) on the next line. Return the int represented by the string. In case user terminates input before entring anything or enters a non integer return 0 and print an error message.

17 public static int readInt() { try { return new Integer (inputStream.readLine()).intValue()); } catch (Exception e) { System.out.println(e); return 0; } Alternative readInt() Wait for the user to enter a string (of digits) on the next line. Return the int represented by the string. In case user terminates input before entring anything or enters a non integer return 0 and print an error message. Less efficient and not clear that parsing occurs

18 Reading other primitive values new Double (inputStream.readLine()).doubleValue()); new Boolean (inputStream.readLine()).doubleValue()); new T (inputStream.readLine()).tValue()); General pattern for reading primitive value of type t

19 Using vs. Creating Objects Driving a car –Using BufferReader for input Building a factory –Creating ABMISpreadsheet

20 ABMISpreadsheet package bmi; 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); } No static Must be put in file ABMISpreadsheet.java, in folder bmi

21 Declaring Instance Variables public class ABMISpreadsheet { double height;... double weight;... public void setWeight(double newWeight) { weight = newWeight; } … } Instance Variables Missing Code instance methodsParameter

22 Instance Variables ABMISpreadsheet Instance getBMI Instance Variables Body accesses Belong to all methods of an instance local variableglobal variable setWeight Parameters Body accesses Belong to a single method accesses

23 Outside Access to a Class public class ABMISpreadsheet { double height;... double weight;... public double getBMI() { return weight/(height*height); } … } Main or other class Variables should not be public But other classes need their values outside access

24 Accessing Instance Variables Via Public Methods Other class ABMISpreadsheet Instance weightheight getBMI() reads setWeight() new Weight calls writes setHeight() new Height calls writes height getHeight() calls reads getWeight() reads weight calls

25 Coding the Methods Other class ABMISpreadsheet Instance weightheight setWeight() new Weight calls writes setHeight() new Height calls writes height getHeight() calls reads getWeight() reads weight calls

26 Coding the Methods Other class ABMISpreadsheet Instance weight setWeight() new Weight calls writes getWeight() reads weight calls

27 Coding Getter and Setter Methods other class 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

28 Functions vs. Procedures package bmi; 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); } functions procedures return nothing

29 Function vs. Procedure Function Procedure

30 Function vs. Procedure Function Procedure

31 Assignment Statement public void setHeight(double newHeight) { height = newHeight; } newHeight 0 height 0 variablesmemory setHeight(1.77) 1.77 LHS RHS = code that yields a value weight 1.75*weight 0.0 weight

32 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

33 public class C { } Read-Only and Editable Properties public T getP() {... } Typed, Named Unit of Exported Object State Name: P Type: T Readonly public void setP(T newValue) {... } Editable newP obtainP Violates Bean Conventions Bean Conventions for humans tools Getter method Setter method

34 Properties Classification 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 Read-only Independent Dependent Read-Only

35 Using ABMISpreadsheet

36 ABMIDriver package main; import bmi.ABMISpreadsheet; import java.io.BufferedReader; import java.io.InputStreamReader; public class ABMIDriver { static BufferedReader dataIn = new BufferedReader(new InputStreamReader(System.in));; public static void main (String args[]) { ABMISpreadsheet bmiSpreadsheet = new ABMISpreadsheet(); bmiSpreadsheet.setWeight(readWeight()); bmiSpreadsheet.setHeight(readHeight()); print (bmiSpreadsheet); }

37 ABMIDriver public static double readWeight() { System.out.println("Please enter weight in Kgs:"); return readDouble(); } public static double readHeight() { System.out.println("Please enter height in Metres:"); return readDouble(); } public static double readDouble() { try { return (new Double(dataIn.readLine())).doubleValue(); } catch (Exception e) { System.out.println(e); return 0; }

38 ABMIDriver public static void print (ABMISpreadsheet bmiSpreadsheet) { System.out.println("****Weight*****"); System.out.println(bmiSpreadsheet.getWeight()); System.out.println("****Height****"); System.out.println(bmiSpreadsheet.getHeight()); System.out.println("****Body Mass Index****"); System.out.println (bmiSpreadsheet.getBMI()); }

39 ABMIEditor package main; import bmi.ABMISpreadsheet; import bus.uigen.ObjectEditor import java.io.InputStreamReader; public class ABMIEditor { ObjectEditor.edit(new ABMISpreadsheet()); }

40 ObjectEditor.edit() 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); } Height Weight BMI

41 Properties Classification 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 Read-only Independent Dependent Read-Only

42 Calling Getter and Setter Methods When ObjectEditor window is created Getter method of each property called to display initial value of property When property is changed to a new value Setter method of property is called with new value as actual parameter Getter method of each property is called to refresh display

43 Calling Getter and Setter Methods public class ABMISpreadsheet { double height = 1.77; public double getHeight() { return height; } public void setHeight(double newHeight) { height = newHeight; } double weight = 77; public double getWeight() { return weight; } public void setWeight(double newWeight) { weight = newWeight; } public double getBMI() { return weight/(height*height); }

44 Tracing Method Calls public class ABMISpreadsheet { double height = 1.77; public double getHeight() { System.out.println (“getHeight Called”); return height; } public void setHeight(double newHeight) { System.out.println (“setWeight Called”); height = newHeight; } double weight = 77; 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); }

45 Actual Trace

46 Modified ABMISpreadsheet 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 calculateBMI(weight, height); } public double calculateBMI(double weight, double height) { return weight / (height*height); | } How will above UI change?

47 Properties + Class Menu

48 Editing in slow motion : Initial value

49 Editing in slow motion: text string edited

50 Editing in slow motion: return triggers setter and getter calls

51 Renaming getter method public class ABMISpreadsheet { double height, weight; public double height() { 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 calculateBMI(weight, height); } public double calculateBMI(double weight, double height) { return weight / (height*height); | }

52 ObjectEditor does not know it is getter

53 Reducing Access public class ABMISpreadsheet { double height, weight; 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 calculateBMI(weight, height); } public double calculateBMI(double weight, double height) { return weight / (height*height); | }

54 Height is not a readable property

55 Changing setter public class ABMISpreadsheet { double height, weight; public double getHeight() { return height; } public int setHeight(double newHeight) { height = newHeight; return height; } public double getWeight() { return weight; } public void setWeight(double newWeight) { weight = newWeight; } public double getBMI() { return calculateBMI(weight, height); } public double calculateBMI(double weight, double height) { return weight / (height*height); | }

56 Height is not a writeable property

57 ABMISpreadsheet Other class ABMISpreadsheet Instance weightheight setWeight() new Weight calls writes setHeight() new Height calls writes height getHeight() calls reads getWeight() reads weight calls reads getBMI()

58 AnotherBMISpreadsheet Other class ABMISpreadsheet Instance weightheight setWeight() new Weight calls writes setHeight() new Height calls writes height getHeight() calls reads getWeight() reads weight calls getBMI() bmi reads

59 AnotherBMISpreadsheet Other class ABMISpreadsheet Instance weightheight setWeight() new Weight calls writes setHeight() new Height calls writes height getHeight() calls reads getWeight() reads weight calls getBMI() bmi reads

60 Methods that Changes Other class AnotherBMISpreadsheet Instance weightheight setWeight() new Weight calls writes setHeight() new Height calls writes getBMI() bmi reads

61 setWeight() Other class AnotherBMISpreadsheet Instance weight setWeight() new Weight calls writes bmi public void setWeight(double newWeight) { weight = newWeight; bmi = weight / (height*height); }

62 setHeight() Other class AnotherBMISpreadsheet Instance height setHeight() new Height calls writes bmi public void setHeight(double newHeight) { height = newHeight; bmi = weight / (height*height); }

63 getBMI() Other class AnotherBMISpreadsheet Instance getBMI() bmi reads public double getBMI() { return bmi; }

64 Complete Code package bmi; public class AnotherBMISpreadsheet { double height, weight, bmi; public double getHeight() { return height; } public void setHeight(double newHeight) { height = newHeight; bmi = weight/(height*height); } public double getWeight() { return weight; } public void setWeight(double newWeight) { weight = newWeight; bmi = weight/(height*height); } public double getBMI() { return bmi; }

65 Similarities in the two Classes package bmi; 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); }

66 Similarities in the two Classes package bmi; public class AnotherBMISpreadsheet { double height, weight, bmi; public double getHeight() { return height; } public void setHeight(double newHeight) { height = newHeight; bmi = weight/(height*height); } public double getWeight() { return weight; } public void setWeight(double newWeight) { weight = newWeight; bmi = weight/(height*height); } public double getBMI() { return bmi; }

67 Similarities in the two Classes

68 Interface package bmi; public interface BMISpreadsheet { public double getHeight(); public void setHeight (double newVal); public double getWeight() ; public void setWeight(double newWeight) ; public double getBMI(); } Must be put in file BMISpreadsheet.java in folder bmi

69 package bmi public class AnotherBMISpreadsheet implements BMISpreadhsheet { double height, weight, bmi; public double getHeight() { return height; } public void setHeight (double newHeight) { height = newHeight; bmi = calculateBMI(); } public double getWeight() { return weight; } public void setWeight(double newWeight) { weight = newWeight; bmi = calculateBMI(); } public double getBMI() { return bmi; } Implementing an Interface contract package bmi; public interface BMISpreadsheet { public double getHeight(); public void setHeight (double newVal); public double getWeight() ; public void setWeight(double newWeight) ; public double getBMI(); } parameter names never matter to Java

70 manufactures Real-World Analogy Accord Specification implements

71 Interface AnotherBMISpreadsheet Instance AnotherBMISpreadsheet instance of AnotherBMISpreadsheet Instance ABMISpreadsheet instance of ABMISpreadsheet Instance ABMISpreadsheet Instance BMISpreadsheet implements

72 Using Car Specification to Classify manufactures Accord Specification implements Accord

73 Using Interface to Classify BMISpreadsheet Instance AnotherBMISpreadsheet instance of BMISpreadsheet Instance ABMISpreadsheet instance of BMISpreadsheet Instance BMISpreadsheet Instance BMISpreadsheet implements

74 Class-based typing package main; import bmi.ABMISpreadsheet; import java.io.BufferedReader; import java.io.InputStreamReader; public class ABMIDriver { static BufferedReader dataIn = new BufferedReader(new InputStreamReader(System.in));; public static void main (String args[]) { ABMISpreadsheet bmiSpreadsheet = new ABMISpreadsheet(); bmiSpreadsheet.setWeight(readWeight()); bmiSpreadsheet.setHeight(readHeight()); print (bmiSpreadsheet); }

75 Class-based Typing public static void print (ABMISpreadsheet bmiSpreadsheet) { System.out.println("****Weight*****"); System.out.println(bmiSpreadsheet.getWeight()); System.out.println("****Height****"); System.out.println(bmiSpreadsheet.getHeight()); System.out.println("****Body Mass Index****"); System.out.println (bmiSpreadsheet.getBMI()); } Should print be in ABMISpreadsheet?

76 Interface-based typing package main; import bmi.ABMISpreadsheet; import java.io.BufferedReader; import java.io.InputStreamReader; public class ABMIDriver { static BufferedReader dataIn = new BufferedReader(new InputStreamReader(System.in));; public static void main (String args[]) { BMISpreadsheet bmiSpreadsheet = new ABMISpreadsheet(); bmiSpreadsheet.setWeight(readWeight()); bmiSpreadsheet.setHeight(readHeight()); print (bmiSpreadsheet); }

77 Interface-based Typing public static void print (BMISpreadsheet bmiSpreadsheet) { System.out.println("****Weight*****"); System.out.println(bmiSpreadsheet.getWeight()); System.out.println("****Height****"); System.out.println(bmiSpreadsheet.getHeight()); System.out.println("****Body Mass Index****"); System.out.println (bmiSpreadsheet.getBMI()); }

78 Changing the class package main; import bmi.ABMISpreadsheet; import java.io.BufferedReader; import java.io.InputStreamReader; public class ABMIDriver { static BufferedReader dataIn = new BufferedReader(new InputStreamReader(System.in));; public static void main (String args[]) { BMISpreadsheet bmiSpreadsheet = new AnotherBMISpreadsheet(); bmiSpreadsheet.setWeight(readWeight()); bmiSpreadsheet.setHeight(readHeight()); print (bmiSpreadsheet); } 1 (vs. 3) changes!

79 Cannot Instantiate Specification Cannot order car from a specification Must order from factory. A car defined by Accord specification ordered from factory implementing the specification. Cannot instantiate interface Must instantiate class. new BMISpreadsheet() - illegal BMISpreadsheet instance created by instantiating class implementing interface.

80 package bmi; public class ABMISpreadsheet implements BMISpreadsheet{ 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 height/(weight*weight); } Interface as Syntactic Specification

81 package bmi; public class ABMISpreadsheet implements BMISpreadsheet{ 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 3245.4 } Interface as Syntactic Specification Syntactic Contract Bombay Market Index?

82 Define interface for: –All classes (that are instantiated.) –Some are not. main class –Include all public instance methods –when interface not given a priori, define it after class implemented bottom up programming Interface Required

83 Pros and cons of two alternatives public class ABMISpreadsheet implements BMISpreadsheet { 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); }

84 Pros and cons of two alternatives public class AnotherBMISpreadsheet implements BMISpreadsheet { double height, weight, bmi; public double getHeight() { return height; } public void setHeight(double newHeight) { height = newHeight; bmi = weight/(height*height); } public double getWeight() { return weight; } public void setWeight(double newWeight) { weight = newWeight; bmi = weight/(height*height); } public double getBMI() { return bmi; }

85 ABMISpreadsheet vs. AnotherBMISpreadSheet AnotherBMISpreadsheet less likely to be correct Have to remember to set all dependents ABMISpreadsheet uses less space (variables) Getter methods of AnotherBMISpreadhseet are faster. Setter methods of ABMISpreadsheet are faster. Usually getter methods are called more often that setter methods - e.g when a graphical user interface is refreshed Typically AnotherBMISpreadsheet will be faster, overall.

86 Time-Space Tradeoff Time Miser Space Miser Space Time

87 Space Time Time MiserSpace Miser Time-Space Tradeoff

88 Relating Interface and Class Names Class Name: (ABMISpreadsheet, ASpaceEfficientBMISpreadsheet, SpaceEfficientBMISpreadsheet) Impl (BMISpreadsheetImpl, BMISpreadsheetSpaceEfficientImpl) Interface Name: Interface (ABMISpreadsheetInterface, IABMISpreadsheet)

89 Look at the airplane fly. The fly is bothering me. Overloading System.out.println(“****Weight****”); System.out.println(bmiSpreadsheet.getWeight()); 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

90 Overloaded Operators 5 + 6 6.0 + 5.0 “6” + “5” “6” + 5 Java: Cannot define programmer defined overloaded operators Can define overloaded methods

91 Ambiguous Context myPrint(“setWeight called”); Time flies like an arrow. public void myPrint (String val) {…} Operation Definitions Fruit flies like an orange.

92 Ambiguous Context double i = read(); public double read () {…} public String read () {…} Operation Definitions Return type not part of disambiguating context String s = read(); String s =read() + read() + read() String s =read s () + ss read s () + ss read s () String s = read s () + sd read d () + ss read s () String s =read s () + sd read d () + sd read d () String s =read s () + ss read s () + sd read s ()

93 ABMIDriver public static void print (ABMISpreadsheet bmiSpreadsheet) { System.out.println("****Weight*****"); System.out.println(bmiSpreadsheet.getWeight()); System.out.println("****Height****"); System.out.println(bmiSpreadsheet.getHeight()); System.out.println("****Body Mass Index****"); System.out.println (bmiSpreadsheet.getBMI()); } Should print be in ABMISpreadsheet?

94 Modified ABMISpreadsheet public void print () { System.out.println("****Weight*****"); System.out.println(getWeight()); System.out.println("****Height****"); System.out.println(getHeight()); System.out.println("****Body Mass Index****"); System.out.println (getBMI()); } Different method invocation syntax

95 Method Invocation Syntax System.out.println(bmiSpreadsheet.getBMI()) getBMI() Internal Call External Call Target Object Method Name

96 Modified ABMISpreadsheet public void print () { System.out.println("****Weight*****"); System.out.println(getWeight()); System.out.println("****Height****"); System.out.println(getHeight()); System.out.println("****Body Mass Index****"); System.out.println (getBMI()); } Object name implicit

97 Modified ABMISpreadsheet public void print () { System.out.println("****Weight*****"); System.out.println(this.getWeight()); System.out.println("****Height****"); System.out.println(this.getHeight()); System.out.println("****Body Mass Index****"); System.out.println (this.getBMI()); } The object on which the method (print) is being invoked

98 Original ABMIDriver package main; import bmi.ABMISpreadsheet; import java.io.BufferedReader; import java.io.InputStreamReader; public class ABMIDriver { static BufferedReader dataIn = new BufferedReader(new InputStreamReader(System.in));; public static void main (String args[]) { ABMISpreadsheet bmiSpreadsheet = new ABMISpreadsheet(); bmiSpreadsheet.setWeight(readWeight()); bmiSpreadsheet.setHeight(readHeight()); print (bmiSpreadsheet); }

99 Modified ABMIDriver package main; import bmi.ABMISpreadsheet; import java.io.BufferedReader; import java.io.InputStreamReader; public class ABMIDriver { static BufferedReader dataIn = new BufferedReader(new InputStreamReader(System.in));; public static void main (String args[]) { ABMISpreadsheet bmiSpreadsheet = new ABMISpreadsheet(); bmiSpreadsheet.setWeight(readWeight()); bmiSpreadsheet.setHeight(readHeight()); bmiSpreadsheet.print (); }

100 Object-based vs. conventional programming Program element is autonomous, active. Its users simply ask it to do things. It is the target of method call –bmiSpreadsheet.getBM I(); –bmiSpreadsheet.print() Users share code Program element is passive. Users write code to manipulate it. It is parameter to method call: –getBMI(bmiSpreadshe et) –print (bmiSpreadsheet) Users write their own code

101 UI Code and Objects Original approach was better –though less “O-O” –lots of text books prefer the modified approach Object can have multiple user interfaces –UI code should not be tied to object code –hard to change independently User-interface code should be in main (or other classes) Only object invocation, instantiation (for now), user interface code (for now), connection (for now) in main –no arithmetic, scanning,...

102 ABMIDriver package main; import bmi.ABMISpreadsheet; import java.io.BufferedReader; import java.io.InputStreamReader; public class ABMIDriver { static BufferedReader dataIn = new BufferedReader(new InputStreamReader(System.in));; public static void main (String args[]) { ABMISpreadsheet bmiSpreadsheet = new ABMISpreadsheet(); bmiSpreadsheet.setWeight(readWeight()); bmiSpreadsheet.setHeight(readHeight()); print (bmiSpreadsheet); } Object instantiation and invocation

103 ABMIDriver public static double readWeight() { System.out.println("Please enter weight in Kgs:"); return readDouble(); } public static double readHeight() { System.out.println("Please enter height in Metres:"); return readDouble(); } public static double readDouble() { try { return (new Double(dataIn.readLine())).doubleValue(); } catch (Exception e) { System.out.println(e); return 0; } Input code

104 ABMIDriver public static void print (ABMISpreadsheet bmiSpreadsheet) { System.out.println("****Weight*****"); System.out.println(bmiSpreadsheet.getWeight()); System.out.println("****Height****"); System.out.println(bmiSpreadsheet.getHeight()); System.out.println("****Body Mass Index****"); System.out.println (bmiSpreadsheet.getBMI()); } Output code and object invocation

105 UI vs. Debugging Code public void setWeight(double newWeight) { System.out.println(“Weight: “ + weight); weight = newWeight; }

106 UI vs. Debugging Code public void getBMI() { System.out.println(“BMI returned: “ + getBMI()); weight/(height*height); } Infinite recursion!

107 Comments double bmi; //computed by setWeight and setHeight Single-line comment /* recompute dependent properties */ bmi = weight / (height*height); Arbitrary comment /* This version recalculates the bmi when weight or height change, not when getBMI is called */ public class AnotherBMISpreadsheet {…}

108 Removing Debugging Code /* System.out.println(newHeight); // debugging statement */ System.out.println(newHeight); /*debugging statement */ /* System.out.println(newHeight); /*debugging statement */ */

109 Javadoc Conventions /* This version recalculates the bmi * when weight or height change, not when * getBMI is called */ public class AnotherBMISpreadsheet {…} /* This version recalculates the bmi when weight or height change, not when getBMI is called */ public class AnotherBMISpreadsheet {…}

110 What to Comment? Any code fragment needing explanation: class top-level algorithm, author, date modified variable declaration purpose, where used method declaration params, return value, algorithm, author, date modified statement sequence explanation Debugging code

111 What to Comment? double w; // weight double weight; // weight double bmi; // computed by setWeight() and setHeight() double weight;Self Commenting Redundant Bad Variable Name Useful Comment

112 /* * @author Prasun Dewan * @param newWeight the new value of the property, weight. * sets new values of the variables, weight and bmi */ public void setWeight (double newWeight) { … } /* * @author Prasun Dewan * @return the value of the variable, weight */ public double getWeight () { … } Javadoc Tags Javadoc tags

113 Improving the Style public class AnotherBMISpreadsheet implements BMISpreadsheet { double height, weight, bmi; public double getHeight() { return height; } public void setHeight(double newHeight) { height = newHeight; bmi = weight/(height*height); } public double getWeight() { return weight; } public void setWeight(double newWeight) { weight = newWeight; bmi = weight/(height*height); } public double getBMI() { return bmi; } Code Repetition

114 Why Avoid Code Duplication? Less Typing Changes (Inches, LB) can forget change all repetitions

115 Example Changes: LB, Inches public class AnotherBMISpreadsheet implements BMISpreadsheet { double height, weight, bmi; public double getHeight() { return height; } public void setHeight(double newHeight) { height = newHeight; bmi = weight/(height*height); } public double getWeight() { return weight; } public void setWeight(double newWeight) { weight = newWeight; bmi = weight/(height*height); }... bmi = (weight/2.2)/(height * 2.54/100*height*2.54/100);

116 How to avoid code duplication? public class AnotherBMISpreadsheet implements BMISpreadsheet { double height, weight, bmi; public double getHeight() { return height; } public void setHeight(double newHeight) { height = newHeight; bmi = calculateBMI(weight, height); } public double getWeight() { return weight; } public void setWeight(double newWeight) { weight = newWeight; bmi = calculateBMI(weight, height); } double calculateBMI(double weight, double height) { return (weight/2.2)/(height * 2.54/100*height*2.54/100); } Not public

117 Principle of Least Privilege Do not give a user of some code more rights than it needs. Code is easier to change. Need to learn less to use code. Less likelihood of accidental or malicious damage to program. Like hiding engine details from car driver. ABMISpreadsheeet Other class setWeight() setHeight()getHeight() getWeight() getBMI()calculateBMI()computeBMI()

118 Only Public Methods in Interface package bmi; public class AnotherBMISpreadsheet implements BMISpreadhsheet { double height, weight, bmi; public double getHeight() { return height; } public void setHeight (double newHeight) { height = newHeight; bmi = calculateBMI(weight, height); } public double getWeight() { return weight; } public void setWeight(double newWeight) { weight = newWeight; bmi = calculateBMI(weight, height); } public double getBMI() { return bmi; } double calculateBMI(double weight, double height) { return weight/ (height*height); } not in interface package bmi; public interface BMISpreadsheet { public double getHeight(); public void setHeight (double newVal); public double getWeight() ; public void setWeight(double newWeight) ; public double getBMI(); }

119 Pure vs. Impure Functions ABMISpreadsheet Instance getWeight weight Body accesses calculateBMI(77,1.77) 24.57 getWeight() setWeight(77) 77 71... setWeight(71) ABMISpreadsheet Instance calculateBMI weight Body accesses height

120 Impure Functions public double calculateBMI(double weight, double height) { System.out.println(“height: “ + height + “weight: “ + weight) return weight/(height*height); } Printing is side effect

121 Side Effect Printing Reading input Changing global variable Benign Done all the time –readLine() Very dangerous –only if implementing input

122 public class AnotherBMISpreadsheet implements BMISpreadsheet{ double height, weight, bmi; public double getHeight() { return height; } public void setHeight(double newHeight) { height = newHeight; bmi = calculateBMI(weight, height); } public double getWeight() { return weight; } public void setWeight(double newWeight) { weight = newWeight; bmi = calculateBMI(); } public double getBMI() { return bmi; } double calculateBMI(double weight, double height) { return (weight/2.2)/(height * 2.54/100*height*2.54/100); } Improving the Style Magic numbers?

123 public class AnotherBMISpreadsheet implements BMISpreadsheet{ double height, weight, bmi; public double getHeight() { return height; } public void setHeight(double newHeight) { height = newHeight; bmi = calculateBMI(weight, height); } public double getWeight() { return weight; } public void setWeight(double newWeight) { weight = newWeight; bmi = calculateBMI(weight, height); } public double getBMI() { return bmi; } double calculateBMI() { return (weight/LBS_IN_KG) / (height*CMS_IN_INCH/100*height*CMS_IN_INCH/100); } Improving the Style Named Constants

124 public class AnotherBMISpreadsheet implements BMISpreadsheet{ double height, weight, bmi;... final double LBS_IN_KG = 2.2; final double CMS_IN_INCH = 2.54; double calculateBMI() { return (weight/LBS_IN_KG) / (height*CMS_IN_INCH/100*height*CMS_IN_INCH/100); } Declaring Named Constants Initializing Declaration Un-initializing Declaration All Caps by Conventions Cannot Change Initial Value

125 Variables vs. Named Constants public class AnotherBMISpreadsheet implements BMISpreadsheet{ double height, weight, bmi;... double lbsInKg = 2.2; double cmsInInch = 2.54; double calculateBMI(double weight, double height) { return (weight/lbsInKg) / (height*cmsInInch/100*height*cmsInInch/100); }

126 public class AnotherBMISpreadsheet implements BMISpreadsheet{ double height, weight, bmi;... double lbsInKg = 2.2; double cmsInInch = 2.54; double calculateBMI() { lbsInKg = 22; return (weight/lbsInKg) / (height*cmsInInch/100*height*cmsInInch/100); } Accidental or Malicious Modification Violating Least Privilege

127 Constant return (weight/lbsInKg) / (height*cmsInInch/100*height*cmsInInch/100); return (weight/2.2) / (height*2.54/100*height*2.54/100); Literals, Named Constants, Constants, Variables Variable Literal return (weight/LBS_IN_KG) / (height*CMS_IN_INCH/100*height*CMS_IN_INCH/100) Named Constant

128 Literals vs. Named Constants vs. Variables Use constants for program values that do not change. –Use named constants for magic numbers

129 return (weight/2.2) / (height*2.54/100*height*2.54/100); What is a Magic Number? return (weight/LBS_IN_KG) / (height*CMS_IN_INCH/100*height*CMS_IN_INCH/100) Natural Constants return hoursWorked*hourlyWage + 50; return hoursWorked*hourlyWage + BONUS; Human-Created Constant System.out.println (“Bonus: “ + 50);

130 Human-created constant is a magic number. Natural constant may be a magic number to some. What is a magic number?

131 public class AnotherBMISpreadsheet implements BMISpreadsheet{ double height, weight, bmi;... final double LBS_IN_KG = 2.2; final double CMS_IN_INCH = 2.54; double calculateBMI() { return (weight/LBS_IN_KG) / (height*CMS_IN_INCH/100*height*CMS_IN_INCH/100); } More Code Repetition Within Same Method and Has Same Value

132 public class AnotherBMISpreadsheet implements BMISpreadsheet{ double height, weight, bmi;... final double LBS_IN_KG = 2.2; final double CMS_IN_INCH = 2.54; double calculateBMI() { double heightInMetres = height*CMS_IN_INCH/100; return (weight/LBS_IN_KG) / (heightInMetres*heightInMetres); } Removing Code Repetition

133 Independent Code = Method Separate Method for: any independent piece of code. even if it is not duplicated. specially if it is more than one line. public void setWeight(double newWeight) { System.out.println(“Weight: “ + weight); weight = newWeight; } public void setWeight(double newWeight) { printWeight(); weight = newWeight; }

134 public class AnotherBMISpreadsheet implements BMISpreadsheet{ double height, weight, bmi; double heightInMetres = height*CMS_IN_INCH/100;... final double LBS_IN_KG = 2.2; final double CMS_IN_INCH = 2.54; double calculateBMI() { return (weight/LBS_IN_KG) / (heightInMetres*heightInMetres); } Local vs. Global Variable

135 public class AnotherBMISpreadsheet implements BMISpreadsheet{ double height, weight, bmi; double heightInMetres = height*CMS_IN_INCH/100; public void setHeight(double newHeight) { height = heightInMetres; bmi = calculateBMI(); }... final double LBS_IN_KG = 2.2; final double CMS_IN_INCH = 2.54; double calculateBMI() { return (weight/LBS_IN_KG) / (heightInMetres*heightInMetres); } Local vs. Global Variable Violating least privilege

136 Identifier Scope Region of code where the identifier is visible. Arbitrary scopes not possible

137 public class AnotherBMISpreadsheet implements BMISpreadsheet{ double height, weight, bmi; public double getHeight() { return height; } public void setHeight(double newHeight) { height = newHeight; bmi = calculateBMI(); } public double getWeight() { return weight; } public void setWeight(double newWeight) { weight = newWeight; bmi = calculateBMI(); } public double getBMI() { return bmi; } double calculateBMI(double weight, double height) { double heightInMetres = height*CMS_IN_INCH/100; return (weight/LBS_IN_KG) / (heightInMetres*heightInMetres); } Scope heightInMetres Scope height Scope Not a Scope

138 public class AnotherBMISpreadsheet implements BMISpreadsheet{ double height, weight, bmi; public double getHeight() { return height; } public void setHeight(double newHeight) { height = newHeight; bmi = calculateBMI(); } public double getWeight() { return weight; } public void setWeight(double newWeight) { weight = newWeight; bmi = calculateBMI(); } public double getBMI() { return bmi; } double calculateBMI(double weight, double height) { double heightInMetres = height*CMS_IN_INCH/100; return (weight/LBS_IN_KG) / (heightInMetres*heightInMetres); } Multiple definitions height Scope

139 Overriding Scopes Narrower scope overrides definitions in more general scope

140 public class AnotherBMISpreadsheet implements BMISpreadsheet{ double height, weight, bmi; public double getHeight() { return height; }... } Scope of Public Items getHeight() Scope ABMISpreadsheetDriver ABMISpreadsheet

141 public class ABMISpreadsheet implements BMISpreadsheet { public double height, weight, bmi;... } Non Public Instance Variables

142 public class ABMISpreadsheetWithPublicVariables { public double height, weight, bmi;... } Making Instance Variables Public Other Classes

143 public class ABMISpreadsheetWithPublicVariables { public double height, weight;... } Hard to Change Representation Other Classes

144 public class ABMISpreadsheetWithPublicVariables { public double height, weight, bmi;... } Internal constraints violated Other Classes bmiSpreadsheet.height = 5.0; System.out.println (bmiSpreadsheet.bmi);

145 Do not make instance variables public –Expose them through public methods Encapsulation Principle

146 Variable Scope Least privilege => –Keep scope of variable as small as possible no public variables as few global variables as possible

147 Named-Constant Scope Least privilege does not apply since named constants cannot be modified. Make scope of named constants as large as possible to prevent duplication.

148 public class AnotherBMISpreadsheet implements BMISpreadsheet{ double height, weight, bmi;... public final double LBS_IN_KG = 2.2; public final double CMS_IN_INCH = 2.54; double calculateBMI() { double heightInMetres = height*CMS_IN_INCH/100; return (weight/LBS_IN_KG) / (heightInMetres*heightInMetres); } Named constant scope

149 public final double CMS_IN_INCH = 2.54; Public Constants Inconsistent value cannot be stored Implementation Independent public interface BMISpreadsheet { } ABMISpreadsheetAnotherBMISpreadsheet

150 Declare implementation-independent named constants in interfaces –implementing classes can access them. Principle

151 public class AnotherBMISpreadsheet implements BMISpreadsheet{ double height, weight, bmi;... final double LBS_IN_KG = 2.2; final double CMS_IN_INCH = 2.54; double calculateBMI() { double heightInMetres = height*CMS_IN_INCH/100; return (weight/LBS_IN_KG) / (heightInMetres*heightInMetres); } Initializing Declaration

152 public class AnotherBMISpreadsheet implements BMISpreadsheet{ double height, weight, bmi;... final double LBS_IN_KG = 2.2; final double CMS_IN_INCH = 2.54; double calculateBMI() { double heightInMetres; heightInMetres = height*CMS_IN_INCH/100; return (weight/LBS_IN_KG) / (heightInMetres*heightInMetres); } Un-initializing Declaration

153 public class AnotherBMISpreadsheet implements BMISpreadsheet{ double height, weight, bmi;... final double LBS_IN_KG = 2.2; final double CMS_IN_INCH = 2.54; double calculateBMI() { double heightInMetres; return (weight/LBS_IN_KG) / (heightInMetres*heightInMetres); } Un-initialized Variable

154 public class AnotherBMISpreadsheet implements BMISpreadsheet{ double height, weight, bmi;... final double LBS_IN_KG = 2.2; final double CMS_IN_INCH = 2.54; double calculateBMI() { double heightInMetres = height*CMS_IN_INCH/100;; return (weight/LBS_IN_KG) / (heightInMetres*heightInMetres); } Initializing all Variables

155 public class AnotherBMISpreadsheet implements BMISpreadsheet{ double height = 70, weight = 160, bmi = calculateBMI();... final double LBS_IN_KG = 2.2; final double CMS_IN_INCH = 2.54; double calculateBMI() { double heightInMetres = height*CMS_IN_INCH/100;; return (weight/LBS_IN_KG) / (heightInMetres*heightInMetres); } Initializing all Variables

156 Instance-independent Instantiation package bmi; public class ABMISpreadsheet { double height = 1.77; double weight = 70; 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); }

157 Instance-dependent Initialization May want instance variables to be initialized to different values in different instances (my and your instance of ABMISpreadsheet) Can be done by adding special method called constructor to class.

158 Instance-dependent Instantiation package bmi; public class ABMISpreadsheet { double height, weight ; public ABMISpreadsheet (double initHeight, double initWeight) { height = initHeight; weight = initWeight; } 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); } Combined type and method name

159 Constructor Special method in a class: name of method same as class name no return type name in header called immediately after an instance is created (after instance variables are allocated in memory and before they can be accessed) used to initialize instance variables based on its parameters prefix new precedes call to it –new ABMISpreadsheet (77, 1.77) Cannot be declared in interface Can have multiple parameters –usually one per instance variable

160 How Java processes a constructor call new C (a1, a2, …) Creates an instance of C (allocates memory for its instance variables) Calls the constructor –actual parameters a1, a2, … must match the formal parameters (as in all methods) new ABMISpreadsheet(77, 1.77) legal new ABMISpreadsheet() illegal Returns the instance (of type C)

161 Default Constructor Every class must have a constructor Java automatically creates one if we do not

162 Default Constructor package bmi; 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); } public ABMISpreadsheet() { } Inserted in Object Code not in Source Code Default new ABMISpreadsheet()

163 Case Conventions Start variable name with lowercase letter (weight). Start class name with uppercase letter (ABMICalculator) Start each new word with upper case letter (ASquareCalculator)

164 class ABMISpreadsheet { double height, weight; static double getBMI() { return (height*heigh)/weight } Errors Syntax Error Semantics Error Logic Error Access Error

165 Static vs. non static In main class –Make all methods and variables static –Main class not instantiated Instantiated class –Only instance methods and variables –At least for now

166 Another Object-based Problem

167 Another Example: Point X. Y R .

168 Solution public class ARocketTracker { public static void main (String args[]) { System.out.println("Please Enter Cartesian Coordinates of Highest Point"); double highestX = readDouble(); double highestY = readDouble(); double highestRadius = Math.sqrt(highestX*highestX + highestY*highestY); double highestAngle = Math.sqrt (Math.atan(highestY/highestX)); print (highestX, highestY, highestRadius, highestAngle); } public static void print(double x, double y, double radius, double angle) { System.out.println ("Highest Horizontal Distance " + x); System.out.println ("Highest Vertical Distance " + y); System.out.println ("Highest Total Distance " + radius); System.out.println ("Highest Angle" + angle); } public staticdouble readDouble() { … }; … }

169 Related Problem

170 Related Solution public class ARocketTracker { public static void main (String args[]) { System.out.println("Please Enter Polar Coordinates of Highest Point"); double highestRadius = readDouble(); double highestAngle = readDouble(); double highestX = highestRadius*Math.cos(highestAngle); double highestY = highestRadius*Math.sin(highestAngle); print (highestX, highestY, highestRadius, highestAngle); } public static void print(double x, double y, double radius, double angle) { System.out.println ("Highest Horizontal Distance " + x); System.out.println ("Highest Vertical Distance " + y); System.out.println ("Highest Total Distance " + radius); System.out.println ("Highest Angle" + angle); } public staticdouble readDouble() { … }; … }

171 Drawbacks of monolithic solutions Read code in main method –cannot return two values from a method Calculation code not reusable

172 Point Interface public interface Point { public int getX(); public int getY(); public double getAngle(); public double getRadius(); }

173 Point Representations X, Y (Cartesian Representation) Radius, Angle (Polar Representation) X, Radius X, Y, Radius, Angle ….

174 Algorithms X. Y R . R = sqrt (X 2 * Y 2 )  = arctan (Y/X) Cartesian Representation Polar Representation X = R*cos(  ) Y = R*sin(  )

175 Class: ACartesianPoint public class ACartesianPoint implements Point { int x, y; public ACartesianPoint(int initX, int initY) { x = initX; y = initY; } public int getX() { return x; } public int getY() { return y; } public double getAngle() { return Math.atan((double) y/x); } public double getRadius() { return Math.sqrt(x*x + y*y); }

176 Class: APolarPoint public class APolarPoint implements Point { double radius, angle; public APolarPoint(int initRadius, int initAngle) { radius = initRadius; angle = initAngle; } public int getX() { return (int) (radius*Math.cos(angle)); } public int getY() { return (int) (radius*Math.sin(angle)); } public double getAngle() { return angle; } public double getRadius() { return radius; }

177 Using the Interface and its Implementations Point point1 = new ACartesianPoint (50, 50); Point point2 = new APolarPoint (70.5, Math.pi()/4); point1 = point2; Constructor chooses implementation Cannot be in interface.

178 Shared main method public static void main (String args[]) { Point highest = getPoint(); print (highest); }

179 Shared print method public static void print (Point p) { System.out.println ("Higest Horizontal Distance" + p.getX()); System.out.println ("Highest Vertical Distance" + p.getY()); System.out.println ("Highest Total Distance " + p.getRadius()); System.out.println ("Highest Angle " + p.getAngle()); }

180 Get method Cartesian public static Point getPoint () { System.out.println("Please Enter Cartesian Coordinates of Highest Point"); return new ACartesianPoint (readDouble(),readDouble()); }

181 Get method Polar public static Point getPoint () { System.out.println("Please Enter Polar Coordinates of Highest Point"); return new APolarPoint (readDouble(), readDouble()); }

182 Bottom-up Programming with Coarse-grained steps Interface Class Used in ABMISpreadsheet example

183 Top-Down with Finer Steps Interface Representation Algorithm Class Used in Point example

184 Real life Define initial interface Modify it incrementally as you change


Download ppt "Object-based Programming Intuitive explanation Using objects to read input Creating objects Style rules."

Similar presentations


Ads by Google