1 / 81

Class Variables & Methods

Class Variables & Methods. More on dependencies Representation and representation errors Primitive vs. Object Properties Class Variables/Methods Primitive vs. Object Types Simple vs. Structured Types. Loan Object. Loan vs. BMI. Read-Only. Editable. Independent. Editable. Independent.

ferris
Download Presentation

Class Variables & Methods

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Class Variables & Methods • More on dependencies • Representation and representation errors • Primitive vs. Object Properties • Class Variables/Methods • Primitive vs. Object Types • Simple vs. Structured Types

  2. Loan Object

  3. Loan vs. BMI

  4. Read-Only Editable Independent Editable Independent Read-only Dependent Properties Classification publicclass ABMISpreadsheet { double height; publicdouble getHeight() { return height; } publicvoid setHeight(double newHeight) { height = newHeight; } double weight; publicdouble getWeight() { return weight; } publicvoid setWeight(double newWeight) { weight = newWeight; } publicdouble getBMI() { return weight/(height*height); } } Height Weight BMI

  5. Editable Read-Only vs. Editable, Dependent vs. Independent Dependent

  6. 1-Way vs. 2-Way Dependencies Height BMI Weight Monthly Interest Principle Yearly Interest

  7. Loan Object

  8. Loan Interface publicinterface Loan { publicint getPrincipal(); publicvoid setPrincipal(int newValue); publicint getYearlyInterest(); publicvoid setYearlyInterest(int newVal); publicint getMonthlyInterest(); publicvoid setMonthlyInterest(int newVal); }

  9. setYearly Interest() getYearly interest() setPrincipal() getPrincipal() getMonthly Interest() setMonthly Interest() A Loan Representation write read double principal

  10. Stored Editable Computed Stored vs. Computed Dependent

  11. setYearly Interest() getYearly interest() setPrincipal() getPrincipal() getMonthly Interest() setMonthly Interest() Loan Algorithm F1 write read F1-1 int principal F2-1 F2

  12. publicvoid setPrincipal(int newVal) { principal = newVal; } setPrincipal() getPrincipal() publicint getPrincipal(){ return principal; } Setting and Getting Stored Property int principal

  13. setYearly Interest() getYearly interest() publicint getYearlyInterest() { return principal* INTEREST_RATE/100; } publicvoid setYearlyInterest(int newVal) { principal =newVal*100/INTEREST_RATE; } Getting and Setting Computed Property F1 write read F1-1 int principal

  14. publicvoid setMonthlyInterest(int newVal) { principal =setYearlyInterest(newVal*12); } publicint getMonthlyInterest() { return getYearlyInterest()/12; } getMonthly Interest() setMonthly Interest() Getting and Setting Computed Property int principal F2-1 F2

  15. setYearly Interest() getYearly interest() setPrincipal() getPrincipal() getMonthly Interest() setMonthly Interest() Another Loan Representation write read int yearlyInterest

  16. Conversion Errors with Principal Repn.

  17. No Conversion Errors with YearlyInterest Repn.

  18. Car Loan Principal Car Loan Yearly Interest Car Loan Monthly Interest House Loan Principal ... Car Loan Principal House Loan Yearly Interest Monthly Interest Total Loan Loan Pair

  19. Car Loan Principal Car Loan Yearly Interest Loan Pair Car Loan Monthly Interest House Loan Principal ... Primitive vs. Object Properties Primitive Properties Car Loan Object Properties House Loan Loan Pair Reusing Loan! Total Loan

  20. Object Types Classes Primitive types double int String Primitive vs. Object Types Types ABMISpreadsheet AnotherBMISpreadsheet ALoan AnotherLoan Interfaces BMISpreadsheet Loan Type = Set of operations

  21. Loan Pair Interface publicinterface LoanPair { public Loan getCarLoan(); publicvoid setCarLoan(Loan newValue); public Loan getHouseLoan(); public void setHouseLoan(Loan newValue); public Loan getTotalLoan(); }

  22. Car Loan Principal Independent House Loan Yearly Interest Dependent Monthly Interest Total Loan Space Efficient Representation Dependent Stored Computed

  23. setCarLoan() getCarLoan() getTotalLoan() write read setHouseLoan() getHouseLoan() Space Efficient Representation Loan carLoan Loan houseLoan

  24. getCarLoan() public Loan getCarLoan(){ return carLoan; } Accessing Uninitialized Object Variable !! Getter Method Loan carLoan

  25. Primitive Variables variables memory double height height 0.0 Legal double Values weight 0.0 double weight Object Variables carLoan null Illegal Loan values Loan carLoan houseLoan null Loan houseLoan Default Values for Variables

  26. Invoking methods on null carLoan.getPrincipal() • null pointer exception. • unchecked exception

  27. ObjectEditor Display of Null

  28. Initialization of Object Variables Loan carLoan = new ALoan() Loan houseLoan = new AnotherLoan()

  29. setCarLoan() getCarLoan() getTotalLoan() write read setHouseLoan() getHouseLoan() ALoanPair Loan carLoan = new ALoan() Loan houseLoan = new AnotherLoan()

  30. getTotalLoan() publicLoan getTotalLoan(){ houseLoan + carLoan; } getTotalLoan() Loan carLoan = new ALoan() Loan houseLoan = new AnotherLoan()

  31. 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 • }

  32. 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; }

  33. 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; }

  34. Other alternatives public Loan add(Loan loan1, Loan loan2) { returnnew ALoan(loan1.getPrincipal() + loan2.getPrincipal())); }

  35. Operation on an instance? Instance vs. Class operation public Loan add(Loan loan1, Loan loan2) { returnnew ALoan(loan1.getPrincipal() + loan2.getPrincipal())); } Should be class operation

  36. numCarsProduced() 101234 getMileage() blend(beigeCar, greyCar) 64000 Real-World Analogy new AnAccord(beige) new AnAccord(grey)

  37. numInstances() 3 getPrincipal() 100000 add(carLoan, totalLoan) totalLoan O-O Word new ALoan(10000) ALoan carLoan houseLoan new ALoan(100000)

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

  39. Accesses instance variables Accesses no instance variable Programmer-Defined Add Instance Method publicLoan getTotalLoan(){ return ALoan.add (houseLoan, carLoan); } Class Method public static Loan add(Loan loan1, Loan loan2) { returnnew ALoan(loan1.getPrincipal() + loan2.getPrincipal())); }

  40. Class as target External Call of Class Method ALoan.add(carLoan, houseLoan) Math.round(5.7)

  41. Increment numInstances each time a new instance is created getNumLoans() returns numInstances numInstances() Algorithm Declare variable, numInstances initialized to zero

  42. publicstaticint getNumLoans() { return numInstances; } returning numLoans getNumLoans() returns numInstances

  43. Incrementing getNumLoans ??? Increment numInstances each time a new instance is created

  44. Incrementing getNumLoans (soln) public ALoan(int initPrincipal) { setPrincipal(initPrincipal); numInstances = numInstances + 1; } Increment numInstances each time a new instance is created

  45. int numInstances = 0; Declaring numInstances Declare variable, numInstances initialized to zero // instance variable

  46. principal 0 10000 0 1 numInstances 5000 principal 0 1 0 1 numInstances Loan Object memory int numInstances = 0; public ALoan(int initPrincipal) { setPrincipal(initPrincipal); numInstances = numInstances + 1; }

  47. numInstances 0 1 2 0 10000 principal 0 5000 principal 1 Loan Object memory static int numInstances = 0; public ALoan(int initPrincipal) { setPrincipal(initPrincipal); numInstances = numInstances + 1; }

  48. Class Members Class methods Class variables Instance Members Instance methods Instance variables Instance Vs Class Members

  49. Legal & Illegal Accesses static int numInstances = 0; int principal; publicstaticint 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; }

  50. 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? Scope of Class and Instance Members

More Related