Unformatted text preview:

Programmer Defined Types Object Types as Programmer defined Types Two way Dependencies Representation Errors Primitive Vs Object Properties Constructors Class variables methods Top Down Bottom up Middle out Programming Loan Object Properties Classification public class ABMISpreadsheet double height public double getHeight return height Height public void setHeight double newHeight height newHeight double weight public double getWeight return weight Weight public void setWeight double newWeight weight newWeight public double getBMI BMI return weight height height Read Only Editable Independent Editable Independent Read only Dependent Read Only Vs Editable Dependent Vs Independent Editable Dependent 1 Way Vs 2 Way Dependencies Height BMI Weight Monthly Interest Principle Yearly Interest Top Down Programming Interface Representation Algorithm Class Bottom up Programming in BMI Interface Class Loan Object 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 A Loan Representation setYearly Interest setPrincipal setMonthly Interest write read double principal getYearly interest getPrincipal getMonthly Interest Stored Vs Computed Stored Editable Dependent Computed Loan Algorithm setYearly Interest write read getYearly interest F1 F1 1 int principal setPrincipal getPrincipal F2 1 setMonthly Interest getMonthly Interest F2 Setting and Getting Stored Property public void setPrincipal int newVal principal newVal setPrincipal int principal getPrincipal public int getPrincipal return principal Setting and Getting Stored Property public void setPrincipal int newVal principal newVal setPrincipal int principal getPrincipal public int getPrincipal return principal Getting and Setting Computed Property edit setYearly Interest write read getYearly interest F1 F1 1 int principal public int getYearlyInterest return principal INTEREST RATE 100 public void setYearlyInterest int newVal Getting and Setting Computed Property edited setYearly Interest write read getYearly interest F1 F1 1 int principal public int getYearlyInterest return principal INTEREST RATE 100 public void setYearlyInterest int newVal principal newVal INTEREST RATE 100 Getting and Setting Computed Property soln setYearly Interest write read getYearly interest F1 F1 1 int principal public int getYearlyInterest return principal INTEREST RATE 100 public void setYearlyInterest int newVal principal newVal 100 INTEREST RATE Getting and Setting Computed Property edit public void setMonthlyInterest int newVal public int getMonthlyInterest int principal F2 1 setMonthly Interest getMonthly Interest F2 Getting and Setting Computed Property edited public void setMonthlyInterest int newVal setYearlyInterest newVal 12 public int getMonthlyInterest int return getYearlyInterest 12 principal F2 1 setMonthly Interest getMonthly Interest F2 Getting and Setting Computed Property soln public void setMonthlyInterest int newVal principal setYearlyInterest newVal 12 public int getMonthlyInterest int return getYearlyInterest 12 principal F2 1 setMonthly Interest getMonthly Interest F2 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 Middle Out Programming Interface Representation Algorithm Class Another Loan Representation setYearly Interest setPrincipal setMonthly Interest write read int yearlyInteres t getYearly interest getPrincipal getMonthly Interest Conversion Errors with Principal Repn No Conversion Errors with YearlyInterest Repn Loan Pair Car Loan Principal Car Loan Yearly Interest Car Loan Monthly Interest House Loan Principal Car Loan Principal House Loan Yearly Interest Total Loan Monthly Interes Primitive Vs Object Properties Car Loan Principal Primitive Properties Car Loan Yearly Interest Loan Pair Car Loan Monthly Interest House Loan Principal Car Loan Loan Pair Object Properties House Loan Total Loan Reusing Loan Loan Pair Interface edit public interface LoanPair Loan Pair Interface edited public interface LoanPair Typing Objects AnotherLoan ALoan Loan Kinds of Types Types Object Types Primitive types double int Classes String ABMISpreadsheet AnotherBMISpreadsheet ALoan AnotherLoan Interfaces BMISpreadsheet Loan Type Set of operations Loan Pair Interface soln public interface LoanPair public Loan getCarLoan public void setCarLoan Loan newValue public Loan getHouseLoan public void setHouseLoan Loan newValue public Loan getTotalLoan ALoan Instance AnotherLoan Instance Actual Parameters Loan Pair Interface Restricted public interface LoanPair public Loan getCarLoan public void setCarLoan ALoan newValue public Loan getHouseLoan public void setHouseLoan ALoan newValue public Loan getTotalLoan ALoan Instance AnotherLoan Instance Actual Parameters Typing an Object Use interface rather than class to type object variables Define interfaces for all classes that include headers of all public method 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 Space Efficient Representation Car Loan Principal House LoanDependent Yearly Interest Total Loan Monthly Interes Independent Stored Dependent Computed Space Efficient Representation setCarLoan getCarLoan Loan carLoan write read getTotalLoan Loan houseLoan setHouseLoan getHouseLoan Getter Method getCarLoan Loan carLoan public Loan getCarLoan return carLoan Accessing Uninitialized Object Variable Default Values for Variables Primitive Variables double height double weight Object Variables Loan carLoan Loan carLoan variables memory height weight 0 0 0 0 Legal double Values carLoan houseLoan null null Illegal Loan values Invoking methods on null carLoan getPrincipal null pointer exception Exception is an unexpected event error Guilty method will be terminated and exception reported Will see other exceptions later Getter Method getCarLoan Loan carLoan public Loan getCarLoan return carLoan ObjectEditor does not try to invoke methods if return value is null ObjectEditor Display of Null How to initialize object


View Full Document

UNC-Chapel Hill COMP 14 - Programmer-Defined Types

Download Programmer-Defined Types
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 Programmer-Defined Types 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 Programmer-Defined Types 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?