+ All Categories
Home > Documents > Comp 110 Loan Case Study

Comp 110 Loan Case Study

Date post: 22-Feb-2016
Category:
Upload: nike
View: 23 times
Download: 0 times
Share this document with a friend
Description:
Comp 110 Loan Case Study. Instructor : Jason Carter. Contents. Revisit, through non-graphical objects, concepts illustrated in previous sections. Loan Object. Properties classification. public class ABMISpreadsheet { double height; public double getHeight () { - PowerPoint PPT Presentation
66
COMP 110 LOAN CASE STUDY Instructor: Jason Carter
Transcript
Page 1: Comp 110 Loan Case Study

COMP 110LOAN CASE STUDY

Instructor: Jason Carter

Page 2: Comp 110 Loan Case Study

2

CONTENTS Revisit, through non-graphical objects, concepts

illustrated in previous sections

Page 3: Comp 110 Loan Case Study

3

LOAN OBJECT

Page 4: Comp 110 Loan Case Study

4

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); }}

Editable

Read-only

Independent

Dependent

Page 5: Comp 110 Loan Case Study

5

LOAN OBJECT

Editableand dependent

Page 6: Comp 110 Loan Case Study

6

1-WAY VS. 2-WAY DEPENDENCIES

bmi

weight

height

principal

monthly interest

yearly interest

Page 7: Comp 110 Loan Case Study

7

TOP-DOWN PROGRAMMING

Interface

Representation

Algorithm

Class

Page 8: Comp 110 Loan Case Study

8

BOTTOM-UP PROGRAMMING

Interface

Class

Page 9: Comp 110 Loan Case Study

9

LOAN OBJECT

Page 10: Comp 110 Loan Case Study

10

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);

}

Page 11: Comp 110 Loan Case Study

11

ALOAN REPRESENTATION

intprincipal getPrincipal()setPrincipal()

write read

getYearlyInterest()

getMonthlyInterest()

setYearlyInterest()

setMonhtlyInterest()

Representation = set of instance variables that stores object state

Page 12: Comp 110 Loan Case Study

12

LOAN OBJECT

Editableand dependent

Stored

Computed

Page 13: Comp 110 Loan Case Study

13

ALOAN ALGORITHM

intprincipal getPrincipal()setPrincipal()

write read

getYearlyInterest()

getMonthlyInterest()

setYearlyInterest()

setMonhtlyInterest()

F1

F2F2-1

F1-1

Page 14: Comp 110 Loan Case Study

14

SETTING AND GETTING THE STORED PROPERTY (EDIT)

intprincipal

intprincipal getPrincipal()setPrincipal()

public void setPrincipal(int newPrincipal) { }

public int getPrincipal() { }

Page 15: Comp 110 Loan Case Study

15

SETTING AND GETTING THE STORED PROPERTY

intprincipal

intprincipal getPrincipal()setPrincipal()

public void setPrincipal(int newPrincipal) { principal = newPrincipal;}

public int getPrincipal() { return principal;}

Page 16: Comp 110 Loan Case Study

16

SETTING AND GETTING COMPUTED PROPERTY (EDIT)

intprincipal

intprincipal getYearlyInterest()setYearlyInterest()

public void setYearlyInterest(int newInterest) { principal = (newInterest /INTEREST_RATE )*100;}

public int getYearlyInterest() { return principal*INTEREST_RATE/100;}

F1

F1-1

Page 17: Comp 110 Loan Case Study

17

SETTING AND GETTING COMPUTED PROPERTY

intprincipal

intprincipal getYearlyInterest()getYearlyInterest()

public void setYearlyInterest(int newInterest) { principal = newInterest/INTEREST_RATE*100;}

public int getYearlyInterest() { return principal*INTEREST_RATE/100;}

F1

F1-1

Page 18: Comp 110 Loan Case Study

18

SETTING AND GETTING COMPUTED PROPERTY (EDIT)

