Presentation is loading. Please wait.

Presentation is loading. Please wait.

C OMP 110 L OAN C ASE S TUDY Instructor: Jason Carter.

Similar presentations


Presentation on theme: "C OMP 110 L OAN C ASE S TUDY Instructor: Jason Carter."— Presentation transcript:

1 C OMP 110 L OAN C ASE S TUDY Instructor: Jason Carter

2 2 C ONTENTS Revisit, through non-graphical objects, concepts illustrated in previous sections

3 3 L OAN O BJECT

4 4 P ROPERTIES 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); } Editable Read-only Independent Dependent

5 5 L OAN O BJECT Editable and dependent

6 6 1-W AY VS. 2-W AY D EPENDENCIES bmi weight height principal monthly interest yearly interest

7 7 T OP -D OWN P ROGRAMMING Interface Representation Algorithm Class

8 8 B OTTOM -U P P ROGRAMMING Interface Class

9 9 L OAN O BJECT

10 10 L OAN I NTERFACE 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 11 AL OAN R EPRESENTATION int principal getPrincipal()setPrincipal() write read getYearlyInterest() getMonthlyInterest() setYearlyInterest() setMonhtlyInterest() Representation = set of instance variables that stores object state

12 12 L OAN O BJECT Editable and dependent Stored Computed

13 13 AL OAN A LGORITHM int principal getPrincipal()setPrincipal() write read getYearlyInterest() getMonthlyInterest() setYearlyInterest() setMonhtlyInterest() F1 F2 F2 -1 F1 -1

14 14 S ETTING AND G ETTING THE S TORED P ROPERTY ( EDIT ) int principal int principal getPrincipal()setPrincipal() public void setPrincipal( int newPrincipal) { } public int getPrincipal() { }

15 15 S ETTING AND G ETTING THE S TORED P ROPERTY int principal int principal getPrincipal()setPrincipal() public void setPrincipal( int newPrincipal) { principal = newPrincipal; } public int getPrincipal() { return principal; }

16 16 S ETTING AND G ETTING C OMPUTED P ROPERTY ( EDIT ) int principal int principal getYearlyInterest()setYearlyInterest() public void setYearlyInterest( int newInterest) { principal = (newInterest /INTEREST_RATE )*100; } public int getYearlyInterest() { return principal*INTEREST_RATE/100; } F1 F1 -1

17 17 S ETTING AND G ETTING C OMPUTED P ROPERTY int principal int principal getYearlyInterest() public void setYearlyInterest( int newInterest) { principal = newInterest/INTEREST_RATE*100; } public int getYearlyInterest() { return principal*INTEREST_RATE/100; } F1 F1 -1

18 18 S ETTING AND G ETTING C OMPUTED P ROPERTY ( EDIT ) int principal int principal getMonthlyInterest()setMontlyInterest() public void setMonthlyInterest( int newVal) { principal = 12*newVal/INTEREST_RATE*100; } public int getMonthlyInterest() { return getYearlyInterest()/ 12; } F2 F2 -1

19 19 S ETTING AND G ETTING C OMPUTED P ROPERTY ( EDIT ) int principal int principal getMonthlyInterest()setMontlyInterest() public void setMonthlyInterest( int newVal) { setYearlyInterest (newVal*12); } public int getMonthlyInterest() { return getYearlyInterest()/12; } F2 F2 -1

20 20 M ODIFIED L OAN I NTERFACE 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 21 M IDDLE -O UT P ROGRAMMING Interface Representation Algorithm Class

22 22 A NOTHER L OAN R EPRESENTATION int yearlyInterest getPrincipal()setPrincipal() write read getYearlyInterest() getMonthlyInterest() setYearlyInterest() setMonhtlyInterest()

23 23 C ONVERSION E RRORS WITH P RINCIPAL R EPRESENTATION

24 24 N O C ONVERSION E RRORS WITH Y EARLY I NTEREST R EPRESENTATION

25 25 L OAN P AIR Car 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 26 P RIMITIVE VS. O BJECT P ROPERTIES Car 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 Object Properties Reusing Loan!

27 27 L OAN P AIR I NTERFACE ( EDIT ) public interface LoanPair { }

28 28 L OAN P AIR I NTERFACE ( EDIT ) public interface LoanPair { }

29 29 T YPING O BJECTS ALoan AnotherLoan Loan

30 30 L OAN P AIR I NTERFACE public interface LoanPair { public Loan getCarLoan(); public void setCarLoan( Loan newValue); public Loan getHouseLoan(); public void setHouseLoan( Loan newValue); public Loan getTotalLoan(); } Actual Parameters ALoan AnotherLoan

31 31 L OAN P AIR I NTERFACE : R ESTRICTED public interface LoanPair { public Loan getCarLoan(); public void setCarLoan( ALoan newValue); public Loan getHouseLoan(); public void setHouseLoan( ALoan newValue); public Loan getTotalLoan(); } Actual Parameters ALoan AnotherLoan

32 32 S PACE - EFFICIENT I MPLEMENTATION Car Loan House Loan Total Loan Independent Dependent Computed Stored

33 33 S PACE - EFFICIENT R EPRESENTATION Loan carLoan getTotalLoan() write read getCarLoan () getHouseLoan() setCarLoan() setHouseLoan() Loan houseLoan

