Presentation is loading. Please wait.

Presentation is loading. Please wait.

Class Variables & Methods More on dependencies Representation and representation errors Primitive vs. Object Properties Class Variables/Methods Primitive.

Similar presentations


Presentation on theme: "Class Variables & Methods More on dependencies Representation and representation errors Primitive vs. Object Properties Class Variables/Methods Primitive."— 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 Properties 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); } Height Weight BMI Editable Read-only Independent Dependent Read-Only

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

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

7 Loan Object

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

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

10 Stored vs. Computed Editable Dependent Stored Computed

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

12 Setting and Getting Stored Property setPrincipal()getPrincipal() public void setPrincipal(int newVal) { principal = newVal; } public int getPrincipal(){ return principal; } int principal

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

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

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

16 Middle-Out Programming Interface Representation Algorithm Class

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

18 Conversion Errors with Principal Repn.

19 No Conversion Errors with YearlyInterest Repn.

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

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

22 Object Types Primitive types Primitive vs. Object Types Types Classes ABMISpreadsheet AnotherBMISpreadsheet Interfaces BMISpreadsheet Loan Stringdoubleint Type = Set of operations ALoan AnotherLoan

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

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

25 Space Efficient Representation Loan carLoan setHouseLoan() getHouseLoan() setCarLoan() getCarLoan() writeread Loan houseLoan getTotalLoan()

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

27 variablesmemory Object Variables Primitive Variables Default Values for Variables height 0.0 weight null carLoan houseLoan Loan carLoan Loan houseLoan double height double weight Legal double Values Illegal Loan values

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

29 ObjectEditor Display of Null

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

31 ALoanPair Loan carLoan = new ALoan() setCarLoan() getCarLoan() writeread Loan houseLoan = new AnotherLoan() getTotalLoan() setHouseLoan() getHouseLoan()

32 getTotalLoan() Loan carLoan = new ALoan() Loan houseLoan = new AnotherLoan() getTotalLoan() public Loan getTotalLoan(){ houseLoan + carLoan; }

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

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

35 public Loan add(Loan loan1, Loan loan2) { Loan retVal = new AnotherLoan(); retVal.setPrincipal(loan1.getPrincipal() + loan2.getPrincipal()); return retVal; } Returning AnotherLoan() 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; }

36 Other alternatives 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())); }

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

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

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

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

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

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

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

44 returning numLoans getNumLoans() returns numInstances public static int getNumLoans() { return numInstances; }

45 Incrementing getNumLoans (edit) Increment numInstances each time a new instance is created ???

46 Incrementing getNumLoans (edited) Increment numInstances each time a new instance is created Public Aloan() { numInstances++ }

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

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

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

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

51 Instance Vs Class Members Class Members –Class methods –Class variables Instance Members –Instance methods –Instance variables

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

53 public static int getNumLoans() { System.out.println(principal); return numInstances; } Legal & Illegal Accesses static int numInstances = 0; public ALoan(int initPrincipal) { setPrincipal(initPrincipal); numInstances = numInstances + 1; System.out.println(getNumLoans()); } int principal; public setPrincipal(int newPrincipal) { principal = newPrincipal; }

54 Class vs. Instance Constant Should be class constant –guaranteed to have one copy –easy to refer (require no instance creation) –Class methods can refer to it. Unless some good reason for hiding named constants from class methods

55 Class vs. Instance Methods public static Loan add(Loan loan1, Loan loan2) { return new ALoan(loan1.getPrincipal() + loan2.getPrincipal())); } Class MethodAccesses no instance variable public Loan add(Loan loan1, Loan loan2) { return new ALoan(loan1.getPrincipal() + loan2.getPrincipal())); } Instance Method ALoan.add(carLoan, houseLoan) Math.round(5.7) (new ALoan).add(carLoan, houseLoan) (new Math).round(5.7)

56 Class vs. Instance Method Instance method has all the privileges of a class method. Any class method can be made an instance method. Should not have instance method that does not access any instance variable. –They belong to the class. –Violate least privilege principle. –Require needless instantiation. But instance method can be put in interfaces Tradeoff!

