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

Slides:



Advertisements
Similar presentations
Basic -2 Classes and Objects. Classes and Objects A class is a complex data TYPE An object is an instance of a class. Example: Class: Person Objects:
Advertisements

C OMP 110 S TATE Instructor: Jason Carter. 2 O UTLINE Instance Variables Procedures Properties Print Statements Println vs. Print Overloading.
C OMP 110 T YPES Instructor: Jason Carter. 2 O BJECTS VS. P RIMITIVE T YPES Instances of classes and interfaces are objects All other values are primitives.
C OMP 110 F UNCTIONS Instructor: Jason Carter. 2 O UTLINE Programmatic instantiation of objects Functions calling other functions Algorithm and stepwise.
C OMP 401 M EMORY R EPRESENTATION OF P RIMITIVE V ALUES AND O BJECTS Instructor: Prasun Dewan.
C OMP 110 A RRAYS Instructor: Jason Carter. 2 O UTLINE for loops Arrays.
C OMP 401 C LASS S TATE Instructor: Prasun Dewan.
C OMP 110 C ONDITIONALS Instructor: Jason Carter.
C OMP 110 R EPRESENTATION Instructor: Jason Carter.
C OMP 110 S TYLE Instructor: Jason Carter. 2 I NTERFACES AND M ORE S TYLE Define contracts between our users and implementers Optional – they may not.
H OW O BJECTS B EHAVE. O VERVIEW Methods use object state Arguments and return types in methods Java passes by value Getters and Setters Encapsulation.
C OMP 401 O BJECTS Instructor: Prasun Dewan 2 C OMPUTER VS. P ROGRAM M ODEL Processor Compiler Program (source code)
C OMP 110 O BJECTS Instructor: Jason Carter. 2 C OMPUTER VS. P ROGRAM M ODEL Processor Compiler Program (source code)
C OMP 110 M AIN & C ONSOLE I NPUT Instructor: Jason Carter.
By - Qiong Han. DON’T FALL BEHIND IN READING Submission Instruction Everyone needs to submit the printed HARD- COPY of the codes and the grading sheets.
Midterm Exam 75 points 1 min per point Allocate time proportionate to points Closed book Chapters 1-5 (except char) PDF/PS with index and corrections coming.
Program Style Identifier Names Comments Named Constants Avoiding Code Repetition Giving Least Privilege Efficiency Interfaces.
Comp 14 (3) By Stephan Sherman Office hours, W, 3:00-3:50pm.
Class Variables & Methods More on dependencies Representation and representation errors Primitive vs. Object Properties Class Variables/Methods Primitive.
Evan Korth New York University Computer Science I Classes and Objects Professor: Evan Korth New York University.
Main and Control Flow Programming without ObjectEditor Main Class Control Flow “Real” Programming Linear Branching Looping Programmer-Defined Overloading.
Programmer-Defined Types Object Types as Programmer-defined Types Two-way Dependencies Representation Errors Primitive Vs Object Properties Constructors.
Object Types Primitive types Primitive Vs Object Types Types Classes ABMISpreadsheet AnotherBMISpreadsheet Interfaces BMISpreadsheet Loan Stringdoubleint.
ObjectEditor Prasun Dewan Comp 114. ObjectEditor Automatic user-interface generation. You only write computation code Separate object to do I/O Main just.
C OMP 110 M ODEL -V IEW -C ONTROLLER MVC Instructor: Jason Carter.
Class Variables & Methods More on dependencies Representation and representation errors Primitive vs. Object Properties Class Variables/Methods Primitive.
State Instance Variables Procedures Properties Print Statements Println Vs Print Overloading J++
© The McGraw-Hill Companies, 2006 Chapter 7 Implementing classes.
Bicycle Car Types pedal() pushAccelerator(). Stringdoubleint Programming Language Types “three”“four” = 7 = “threefour” + + / = 0 / = 0.75.
Introduction to Java Programming, 4E Y. Daniel Liang.
Chapter Four Defining Your Own Classes continued.
C OMP 110 M AIN & C ONSOLE I NPUT Instructor: Sasa Junuzovic.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved COS240 O-O Languages AUBG,
C OMP 401 C LASS S TATE Instructor: Prasun Dewan.
C OMP 401 P ATTERNS, I NTERFACES AND O BJECT E DITOR Instructor: Prasun Dewan.
C OMP 110/401 C OMPOSITE A NNOTATIONS Instructor: Prasun Dewan.
INLS 560 – O BJECTS Instructor: Jason Carter. O BJECTS Natural Objects Manufactured Objects.
Chapter 7 Objects and Classes 1 Fall 2012 CS2302: Programming Principles.
C OMP 401: C ONSTRUCTORS AND P OINTERS Instructor: Prasun Dewan (FB 150,
C OMP 110 T YPES Instructor: Prasun Dewan 2 P REREQUISITES Interfaces.
ECE122 Feb. 22, Any question on Vehicle sample code?
C OMP 110/401 D OCUMENTATION : C OMMENTS Instructor: Prasun Dewan.
C OMP 110/401 E NCAPSULATION AND L EAST P RIVILEGE Instructor: Prasun Dewan.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
Classes. Constructor A constructor is a special method whose purpose is to construct and initialize objects. Constructor name must be the same as the.
I NCREASING THE A UTOMATION OF A T OOLKIT WITHOUT R EDUCING ITS A BSTRACTION AND U SER - I NTERFACE F LEXIBILITY Prasun Dewan University of North Carolina.
IT108 Objects and Classes Part I George Mason University Revised 4/3/2012.
CSC 143F 1 CSC 143 Constructors Revisited. CSC 143F 2 Constructors In C++, the constructor is a special function automatically called when a class instance.
C OMP 401 R EFLECTION AND A CTION O BJECTS Instructor: Prasun Dewan.
Recitation 7 Collections. Array List and Linked List Array List and Linked List are implementations of the same interface: List. As a result, they have.
CMSC 202 Advanced Section Classes and Objects: Object Creation and Constructors.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
Java Programming, Second Edition Chapter Three Using Methods, Classes, and Objects.
Chapter 7 Classes and Methods III: Static Methods and Variables Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition)
A DVANCED P ROGRAMMING C HAPTER 4: I NTRODUCTION TO I NTRODUCTION TO C LASSES, O BJECTS, M ETHODS AND STRINGS Dr Shahriar Bijani Winter 2016.
C OMP 401 P RACTICAL A PPLICATIONS OF MVC AND O BSERVER : P ROPERTY N OTIFICATION Instructor: Prasun Dewan.
College Board Topics – A.P. Computer Science A Program Design - Read and understand a problem's description, purpose, and goals; Apply data abstraction.
This In Java, the keyword this allows an object to refer to itself. Or, in other words, this refers to the current object – the object whose method or.
C OMP 110/401 D OCUMENTATION : A NNOTATIONS Instructor: Prasun Dewan.
C OMP 401 C OPY : S HALLOW AND D EEP Instructor: Prasun Dewan.
Programming in Java Transitioning from Alice. Becomes not myFirstMethod but …. public static void main (String[] arg) { // code for testing classes goes.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. P ART 3: A GGREGATION, I NHERITANCE AND P.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. P ART 1: O BJECTS AND C LASSES 1.
J AVA P ROGRAMMING 2 CH 04: C LASSES, O BJECTS AND M ETHODS (II) 0.
Reference: COS240 Syllabus
Chapter 3: Using Methods, Classes, and Objects
Comp 110/401 Documentation: Comments
Recitation 4 September 16, 2011.
Recitation 3 September 9, 2011 Wade Gobel.
Presentation transcript:

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

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

3 L OAN O BJECT

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 L OAN O BJECT Editable and dependent

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

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

8 B OTTOM -U P P ROGRAMMING Interface Class

9 L OAN O BJECT

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

12 L OAN O BJECT Editable and dependent Stored Computed

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

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 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 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 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 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 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 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 M IDDLE -O UT P ROGRAMMING Interface Representation Algorithm Class

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

23 C ONVERSION E RRORS WITH P RINCIPAL R EPRESENTATION

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

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 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 L OAN P AIR I NTERFACE ( EDIT ) public interface LoanPair { }

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

29 T YPING O BJECTS ALoan AnotherLoan Loan

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 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 S PACE - EFFICIENT I MPLEMENTATION Car Loan House Loan Total Loan Independent Dependent Computed Stored

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

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

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 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 O BJECT E DITOR D ISPLAY OF NULL

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 I NITIALIZATION OF O BJECT V ARIABLES Loan carLoan = new ALoan(); Loan houseLoan = new AnotherLoan(); Loan houseLoan = new Loan();

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

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

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

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 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 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 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 A DDING C ONSTRUCTOR IN AL OAN int principal getPrincipal()setPrincipal() write read getYearlyInterest() getMonthlyInterest() setYearlyInterest() setMonhtlyInterest() ALoan() initialize Constructor

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 I NSTANTIATION R EQUIRES P ARAMETERS new ALoan(10000) new ALoan()

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

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

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

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 AL OAN P AIR Loan carLoan getTotalLoan() write read getCarLoan () getHouseLoan() setCarLoan() setHouseLoan() Loan houseLoan

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