Title: ProgrammerDefined Types
1Programmer-Defined Types
- Object Types as Programmer-defined Types
- Two-way Dependencies
- Representation Errors
- Primitive Vs Object Properties
- Constructors
- Class variables/methods
- Top-Down, Bottom-up, Middle-out Programming
2Loan Object
3Properties Classification
public class ABMISpreadsheet double
height public double getHeight() return
height public void setHeight(double
newHeight) height newHeight double
weight public double getWeight() return
weight public void setWeight(double
newWeight) weight newWeight public
double getBMI() return weight/(heightheight)
Height
Weight
BMI
4Read-Only Vs Editable, Dependent Vs Independent
Dependent
51-Way Vs. 2-Way Dependencies
Height
BMI
Weight
Monthly Interest
Principle
Yearly Interest
6Top-Down Programming
Interface
Representation
Algorithm
Class
7Bottom-up Programming in BMI
Interface
Class
8Loan Object
9Loan Interface
public interface Loan public int
getPrincipal() public void setPrincipal(int
newValue) public int getYearlyInterest()
public void setYearlyInterest(int newVal)
public int getMonthlyInterest() public void
setMonthlyInterest(int newVal)
10A Loan Representation
write
read
double principal
11Stored Vs Computed
Dependent
12Loan Algorithm
F1
write
read
F1-1
int principal
F2-1
F2
13Setting and Getting Stored Property
int principal
14Setting and Getting Stored Property
int principal
15Getting and Setting Computed Property (edit)
F1
write
read
F1-1
int principal
16Getting and Setting Computed Property (edited)
F1
write
read
F1-1
int principal
17Getting and Setting Computed Property(soln)
F1
write
read
F1-1
int principal
18Getting and Setting Computed Property (edit)
int principal
F2-1
F2
19Getting and Setting Computed Property (edited)
int principal
F2-1
F2
20Getting and Setting Computed Property(soln)
int principal
F2-1
F2
21Modified Loan Interface
public interface Loan public final int
INTEREST_RATE 6 public int getPrincipal()
public void setPrincipal(int newValue) public
int getYearlyInterest() public void
setYearlyInterest(int newVal) public int
getMonthlyInterest() public void
setMonthlyInterest(int newVal)
22Middle-Out Programming
Interface
Representation
Algorithm
Class
23Another Loan Representation
write
read
int yearlyInterest
24Conversion Errors with Principal Repn.
25No Conversion Errors with YearlyInterest Repn.
26Loan Pair
27Primitive Vs. Object Properties
Primitive Properties
Car Loan
Object Properties
House Loan
Loan Pair
Reusing Loan!
Total Loan
28Loan Pair Interface (edit)
public interface LoanPair ...
29Loan Pair Interface (edited)
public interface LoanPair ...
30Typing Objects
31Kinds of Types
Types
ABMISpreadsheet
AnotherBMISpreadsheet
ALoan AnotherLoan
Interfaces
BMISpreadsheet Loan
Type Set of operations
32Loan Pair Interface (soln)
public interface LoanPair public Loan
getCarLoan() public void setCarLoan(Loan
newValue) public Loan getHouseLoan()
public void setHouseLoan(Loan newValue)
public Loan getTotalLoan()
33Loan Pair Interface Restricted
public interface LoanPair public Loan
getCarLoan() public void setCarLoan(ALoan
newValue) public Loan getHouseLoan()
public void setHouseLoan(ALoan newValue)
public Loan getTotalLoan()
34Typing an Object
- Use interface rather than class to type object
variables. - gt Define interfaces for all classes that include
headers of all public method
35Programmer-defined Vs Predefined Types
- Programmer-defined interface/class (Loan/ALoan)
is programmer-defined type. - Programmer-defined types in Java must be object
types - Some object types are predefined (String)
- All primitive types are predefined.
36Space Efficient Representation
Dependent
Stored
Computed
37Space Efficient Representation
Loan carLoan
Loan houseLoan
38Getter Method
Loan carLoan
39Default Values for Variables
40Invoking methods on null
- carLoan.getPrincipal()
- null pointer exception
- Exception is an unexpected event (error)
- Guilty method will be terminated and exception
reported. - Will see other exceptions later.
41Getter Method
Loan carLoan
42ObjectEditor Display of Null
43How to initialize object variable?
Loan carLoan
44Initialization of Object Variables
Loan carLoan new ALoan()
Loan houseLoan new AnotherLoan()
Loan houseLoan new Loan()
45Interactive Vs Programmed Instantiation
new ALoan()
46Programmed Instantiation
- new C(...) creates an instance of class C
- is actual parameters. C is method and class
name. - cannot instantiate interface
- new Loan() is illegal
- must order from a factory
- cannot order car from specification
47ALoanPair
Loan carLoan new ALoan()
Loan houseLoan new AnotherLoan()
48getTotalLoan()
Loan carLoan new ALoan()
Loan houseLoan new AnotherLoan()
49Programmer-Defined Add Algorithm
- public Loan add(Loan loan1, Loan loan2)
- // create Loan instance
- // set one of its properties to sum of
corresponding properties of loan 1 and loan2 - // other properties are dependent and will
be set automatically - // return Loan instance
50Programmer-Defined Add
public Loan add(Loan loan1, Loan loan2)
Loan retVal new ALoan()
retVal.setPrincipal(loan1.getPrincipal()
loan2.getPrincipal()) return retVal
public Loan add(Loan loan1, Loan loan2)
Loan retVal new ALoan()
retVal.setYearlyInterest(loan1.getYearlyInterest()
loan2.getYearlyInterest()) return
retVal
public Loan add(Loan loan1, Loan loan2)
Loan retVal new ALoan()
retVal.setMonthlyInterest(loan1.getMonthlyInterest
() loan2.getMonthlyInterest())
return retVal
51Returning AnotherLoan()
public Loan add(Loan loan1, Loan loan2)
Loan retVal new AnotherLoan()
retVal.setPrincipal(loan1.getPrincipal()
loan2.getPrincipal()) return retVal
public Loan add(Loan loan1, Loan loan2)
Loan retVal new AnotherLoan()
retVal.setYearlyInterest(loan1.getYearlyInterest()
loan2.getYearlyInterest()) return
retVal
public Loan add(Loan loan1, Loan loan2)
Loan retVal new AnotherLoan()
retVal.setMonthlyInterest(loan1.getMonthlyInterest
() loan2.getMonthlyInterest())
return retVal
52Combining Object Creation and Initialization
public Loan add(Loan loan1, Loan loan2)
Loan retVal new ALoan()
retVal.setPrincipal(loan1.getPrincipal()
loan2.getPrincipal()) return retVal
public Loan add(Loan loan1, Loan loan2)
return new ALoan(loan1.getPrincipal()
loan2.getPrincipal()))
53Adding Constructor in ALoan
getYearly interest()
write
read
int principal
getPrincipal()
getMonthly Interest()
54Constructor Vs Regular Method
public class ALoan implements Loan int
principal public int getPrincipal()
return principal public void setPrincipal(int
newVal) principal newVal public int
getYearlyInterest() return
principalINTEREST_RATE/100 public void
setYearlyInterest(int newVal) principal
newVal100/INTEREST_RATE public int
getMonthlyInterest() return
getYearlyInterest()/12 public void
setMonthlyInterest(int newVal)
setYearlyInterest(newVal12)
public ALoan(int initPrincipal)
setPrincipal(initPrincipal)
public ALoan ALoan(int initPrincipal)
setPrincipal(initPrincipal)
55Instantiation Requires Parameters
new ALoan()
new ALoan(10000)
56Constructor
- Method that constructs a new object based on its
parameters. - new ALoan(10000)
- Actually, just initializes object
- Object constructed by Java
- Front-end to object construction.
- Cannot be declared in interface
- Chooses implementation
57Multi-Parameter Constructor
public ALoanPair (Loan initCarLoan, Loan
initHouseLoan) setCarLoan(initCarLoan)
setHouseLoan(initHouseLoan)
new ALoanPair(new ALoan(10000), new ALoan(10000))
Usually as many constructor parameters as are
required to initialize independent instance
variables.
58Default Constructor
public class ALoan implements Loan int
principal public int getPrincipal()
return principal public void setPrincipal(int
newVal) principal newVal public int
getYearlyInterest() return
principalINTEREST_RATE/100 public void
setYearlyInterest(int newVal) principal
newVal100/INTEREST_RATE public int
getMonthlyInterest() return
getYearlyInterest()/12 public void
setMonthlyInterest(int newVal)
setYearlyInterest(newVal12)
public ALoan()
new ALoan()
59Real-World Analogy
new AnAccord(beige)
new AnAccord(grey)
60O-O Word
new ALoan(10000)
ALoan
carLoan
houseLoan
new ALoan(100000)
61Class Methods
- Methods can be invoked on class itself.
- Called class or static methods
- Declared in class on which they are invoked.
- Keyword static in header.
- Accesses no instance variable.
- Header cannot appear in interface.
62Programmer-Defined Add
Instance Method
public Loan getTotalLoan() return
ALoan.add (houseLoan, carLoan)
Class Method
public static Loan add(Loan loan1, Loan loan2)
return new ALoan(loan1.getPrincipal()
loan2.getPrincipal()))
63External Call of Class Method
ALoan.add(carLoan, houseLoan)
Math.round(5.7)
64numInstances() Algorithm
Declare variable, numInstances initialized to zero
65returning numLoans
getNumLoans() returns numInstances
66Incrementing getNumLoans (edit)
???
Increment numInstances each time a new instance
is created
67Incrementing getNumLoans (edited)
???
Increment numInstances each time a new instance
is created
68Incrementing getNumLoans (soln)
public ALoan(int initPrincipal)
setPrincipal(initPrincipal)
numInstances numInstances 1
Increment numInstances each time a new instance
is created
69Declaring numInstances
Declare variable, numInstances initialized to zero
// instance variable
70Loan Object
memory
int numInstances 0 public ALoan(int
initPrincipal) setPrincipal(initPrinci
pal) numInstances numInstances 1
71Loan Object
memory
static int numInstances 0 public ALoan(int
initPrincipal) setPrincipal(initPrinci
pal) numInstances numInstances 1
72Instance Vs Class Members
- Class Members
- Class methods
- Class variables
- Instance Members
- Instance methods
- Instance variables
73Scope of Class and Instance Members
- Class Members
- visible to other class members
- visible to all instance members
- class instance methods can access class
variables - class and instance methods can call class methods
- Instance Members
- visible to other instance members
- not visible to class members
- which of (zero to many) copies of an instance
variable should a class member refer to?
74Legal Illegal Accesses
static int numInstances 0
int principal
public static int getNumLoans()
System.out.println(principal) return
numInstances
public ALoan(int initPrincipal)
setPrincipal(initPrincipal)
numInstances numInstances 1
System.out.println(getNumLoans())
public setPrincipal(int newPrincipal)
principal newPrincipal
75Instance Vs Class Named Constant
public class AnotherBMISpreadsheet implements
BMISpreadsheet double height, weight,
bmi ... public static final double LBS_IN_KG
2.2 public static final double CMS_IN_INCH
2.54 double calculateBMI() return
(weight/LBS_IN_KG) / (heightCMS_IN_INCH/100heigh
tCMS_IN_INCH/100)
new AnotherBMISpreadsheet()
new AnotherBMISpreadsheet()
AnotherBMISpreadsheet.LBS_IN_KG
76Instance Vs Class Named Constant
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) /
(heightCMS_IN_INCH/100heightCMS_IN_INCH/100)
public final double LBS_IN_KG 2.2
new AnotherBMISpreadsheet()
new AnotherBMISpreadsheet()
(new AnotherBMISpreadsheet()).LBS_IN_KG
77Class Vs Instance Constant
- Should be class constant
- one copy
- easy to refer (require no instance creation)
- Unless some good reason for hiding named
constants from static methods - See inheritance-based exception later
78Class Vs Instance Methods
Class Method
public static Loan add(Loan loan1, Loan loan2)
return new ALoan(loan1.getPrincipal()
loan2.getPrincipal()))
ALoan.add(carLoan, houseLoan)
Math.round(5.7)
Instance Method
public Loan add(Loan loan1, Loan loan2)
return new ALoan(loan1.getPrincipal()
loan2.getPrincipal()))
(new ALoan).add(carLoan, houseLoan)
(new Math).round(5.7)
79Class Vs Instance Method
- Instance method has all the privileges of a
class method. - Any class method can be made an instance method.
- Bad style to have instance method that does not
access any instance variable. - They belong to the class.
- Violate least privilege principle.
- Require needless instantiation.
80Class Vs Instance Constant/Method
- Named constants should be static
- Unless some good reason for hiding named
constants from static methods - See inheritance-based exception later
- Methods not accessing instance variables should
be static - Unless need to be listed in interface.
- See inheritance-based exception later
81Polymorphic Methods
public static Loan add(Loan loan1, Loan loan2)
return new ALoan(loan1.getPrincipal()
loan2.getPrincipal()))
Methods that take actual parameters of different
types
82Non Polymorphic Methods
public static Loan add(ALoan loan1, ALoan loan2)
return new ALoan(loan1.getPrincipal()
loan2.getPrincipal()))
public static Loan add(AnotherLoan loan1,
AnotherLoan loan2) return new
ALoan(loan1.getPrincipal() loan2.getPrincipal())
)
public static Loan add(ALoan loan1, AnotherLoan
loan2) return new ALoan(loan1.getPrinci
pal() loan2.getPrincipal()))
83Overloading Vs Polymprphism
add (new ALoan(10000), new ALoan(5000))
add (new ALoan(10000), new AnotherLoan(5000))
Polymorphism
Overloading
84Primitive Vs Object Types
Types
ABMISpreadsheet
AnotherBMISpreadsheet
ALoan AnotherLoan
Interfaces
BMISpreadsheet Loan
Type Set of operations
85Structure Vs Atomic Types
Types
ABMISpreadsheet
AnotherBMISpreadsheet
ALoan AnotherLoan
Interfaces
BMISpreadsheet Loan
Instances of structure type decomposed into one
or more smaller values
86Loan Interface
public interface Loan public int
getPrincipal() public void setPrincipal(int
newValue) public int getYearlyInterest()
public void setYearlyInterest(int newVal)
public int getMonthlyInterest() public void
setMonthlyInterest(int newVal)
87A Loan Representation
write
read
double principal
88Another Loan Representation
write
read
int yearlyInterest
89Physical Vs. Logical Structure
Physical
ALoan instance
Logical
90Loan Pair Interface
public interface LoanPair public Loan
getCarLoan() public void setCarLoan(Loan
newValue) public Loan getHouseLoan()
public void setHouseLoan(Loan newValue)
public Loan getTotalLoan()
91ALoanPair
Loan carLoan new ALoan()
Loan houseLoan new AnotherLoan()
92Physical Vs. Logical Structure
Variable Name
Physical
Class/primitive type of value Stored in Variable
ALoanPair Instance
Logical
Property Name
ALoanPair Instance
Property Type