intprincipal

intprincipal

getMonthlyInterest()setMontlyInterest()

public void setMonthlyInterest(int newVal) { principal = 12*newVal/INTEREST_RATE*100;}

public int getMonthlyInterest() { return getYearlyInterest()/ 12;}

F2

F2-1

Page 19: Comp 110 Loan Case Study

19

SETTING AND GETTING COMPUTED PROPERTY (EDIT)

intprincipal

intprincipal

getMonthlyInterest()setMontlyInterest()

public void setMonthlyInterest(int newVal) { setYearlyInterest (newVal*12);}

public int getMonthlyInterest() { return getYearlyInterest()/12;}

F2

F2-1

Page 20: Comp 110 Loan Case Study

20

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);

}

Page 21: Comp 110 Loan Case Study

21

MIDDLE-OUT PROGRAMMING

Interface

Representation

Algorithm

Class

Page 22: Comp 110 Loan Case Study

22

ANOTHERLOAN REPRESENTATION

intyearlyInteres

tgetPrincipal()setPrincipal()

write read

getYearlyInterest()

getMonthlyInterest()

setYearlyInterest()

setMonhtlyInterest()

Page 23: Comp 110 Loan Case Study

23

CONVERSION ERRORS WITH PRINCIPAL REPRESENTATION

Page 24: Comp 110 Loan Case Study

24

NO CONVERSION ERRORS WITH YEARLY INTEREST REPRESENTATION

Page 25: Comp 110 Loan Case Study

25

LOANPAIRCar Loan Principal

Car Loan Yearly Interest

Car Loan Monthly Interest

House Loan Principal…

Car Loan

House Loan

Total Loan

Principal

Yearly Interest

Monthly Interest

Page 26: Comp 110 Loan Case Study

26

PRIMITIVE VS. OBJECT PROPERTIESCar Loan Principal

Car Loan Yearly Interest

Car Loan Monthly Interest

House Loan Principal…

Car Loan

House Loan

Total Loan

Principal

Yearly Interest

Monthly Interest

Primitive Properties

ObjectProperties

Reusing Loan!

Page 27: Comp 110 Loan Case Study

27

LOANPAIR INTERFACE (EDIT)

public interface LoanPair {

}

Page 28: Comp 110 Loan Case Study

28

LOANPAIR INTERFACE (EDIT)

public interface LoanPair { }

Page 29: Comp 110 Loan Case Study

29

TYPING OBJECTS

ALoan

AnotherLoan

Loan

Page 30: Comp 110 Loan Case Study

30

LOANPAIR INTERFACE

public interface LoanPair { public Loan getCarLoan(); public void setCarLoan( Loan newValue); public Loan getHouseLoan(); public void setHouseLoan( Loan newValue); public Loan getTotalLoan(); }

Actual ParametersALoan

AnotherLoan

Page 31: Comp 110 Loan Case Study

31

LOANPAIR INTERFACE: RESTRICTED

public interface LoanPair { public Loan getCarLoan(); public void setCarLoan( ALoan newValue); public Loan getHouseLoan(); public void setHouseLoan( ALoan newValue); public Loan getTotalLoan(); }

Actual ParametersALoan

AnotherLoan

Page 32: Comp 110 Loan Case Study

32

SPACE-EFFICIENT IMPLEMENTATION

Car Loan

House Loan

Total Loan

Independent

DependentComputed

Stored

Page 33: Comp 110 Loan Case Study

33

SPACE-EFFICIENT REPRESENTATION

Loan carLoan

getTotalLoan()

write read

getCarLoan ()

getHouseLoan()

setCarLoan()

setHouseLoan()

Loan houseLoan

Page 34: Comp 110 Loan Case Study

34

GETTER METHOD

Loan carLoan

getCarLoan ()

public Loan getCarLoan(){ return carLoan;

}

Accessing uninitialized object variable!

Page 35: Comp 110 Loan Case Study