34 34 G ETTER M ETHOD Loan carLoan getCarLoan () public Loan getCarLoan(){ return carLoan; } Accessing uninitialized object variable!

35 35 D EFAULT V ALUES FOR V ARIABLES Primitive Variables double computedBMI; double weight; Object Variables Loan loan; variablesmemory 0.0 null computedBMI; weight; loan; Legal double values Illegal Loan value

36 36 G ETTER M ETHOD Loan carLoan getCarLoan () public Loan getCarLoan(){ return carLoan; } ObjectEditor does not try to invoke methods if return value is null!

37 37 O BJECT E DITOR D ISPLAY OF NULL

38 38 H OW TO I NITIALIZE O BJECT V ARIABLE ? Loan carLoan getCarLoan () public Loan getCarLoan(){ return carLoan; } Create instance of ALoan or AnotherLoan and assign to variable

39 39 I NITIALIZATION OF O BJECT V ARIABLES Loan carLoan = new ALoan(); Loan houseLoan = new AnotherLoan(); Loan houseLoan = new Loan();

40 40 I NTERACTIVE VS. P ROGRAMMED I NSTANTIATION new ALoan()

41 41 AL OAN P AIR Loan carLoan = new Aloan() getTotalLoan() write read getCarLoan () getHouseLoan() setCarLoan() setHouseLoan() Loan houseLoan = new AnotherLoan()

42 42 GET T OTAL L OAN () Loan carLoan getTotalLoan() Loan houseLoan public Loan getTotalLoan(){ return carLoan + houseLoan; } + not defined for Loan!

43 43 P ROGRAMMER -D EFINED A DD A LGORITHM 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 44 P ROGRAMMER -D EFINED A DD 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; }

45 45 R ETURNING A NOTHER L OAN 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; }

46 46 C OMBINING O BJECT C REATION AND I NITIALIZATION 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 creation and initialization

47 47 A DDING C ONSTRUCTOR IN AL OAN int principal getPrincipal()setPrincipal() write read getYearlyInterest() getMonthlyInterest() setYearlyInterest() setMonhtlyInterest() ALoan() initialize Constructor

48 48 C ONSTRUCTOR VS. R EGULAR M ETHOD public 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 49 I NSTANTIATION R EQUIRES P ARAMETERS new ALoan(10000) new ALoan()

50 50 C ONSTRUCTOR 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 51 M ULTI -P ARAMETER C ONSTRUCTOR 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 52 D EFAULT C ONSTRUCTOR public 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 53 P ROGRAMMER -D EFINED A DD 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 54 P OLYMORPHIC M ETHODS public static Loan add(Loan loan1, Loan loan2) { return new ALoan(loan1.getPrincipal() + loan2.getPrincipal())); } Methods that take actual parameters of different types ALoan instance AnotherLoan instance Actual parameters of different types

55 55 N ON -P OLYMORPHIC M ETHODS 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 56 O VERLOADING VS. P OLYMORPHISM 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())); } PolymorphismOverloading add (new ALoan(10000), new ALoan(5000)); add (new ALoan(10000), new AnotherLoan(5000));

57 57 P RIMITIVE VS. O BJECT T YPES types Primitive types Object types double int ABMICalculatorABMISpreadsheet ALoan BMISpreadsheet Classes Interfaces type = set of operations AnotherLoan Loan

58 58 S TRUCTURED VS. A TOMIC T YPES types Primitive types Structured types double int ABMICalculatorABMISpreadsheet ALoan BMISpreadsheet Classes Interfaces Instances of structured type decomposed into one or more smaller values AnotherLoan Loan

59 59 L OAN I NTERFACE 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 60 AL OAN R EPRESENTATION int principal getPrincipal()setPrincipal() write read getYearlyInterest() getMonthlyInterest() setYearlyInterest() setMonhtlyInterest()

61 61 A NOTHER L OAN R EPRESENTATION int yearlyInterest getPrincipal()setPrincipal() write read getYearlyInterest() getMonthlyInterest() setYearlyInterest() setMonhtlyInterest()

62 62 P HYSICAL VS. L OGICAL S TRUCTURE ALoan Instanceint principal; Physical ALoan Instanceint Principal; Logical int YearlyInterest; MonthlyInterest;

63 63 L OAN P AIR I NTERFACE public interface LoanPair { public Loan getCarLoan(); public void setCarLoan(Loan newValue); public Loan getHouseLoan(); public void setHouseLoan(Loan newValue); public Loan getTotalLoan(); }

64 64 AL OAN P AIR Loan carLoan getTotalLoan() write read getCarLoan () getHouseLoan() setCarLoan() setHouseLoan() Loan houseLoan

65 65 P HYSICAL VS. L OGICAL S TRUCTURE ALoanPair Instance Loan carLoan; Physical int principal; Loanint principal; houseLoan; Variable name Class or primitive type of value stored in variable

66 66 P HYSICAL VS. L OGICAL S TRUCTURE ALoanPair Instance Loan CarLoan; Logical Loan TotalLoan; HouseLoan; int Principal; int YearlyInterest; MonthlyInterest; Property name Class or primitive type of property value


Download ppt "C OMP 110 L OAN C ASE S TUDY Instructor: Jason Carter."

Similar presentations


Ads by Google