object-based programming intuitive explanation using objects to read input creating objects style...
Post on 20-Dec-2015
239 views
TRANSCRIPT
![Page 1: Object-based Programming Intuitive explanation Using objects to read input Creating objects Style rules](https://reader030.vdocument.in/reader030/viewer/2022032800/56649d425503460f94a1daf0/html5/thumbnails/1.jpg)
Object-based Programming
• Intuitive explanation
• Using objects to read input
• Creating objects
• Style rules
![Page 2: Object-based Programming Intuitive explanation Using objects to read input Creating objects Style rules](https://reader030.vdocument.in/reader030/viewer/2022032800/56649d425503460f94a1daf0/html5/thumbnails/2.jpg)
Manufactured Objects
Natural Objects
Program Components ~ Physical Objects
~ Program
Objects
![Page 3: Object-based Programming Intuitive explanation Using objects to read input Creating objects Style rules](https://reader030.vdocument.in/reader030/viewer/2022032800/56649d425503460f94a1daf0/html5/thumbnails/3.jpg)
Program Objects ~ Manufactured Objects
Program
Object
add
subtract
methodsexecuteinvokecall
manufactured byaccelerate
brake
operations
perform
Classinstance of
Program
Object
![Page 4: Object-based Programming Intuitive explanation Using objects to read input Creating objects Style rules](https://reader030.vdocument.in/reader030/viewer/2022032800/56649d425503460f94a1daf0/html5/thumbnails/4.jpg)
Classification through Factories
manufactured by
manufactured by
![Page 5: Object-based Programming Intuitive explanation Using objects to read input Creating objects Style rules](https://reader030.vdocument.in/reader030/viewer/2022032800/56649d425503460f94a1daf0/html5/thumbnails/5.jpg)
Classification through Classes
BufferReader
Instance
BufferReader instance of
BufferReader
Instance
ABMISpreadsheet
Instance
ABMISpreadsheet instance of
ABMISpreadsheet
Instance
![Page 6: Object-based Programming Intuitive explanation Using objects to read input Creating objects Style rules](https://reader030.vdocument.in/reader030/viewer/2022032800/56649d425503460f94a1daf0/html5/thumbnails/6.jpg)
Using vs. Creating Objects
• Driving a car– Using BufferReader for input
• Building a factory– Creating ABMISpreadsheet
![Page 7: Object-based Programming Intuitive explanation Using objects to read input Creating objects Style rules](https://reader030.vdocument.in/reader030/viewer/2022032800/56649d425503460f94a1daf0/html5/thumbnails/7.jpg)
String Input
![Page 8: Object-based Programming Intuitive explanation Using objects to read input Creating objects Style rules](https://reader030.vdocument.in/reader030/viewer/2022032800/56649d425503460f94a1daf0/html5/thumbnails/8.jpg)
String Inputpackage 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
![Page 9: Object-based Programming Intuitive explanation Using objects to read input Creating objects Style rules](https://reader030.vdocument.in/reader030/viewer/2022032800/56649d425503460f94a1daf0/html5/thumbnails/9.jpg)
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
![Page 10: Object-based Programming Intuitive explanation Using objects to read input Creating objects Style rules](https://reader030.vdocument.in/reader030/viewer/2022032800/56649d425503460f94a1daf0/html5/thumbnails/10.jpg)
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
![Page 11: Object-based Programming Intuitive explanation Using objects to read input Creating objects Style rules](https://reader030.vdocument.in/reader030/viewer/2022032800/56649d425503460f94a1daf0/html5/thumbnails/11.jpg)
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
![Page 12: Object-based Programming Intuitive explanation Using objects to read input Creating objects Style rules](https://reader030.vdocument.in/reader030/viewer/2022032800/56649d425503460f94a1daf0/html5/thumbnails/12.jpg)
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
![Page 13: Object-based Programming Intuitive explanation Using objects to read input Creating objects Style rules](https://reader030.vdocument.in/reader030/viewer/2022032800/56649d425503460f94a1daf0/html5/thumbnails/13.jpg)
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
![Page 14: Object-based Programming Intuitive explanation Using objects to read input Creating objects Style rules](https://reader030.vdocument.in/reader030/viewer/2022032800/56649d425503460f94a1daf0/html5/thumbnails/14.jpg)
Integer Input
![Page 15: Object-based Programming Intuitive explanation Using objects to read input Creating objects Style rules](https://reader030.vdocument.in/reader030/viewer/2022032800/56649d425503460f94a1daf0/html5/thumbnails/15.jpg)
Integer Inputpackage 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;
}}
}
![Page 16: Object-based Programming Intuitive explanation Using objects to read input Creating objects Style rules](https://reader030.vdocument.in/reader030/viewer/2022032800/56649d425503460f94a1daf0/html5/thumbnails/16.jpg)
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.
![Page 17: Object-based Programming Intuitive explanation Using objects to read input Creating objects Style rules](https://reader030.vdocument.in/reader030/viewer/2022032800/56649d425503460f94a1daf0/html5/thumbnails/17.jpg)
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
![Page 18: Object-based Programming Intuitive explanation Using objects to read input Creating objects Style rules](https://reader030.vdocument.in/reader030/viewer/2022032800/56649d425503460f94a1daf0/html5/thumbnails/18.jpg)
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
![Page 19: Object-based Programming Intuitive explanation Using objects to read input Creating objects Style rules](https://reader030.vdocument.in/reader030/viewer/2022032800/56649d425503460f94a1daf0/html5/thumbnails/19.jpg)
Using vs. Creating Objects
• Driving a car– Using BufferReader for input
• Building a factory– Creating ABMISpreadsheet
![Page 20: Object-based Programming Intuitive explanation Using objects to read input Creating objects Style rules](https://reader030.vdocument.in/reader030/viewer/2022032800/56649d425503460f94a1daf0/html5/thumbnails/20.jpg)
ABMISpreadsheetpackage 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
![Page 21: Object-based Programming Intuitive explanation Using objects to read input Creating objects Style rules](https://reader030.vdocument.in/reader030/viewer/2022032800/56649d425503460f94a1daf0/html5/thumbnails/21.jpg)
Declaring Instance Variables
public class ABMISpreadsheet {double height;...double weight; ...public void setWeight(double newWeight) {
weight = newWeight;}
…}
Instance Variables
Missing Code
instance methods Parameter
![Page 22: Object-based Programming Intuitive explanation Using objects to read input Creating objects Style rules](https://reader030.vdocument.in/reader030/viewer/2022032800/56649d425503460f94a1daf0/html5/thumbnails/22.jpg)
Instance VariablesABMISpreadsheet Instance
getBMI
InstanceVariables
Body accesses
Belong to all methods of an instance
local variable global variable
setWeight
Parameters
Body
accesses
Belong to a single method
accesses
![Page 23: Object-based Programming Intuitive explanation Using objects to read input Creating objects Style rules](https://reader030.vdocument.in/reader030/viewer/2022032800/56649d425503460f94a1daf0/html5/thumbnails/23.jpg)
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 publicBut other classes need their values
outside access
![Page 24: Object-based Programming Intuitive explanation Using objects to read input Creating objects Style rules](https://reader030.vdocument.in/reader030/viewer/2022032800/56649d425503460f94a1daf0/html5/thumbnails/24.jpg)
Accessing Instance Variables Via Public Methods
Other class
ABMISpreadsheet Instance
weight height
getBMI()
reads
setWeight()
new Weight
calls
writes
setHeight()
new Height
calls
writes
height
getHeight()
calls
reads
getWeight()
reads
weight calls
![Page 25: Object-based Programming Intuitive explanation Using objects to read input Creating objects Style rules](https://reader030.vdocument.in/reader030/viewer/2022032800/56649d425503460f94a1daf0/html5/thumbnails/25.jpg)
Coding the Methods
Other class
ABMISpreadsheet Instance
weight height
setWeight()
new Weight
calls
writes
setHeight()
new Height
calls
writes
height
getHeight()
calls
reads
getWeight()
reads
weight calls
![Page 26: Object-based Programming Intuitive explanation Using objects to read input Creating objects Style rules](https://reader030.vdocument.in/reader030/viewer/2022032800/56649d425503460f94a1daf0/html5/thumbnails/26.jpg)
Coding the Methods
Other class
ABMISpreadsheet Instance
weight
setWeight()
new Weight
calls
writes
getWeight()
reads
weight calls
![Page 27: Object-based Programming Intuitive explanation Using objects to read input Creating objects Style rules](https://reader030.vdocument.in/reader030/viewer/2022032800/56649d425503460f94a1daf0/html5/thumbnails/27.jpg)
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
![Page 28: Object-based Programming Intuitive explanation Using objects to read input Creating objects Style rules](https://reader030.vdocument.in/reader030/viewer/2022032800/56649d425503460f94a1daf0/html5/thumbnails/28.jpg)
Functions vs. Procedurespackage 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
![Page 29: Object-based Programming Intuitive explanation Using objects to read input Creating objects Style rules](https://reader030.vdocument.in/reader030/viewer/2022032800/56649d425503460f94a1daf0/html5/thumbnails/29.jpg)
Function Vs Procedure
Function Procedure
![Page 30: Object-based Programming Intuitive explanation Using objects to read input Creating objects Style rules](https://reader030.vdocument.in/reader030/viewer/2022032800/56649d425503460f94a1daf0/html5/thumbnails/30.jpg)
Function Vs Procedure
Function Procedure
![Page 31: Object-based Programming Intuitive explanation Using objects to read input Creating objects Style rules](https://reader030.vdocument.in/reader030/viewer/2022032800/56649d425503460f94a1daf0/html5/thumbnails/31.jpg)
Assignment Statementpublic void setHeight(double newHeight) {
height = newHeight;}
newHeight 0
height 0
variables memory
setHeight(1.77)
1.77
1.77
<expression>
LHS RHS
<variable> = code that yields a value
weight
1.75*weight 0.0weight
![Page 32: Object-based Programming Intuitive explanation Using objects to read input Creating objects Style rules](https://reader030.vdocument.in/reader030/viewer/2022032800/56649d425503460f94a1daf0/html5/thumbnails/32.jpg)
Propertiespublic class ABMISpreadsheet {
double height;public double getHeight() {
return height;}public void setHeight(double newHeight) {
height = newHeight;}double weight;public double getWeight() {
return weight;}public void setWeight(double newWeight) {
weight = newWeight;}public double getBMI() {
return weight/(height*height);}
}
Height
Weight
BMI
![Page 33: Object-based Programming Intuitive explanation Using objects to read input Creating objects Style rules](https://reader030.vdocument.in/reader030/viewer/2022032800/56649d425503460f94a1daf0/html5/thumbnails/33.jpg)
public class C {
}
Read-Only and Editable Properties
public T getP() { ...}
Typed, Named Unit of Exported Object State
Name: P
Type: T
Readonlypublic void setP(T newValue) { ...}
Editable
newP
obtainPViolates Bean Conventions
Bean
Conventions for
•humans
•tools
Getter methodSetter method
![Page 34: Object-based Programming Intuitive explanation Using objects to read input Creating objects Style rules](https://reader030.vdocument.in/reader030/viewer/2022032800/56649d425503460f94a1daf0/html5/thumbnails/34.jpg)
Properties Classificationpublic class ABMISpreadsheet {
double height;public double getHeight() {
return height;}public void setHeight(double newHeight) {
height = newHeight;}double weight;public double getWeight() {
return weight;}public void setWeight(double newWeight) {
weight = newWeight;}public double getBMI() {
return weight/(height*height);}
}
Height
Weight
BMI
Editable
Editable
Read-only
Independent
Independent
Dependent
Read-Only
![Page 35: Object-based Programming Intuitive explanation Using objects to read input Creating objects Style rules](https://reader030.vdocument.in/reader030/viewer/2022032800/56649d425503460f94a1daf0/html5/thumbnails/35.jpg)
Using ABMISpreadsheet
![Page 36: Object-based Programming Intuitive explanation Using objects to read input Creating objects Style rules](https://reader030.vdocument.in/reader030/viewer/2022032800/56649d425503460f94a1daf0/html5/thumbnails/36.jpg)
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);
}
![Page 37: Object-based Programming Intuitive explanation Using objects to read input Creating objects Style rules](https://reader030.vdocument.in/reader030/viewer/2022032800/56649d425503460f94a1daf0/html5/thumbnails/37.jpg)
ABMIDriverpublic 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;
}}
![Page 38: Object-based Programming Intuitive explanation Using objects to read input Creating objects Style rules](https://reader030.vdocument.in/reader030/viewer/2022032800/56649d425503460f94a1daf0/html5/thumbnails/38.jpg)
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());
}}
![Page 39: Object-based Programming Intuitive explanation Using objects to read input Creating objects Style rules](https://reader030.vdocument.in/reader030/viewer/2022032800/56649d425503460f94a1daf0/html5/thumbnails/39.jpg)
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
![Page 40: Object-based Programming Intuitive explanation Using objects to read input Creating objects Style rules](https://reader030.vdocument.in/reader030/viewer/2022032800/56649d425503460f94a1daf0/html5/thumbnails/40.jpg)
Overloaded Operators
5 + 6
6.0 + 5.0
“6” + “5”
“6” + 5
Java: Cannot define programmer defined overloaded operators
Can define overloaded methods
![Page 41: Object-based Programming Intuitive explanation Using objects to read input Creating objects Style rules](https://reader030.vdocument.in/reader030/viewer/2022032800/56649d425503460f94a1daf0/html5/thumbnails/41.jpg)
Ambiguous Context
myPrint(“setWeight called”);
Time flies like an arrow.
public void myPrint (String val) {…}
public void myPrint (String val) {…}
Operation Definitions
Fruit flies like an orange.
![Page 42: Object-based Programming Intuitive explanation Using objects to read input Creating objects Style rules](https://reader030.vdocument.in/reader030/viewer/2022032800/56649d425503460f94a1daf0/html5/thumbnails/42.jpg)
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 =reads () +ss reads () + ss reads ()
String s = reads () +sd readd() +ss reads ()
String s =reads () +sd readd() +sd readd ()
String s =reads () +ss reads () + sd reads ()
![Page 43: Object-based Programming Intuitive explanation Using objects to read input Creating objects Style rules](https://reader030.vdocument.in/reader030/viewer/2022032800/56649d425503460f94a1daf0/html5/thumbnails/43.jpg)
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?
![Page 44: Object-based Programming Intuitive explanation Using objects to read input Creating objects Style rules](https://reader030.vdocument.in/reader030/viewer/2022032800/56649d425503460f94a1daf0/html5/thumbnails/44.jpg)
Modified ABMISpreadsheetpackage 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);}
![Page 45: Object-based Programming Intuitive explanation Using objects to read input Creating objects Style rules](https://reader030.vdocument.in/reader030/viewer/2022032800/56649d425503460f94a1daf0/html5/thumbnails/45.jpg)
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
![Page 46: Object-based Programming Intuitive explanation Using objects to read input Creating objects Style rules](https://reader030.vdocument.in/reader030/viewer/2022032800/56649d425503460f94a1daf0/html5/thumbnails/46.jpg)
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);
}
![Page 47: Object-based Programming Intuitive explanation Using objects to read input Creating objects Style rules](https://reader030.vdocument.in/reader030/viewer/2022032800/56649d425503460f94a1daf0/html5/thumbnails/47.jpg)
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 ();
}
![Page 48: Object-based Programming Intuitive explanation Using objects to read input Creating objects Style rules](https://reader030.vdocument.in/reader030/viewer/2022032800/56649d425503460f94a1daf0/html5/thumbnails/48.jpg)
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.getBMI(
);
– bmiSpreadsheet.print()
• Users share code
• Program element is passive. Users write code to manipulate it.
• It is parameter to method call:– getBMI(bmiSpreadsheet)
– print (bmiSpreadsheet)
• Users write their own code
![Page 49: Object-based Programming Intuitive explanation Using objects to read input Creating objects Style rules](https://reader030.vdocument.in/reader030/viewer/2022032800/56649d425503460f94a1daf0/html5/thumbnails/49.jpg)
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, ...
![Page 50: Object-based Programming Intuitive explanation Using objects to read input Creating objects Style rules](https://reader030.vdocument.in/reader030/viewer/2022032800/56649d425503460f94a1daf0/html5/thumbnails/50.jpg)
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
![Page 51: Object-based Programming Intuitive explanation Using objects to read input Creating objects Style rules](https://reader030.vdocument.in/reader030/viewer/2022032800/56649d425503460f94a1daf0/html5/thumbnails/51.jpg)
ABMIDriverpublic 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
![Page 52: Object-based Programming Intuitive explanation Using objects to read input Creating objects Style rules](https://reader030.vdocument.in/reader030/viewer/2022032800/56649d425503460f94a1daf0/html5/thumbnails/52.jpg)
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
![Page 53: Object-based Programming Intuitive explanation Using objects to read input Creating objects Style rules](https://reader030.vdocument.in/reader030/viewer/2022032800/56649d425503460f94a1daf0/html5/thumbnails/53.jpg)
UI vs. Debugging Code
public void setWeight(double newWeight) {System.out.println(“Weight: “ + weight);weight = newWeight;
}
![Page 54: Object-based Programming Intuitive explanation Using objects to read input Creating objects Style rules](https://reader030.vdocument.in/reader030/viewer/2022032800/56649d425503460f94a1daf0/html5/thumbnails/54.jpg)
UI vs. Debugging Code
public void getBMI() {System.out.println(“BMI returned: “ + getBMI()); weight/(height*height);
}
Infinite recursion!
![Page 55: Object-based Programming Intuitive explanation Using objects to read input Creating objects Style rules](https://reader030.vdocument.in/reader030/viewer/2022032800/56649d425503460f94a1daf0/html5/thumbnails/55.jpg)
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
![Page 56: Object-based Programming Intuitive explanation Using objects to read input Creating objects Style rules](https://reader030.vdocument.in/reader030/viewer/2022032800/56649d425503460f94a1daf0/html5/thumbnails/56.jpg)
Method Invocation Syntax
System.out.println(bmiSpreadsheet.getBMI())
Method Name
Target Object
getBMI()Internal Call
External Call
![Page 57: Object-based Programming Intuitive explanation Using objects to read input Creating objects Style rules](https://reader030.vdocument.in/reader030/viewer/2022032800/56649d425503460f94a1daf0/html5/thumbnails/57.jpg)
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
![Page 58: Object-based Programming Intuitive explanation Using objects to read input Creating objects Style rules](https://reader030.vdocument.in/reader030/viewer/2022032800/56649d425503460f94a1daf0/html5/thumbnails/58.jpg)
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
![Page 59: Object-based Programming Intuitive explanation Using objects to read input Creating objects Style rules](https://reader030.vdocument.in/reader030/viewer/2022032800/56649d425503460f94a1daf0/html5/thumbnails/59.jpg)
ABMISpreadsheet
Other class
ABMISpreadsheet Instance
weight height
setWeight()
new Weight
calls
writes
setHeight()
new Height
calls
writes
height
getHeight()
calls
reads
getWeight()
reads
weight calls
reads
getBMI()
![Page 60: Object-based Programming Intuitive explanation Using objects to read input Creating objects Style rules](https://reader030.vdocument.in/reader030/viewer/2022032800/56649d425503460f94a1daf0/html5/thumbnails/60.jpg)
AnotherBMISpreadsheet
Other class
ABMISpreadsheet Instance
weight height
setWeight()
new Weight
calls
writes
setHeight()
new Height
calls
writes
height
getHeight()
calls
reads
getWeight()
reads
weight calls
getBMI()
bmi
reads
![Page 61: Object-based Programming Intuitive explanation Using objects to read input Creating objects Style rules](https://reader030.vdocument.in/reader030/viewer/2022032800/56649d425503460f94a1daf0/html5/thumbnails/61.jpg)
AnotherBMISpreadsheet
Other class
ABMISpreadsheet Instance
weight height
setWeight()
new Weight
calls
writes
setHeight()
new Height
calls
writes
height
getHeight()
calls
reads
getWeight()
reads
weight calls
getBMI()
bmi
reads
![Page 62: Object-based Programming Intuitive explanation Using objects to read input Creating objects Style rules](https://reader030.vdocument.in/reader030/viewer/2022032800/56649d425503460f94a1daf0/html5/thumbnails/62.jpg)
Methods that Changes
Other class
AnotherBMISpreadsheet Instance
weight height
setWeight()
new Weight
calls
writes
setHeight()
new Height
calls
writes
getBMI()
bmi
reads
![Page 63: Object-based Programming Intuitive explanation Using objects to read input Creating objects Style rules](https://reader030.vdocument.in/reader030/viewer/2022032800/56649d425503460f94a1daf0/html5/thumbnails/63.jpg)
setWeight()
Other class
AnotherBMISpreadsheet Instance
weight
setWeight()
new Weight
calls
writes
bmi
public void setWeight(double newWeight) {weight = newWeight;bmi = weight / (height*height);
}
![Page 64: Object-based Programming Intuitive explanation Using objects to read input Creating objects Style rules](https://reader030.vdocument.in/reader030/viewer/2022032800/56649d425503460f94a1daf0/html5/thumbnails/64.jpg)
setHeight()
Other class
AnotherBMISpreadsheet Instance
height
setHeight()
new Height
calls
writes
bmi
public void setHeight(double newHeight) {height = newHeight;bmi = weight / (height*height);
}
![Page 65: Object-based Programming Intuitive explanation Using objects to read input Creating objects Style rules](https://reader030.vdocument.in/reader030/viewer/2022032800/56649d425503460f94a1daf0/html5/thumbnails/65.jpg)
getBMI()
Other class
AnotherBMISpreadsheet Instance
getBMI()
bmi
reads
public double getBMI() {return bmi;
}
![Page 66: Object-based Programming Intuitive explanation Using objects to read input Creating objects Style rules](https://reader030.vdocument.in/reader030/viewer/2022032800/56649d425503460f94a1daf0/html5/thumbnails/66.jpg)
Complete Codepackage 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;}
}
![Page 67: Object-based Programming Intuitive explanation Using objects to read input Creating objects Style rules](https://reader030.vdocument.in/reader030/viewer/2022032800/56649d425503460f94a1daf0/html5/thumbnails/67.jpg)
Similarities in the two Classespackage 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);}
}
![Page 68: Object-based Programming Intuitive explanation Using objects to read input Creating objects Style rules](https://reader030.vdocument.in/reader030/viewer/2022032800/56649d425503460f94a1daf0/html5/thumbnails/68.jpg)
Similarities in the two Classespackage 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;}
}
![Page 69: Object-based Programming Intuitive explanation Using objects to read input Creating objects Style rules](https://reader030.vdocument.in/reader030/viewer/2022032800/56649d425503460f94a1daf0/html5/thumbnails/69.jpg)
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();
}
![Page 70: Object-based Programming Intuitive explanation Using objects to read input Creating objects Style rules](https://reader030.vdocument.in/reader030/viewer/2022032800/56649d425503460f94a1daf0/html5/thumbnails/70.jpg)
package bmipublic 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
![Page 71: Object-based Programming Intuitive explanation Using objects to read input Creating objects Style rules](https://reader030.vdocument.in/reader030/viewer/2022032800/56649d425503460f94a1daf0/html5/thumbnails/71.jpg)
manufactures
Real-World Analogy
AccordSpecification
implements
![Page 72: Object-based Programming Intuitive explanation Using objects to read input Creating objects Style rules](https://reader030.vdocument.in/reader030/viewer/2022032800/56649d425503460f94a1daf0/html5/thumbnails/72.jpg)
Interface
AnotherBMISpreadsheet
Instance
AnotherBMISpreadsheet instance of
AnotherBMISpreadsheet
Instance
ABMISpreadsheet instance of
ABMISpreadsheet
Instance
ABMISpreadsheet
InstanceBMISpreadsheet
implements
![Page 73: Object-based Programming Intuitive explanation Using objects to read input Creating objects Style rules](https://reader030.vdocument.in/reader030/viewer/2022032800/56649d425503460f94a1daf0/html5/thumbnails/73.jpg)
Using Interface to Classify
BMISpreadsheet
Instance
AnotherBMISpreadsheet instance of
BMISpreadsheet
Instance
ABMISpreadsheet instance of
BMISpreadsheet
Instance
BMISpreadsheet
InstanceBMISpreadsheet
implements
![Page 74: Object-based Programming Intuitive explanation Using objects to read input Creating objects Style rules](https://reader030.vdocument.in/reader030/viewer/2022032800/56649d425503460f94a1daf0/html5/thumbnails/74.jpg)
Using Car Specification to Classify
manufactures
AccordSpecification
implements
AccordAccord
AccordAccord
![Page 75: Object-based Programming Intuitive explanation Using objects to read input Creating objects Style rules](https://reader030.vdocument.in/reader030/viewer/2022032800/56649d425503460f94a1daf0/html5/thumbnails/75.jpg)
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);
}
![Page 76: Object-based Programming Intuitive explanation Using objects to read input Creating objects Style rules](https://reader030.vdocument.in/reader030/viewer/2022032800/56649d425503460f94a1daf0/html5/thumbnails/76.jpg)
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?
![Page 77: Object-based Programming Intuitive explanation Using objects to read input Creating objects Style rules](https://reader030.vdocument.in/reader030/viewer/2022032800/56649d425503460f94a1daf0/html5/thumbnails/77.jpg)
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);
}
![Page 78: Object-based Programming Intuitive explanation Using objects to read input Creating objects Style rules](https://reader030.vdocument.in/reader030/viewer/2022032800/56649d425503460f94a1daf0/html5/thumbnails/78.jpg)
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());
}}
![Page 79: Object-based Programming Intuitive explanation Using objects to read input Creating objects Style rules](https://reader030.vdocument.in/reader030/viewer/2022032800/56649d425503460f94a1daf0/html5/thumbnails/79.jpg)
Changing the classpackage 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!
![Page 80: Object-based Programming Intuitive explanation Using objects to read input Creating objects Style rules](https://reader030.vdocument.in/reader030/viewer/2022032800/56649d425503460f94a1daf0/html5/thumbnails/80.jpg)
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.
![Page 81: Object-based Programming Intuitive explanation Using objects to read input Creating objects Style rules](https://reader030.vdocument.in/reader030/viewer/2022032800/56649d425503460f94a1daf0/html5/thumbnails/81.jpg)
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
![Page 82: Object-based Programming Intuitive explanation Using objects to read input Creating objects Style rules](https://reader030.vdocument.in/reader030/viewer/2022032800/56649d425503460f94a1daf0/html5/thumbnails/82.jpg)
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?
![Page 83: Object-based Programming Intuitive explanation Using objects to read input Creating objects Style rules](https://reader030.vdocument.in/reader030/viewer/2022032800/56649d425503460f94a1daf0/html5/thumbnails/83.jpg)
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
![Page 84: Object-based Programming Intuitive explanation Using objects to read input Creating objects Style rules](https://reader030.vdocument.in/reader030/viewer/2022032800/56649d425503460f94a1daf0/html5/thumbnails/84.jpg)
Pros and cons of two alternativespublic 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);}
}
![Page 85: Object-based Programming Intuitive explanation Using objects to read input Creating objects Style rules](https://reader030.vdocument.in/reader030/viewer/2022032800/56649d425503460f94a1daf0/html5/thumbnails/85.jpg)
Pros and cons of two alternativespublic 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;}
}
![Page 86: Object-based Programming Intuitive explanation Using objects to read input Creating objects Style rules](https://reader030.vdocument.in/reader030/viewer/2022032800/56649d425503460f94a1daf0/html5/thumbnails/86.jpg)
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.
![Page 87: Object-based Programming Intuitive explanation Using objects to read input Creating objects Style rules](https://reader030.vdocument.in/reader030/viewer/2022032800/56649d425503460f94a1daf0/html5/thumbnails/87.jpg)
Time-Space Tradeoff
Time MiserSpace Miser
Space
Time
![Page 88: Object-based Programming Intuitive explanation Using objects to read input Creating objects Style rules](https://reader030.vdocument.in/reader030/viewer/2022032800/56649d425503460f94a1daf0/html5/thumbnails/88.jpg)
Space
Time
Time MiserSpace Miser
Time-Space Tradeoff
![Page 89: Object-based Programming Intuitive explanation Using objects to read input Creating objects Style rules](https://reader030.vdocument.in/reader030/viewer/2022032800/56649d425503460f94a1daf0/html5/thumbnails/89.jpg)
Relating Interface and Class Names
Class Name:•<Qualifier><Interface> (ABMISpreadsheet, ASpaceEfficientBMISpreadsheet, SpaceEfficientBMISpreadsheet)•<Interface><Qualifier>Impl (BMISpreadsheetImpl, BMISpreadsheetSpaceEfficientImpl)
Interface Name:•<ClassName>Interface (ABMISpreadsheetInterface)
![Page 90: Object-based Programming Intuitive explanation Using objects to read input Creating objects Style rules](https://reader030.vdocument.in/reader030/viewer/2022032800/56649d425503460f94a1daf0/html5/thumbnails/90.jpg)
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 {…}
![Page 91: Object-based Programming Intuitive explanation Using objects to read input Creating objects Style rules](https://reader030.vdocument.in/reader030/viewer/2022032800/56649d425503460f94a1daf0/html5/thumbnails/91.jpg)
Removing Debugging Code
/*System.out.println(newHeight); // debugging
statement*/
System.out.println(newHeight); /*debugging statement */
/*System.out.println(newHeight); /*debugging
statement */*/
![Page 92: Object-based Programming Intuitive explanation Using objects to read input Creating objects Style rules](https://reader030.vdocument.in/reader030/viewer/2022032800/56649d425503460f94a1daf0/html5/thumbnails/92.jpg)
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 {…}
![Page 93: Object-based Programming Intuitive explanation Using objects to read input Creating objects Style rules](https://reader030.vdocument.in/reader030/viewer/2022032800/56649d425503460f94a1daf0/html5/thumbnails/93.jpg)
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
![Page 94: Object-based Programming Intuitive explanation Using objects to read input Creating objects Style rules](https://reader030.vdocument.in/reader030/viewer/2022032800/56649d425503460f94a1daf0/html5/thumbnails/94.jpg)
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
![Page 95: Object-based Programming Intuitive explanation Using objects to read input Creating objects Style rules](https://reader030.vdocument.in/reader030/viewer/2022032800/56649d425503460f94a1daf0/html5/thumbnails/95.jpg)
/* * @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 TagsJavadoc tags
![Page 96: Object-based Programming Intuitive explanation Using objects to read input Creating objects Style rules](https://reader030.vdocument.in/reader030/viewer/2022032800/56649d425503460f94a1daf0/html5/thumbnails/96.jpg)
Improving the Stylepublic 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
![Page 97: Object-based Programming Intuitive explanation Using objects to read input Creating objects Style rules](https://reader030.vdocument.in/reader030/viewer/2022032800/56649d425503460f94a1daf0/html5/thumbnails/97.jpg)
Why Avoid Code Duplication?
• Less Typing
• Changes (Inches, LB)
•can forget change all repetitions
![Page 98: Object-based Programming Intuitive explanation Using objects to read input Creating objects Style rules](https://reader030.vdocument.in/reader030/viewer/2022032800/56649d425503460f94a1daf0/html5/thumbnails/98.jpg)
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);
bmi = (weight/2.2)/(height * 2.54/100*height*2.54/100);
![Page 99: Object-based Programming Intuitive explanation Using objects to read input Creating objects Style rules](https://reader030.vdocument.in/reader030/viewer/2022032800/56649d425503460f94a1daf0/html5/thumbnails/99.jpg)
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 = (weight/2.2)/(height * 2.54/100*height*2.54/100);
}public double getWeight() {
return weight;}public void setWeight(double newWeight) {
weight = newWeight;bmi = (weight/2.2)/(height * 2.54/100*height*2.54/100);
}...
![Page 100: Object-based Programming Intuitive explanation Using objects to read input Creating objects Style rules](https://reader030.vdocument.in/reader030/viewer/2022032800/56649d425503460f94a1daf0/html5/thumbnails/100.jpg)
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); }
![Page 101: Object-based Programming Intuitive explanation Using objects to read input Creating objects Style rules](https://reader030.vdocument.in/reader030/viewer/2022032800/56649d425503460f94a1daf0/html5/thumbnails/101.jpg)
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()
![Page 102: Object-based Programming Intuitive explanation Using objects to read input Creating objects Style rules](https://reader030.vdocument.in/reader030/viewer/2022032800/56649d425503460f94a1daf0/html5/thumbnails/102.jpg)
Only Public Methods in Interfacepackage 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();
}
![Page 103: Object-based Programming Intuitive explanation Using objects to read input Creating objects Style rules](https://reader030.vdocument.in/reader030/viewer/2022032800/56649d425503460f94a1daf0/html5/thumbnails/103.jpg)
Pure Vs Impure FunctionsABMISpreadsheet Instance
getWeight
weight
Body accesses
calculateBMI(77,1.77)
calculateBMI(77,1.77) 24.57
24.57 getWeight()
getWeight()
setWeight(77)
77
71
......setWeight(71)
ABMISpreadsheet Instance
calculateBMI
weight
Body
accesses
height
![Page 104: Object-based Programming Intuitive explanation Using objects to read input Creating objects Style rules](https://reader030.vdocument.in/reader030/viewer/2022032800/56649d425503460f94a1daf0/html5/thumbnails/104.jpg)
Impure Functions
public double calculateBMI(double weight, double height) {System.out.println(“height: “ + height + “weight: “ +
weight)return weight/(height*height);
}
Printing is side effect
![Page 105: Object-based Programming Intuitive explanation Using objects to read input Creating objects Style rules](https://reader030.vdocument.in/reader030/viewer/2022032800/56649d425503460f94a1daf0/html5/thumbnails/105.jpg)
Side Effect
• Printing• Reading input• Changing global
variable
• Benign• Done all the time
– readLine()
• Very dangerous– only if implementing
input
![Page 106: Object-based Programming Intuitive explanation Using objects to read input Creating objects Style rules](https://reader030.vdocument.in/reader030/viewer/2022032800/56649d425503460f94a1daf0/html5/thumbnails/106.jpg)
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?
![Page 107: Object-based Programming Intuitive explanation Using objects to read input Creating objects Style rules](https://reader030.vdocument.in/reader030/viewer/2022032800/56649d425503460f94a1daf0/html5/thumbnails/107.jpg)
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
![Page 108: Object-based Programming Intuitive explanation Using objects to read input Creating objects Style rules](https://reader030.vdocument.in/reader030/viewer/2022032800/56649d425503460f94a1daf0/html5/thumbnails/108.jpg)
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
![Page 109: Object-based Programming Intuitive explanation Using objects to read input Creating objects Style rules](https://reader030.vdocument.in/reader030/viewer/2022032800/56649d425503460f94a1daf0/html5/thumbnails/109.jpg)
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);}
}
![Page 110: Object-based Programming Intuitive explanation Using objects to read input Creating objects Style rules](https://reader030.vdocument.in/reader030/viewer/2022032800/56649d425503460f94a1daf0/html5/thumbnails/110.jpg)
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
![Page 111: Object-based Programming Intuitive explanation Using objects to read input Creating objects Style rules](https://reader030.vdocument.in/reader030/viewer/2022032800/56649d425503460f94a1daf0/html5/thumbnails/111.jpg)
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
![Page 112: Object-based Programming Intuitive explanation Using objects to read input Creating objects Style rules](https://reader030.vdocument.in/reader030/viewer/2022032800/56649d425503460f94a1daf0/html5/thumbnails/112.jpg)
Literals Vs Named Constants Vs Variables
Use constants for program values that do not change.– Use named constants for magic numbers
![Page 113: Object-based Programming Intuitive explanation Using objects to read input Creating objects Style rules](https://reader030.vdocument.in/reader030/viewer/2022032800/56649d425503460f94a1daf0/html5/thumbnails/113.jpg)
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);
![Page 114: Object-based Programming Intuitive explanation Using objects to read input Creating objects Style rules](https://reader030.vdocument.in/reader030/viewer/2022032800/56649d425503460f94a1daf0/html5/thumbnails/114.jpg)
• Human-created constant is a magic number.
• Natural constant may be a magic number to some.
What is a magic number?
![Page 115: Object-based Programming Intuitive explanation Using objects to read input Creating objects Style rules](https://reader030.vdocument.in/reader030/viewer/2022032800/56649d425503460f94a1daf0/html5/thumbnails/115.jpg)
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
![Page 116: Object-based Programming Intuitive explanation Using objects to read input Creating objects Style rules](https://reader030.vdocument.in/reader030/viewer/2022032800/56649d425503460f94a1daf0/html5/thumbnails/116.jpg)
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
![Page 117: Object-based Programming Intuitive explanation Using objects to read input Creating objects Style rules](https://reader030.vdocument.in/reader030/viewer/2022032800/56649d425503460f94a1daf0/html5/thumbnails/117.jpg)
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;
}
![Page 118: Object-based Programming Intuitive explanation Using objects to read input Creating objects Style rules](https://reader030.vdocument.in/reader030/viewer/2022032800/56649d425503460f94a1daf0/html5/thumbnails/118.jpg)
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
![Page 119: Object-based Programming Intuitive explanation Using objects to read input Creating objects Style rules](https://reader030.vdocument.in/reader030/viewer/2022032800/56649d425503460f94a1daf0/html5/thumbnails/119.jpg)
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
![Page 120: Object-based Programming Intuitive explanation Using objects to read input Creating objects Style rules](https://reader030.vdocument.in/reader030/viewer/2022032800/56649d425503460f94a1daf0/html5/thumbnails/120.jpg)
Identifier Scope
• Region of code where the identifier is visible.
• Arbitrary scopes not possible
![Page 121: Object-based Programming Intuitive explanation Using objects to read input Creating objects Style rules](https://reader030.vdocument.in/reader030/viewer/2022032800/56649d425503460f94a1daf0/html5/thumbnails/121.jpg)
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
![Page 122: Object-based Programming Intuitive explanation Using objects to read input Creating objects Style rules](https://reader030.vdocument.in/reader030/viewer/2022032800/56649d425503460f94a1daf0/html5/thumbnails/122.jpg)
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
height Scope
![Page 123: Object-based Programming Intuitive explanation Using objects to read input Creating objects Style rules](https://reader030.vdocument.in/reader030/viewer/2022032800/56649d425503460f94a1daf0/html5/thumbnails/123.jpg)
Overriding Scopes
• Narrower scope overrides definitions in more general scope
![Page 124: Object-based Programming Intuitive explanation Using objects to read input Creating objects Style rules](https://reader030.vdocument.in/reader030/viewer/2022032800/56649d425503460f94a1daf0/html5/thumbnails/124.jpg)
public class AnotherBMISpreadsheet implements BMISpreadsheet{double height, weight, bmi;public double getHeight() {
return height;}...
}
Scope of Public Items getHeight() Scope
ABMISpreadsheetDriver
ABMISpreadsheet
![Page 125: Object-based Programming Intuitive explanation Using objects to read input Creating objects Style rules](https://reader030.vdocument.in/reader030/viewer/2022032800/56649d425503460f94a1daf0/html5/thumbnails/125.jpg)
public class ABMISpreadsheet implements BMISpreadsheet {
public double height, weight, bmi;...
}
Non Public Instance Variables
![Page 126: Object-based Programming Intuitive explanation Using objects to read input Creating objects Style rules](https://reader030.vdocument.in/reader030/viewer/2022032800/56649d425503460f94a1daf0/html5/thumbnails/126.jpg)
public class ABMISpreadsheetWithPublicVariables {
public double height, weight, bmi;
...
}
Making Instance Variables Public
Other Classes
![Page 127: Object-based Programming Intuitive explanation Using objects to read input Creating objects Style rules](https://reader030.vdocument.in/reader030/viewer/2022032800/56649d425503460f94a1daf0/html5/thumbnails/127.jpg)
public class ABMISpreadsheetWithPublicVariables {
public double height, weight;
...
}
Hard to Change Representation
Other Classes
![Page 128: Object-based Programming Intuitive explanation Using objects to read input Creating objects Style rules](https://reader030.vdocument.in/reader030/viewer/2022032800/56649d425503460f94a1daf0/html5/thumbnails/128.jpg)
public class ABMISpreadsheetWithPublicVariables {
public double height, weight, bmi;
...
}
Internal constraints violated
Other Classes
bmiSpreadsheet.height = 5.0;
System.out.println (bmiSpreadsheet.bmi);
![Page 129: Object-based Programming Intuitive explanation Using objects to read input Creating objects Style rules](https://reader030.vdocument.in/reader030/viewer/2022032800/56649d425503460f94a1daf0/html5/thumbnails/129.jpg)
Do not make instance variables public– Expose them through public methods
Encapsulation Principle
![Page 130: Object-based Programming Intuitive explanation Using objects to read input Creating objects Style rules](https://reader030.vdocument.in/reader030/viewer/2022032800/56649d425503460f94a1daf0/html5/thumbnails/130.jpg)
Variable Scope
• Least privilege =>– Keep scope of variable as small as possible
• no public variables
• as few global variables as possible
![Page 131: Object-based Programming Intuitive explanation Using objects to read input Creating objects Style rules](https://reader030.vdocument.in/reader030/viewer/2022032800/56649d425503460f94a1daf0/html5/thumbnails/131.jpg)
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.
![Page 132: Object-based Programming Intuitive explanation Using objects to read input Creating objects Style rules](https://reader030.vdocument.in/reader030/viewer/2022032800/56649d425503460f94a1daf0/html5/thumbnails/132.jpg)
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
![Page 133: Object-based Programming Intuitive explanation Using objects to read input Creating objects Style rules](https://reader030.vdocument.in/reader030/viewer/2022032800/56649d425503460f94a1daf0/html5/thumbnails/133.jpg)
public final double CMS_IN_INCH = 2.54;
Public ConstantsInconsistent value cannot be stored
Implementation Independent
public interface BMISpreadsheet {
}
ABMISpreadsheet AnotherBMISpreadsheet
![Page 134: Object-based Programming Intuitive explanation Using objects to read input Creating objects Style rules](https://reader030.vdocument.in/reader030/viewer/2022032800/56649d425503460f94a1daf0/html5/thumbnails/134.jpg)
Declare implementation-independent named constants in interfaces– implementing classes can access them.
Principle
![Page 135: Object-based Programming Intuitive explanation Using objects to read input Creating objects Style rules](https://reader030.vdocument.in/reader030/viewer/2022032800/56649d425503460f94a1daf0/html5/thumbnails/135.jpg)
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
![Page 136: Object-based Programming Intuitive explanation Using objects to read input Creating objects Style rules](https://reader030.vdocument.in/reader030/viewer/2022032800/56649d425503460f94a1daf0/html5/thumbnails/136.jpg)
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
![Page 137: Object-based Programming Intuitive explanation Using objects to read input Creating objects Style rules](https://reader030.vdocument.in/reader030/viewer/2022032800/56649d425503460f94a1daf0/html5/thumbnails/137.jpg)
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
![Page 138: Object-based Programming Intuitive explanation Using objects to read input Creating objects Style rules](https://reader030.vdocument.in/reader030/viewer/2022032800/56649d425503460f94a1daf0/html5/thumbnails/138.jpg)
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
![Page 139: Object-based Programming Intuitive explanation Using objects to read input Creating objects Style rules](https://reader030.vdocument.in/reader030/viewer/2022032800/56649d425503460f94a1daf0/html5/thumbnails/139.jpg)
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
![Page 140: Object-based Programming Intuitive explanation Using objects to read input Creating objects Style rules](https://reader030.vdocument.in/reader030/viewer/2022032800/56649d425503460f94a1daf0/html5/thumbnails/140.jpg)
Instance-independent Instantiationpackage 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);}
![Page 141: Object-based Programming Intuitive explanation Using objects to read input Creating objects Style rules](https://reader030.vdocument.in/reader030/viewer/2022032800/56649d425503460f94a1daf0/html5/thumbnails/141.jpg)
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.
![Page 142: Object-based Programming Intuitive explanation Using objects to read input Creating objects Style rules](https://reader030.vdocument.in/reader030/viewer/2022032800/56649d425503460f94a1daf0/html5/thumbnails/142.jpg)
Instance-dependent Instantiationpackage 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
![Page 143: Object-based Programming Intuitive explanation Using objects to read input Creating objects Style rules](https://reader030.vdocument.in/reader030/viewer/2022032800/56649d425503460f94a1daf0/html5/thumbnails/143.jpg)
ConstructorSpecial 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
![Page 144: Object-based Programming Intuitive explanation Using objects to read input Creating objects Style rules](https://reader030.vdocument.in/reader030/viewer/2022032800/56649d425503460f94a1daf0/html5/thumbnails/144.jpg)
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)
![Page 145: Object-based Programming Intuitive explanation Using objects to read input Creating objects Style rules](https://reader030.vdocument.in/reader030/viewer/2022032800/56649d425503460f94a1daf0/html5/thumbnails/145.jpg)
Default Constructor
• Every class must have a constructor
• Java automatically creates one if we do not
![Page 146: Object-based Programming Intuitive explanation Using objects to read input Creating objects Style rules](https://reader030.vdocument.in/reader030/viewer/2022032800/56649d425503460f94a1daf0/html5/thumbnails/146.jpg)
Default Constructorpackage 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()
![Page 147: Object-based Programming Intuitive explanation Using objects to read input Creating objects Style rules](https://reader030.vdocument.in/reader030/viewer/2022032800/56649d425503460f94a1daf0/html5/thumbnails/147.jpg)
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)
![Page 148: Object-based Programming Intuitive explanation Using objects to read input Creating objects Style rules](https://reader030.vdocument.in/reader030/viewer/2022032800/56649d425503460f94a1daf0/html5/thumbnails/148.jpg)
class ABMISpreadsheet {double height, weight;static double getBMI() {
return (height*heigh)/weight}
Errors
Syntax Error
Semantics Error
Logic Error
Access Error
![Page 149: Object-based Programming Intuitive explanation Using objects to read input Creating objects Style rules](https://reader030.vdocument.in/reader030/viewer/2022032800/56649d425503460f94a1daf0/html5/thumbnails/149.jpg)
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
![Page 150: Object-based Programming Intuitive explanation Using objects to read input Creating objects Style rules](https://reader030.vdocument.in/reader030/viewer/2022032800/56649d425503460f94a1daf0/html5/thumbnails/150.jpg)
Another Object-based Problem
![Page 151: Object-based Programming Intuitive explanation Using objects to read input Creating objects Style rules](https://reader030.vdocument.in/reader030/viewer/2022032800/56649d425503460f94a1daf0/html5/thumbnails/151.jpg)
Another Example: Point
X.
Y R
.
![Page 152: Object-based Programming Intuitive explanation Using objects to read input Creating objects Style rules](https://reader030.vdocument.in/reader030/viewer/2022032800/56649d425503460f94a1daf0/html5/thumbnails/152.jpg)
Solutionpublic 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() { … };
…}}
![Page 153: Object-based Programming Intuitive explanation Using objects to read input Creating objects Style rules](https://reader030.vdocument.in/reader030/viewer/2022032800/56649d425503460f94a1daf0/html5/thumbnails/153.jpg)
Related Problem
![Page 154: Object-based Programming Intuitive explanation Using objects to read input Creating objects Style rules](https://reader030.vdocument.in/reader030/viewer/2022032800/56649d425503460f94a1daf0/html5/thumbnails/154.jpg)
Related Solutionpublic 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() { … };
…}}
![Page 155: Object-based Programming Intuitive explanation Using objects to read input Creating objects Style rules](https://reader030.vdocument.in/reader030/viewer/2022032800/56649d425503460f94a1daf0/html5/thumbnails/155.jpg)
Drawbacks of monolithic solutions
• Read code in main method– cannot return two values from a method
• Calculation code not reusable
![Page 156: Object-based Programming Intuitive explanation Using objects to read input Creating objects Style rules](https://reader030.vdocument.in/reader030/viewer/2022032800/56649d425503460f94a1daf0/html5/thumbnails/156.jpg)
Point Interface
public interface Point {public int getX(); public int getY(); public double getAngle(); public double getRadius();
}
![Page 157: Object-based Programming Intuitive explanation Using objects to read input Creating objects Style rules](https://reader030.vdocument.in/reader030/viewer/2022032800/56649d425503460f94a1daf0/html5/thumbnails/157.jpg)
Point Representations
• X, Y (Cartesian Representation)
• Radius, Angle (Polar Representation)
• X, Radius
• X, Y, Radius, Angle
• ….
![Page 158: Object-based Programming Intuitive explanation Using objects to read input Creating objects Style rules](https://reader030.vdocument.in/reader030/viewer/2022032800/56649d425503460f94a1daf0/html5/thumbnails/158.jpg)
Algorithms
X.
Y R
.
R = sqrt (X2 * Y2)
= arctan (Y/X)
Cartesian Representation
Polar Representation
X = R*cos()
Y = R*sin()
![Page 159: Object-based Programming Intuitive explanation Using objects to read input Creating objects Style rules](https://reader030.vdocument.in/reader030/viewer/2022032800/56649d425503460f94a1daf0/html5/thumbnails/159.jpg)
Class: ACartesianPointpublic 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);}
}
![Page 160: Object-based Programming Intuitive explanation Using objects to read input Creating objects Style rules](https://reader030.vdocument.in/reader030/viewer/2022032800/56649d425503460f94a1daf0/html5/thumbnails/160.jpg)
Class: APolarPointpublic 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;}
}
![Page 161: Object-based Programming Intuitive explanation Using objects to read input Creating objects Style rules](https://reader030.vdocument.in/reader030/viewer/2022032800/56649d425503460f94a1daf0/html5/thumbnails/161.jpg)
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.
![Page 162: Object-based Programming Intuitive explanation Using objects to read input Creating objects Style rules](https://reader030.vdocument.in/reader030/viewer/2022032800/56649d425503460f94a1daf0/html5/thumbnails/162.jpg)
Shared main methodpublic static void main (String args[]) { Point highest = getPoint(); print (highest); }
![Page 163: Object-based Programming Intuitive explanation Using objects to read input Creating objects Style rules](https://reader030.vdocument.in/reader030/viewer/2022032800/56649d425503460f94a1daf0/html5/thumbnails/163.jpg)
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()); }
![Page 164: Object-based Programming Intuitive explanation Using objects to read input Creating objects Style rules](https://reader030.vdocument.in/reader030/viewer/2022032800/56649d425503460f94a1daf0/html5/thumbnails/164.jpg)
Get method Cartesian
public static Point getPoint () { System.out.println("Please Enter Cartesian Coordinates of Highest Point"); return new ACartesianPoint (readDouble(),readDouble()); }
![Page 165: Object-based Programming Intuitive explanation Using objects to read input Creating objects Style rules](https://reader030.vdocument.in/reader030/viewer/2022032800/56649d425503460f94a1daf0/html5/thumbnails/165.jpg)
Get method Polar
public static Point getPoint () { System.out.println("Please Enter Polar Coordinates of Highest Point"); return new APolarPoint (readDouble(), readDouble()); }
![Page 166: Object-based Programming Intuitive explanation Using objects to read input Creating objects Style rules](https://reader030.vdocument.in/reader030/viewer/2022032800/56649d425503460f94a1daf0/html5/thumbnails/166.jpg)
Bottom-up Programming with Coarse-grained steps
Interface
Class
Used in ABMISpreadsheet example
![Page 167: Object-based Programming Intuitive explanation Using objects to read input Creating objects Style rules](https://reader030.vdocument.in/reader030/viewer/2022032800/56649d425503460f94a1daf0/html5/thumbnails/167.jpg)
Top-Down with Finer Steps
Interface
Representation
Algorithm
Class
Used in Point example
![Page 168: Object-based Programming Intuitive explanation Using objects to read input Creating objects Style rules](https://reader030.vdocument.in/reader030/viewer/2022032800/56649d425503460f94a1daf0/html5/thumbnails/168.jpg)
Real life
• Define initial interface
• Modify it incrementally as you change