Download presentation
Presentation is loading. Please wait.
1
Programmer-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
2
Loan Object
3
Properties Classification public class ABMISpreadsheet { double height; public double getHeight() { return height; } public void setHeight(double newHeight) { height = newHeight; } double weight; public double getWeight() { return weight; } public void setWeight(double newWeight) { weight = newWeight; } public double getBMI() { return weight/(height*height); } Height Weight BMI Editable Read-only Independent Dependent Read-Only
4
Read-Only Vs Editable, Dependent Vs Independent Editable Dependent
5
1-Way Vs. 2-Way Dependencies BMI Height Weight Principle Monthly Interest Yearly Interest
6
Top-Down Programming Interface Representation Algorithm Class
7
Bottom-up Programming in BMI Interface Class
8
Loan Object
9
Loan 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); }
10
A Loan Representation setYearly Interest() setMonthly Interest() getYearly interest() setPrincipal()getPrincipal() getMonthly Interest() write read double principal
11
Stored Vs Computed Editable Dependent Stored Computed
12
Loan Algorithm setYearly Interest() setMonthly Interest() getYearly interest() setPrincipal()getPrincipal() getMonthly Interest() write read F1 -1 F2 -1 F1 F2 int principal
13
Setting and Getting Stored Property setPrincipal()getPrincipal() public void setPrincipal(int newVal) { principal = newVal; } public int getPrincipal(){ return principal; } int principal
14
Setting and Getting Stored Property setPrincipal()getPrincipal() public void setPrincipal(int newVal) { principal = newVal; } public int getPrincipal(){ return principal; } int principal
15
Getting and Setting Computed Property (edit) setYearly Interest() getYearly interest() write read F1 -1 F1 public int getYearlyInterest() { return principal* INTEREST_RATE/100; } public void setYearlyInterest(int newVal) { ???? } int principal
16
Getting and Setting Computed Property (edited) setYearly Interest() getYearly interest() write read F1 -1 F1 public int getYearlyInterest() { return principal* INTEREST_RATE/100; } public void setYearlyInterest(int newVal) { principal = newVal/INTEREST_RATE*100; } int principal
17
Getting and Setting Computed Property(soln) setYearly Interest() getYearly interest() write read F1 -1 F1 public int getYearlyInterest() { return principal* INTEREST_RATE/100; } public void setYearlyInterest(int newVal) { principal = newVal*100/INTEREST_RATE; } int principal
18
Getting and Setting Computed Property (edit) setMonthly Interest() getMonthly Interest() F2 -1 F2 public int getMonthlyInterest() { ??? } public void setMonthlyInterest(int newVal) { ??? } int principal
19
Getting and Setting Computed Property (edited) setMonthly Interest() getMonthly Interest() F2 -1 F2 public int getMonthlyInterest() { return getYearlyInterest()/12; } public void setMonthlyInterest(int newVal) { setYearlyInterest (newVal*12); } int principal
20
Getting and Setting Computed Property(soln) setMonthly Interest() getMonthly Interest() F2 -1 F2 public int getMonthlyInterest() { return getYearlyInterest()/12; } public void setMonthlyInterest(int newVal) { principal = setYearlyInterest(newVal*12); } int principal
21
Modified 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); }
22
Middle-Out Programming Interface Representation Algorithm Class
23
Another Loan Representation int yearlyInterest setYearly Interest() setMonthly Interest() getYearly interest() setPrincipal()getPrincipal() getMonthly Interest() write read
24
Conversion Errors with Principal Repn.
25
No Conversion Errors with YearlyInterest Repn.
26
Car Loan House Loan Total Loan Principal Yearly Interest Monthly Interest Loan Pair Car Loan Yearly Interest Car Loan Monthly Interest House Loan Principal Car Loan Principal...
27
Primitive Vs. Object Properties Loan Pair Car Loan Principal Car Loan Yearly Interest Car Loan Monthly Interest House Loan Principal... Loan Pair Car Loan House Loan Total Loan Primitive Properties Object Properties Reusing Loan!
28
Loan Pair Interface (edit) public interface LoanPair {... }
29
Loan Pair Interface (edited) public interface LoanPair {... }
30
Loan AnotherLoan ALoan Typing Objects
31
Object Types Primitive types Kinds of Types Types Classes ABMISpreadsheet AnotherBMISpreadsheet Interfaces BMISpreadsheet Loan Stringdoubleint Type = Set of operations ALoan AnotherLoan
32
Loan 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(); } AnotherLoan Instance ALoan Instance Actual Parameters
33
Loan 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(); } ALoan Instance AnotherLoan Instance Actual Parameters
34
Typing an Object Use interface rather than class to type object variables. => Define interfaces for all classes that include headers of all public method
35
Programmer-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.
36
Space Efficient Representation Dependent Stored Computed Car Loan House Loan Total Loan Principal Yearly Interest Monthly Interest Dependent Independent
37
Space Efficient Representation Loan carLoan setHouseLoan() getHouseLoan() setCarLoan() getCarLoan() writeread Loan houseLoan getTotalLoan()
38
Getter Method Loan carLoan getCarLoan() public Loan getCarLoan(){ return carLoan; } Accessing Uninitialized Object Variable !!
39
variablesmemory Object Variables Primitive Variables Default Values for Variables height 0.0 weight null carLoan houseLoan Loan carLoan double height double weight Legal double Values Illegal Loan values
40
Invoking 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.
41
Getter Method Loan carLoan getCarLoan() public Loan getCarLoan(){ return carLoan; } ObjectEditor does not try to invoke methods if return value is null
42
ObjectEditor Display of Null
43
How to initialize object variable? Loan carLoan getCarLoan() public Loan getCarLoan(){ return carLoan; } Create instance (of ALoan or AnotherLoan) and assign to variable.
44
Initialization of Object Variables Loan carLoan = new ALoan() Loan houseLoan = new AnotherLoan() Loan houseLoan = new Loan()
45
Interactive Vs Programmed Instantiation new ALoan()
46
Programmed 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
47
ALoanPair Loan carLoan = new ALoan() setCarLoan() getCarLoan() writeread Loan houseLoan = new AnotherLoan() getTotalLoan() setHouseLoan() getHouseLoan()
48
getTotalLoan() Loan carLoan = new ALoan() Loan houseLoan = new AnotherLoan() getTotalLoan() public Loan getTotalLoan(){ houseLoan + carLoan; }
49
Programmer-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 }
50
Programmer-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; }
51
public Loan add(Loan loan1, Loan loan2) { Loan retVal = new AnotherLoan(); retVal.setPrincipal(loan1.getPrincipal() + loan2.getPrincipal()); return retVal; } Returning AnotherLoan() 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; }
52
Combining 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())); } Object creation Object initialization Combined object creation and initialization
53
Adding Constructor in ALoan setYearly Interest() setMonthly Interest() getYearly interest() setPrincipal()getPrincipal() getMonthly Interest() write read int principal ALoan() initialize Constructor
54
Constructor 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 principal*INTEREST_RATE/100; } public void setYearlyInterest(int newVal) { principal = newVal*100/INTEREST_RATE; } public int getMonthlyInterest() { return getYearlyInterest()/12; } public void setMonthlyInterest(int newVal) { setYearlyInterest(newVal*12); } public ALoan(int initPrincipal) { setPrincipal(initPrincipal); } Combined type and method name Missing type name public ALoan ALoan(int initPrincipal) { setPrincipal(initPrincipal); } Not a constructor
55
Instantiation Requires Parameters new ALoan() new ALoan(10000)
56
Constructor 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
57
Multi-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.
58
Default Constructor public class ALoan implements Loan{ int principal; public int getPrincipal() { return principal; } public void setPrincipal(int newVal) { principal = newVal; } public int getYearlyInterest() { return principal*INTEREST_RATE/100; } public void setYearlyInterest(int newVal) { principal = newVal*100/INTEREST_RATE; } public int getMonthlyInterest() { return getYearlyInterest()/12; } public void setMonthlyInterest(int newVal) { setYearlyInterest(newVal*12); } public ALoan() { } Inserted In Object Code not in Source Code Default new ALoan() Invoking Default, Parameterless Constructor
59
Real-World Analogy new AnAccord(beige) new AnAccord(grey) getMileage() 64000 blend(beigeCar, greyCar)numCarsProduced() 101234
60
O-O Word new ALoan(10000) new ALoan(100000) getPrincipal() 100000 ALoan carLoan houseLoan add(carLoan, totalLoan) totalLoan numInstances() 3
61
Class 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.
62
Programmer-Defined Add public static Loan add(Loan loan1, Loan loan2) { return new ALoan(loan1.getPrincipal() + loan2.getPrincipal())); } public Loan getTotalLoan(){ return ALoan.add (houseLoan, carLoan); } Accesses instance variables Instance Method Class Method Accesses no instance variable
63
External Call of Class Method ALoan.add(carLoan, houseLoan) Class as target Math.round(5.7)
64
numInstances() Algorithm Declare variable, numInstances initialized to zero Increment numInstances each time a new instance is created numInstances() returns numInstances
65
returning numLoans getNumLoans() returns numInstances public static int getNumLoans() { return numInstances; }
66
Incrementing getNumLoans (edit) Increment numInstances each time a new instance is created ???
67
Incrementing getNumLoans (edited) Increment numInstances each time a new instance is created ???
68
Incrementing getNumLoans (soln) Increment numInstances each time a new instance is created public ALoan(int initPrincipal) { setPrincipal(initPrincipal); numInstances = numInstances + 1; }
69
Declaring numInstances Declare variable, numInstances initialized to zero int numInstances = 0; // instance variable
70
1 memory Loan Object int numInstances = 0; public ALoan(int initPrincipal) { setPrincipal(initPrincipal); numInstances = numInstances + 1; } 0 principal 0 numInstances 0 principal 0 numInstances 1 10000 1 5000
71
1 memory Loan Object static int numInstances = 0; public ALoan(int initPrincipal) { setPrincipal(initPrincipal); numInstances = numInstances + 1; } 0 numInstances 0 principal 10000 10 principal 5000 2
72
Instance Vs Class Members Class Members –Class methods –Class variables Instance Members –Instance methods –Instance variables
73
Scope 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?
74
public static int getNumLoans() { System.out.println(principal); return numInstances; } Legal & Illegal Accesses static int numInstances = 0; public ALoan(int initPrincipal) { setPrincipal(initPrincipal); numInstances = numInstances + 1; System.out.println(getNumLoans()); } int principal; public setPrincipal(int newPrincipal) { principal = newPrincipal; }
75
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) / (height*CMS_IN_INCH/100*height*CMS_IN_INCH/100); } Instance Vs Class Named Constant memory LBS_IN_KG 0 new AnotherBMISpreadsheet() AnotherBMISpreadsheet.LBS_IN_KG
76
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); } Instance Vs Class Named Constant memory LBS_IN_KG 0 new AnotherBMISpreadsheet() LBS_IN_KG 0 public final double LBS_IN_KG = 2.2; (new AnotherBMISpreadsheet()).LBS_IN_KG
77
Class 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
78
Class Vs Instance Methods public static Loan add(Loan loan1, Loan loan2) { return new ALoan(loan1.getPrincipal() + loan2.getPrincipal())); } Class MethodAccesses no instance variable public Loan add(Loan loan1, Loan loan2) { return new ALoan(loan1.getPrincipal() + loan2.getPrincipal())); } Instance Method ALoan.add(carLoan, houseLoan) Math.round(5.7) (new ALoan).add(carLoan, houseLoan) (new Math).round(5.7)
79
Class 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.
80
Class 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
81
Polymorphic Methods public static Loan add(Loan loan1, Loan loan2) { return new ALoan(loan1.getPrincipal() + loan2.getPrincipal())); } AnotherLoan Instance ALoan Instance Actual Parameters of different types Methods that take actual parameters of different types
82
Non 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.getPrincipal() + loan2.getPrincipal())); } Code Duplication
83
public static Loan add(ALoan loan1, ALoan loan2) { return new ALoan(loan1.getPrincipal() + loan2.getPrincipal())); } Overloading Vs Polymprphism add (new ALoan(10000), new ALoan(5000)); add (new ALoan(10000), new AnotherLoan(5000)); PolymorphismOverloading public static Loan add(ALoan loan1, AnotherLoan loan2) { return new ALoan(loan1.getPrincipal() + loan2.getPrincipal())); } public static Loan add(ALoan loan1, ALoan loan2) { return new ALoan(loan1.getPrincipal() + loan2.getPrincipal())); }
84
Object Types Primitive types Primitive Vs Object Types Types Classes ABMISpreadsheet AnotherBMISpreadsheet Interfaces BMISpreadsheet Loan Stringdoubleint Type = Set of operations ALoan AnotherLoan
85
Structure Types Primitive types Structure Vs Atomic Types Types Classes ABMISpreadsheet AnotherBMISpreadsheet Interfaces BMISpreadsheet Loan Stringdoubleint ALoan AnotherLoan Instances of structure type decomposed into one or more smaller values
86
Loan 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); }
87
A Loan Representation setYearly Interest() setMonthly Interest() getYearly interest() setPrincipal()getPrincipal() getMonthly Interest() write read double principal
88
Another Loan Representation int yearlyInterest setYearly Interest() setMonthly Interest() getYearly interest() setPrincipal()getPrincipal() getMonthly Interest() write read
89
Physical Vs. Logical Structure ALoan instance double principalALoan instance double Principal Yearly Interest Monthly Interest double Physical Logical
90
Loan Pair Interface public interface LoanPair { public Loan getCarLoan(); public void setCarLoan(Loan newValue); public Loan getHouseLoan(); public void setHouseLoan(Loan newValue); public Loan getTotalLoan(); }
91
ALoanPair Loan carLoan = new ALoan() setCarLoan() getCarLoan() writeread Loan houseLoan = new AnotherLoan() getTotalLoan() setHouseLoan() getHouseLoan()
92
Physical Vs. Logical Structure ALoan double principal Loan double Principal Yearly Interest Monthly Interest double Physical Logical ALoanPair Instance carLoan houseLoan yearlyInterest AnotherLoandouble ALoanPair Instance CarLoan Loan HouseLoan TotalLoan Loan Variable Name Class/primitive type of value Stored in Variable Property Name Property Type
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.