35

DEFAULT VALUES FOR VARIABLESPrimitive Variables

double computedBMI;double weight;

Object Variables

Loan loan;

variables memory

0.00.0

null

computedBMI;weight;

loan;

Legal double values

Illegal Loan value

Page 36: Comp 110 Loan Case Study

36

GETTER METHOD

Loan carLoan

getCarLoan ()

public Loan getCarLoan(){ return carLoan;

}

ObjectEditor does not try to invoke methods if return

value is null!

Page 37: Comp 110 Loan Case Study

37

OBJECTEDITOR DISPLAY OF NULL

Page 38: Comp 110 Loan Case Study

38

HOW TO INITIALIZE OBJECT VARIABLE?

Loan carLoan

getCarLoan ()

public Loan getCarLoan(){ return carLoan;

}

Create instance of ALoan or AnotherLoan and assign to

variable

Page 39: Comp 110 Loan Case Study

39

INITIALIZATION OF OBJECT VARIABLES

Loan carLoan = new ALoan();

Loan houseLoan = new AnotherLoan();

Loan houseLoan = new Loan();

Page 40: Comp 110 Loan Case Study

40

INTERACTIVE VS. PROGRAMMED INSTANTIATION

new ALoan()

Page 41: Comp 110 Loan Case Study

41

ALOANPAIR

Loan carLoan = new Aloan()

getTotalLoan()

write read

getCarLoan ()

getHouseLoan()

setCarLoan()

setHouseLoan()

Loan houseLoan =

new AnotherLoan(

)

Page 42: Comp 110 Loan Case Study

42

GETTOTALLOAN()

Loan carLoan

getTotalLoan()

Loan houseLoan

public Loan getTotalLoan(){ return carLoan +

houseLoan; }

+ not defined for Loan!

Page 43: Comp 110 Loan Case Study

43

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

}

Page 44: Comp 110 Loan Case Study

44

PROGRAMMER-DEFINED ADDpublic 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;

}

Page 45: Comp 110 Loan Case Study

45

RETURNING ANOTHERLOANpublic 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;

}

Page 46: Comp 110 Loan Case Study

46

COMBINING OBJECT CREATION AND INITIALIZATIONpublic 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 creation and initialization

Page 47: Comp 110 Loan Case Study

47

ADDING CONSTRUCTOR IN ALOAN

intprincipal getPrincipal()setPrincipal()

write read

getYearlyInterest()

getMonthlyInterest()

setYearlyInterest()

setMonhtlyInterest()

ALoan()

initialize

Constructor

Page 48: Comp 110 Loan Case Study

48

CONSTRUCTOR VS. REGULAR METHODpublic class ALoan implements Loan{

int principal;

public int getPrincipal() { return principal;}public void setPrincipal(int newVal) { principal = newVal;}…

}

public ALoan(int initPrincipal) {setPrincipal(initPrincipal);

}

Missing return type name

Combined return type and method name

public ALoan ALoan(int initPrincipal) {

setPrincipal(initPrincipal);}

Not a constructor!

Page 49: Comp 110 Loan Case Study

49

INSTANTIATION REQUIRES PARAMETERS

new ALoan(10000)

new ALoan()

Page 50: Comp 110 Loan Case Study

50

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

Page 51: Comp 110 Loan Case Study

51

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

Page 52: Comp 110 Loan Case Study

52

DEFAULT CONSTRUCTORpublic class ALoan implements Loan{

int principal;

public int getPrincipal() { return principal;}public void setPrincipal(int newVal) { principal = newVal;}…

}

public ALoan();}

Default

Inserted in object code, not in source code

new ALoan()

Invoking the default constructor

Page 53: Comp 110 Loan Case Study

53

PROGRAMMER-DEFINED ADD

public Loan getTotalLoan(){ return ALoan.add(houseLoan, carLoan);

}

