java program = collection of classes

Download Java Program = collection of Classes

Post on 06-Jan-2016

23 views

Category:

Documents

1 download

Embed Size (px)

DESCRIPTION

Java Program = collection of Classes. Class::Object. instances of the class. abstract concept. BankAccount::BankAccountOfJohnSmith#127. Defining a class. class class-name [extends superclass name] [implements interface name] { [Declaration of variables] - PowerPoint PPT Presentation

TRANSCRIPT

  • Java Program = collection of ClassesClass::Objectabstract conceptinstances of the classBankAccount::BankAccountOfJohnSmith#127

  • class class-name [extends superclass name] [implements interface name] { [Declaration of variables] [Declaration of methods]}Defining a class

  • Instance variablespublic class Book { public String title,author,publisher; public int pages; public double price; ...}Book textbook=new Book();textbook.title=JAVA Introduction to CS and programming;textbook.author=W. Savitch;textbook.publisher=Prentice Hall;textbook.pages=1052;...(file Book.java)

  • Instance variablespublic class ToyBankAccount { public String ownersName; public double savingsBalance,checkingBalance; public double penalty; public int pin; ...}ToyBankAccount johnsAccount;johnsAccount=new BankAccount();johnsAccount.ownersName=John Smith;johnsAccount.savingsBalance=1000.0;johnsAccount.checkingBalance=500;johnsAccount.pin=7534;...

    (file ToyBankAccount.java)

  • Instance methodspublic class ToyBankAccount { ............ ............ /* * returns true if the withdrawal from checking is succesful */ public boolean withdrawMoney(int pin,double amount) { if (pin==this.pin) { if (checkingBalance>amount) { checkingBalance-=amount; return true; } } return false; }}

  • Instance methodspublic class ToyBankAccount { ............ ............ /* * returns true if the check clears. */ public boolean payCheck(double amount) { if (checkingBalance>amount) { checkingBalance-=amount; return true; } else { penalty+=25.0; return false; } } }}

  • Instance methodspublic class ToyBankAccount { ............ ............ /* * returns true if the check clears. */ public boolean payCheck(double amount) { if (checkingBalance>amount) { checkingBalance-=amount; return true; } else { penalty+=25.0; return false; } } }}Not a good programming practice!

  • Constantspublic class ToyBankAccount { public static final int PENALTY_FOR_CHECK_OVER_LIMIT=25; ............ /* * returns true if the check clears. */ public boolean payCheck(double amount) { if (checkingBalance>amount) { checkingBalance-=amount; return true; } else { penalty+= PENALTY_FOR_CHECK_OVER_LIMIT; return false; } } }}

  • ParameterswithdrawalOK=johnsAccount.withdrawMoney(7534,500);formal parametersactual parametersobjectmethod public boolean withdrawMoney(int pin,double amount) { if (pin==this.pin) { if (checkingBalance>amount) { checkingBalance-=amount; return true; } } return false; }

  • Return type public boolean withdrawMoney(int pin,double amount) { if (pin==this.pin) { if (checkingBalance>amount) { checkingBalance-=amount; return true; } } return false; }withdrawalOK=johnsAccount.withdrawMoney(7534,500);

  • Return type - voidjohnsAccount.depositMoney(500); public void depositMoney(double amount) { checkingBalance+=amount; }

  • Creating objects - newToyBankAccount johnsAccount;johnsAccount=new ToyBankAccount();johnsAccount is a variable which canpoint to an object of type ToyBankAccountcreate a new object of type ToyBankAccountand let johnsAccount point to it

  • ConstructorToyBankAccount johnsAccount;johnsAccount=new ToyBankAccount();Call to a special method ofthe class constructor.public ToyBankAccount() {}default constructor is

  • Constructorpublic ToyBankAccount(String ownersName, double checkingInitialAmount, int pin) { checkingAmount=checkingInitialAmount; savingsAcount=penalty=0; this.pin=pin; this.ownersName=new String(ownersName);}ToyBankAccount johnsAccount;johnsAccount=new ToyBankAccount(John Smith,0,1234);

  • Constructor - overloadingpublic ToyBankAccount(String ownersName, int checkingInitialAmount, int pin) { checkingAmount=checkingInitialAmount; savingsAcount=penalty=0; this.pin=pin; this.ownersName=new String(ownersName);}public ToyBankAccount(String ownersName, int pin) { savingsAcount=penalty=checkingInitialAmount=0; this.pin=pin; this.ownersName=new String(ownersName);}

  • Constructor - this()public ToyBankAccount(String ownersName, int checkingInitialAmount, int pin) { checkingAmount=checkingInitialAmount; savingsAcount=penalty=0; this.pin=pin; this.ownersName=new String(ownersName);}public ToyBankAccount(String ownersName, int pin) { this(ownersName,0,pin);}(must be first statement)

  • public/private for instance variablespublic anybody can accessprivate only methods of the class can accesspublic class ToyBankAccount { public String ownersName; public double savingsBalance,checkingBalance; public double penalty; public int pin; ...}

  • ToyBankAccount johnsAccount;johnsAccount=new ToyBankAccount(John Smith,100,1234);

    johnsAccount.savingsAccount+=50;Not good. Suppose that we want to modifythe code to store all transactions. Have to find all occurences of public/private for instance variables

  • public/private for instance variablespublic anybody can accessprivate only methods of the object can accesspublic class ToyBankAccount { private String ownersName; private double savingsBalance,checkingBalance; private double penalty; private int pin; ...}Instance variables should be private

  • public class Book { private String title,author,publisher; private int pages; private double price;

    public String getTitle() { return title; } public void setTitle(String title) { this.title=title; }}Accessor/Mutator methods

  • public for classesin each file:one public class withthe same name as the filenameany number of classes whichare visible only to classes inthe file

  • ToyBankAccount johnsAccount;johnsAccount=new ToyBankAccount(John Smith,0,1234);Variablesof primitive typeof class typeint x;ToyBankAccount marysAccount=johnsAccount;marysAccount.depositMoney(1000);System.out.println(+johnsAccount.balance())

  • ToyBankAccount johnsAccount;johnsAccount=new ToyBankAccount(John Smith,0,1234);ToyBankAccount marysAccount=johnsAccount;marysAccount.depositMoney(1000);System.out.println(+johnsAccount.balance())VariablesmarysAccountjohnsAccountint x;x=10;int y=x;y+=10

  • Privacy leaksprivate Address ownersAddress ;

    public Address getOwnersAddress() { return ownersAddress; }address=johnsAccount.getOwnersAddress();

    address.set(John Evil,....);

  • Privacy leaksprivate Address ownersAddress ;

    public Address getOwnersAddress() { return new Address(ownersAddress); }address=johnsAccount.getOwnersAddress();

    address.set(John Evil,....);

  • nullToyBankAccount johnsAccount=null;does not point to any object...

  • null Pointer Exception errorjohnsAccount.depositMoney(100);

  • ==, = and equalsfor variables of class type == testswhether they point to the same object.String s=abc, t=abc;if (s==t) system.out.println(You win $1,000,000);

  • ==, = and equalsfor variables of class type == testswhether they point to the same object.String s=abc, t=abc;if (s.equals(t)) system.out.println( You were chosen for the next round!);You have to implement equals method if you want deeper equality testing for your class.

  • ==, = and equalsYou have to implement equals method if you want deeper equality testing for your class.boolean equals(Book s) { return author.equals(s.author)&& title.equals(s.title)&& ... ;}

  • Method overloadingpublic void depositMoney(double amount,Currency currency) { checkingBalance+=Currency.Convert(amount,currency,Currency.USD);} public void depositMoney(double amount) { checkingBalance+=amount; }JohnsAccount.depositMoney(100,Currency.SKK);JohnsAccount.depositMoney(100);

  • Method overloadingpublic void depositMoney(double amount,Currency currency) { checkingBalance+=Currency.Convert(amount,currency,Currency.USD);} public void depositMoney(double amount) { checkingBalance+=amount; } public void depositMoney(double amountInSkk) { checkingBalance+=amount*50; }

  • Method overloadingpublic void depositMoney(double amount,Currency currency) { checkingBalance+=Currency.Convert(amount,currency,Currency.USD);} public void depositMoney(double amount) { checkingBalance+=amount; } public char depositMoney(double amountInSkk) { checkingBalance+=amount*50; }cannot overload based on the returned type

  • Automatic conversionjohnsAccount.depositMoney(500); public void depositMoney(double amount) { checkingBalance+=amount; }int

  • Class variablespublic class ToyBankAccount { private static int totalAccounts=0; private static totalAccountBalance;

    private String ownersName; private double savingsBalance,checkingBalance; private double penalty; private int pin; ...}

  • Class variablespublic class ToyBankAccount { private static int totalAccounts=0;{totalAccounts++;} private static totalAccountBalance;

    private String ownersName; private double savingsBalance,checkingBalance; private double penalty; private int pin; ...}initializer block

  • Class variablespublic class ToyBankAccount { private static final int PENALTY_FOR_CHECK_OVER_LIMIT=25; private static int totalAccounts