1 / 184

Object-based Programming

Object-based Programming. Intuitive explanation Using objects to read input Creating objects Style rules. Natural Objects. Manufactured Objects. Program Components ~ Physical Objects. ~ Program Objects. operations. manufactured by. accelerate. perform. brake. add. instance of.

diza
Download Presentation

Object-based Programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

  2. Natural Objects Manufactured Objects Program Components ~ Physical Objects ~ Program Objects

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

  4. Classification through Factories manufactured by manufactured by

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

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

  7. String Input

  8. Classifying/ typing instances constructing new instance Invoking operation package main; import java.io.BufferedReader; import java.io.InputStreamReader; publicclass AnInputPrinter { publicstaticvoid 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)); publicstatic String readString() { try { return inputStream.readLine(); } catch (Exception e) { System.out.println(e); return ""; } } } String Input

  9. Objects constructed to allow reading of strings from System.in 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. static BufferedReader inputStream = new BufferedReader(new InputStreamReader(System.in)); publicstatic String readString() { try { return inputStream.readLine(); } catch (Exception e) { System.out.println(e); return ""; } } }

  10. Line tokens Char tokens Byte input stream Chained construction static BufferedReader inputStream = new BufferedReader(new InputStreamReader(System.in)); • 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

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

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

  13. 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 Import all vs. selective import Always do selective imports

  14. Integer Input

  15. package main; import java.io.BufferedReader; import java.io.InputStreamReader; publicclass AnInputSquarer { publicstaticvoid 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)); publicstaticint readInt() { try { return Integer.parseInt(inputStream.readLine()); } catch (Exception e) { System.out.println(e); return 0; } } } Integer Input

  16. 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. publicstaticint readInt() { try { return Integer.parseInt(inputStream.readLine()); } catch (Exception e) { System.out.println(e); return 0; } }

  17. 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. publicstaticint readInt() { try { returnnew Integer (inputStream.readLine()).intValue()); } catch (Exception e) { System.out.println(e); return 0; } } 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. No static package bmi; publicclass ABMISpreadsheet { double height; publicdouble getHeight() { return height; } publicvoid setHeight(double newHeight) { height = newHeight; } double weight; publicdouble getWeight() { return weight; } publicvoid setWeight(double newWeight) { weight = newWeight; } publicdouble getBMI() { return weight/(height*height); } } ABMISpreadsheet Must be put in file ABMISpreadsheet.java, in folder bmi

  21. Missing Code instance methods Parameter Declaring Instance Variables • publicclass ABMISpreadsheet { • double height; • ... • double weight; • ... • publicvoid setWeight(double newWeight) { • weight = newWeight; • } • … • } Instance Variables

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

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

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

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

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

  27. reads writes function procedure weight calls calls Coding Getter and Setter Methods ABMISpreadsheet Instance publicdouble getWeight() { return weight; } weight publicvoid setWeight(double newWeight) { weight = newWeight; } getWeight() setWeight() returns nothing new Weight other class

  28. functions procedures package bmi; publicclass ABMISpreadsheet { double height; publicdouble getHeight() { return height; } publicvoid setHeight(double newHeight) { height = newHeight; } double weight; publicdouble getWeight() { return weight; } publicvoid setWeight(double newWeight) { weight = newWeight; } publicdouble getBMI() { return weight/(height*height); } } Functions vs. Procedures return nothing

  29. Procedure Function Function vs. Procedure

  30. Function vs. Procedure Function Procedure

  31. LHS RHS newHeight 0 1.77 weight 1.77 1.75*weight Assignment Statement publicvoid setHeight(double newHeight) { height = newHeight; } setHeight(1.77) code that yields a value <variable> = <expression> variables memory height 0 weight 0.0

  32. Properties publicclass ABMISpreadsheet { double height; publicdouble getHeight() { return height; } publicvoid setHeight(double newHeight) { height = newHeight; } double weight; publicdouble getWeight() { return weight; } publicvoid setWeight(double newWeight) { weight = newWeight; } publicdouble getBMI() { return weight/(height*height); } } Height Weight BMI

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

  34. Read-Only Editable Independent Editable Independent Read-only Dependent Properties Classification publicclass ABMISpreadsheet { double height; publicdouble getHeight() { return height; } publicvoid setHeight(double newHeight) { height = newHeight; } double weight; publicdouble getWeight() { return weight; } publicvoid setWeight(double newWeight) { weight = newWeight; } publicdouble getBMI() { return weight/(height*height); } } Height Weight BMI

  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() publicclass ABMISpreadsheet { double height, weight; publicdouble getHeight() { return height; } publicvoid setHeight(double newHeight) { height = newHeight; } publicdouble getWeight() { return weight; } publicvoid setWeight(double newWeight) { weight = newWeight; } publicdouble getBMI() { return weight/(height*height); } } Height Weight BMI

  41. Read-Only Editable Independent Editable Independent Read-only Dependent Properties Classification • publicclass ABMISpreadsheet { • double height; • publicdouble getHeight() { • return height; • } • publicvoid setHeight(double newHeight) { • height = newHeight; • } • double weight; • publicdouble getWeight() { • return weight; • } • publicvoid setWeight(double newWeight) { • weight = newWeight; • } • publicdouble getBMI() { • return weight/(height*height); • } • … Height Weight BMI

  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 publicclass ABMISpreadsheet { double height = 1.77; publicdouble getHeight() { return height; } publicvoid setHeight(double newHeight) { height = newHeight; } double weight = 77; publicdouble getWeight() { return weight; } publicvoid setWeight(double newWeight) { weight = newWeight; } publicdouble getBMI() { return weight/(height*height); } }

  44. publicclass ABMISpreadsheet { double height = 1.77; publicdouble getHeight() { System.out.println (“getHeight Called”); return height; } publicvoid setHeight(double newHeight) { System.out.println (“setWeight Called”); height = newHeight; } double weight = 77; publicdouble getWeight() { System.out.println (“getWeight Called”); return weight; } publicvoid setWeight(double newWeight) { System.out.println (“setWeight Called”); weight = newWeight; } publicdouble getBMI() { System.out.println (“getBMI Called”); return weight/(height*height); } } Tracing Method Calls

  45. Actual Trace

  46. How will above UI change? publicclass ABMISpreadsheet { double height, weight; publicdouble getHeight() { return height; } publicvoid setHeight(double newHeight) { height = newHeight; } publicdouble getWeight() { return weight; } publicvoid setWeight(double newWeight) { weight = newWeight; } publicdouble getBMI() { return calculateBMI(weight, height); } publicdouble calculateBMI(double weight, double height) { return weight / (height*height); | } Modified ABMISpreadsheet

  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

More Related