public static Loan add(Loan loan1, Loan loan2) { return new ALoan(loan1.getPrincipal() + loan2.getPrincipal()));}

Instance Method

Class Method

Accesses instance variables

Access no instance variable

Page 54: Comp 110 Loan Case Study

54

POLYMORPHIC METHODS

public static Loan add(Loan loan1, Loan loan2) { return new ALoan(loan1.getPrincipal() + loan2.getPrincipal()));}

Methods that take actual parameters of different

typesALoan

instanceAnotherLoan

instance

Actual parameters of different types

Page 55: Comp 110 Loan Case Study

55

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!

Page 56: Comp 110 Loan Case Study

56

OVERLOADING VS. POLYMORPHISM

public static Loan add(Loan loan1, Loan loan2) { return new ALoan(loan1.getPrincipal() + loan2.getPrincipal()));}public static Loan add(ALoan loan1, ALoan loan2) { return new ALoan(loan1.getPrincipal() + loan2.getPrincipal()));}public static Loan add(ALoan loan1, AnotherLoan loan2) { return new ALoan(loan1.getPrincipal() + loan2.getPrincipal()));}

Polymorphism Overloading

add (new ALoan(10000), new ALoan(5000));add (new ALoan(10000), new

AnotherLoan(5000));

Page 57: Comp 110 Loan Case Study

57

PRIMITIVE VS. OBJECT TYPEStypes

Primitive types

Object types

double int

ABMICalculator ABMISpreadsheet

ALoan

BMISpreadsheet

Classes

Interfaces

type = set of operations

AnotherLoan

Loan

Page 58: Comp 110 Loan Case Study

58

STRUCTURED VS. ATOMIC TYPEStypes

Primitive types

Structured types

double int

ABMICalculator ABMISpreadsheet

ALoan

BMISpreadsheet

Classes

Interfaces

Instances of structured type decomposed into one or more smaller values

AnotherLoan

Loan

Page 59: Comp 110 Loan Case Study

59

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);

}

Page 60: Comp 110 Loan Case Study

60

ALOAN REPRESENTATION

intprincipal getPrincipal()setPrincipal()

write read

getYearlyInterest()

getMonthlyInterest()

setYearlyInterest()

setMonhtlyInterest()

Page 61: Comp 110 Loan Case Study

61

ANOTHERLOAN REPRESENTATION

intyearlyInteres

tgetPrincipal()setPrincipal()

write read

getYearlyInterest()

getMonthlyInterest()

setYearlyInterest()

setMonhtlyInterest()

Page 62: Comp 110 Loan Case Study

62

PHYSICAL VS. LOGICAL STRUCTURE

ALoan Instance int

principal;

Physical

ALoan Instance int

Principal;

Logical

int

int

YearlyInterest

;

MonthlyInterest;

Page 63: Comp 110 Loan Case Study

63

LOANPAIR INTERFACE

public interface LoanPair {public Loan getCarLoan();public void setCarLoan(Loan newValue);

public Loan getHouseLoan(); public void setHouseLoan(Loan newValue); public Loan getTotalLoan();

}

Page 64: Comp 110 Loan Case Study

64

ALOANPAIR

Loan carLoan

getTotalLoan()

write read

getCarLoan ()

getHouseLoan()

setCarLoan()

setHouseLoan()

Loan houseLoan

Page 65: Comp 110 Loan Case Study

65

PHYSICAL VS. LOGICAL STRUCTURE

ALoanPair Instance

LoancarLoan;

Physicalint

principal;

Loan intprincipal

;houseLoan;

Variable name

Class or primitive type

of value stored in variable

Page 66: Comp 110 Loan Case Study

66

PHYSICAL VS. LOGICAL STRUCTURE

ALoanPair Instance LoanCarLoan;

Logical

Loan

Loan

TotalLoan;

HouseLoan;

intPrincipal

;

int

int

YearlyInterest

;

MonthlyInterest;

Property name

Class or primitive type

of property value


Recommended