57 ACourseFactory package factories; import courseTree.RegularCourse; import courseTree.FreshmanSeminar; import courseTree.ARegularCourse; import courseTree.AFreshmanSeminar; public class ACourseFactory implements CourseFactory { public RegularCourse getRegularCourse() { return new ARegularCourse(); } public FreshmanSeminar getFreshmanSeminar() { return new AFreshmanSeminar(); } No instance variables accessed.

58 ACourseFactory with class methods package factories; import courseTree.RegularCourse; import courseTree.FreshmanSeminar; import courseTree.ARegularCourse; import courseTree.AFreshmanSeminar; public class ACourseFactory { public static RegularCourse getRegularCourse() { return new ARegularCourse(); } public static FreshmanSeminar getFreshmanSeminar() { return new AFreshmanSeminar(); } ACourseFactory.getRegularCourse();(new ACourseFactory).getRegularCourse(); No interface

59 ALoggedCourseFactory with class methods package factories; import courseTree.RegularCourse; import courseTree.FreshmanSeminar; import courseTree.ALoggedRegularCourse; import courseTree.ALoggedFreshmanSeminar; public class ALoggedCourseFactory { public static RegularCourse getRegularCourse() { return new ALoggedRegularCourse(); } public static FreshmanSeminar getFreshmanSeminar() { return new ALoggedFreshmanSeminar(); }

60 Cannot substitute factories void fillCourses() { CourseFactory courseFactory = (new ACourseFactorySelector()).getCourseFactory(); CourseList prog = new ACourseList(); RegularCourse introProg = courseFactory.getRegularCourse (); introProg.init("Intro. Prog.", "COMP", 14);… }

61 Resolving tradeoff Create instance methods when substitution expected. Hard to dream of situation when not. –Factory selector class?

62 Factory Selector Class package factories; public class ACourseFactorySelector implements CourseFactorySelector { public static CourseFactory getCourseFactory() { return new ALoggedCourseFactory(); }

63 Class with only static methods? Math. –round(), power(), … Console –Converts various input strings to primitive types. readInt() readDouble() readBoolean() readChar() Console readString()

64 Separate Class for Input import java.io.BufferedReader; import java.io.InputStreamReader; public class Console { static BufferedReader inputStream = new BufferedReader(new InputStreamReader(System.in)); public static int readInt() { try { return Integer.parseInt(inputStream.readLine()); } catch (Exception e) { System.out.println(e); return 0; } public static String readString() { try { return inputStream.readLine(); } catch (Exception e) { System.out.println(e); return ""; }... //other methods }

65 Without Console import java.io.BufferedReader; import java.io.InputStreamReader; public class AnInputPrinter { public static void main (String[] args) { System.out.println("Please enter the line to be printed"); System.out.println ("The input was: " + readString()); } static BufferedReader inputStream = new BufferedReader(new InputStreamReader(System.in)); public static String readString() { try { return inputStream.readLine(); } catch (Exception e) { System.out.println(e); return ""; }

66 Using Console public class AnInputPrinter { public static void main (String[] args) { System.out.println("Please enter the line to be printed"); System.out.println ("The input was: " + Console.readString()); }

67 Object Types Primitive types Primitive vs. Object Types Types Classes ABMISpreadsheet AnotherBMISpreadsheet Interfaces BMISpreadsheet Loan Stringdoubleint Type = Set of operations ALoan AnotherLoan

68 Primitive vs. Object Types Object types –Classes –Interfaces –Names start with upper case letter Primitive types –int, double, float, boolean, … –Names start with lower case letter Other ways to classify types?

69 Programmer-defined Predefined Predefined vs. Programmer- defined Types Types Classes ABMISpreadsheet AnotherBMISpreadsheet Interfaces BMISpreadsheet Loan String doubleint ALoan AnotherLoan

70 Programmer-defined vs. Predefined Types Programmer-defined interface/class (Loan/ALoan) is programmer-defined type. Programmer-defined types in Java must be object types Some object types are predefined (String) All primitive types are predefined.

71 Structure Types Atomic types Structure vs. Atomic Types Types Classes ABMISpreadsheet AnotherBMISpreadsheet Interfaces BMISpreadsheet Loan Stringdoubleint ALoan AnotherLoan Instances of structure type decomposed into one or more smaller values AnExpressionStart AnExpressionEnd

72 Structure vs. Atomic Types Atomic type cannot be decomposed. Structure types have one or more “components”.

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

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

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

76 Physical vs. Logical Structure ALoan instance double principalALoan instance double Principal Yearly Interest Monthly Interest double Physical Logical

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

78 ALoanPair Loan carLoan = new ALoan() setCarLoan() getCarLoan() writeread Loan houseLoan = new AnotherLoan() getTotalLoan() setHouseLoan() getHouseLoan()

79 Physical vs. Logical Structure ALoan double principal Loan double Principal Yearly Interest Monthly Interest double Physical Logical ALoanPair Instance carLoan houseLoan yearlyInterest AnotherLoandouble ALoanPair Instance CarLoan Loan HouseLoan TotalLoan Loan Variable Name Class/primitive type of value stored in Variable Property Name Property Type

80 Physical vs. Logical Physical describes internal (in-memory) state of object. Logical describes external (exported) state of object.

81 Loan Pair Variation public interface LoanPair { public int getCarLoanMonthlyInterest(); public void setCarLoanMonthlyInterest(int newValue); public int getHouseLoanMonthlyInterest(); public void setHouseLoanMonthlyInterest(int newValue); public Loan getTotalLoan (); }

82 ALoanPair Variation Loan carLoan = new ALoan() setCarLoanMonthlyInterest() getCarLoanMonthlyInterest() writeread Loan houseLoan = new AnotherLoan() getTotalLoan() setHouseLoanMonthlyIntetest() getHouseLoanMonthlyInterest()

83 Physical vs. Logical Structure ALoan double principal Loan double Principal Yearly Interest Monthly Interest double Physical Logical ALoanPair Instance carLoan houseLoan yearlyInterest AnotherLoandouble ALoanPair Instance TotalLoan int HouseLoan MonthlyInterest CarLoanMonthly Interest int

84 Hierarchical Structures int/double –Atomic Loan/ALoan –Properties/variables are atomic LoanPair/ALoanPair –Properties/variables are themselves structured. Multi-level structure –Property can be atomic or structured Infinite-level structure –Descendent of type T can be of type T itself –Tree


Download ppt "Class Variables & Methods More on dependencies Representation and representation errors Primitive vs. Object Properties Class Variables/Methods Primitive."

Similar presentations


Ads by Google