ProgrammerDefined Types - PowerPoint PPT Presentation

1 / 92
About This Presentation
Title:

ProgrammerDefined Types

Description:

Programmer-defined Vs Predefined Types ... Programmer-defined types in Java must be object types. Some object types are predefined (String) ... – PowerPoint PPT presentation

Number of Views:41
Avg rating:3.0/5.0
Slides: 93
Provided by: Prasun2
Learn more at: http://www.cs.unc.edu
Category:

less

Transcript and Presenter's Notes

Title: ProgrammerDefined Types


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/(heightheight)

Height
Weight
BMI
4
Read-Only Vs Editable, Dependent Vs Independent
Dependent
5
1-Way Vs. 2-Way Dependencies
Height
BMI
Weight
Monthly Interest
Principle
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
write
read
double principal
11
Stored Vs Computed
Dependent
12
Loan Algorithm
F1
write
read
F1-1
int principal
F2-1
F2
13
Setting and Getting Stored Property
int principal
14
Setting and Getting Stored Property
int principal
15
Getting and Setting Computed Property (edit)
F1
write
read
F1-1
int principal
16
Getting and Setting Computed Property (edited)
F1
write
read
F1-1
int principal
17
Getting and Setting Computed Property(soln)
F1
write
read
F1-1
int principal
18
Getting and Setting Computed Property (edit)
int principal
F2-1
F2
19
Getting and Setting Computed Property (edited)
int principal
F2-1
F2
20
Getting and Setting Computed Property(soln)
int principal
F2-1
F2
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
write
read
int yearlyInterest
24
Conversion Errors with Principal Repn.
25
No Conversion Errors with YearlyInterest Repn.
26
Loan Pair
27
Primitive Vs. Object Properties
Primitive Properties
Car Loan
Object Properties
House Loan
Loan Pair
Reusing Loan!
Total Loan
28
Loan Pair Interface (edit)
public interface LoanPair ...
29
Loan Pair Interface (edited)
public interface LoanPair ...
30
Typing Objects
31
Kinds of Types
Types
ABMISpreadsheet
AnotherBMISpreadsheet
ALoan AnotherLoan
Interfaces
BMISpreadsheet Loan
Type Set of operations
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()
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()
34
Typing an Object
  • Use interface rather than class to type object
    variables.
  • gt 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
37
Space Efficient Representation
Loan carLoan
Loan houseLoan
38
Getter Method
Loan carLoan
39
Default Values for Variables
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
42
ObjectEditor Display of Null
43
How to initialize object variable?
Loan carLoan
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()
Loan houseLoan new AnotherLoan()
48
getTotalLoan()
Loan carLoan new ALoan()
Loan houseLoan new AnotherLoan()
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
Returning 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
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()))
53
Adding Constructor in ALoan
getYearly interest()
write
read
int principal
getPrincipal()
getMonthly Interest()
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
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)
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
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()
59
Real-World Analogy
new AnAccord(beige)
new AnAccord(grey)
60
O-O Word
new ALoan(10000)
ALoan
carLoan
houseLoan
new ALoan(100000)
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
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()))
63
External Call of Class Method
ALoan.add(carLoan, houseLoan)
Math.round(5.7)
64
numInstances() Algorithm
Declare variable, numInstances initialized to zero
65
returning numLoans
getNumLoans() returns 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)
public ALoan(int initPrincipal)
setPrincipal(initPrincipal)
numInstances numInstances 1
Increment numInstances each time a new instance
is created
69
Declaring numInstances
Declare variable, numInstances initialized to zero
// instance variable
70
Loan Object
memory
int numInstances 0 public ALoan(int
initPrincipal) setPrincipal(initPrinci
pal) numInstances numInstances 1
71
Loan Object
memory
static int numInstances 0 public ALoan(int
initPrincipal) setPrincipal(initPrinci
pal) numInstances numInstances 1
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
Legal 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
75
Instance 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
76
Instance 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
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
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)
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()))
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.getPrinci
pal() loan2.getPrincipal()))
83
Overloading Vs Polymprphism
add (new ALoan(10000), new ALoan(5000))
add (new ALoan(10000), new AnotherLoan(5000))
Polymorphism
Overloading
84
Primitive Vs Object Types
Types
ABMISpreadsheet
AnotherBMISpreadsheet
ALoan AnotherLoan
Interfaces
BMISpreadsheet Loan
Type Set of operations
85
Structure Vs Atomic Types
Types
ABMISpreadsheet
AnotherBMISpreadsheet
ALoan AnotherLoan
Interfaces
BMISpreadsheet Loan
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
write
read
double principal
88
Another Loan Representation
write
read
int yearlyInterest
89
Physical Vs. Logical Structure
Physical
ALoan instance
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()
Loan houseLoan new AnotherLoan()
92
Physical Vs. Logical Structure
Variable Name
Physical
Class/primitive type of value Stored in Variable
ALoanPair Instance
Logical
Property Name
ALoanPair Instance
Property Type
Write a Comment
User Comments (0)
About PowerShow.com