1.84k likes | 1.86k Views
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.
E N D
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 Class subtract execute Program Object invoke methods call Program Objects ~ Manufactured Objects Program Object
Classification through Factories manufactured by manufactured by
Classification through Classes instance of BufferReader BufferReader Instance BufferReader Instance instance of ABMISpreadsheet ABMISpreadsheet Instance ABMISpreadsheet Instance
Using vs. Creating Objects • Driving a car • Using BufferReader for input • Building a factory • Creating ABMISpreadsheet
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
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 ""; } } }
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
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; }
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
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
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
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; } }
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
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
Using vs. Creating Objects • Driving a car • Using BufferReader for input • Building a factory • Creating ABMISpreadsheet
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
Missing Code instance methods Parameter Declaring Instance Variables • publicclass ABMISpreadsheet { • double height; • ... • double weight; • ... • publicvoid setWeight(double newWeight) { • weight = newWeight; • } • … • } Instance Variables
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
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
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
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
reads writes weight calls calls Coding the Methods ABMISpreadsheet Instance weight getWeight() setWeight() new Weight Other class
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
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
Procedure Function Function vs. Procedure
Function vs. Procedure Function Procedure
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
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
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
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
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); }
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; } }
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()); } }
ABMIEditor package main; import bmi.ABMISpreadsheet; import bus.uigen.ObjectEditor import java.io.InputStreamReader; public class ABMIEditor { ObjectEditor.edit(new ABMISpreadsheet()); }
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
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
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
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); } }
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
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
Editing in slow motion: return triggers setter and getter calls