Comp 110 Loan Case Study

Preview:

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

Citation preview

COMP 110LOAN CASE STUDY

Instructor: Jason Carter

2

CONTENTS Revisit, through non-graphical objects, concepts

illustrated in previous sections

3

LOAN OBJECT

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

5

LOAN OBJECT

Editableand dependent

6

1-WAY VS. 2-WAY DEPENDENCIES

bmi

weight

height

principal

monthly interest

yearly interest

7

TOP-DOWN PROGRAMMING

Interface

Representation

Algorithm

Class

8

BOTTOM-UP PROGRAMMING

Interface

Class

9

LOAN OBJECT

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

}

11

ALOAN REPRESENTATION

intprincipal getPrincipal()setPrincipal()

write read

getYearlyInterest()

getMonthlyInterest()

setYearlyInterest()

setMonhtlyInterest()

Representation = set of instance variables that stores object state

12

LOAN OBJECT

Editableand dependent

Stored

Computed

13

ALOAN ALGORITHM

intprincipal getPrincipal()setPrincipal()

write read

getYearlyInterest()

getMonthlyInterest()

setYearlyInterest()

setMonhtlyInterest()

F1

F2F2-1

F1-1

14

SETTING AND GETTING THE STORED PROPERTY (EDIT)

intprincipal

intprincipal getPrincipal()setPrincipal()

public void setPrincipal(int newPrincipal) { }

public int getPrincipal() { }

15

SETTING AND GETTING THE STORED PROPERTY

intprincipal

intprincipal getPrincipal()setPrincipal()

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

public int getPrincipal() { return principal;}

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

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

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

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

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

}

21

MIDDLE-OUT PROGRAMMING

Interface

Representation

Algorithm

Class

22

ANOTHERLOAN REPRESENTATION

intyearlyInteres

tgetPrincipal()setPrincipal()

write read

getYearlyInterest()

getMonthlyInterest()

setYearlyInterest()

setMonhtlyInterest()

23

CONVERSION ERRORS WITH PRINCIPAL REPRESENTATION

24

NO CONVERSION ERRORS WITH YEARLY INTEREST REPRESENTATION

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

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!

27

LOANPAIR INTERFACE (EDIT)

public interface LoanPair {

}

28

LOANPAIR INTERFACE (EDIT)

public interface LoanPair { }

29

TYPING OBJECTS

ALoan

AnotherLoan

Loan

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

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

32

SPACE-EFFICIENT IMPLEMENTATION

Car Loan

House Loan

Total Loan

Independent

DependentComputed

Stored

33

SPACE-EFFICIENT REPRESENTATION

Loan carLoan

getTotalLoan()

write read

getCarLoan ()

getHouseLoan()

setCarLoan()

setHouseLoan()

Loan houseLoan

34

GETTER METHOD

Loan carLoan

getCarLoan ()

public Loan getCarLoan(){ return carLoan;

}

Accessing uninitialized object variable!

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

36

GETTER METHOD

Loan carLoan

getCarLoan ()

public Loan getCarLoan(){ return carLoan;

}

ObjectEditor does not try to invoke methods if return

value is null!

37

OBJECTEDITOR DISPLAY OF NULL

38

HOW TO INITIALIZE OBJECT VARIABLE?

Loan carLoan

getCarLoan ()

public Loan getCarLoan(){ return carLoan;

}

Create instance of ALoan or AnotherLoan and assign to

variable

39

INITIALIZATION OF OBJECT VARIABLES

Loan carLoan = new ALoan();

Loan houseLoan = new AnotherLoan();

Loan houseLoan = new Loan();

40

INTERACTIVE VS. PROGRAMMED INSTANTIATION

new ALoan()

41

ALOANPAIR

Loan carLoan = new Aloan()

getTotalLoan()

write read

getCarLoan ()

getHouseLoan()

setCarLoan()

setHouseLoan()

Loan houseLoan =

new AnotherLoan(

)

42

GETTOTALLOAN()

Loan carLoan

getTotalLoan()

Loan houseLoan

public Loan getTotalLoan(){ return carLoan +

houseLoan; }

+ not defined for Loan!

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

}

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;

}

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;

}

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

47

ADDING CONSTRUCTOR IN ALOAN

intprincipal getPrincipal()setPrincipal()

write read

getYearlyInterest()

getMonthlyInterest()

setYearlyInterest()

setMonhtlyInterest()

ALoan()

initialize

Constructor

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!

49

INSTANTIATION REQUIRES PARAMETERS

new ALoan(10000)

new ALoan()

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

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

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

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

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

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!

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

57

PRIMITIVE VS. OBJECT TYPEStypes

Primitive types

Object types

double int

ABMICalculator ABMISpreadsheet

ALoan

BMISpreadsheet

Classes

Interfaces

type = set of operations

AnotherLoan

Loan

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

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

}

60

ALOAN REPRESENTATION

intprincipal getPrincipal()setPrincipal()

write read

getYearlyInterest()

getMonthlyInterest()

setYearlyInterest()

setMonhtlyInterest()

61

ANOTHERLOAN REPRESENTATION

intyearlyInteres

tgetPrincipal()setPrincipal()

write read

getYearlyInterest()

getMonthlyInterest()

setYearlyInterest()

setMonhtlyInterest()

62

PHYSICAL VS. LOGICAL STRUCTURE

ALoan Instance int

principal;

Physical

ALoan Instance int

Principal;

Logical

int

int

YearlyInterest

;

MonthlyInterest;

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

}

64

ALOANPAIR

Loan carLoan

getTotalLoan()

write read

getCarLoan ()

getHouseLoan()

setCarLoan()

setHouseLoan()

Loan houseLoan

65

PHYSICAL VS. LOGICAL STRUCTURE

ALoanPair Instance

LoancarLoan;

Physicalint

principal;

Loan intprincipal

;houseLoan;

Variable name

Class or primitive type

of value stored in variable

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