Unformatted text preview:

Class Variables & MethodsLoan ObjectLoan vs. BMIProperties ClassificationRead-Only vs. Editable, Dependent vs. Independent1-Way vs. 2-Way DependenciesSlide 7Loan InterfaceA Loan RepresentationStored vs. ComputedLoan AlgorithmSetting and Getting Stored PropertyGetting and Setting Computed PropertySlide 14Another Loan RepresentationConversion Errors with Principal Repn.No Conversion Errors with YearlyInterest Repn.Loan PairPrimitive vs. Object PropertiesPrimitive vs. Object TypesLoan Pair InterfaceSpace Efficient RepresentationSlide 23Getter MethodDefault Values for VariablesInvoking methods on nullObjectEditor Display of NullInitialization of Object VariablesALoanPairgetTotalLoan()Programmer-Defined Add AlgorithmProgrammer-Defined AddReturning AnotherLoan()Other alternativesInstance vs. Class operationReal-World AnalogyO-O WordClass MethodsSlide 39External Call of Class MethodnumInstances() Algorithmreturning numLoansIncrementing getNumLoansIncrementing getNumLoans (soln)Declaring numInstancesSlide 46Slide 47Instance Vs Class MembersLegal & Illegal AccessesScope of Class and Instance MembersClass vs. Instance ConstantClass vs. Instance MethodsClass vs. Instance MethodACourseFactoryACourseFactory with class methodsALoggedCourseFactory with class methodsCannot substitute factoriesResolving tradeoffFactory Selector ClassClass with only static methods?Separate Class for InputWithout ConsoleUsing ConsoleSlide 64Slide 65Predefined vs. Programmer-defined TypesProgrammer-defined vs. Predefined TypesStructure vs. Atomic TypesSlide 69Slide 70Slide 71Slide 72Physical vs. Logical StructureSlide 74Slide 75Slide 76Physical vs. LogicalLoan Pair VariationALoanPair VariationSlide 80Hierarchical StructuresClass Variables & Methods•More on dependencies•Representation and representation errors•Primitive vs. Object Properties•Class Variables/Methods•Primitive vs. Object Types•Simple vs. Structured TypesLoan ObjectLoan vs. BMIProperties 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);}}HeightWeightBMIEditableEditableRead-onlyIndependentIndependentDependentRead-OnlyRead-Only vs. Editable, Dependent vs. IndependentEditableDependent1-Way vs. 2-Way DependenciesBMIHeightWeightPrincipleMonthly InterestYearly InterestLoan ObjectLoan Interfacepublic 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); }A Loan RepresentationsetYearlyInterest()setMonthlyInterest()getYearlyinterest()setPrincipal() getPrincipal()getMonthlyInterest()writereaddouble principalStored vs. ComputedEditableDependentStoredComputedLoan AlgorithmsetYearlyInterest()setMonthlyInterest()getYearlyinterest()setPrincipal() getPrincipal()getMonthlyInterest()writereadF1-1F2-1F1F2int principalSetting and Getting Stored PropertysetPrincipal() getPrincipal() public void setPrincipal(int newVal) {principal = newVal;}public int getPrincipal(){ return principal; }int principalGetting and Setting Computed PropertysetYearlyInterest()getYearlyinterest()writereadF1-1F1public int getYearlyInterest() { return principal* INTEREST_RATE/100; } public void setYearlyInterest(int newVal) { principal = newVal*100/INTEREST_RATE;}int principalGetting and Setting Computed PropertysetMonthlyInterest()getMonthlyInterest()F2-1F2public int getMonthlyInterest() { return getYearlyInterest()/12; } public void setMonthlyInterest(int newVal) { principal = setYearlyInterest(newVal*12);}int principalAnother Loan Representationint yearlyInterestsetYearlyInterest()setMonthlyInterest()getYearlyinterest()setPrincipal() getPrincipal()getMonthlyInterest()writereadConversion Errors with Principal Repn.No Conversion Errors with YearlyInterest Repn.Car LoanHouse LoanTotal LoanPrincipalYearly InterestMonthly InterestLoan PairCar Loan Yearly InterestCar Loan Monthly InterestHouse Loan PrincipalCar Loan Principal...Primitive vs. Object PropertiesLoan PairCar Loan PrincipalCar Loan Yearly InterestCar Loan Monthly InterestHouse Loan Principal...Loan PairCar LoanHouse LoanTotal LoanPrimitive PropertiesObject PropertiesReusing Loan!Object TypesPrimitive typesPrimitive vs. Object TypesTypesClassesABMISpreadsheetAnotherBMISpreadsheetInterfacesBMISpreadsheet LoanStringdouble intType = Set of operations ALoan AnotherLoanLoan Pair Interfacepublic interface LoanPair { public Loan getCarLoan(); public void setCarLoan(Loan newValue); public Loan getHouseLoan(); public void setHouseLoan(Loan newValue); public Loan getTotalLoan(); }Space Efficient RepresentationDependentStoredComputedCar LoanHouse LoanTotal LoanPrincipalYearly InterestMonthly InterestDependentIndependentSpace Efficient RepresentationLoan carLoansetHouseLoan()getHouseLoan()setCarLoan()getCarLoan()write readLoan houseLoangetTotalLoan()Getter MethodLoan carLoangetCarLoan()public Loan getCarLoan(){ return carLoan; }Accessing Uninitialized Object Variable !!variables memoryObject VariablesPrimitive VariablesDefault Values for Variablesheight0.00.0weightnullnullcarLoanhouseLoanLoan carLoanLoan houseLoandouble heightdouble weightLegal double ValuesIllegal Loan valuesInvoking methods on nullcarLoan.getPrincipal()•null pointer exception.•unchecked exceptionObjectEditor Display of NullInitialization of Object VariablesLoan carLoan = new ALoan()Loan houseLoan = new AnotherLoan()ALoanPairLoan carLoan = new ALoan()setCarLoan()getCarLoan()write readLoan houseLoan = new AnotherLoan()getTotalLoan()setHouseLoan()getHouseLoan()getTotalLoan()Loan carLoan = new ALoan()Loan houseLoan = new AnotherLoan()getTotalLoan()public Loan getTotalLoan(){ houseLoan + carLoan;}Programmer-Defined Add Algorithmpublic 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}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) {


View Full Document

UNC-Chapel Hill COMP 114 - Class Variables & Methods

Download Class Variables & Methods
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Class Variables & Methods and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Class Variables & Methods 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?