Java Program = collection of Classes - PowerPoint PPT Presentation

1 / 58
About This Presentation
Title:

Java Program = collection of Classes

Description:

textbook.title='JAVA Introduction to CS and programming'; textbook.author='W. Savitch' ... returns true if the withdrawal from checking is succesful ... – PowerPoint PPT presentation

Number of Views:15
Avg rating:3.0/5.0
Slides: 59
Provided by: krt85
Category:

less

Transcript and Presenter's Notes

Title: Java Program = collection of Classes


1
Java Program collection of Classes
ClassObject
instances of the class
abstract concept
BankAccountBankAccountOfJohnSmith127
2
Defining a class
class class-name extends superclass name
implements interface name
Declaration of variables
Declaration of methods
3
Instance variables
(file Book.java)
public class Book public String
title,author,publisher public int pages
public double price ...
Book textbooknew Book() textbook.titleJAVA
Introduction to CS and programming textbook.auth
orW. Savitch textbook.publisherPrentice
Hall textbook.pages1052 ...
4
Instance variables
(file ToyBankAccount.java)
public class ToyBankAccount public String
ownersName public double savingsBalance,checkin
gBalance public double penalty public int
pin ...
ToyBankAccount johnsAccount johnsAccountnew
BankAccount() johnsAccount.ownersNameJohn
Smith johnsAccount.savingsBalance1000.0 johnsA
ccount.checkingBalance500 johnsAccount.pin7534
...
5
Instance methods
public class ToyBankAccount
............ ............ /
returns true if the withdrawal from checking is
succesful / public boolean
withdrawMoney(int pin,double amount) if
(pinthis.pin) if (checkingBalancegtamoun
t) checkingBalance-amount
return true return false
6
Instance methods
public class ToyBankAccount
............ ............ /
returns true if the check clears. / public
boolean payCheck(double amount) if
(checkingBalancegtamount)
checkingBalance-amount return true
else penalty25.0 return false

7
Instance methods
public class ToyBankAccount
............ ............ /
returns true if the check clears. / public
boolean payCheck(double amount) if
(checkingBalancegtamount)
checkingBalance-amount return true
else penalty25.0 return false

Not a good programming practice!
8
Constants
public class ToyBankAccount public
static final int PENALTY_FOR_CHECK_OVER_LIMIT25
............ / returns true if
the check clears. / public boolean
payCheck(double amount) if
(checkingBalancegtamount)
checkingBalance-amount return true
else penalty PENALTY_FOR_CHECK_OVER_L
IMIT return false
9
Parameters
public boolean withdrawMoney(int pin,double
amount) if (pinthis.pin) if
(checkingBalancegtamount)
checkingBalance-amount return true
return false
formal parameters
actual parameters
withdrawalOKjohnsAccount.withdrawMoney(7534,500)
object
method
10
Return type
public boolean withdrawMoney(int pin,double
amount) if (pinthis.pin) if
(checkingBalancegtamount)
checkingBalance-amount return true
return false
withdrawalOKjohnsAccount.withdrawMoney(7534,500)
11
Return type - void
public void depositMoney(double amount)
checkingBalanceamount
johnsAccount.depositMoney(500)
12
Creating objects - new
ToyBankAccount johnsAccount johnsAccountnew
ToyBankAccount()
johnsAccount is a variable which can point to an
object of type ToyBankAccount
create a new object of type ToyBankAccount and
let johnsAccount point to it
13
Constructor
ToyBankAccount johnsAccount johnsAccountnew
ToyBankAccount()
Call to a special method of the class
constructor.
default constructor is
public ToyBankAccount()
14
Constructor
public ToyBankAccount(String ownersName,
double
checkingInitialAmount,
int pin) checkingAmountcheckin
gInitialAmount savingsAcountpenalty0
this.pinpin this.ownersNamenew
String(ownersName)
ToyBankAccount johnsAccount johnsAccountnew
ToyBankAccount(John Smith,0,1234)
15
Constructor - overloading
public ToyBankAccount(String ownersName, int pin)
savingsAcountpenaltycheckingInitialAmount0
this.pinpin this.ownersNamenew
String(ownersName)
public ToyBankAccount(String ownersName,
int checkingInitialAmount
, int pin)
checkingAmountcheckingInitialAmount
savingsAcountpenalty0 this.pinpin
this.ownersNamenew String(ownersName)
16
Constructor - this()
public ToyBankAccount(String ownersName, int pin)
this(ownersName,0,pin)
(must be first statement)
public ToyBankAccount(String ownersName,
int checkingInitialAmount
, int pin)
checkingAmountcheckingInitialAmount
savingsAcountpenalty0 this.pinpin
this.ownersNamenew String(ownersName)
17
public/private for instance variables
public anybody can access private only
methods of the class can access
public class ToyBankAccount public String
ownersName public double savingsBalance,checkin
gBalance public double penalty public int
pin ...
18
public/private for instance variables
ToyBankAccount johnsAccount johnsAccountnew
ToyBankAccount(John Smith,100,1234) johnsAccou
nt.savingsAccount50
Not good. Suppose that we want to modify the code
to store all transactions.
Have to find all occurences of
19
public/private for instance variables
public anybody can access private only
methods of the object can access
public class ToyBankAccount private String
ownersName private double savingsBalance,checki
ngBalance private double penalty private
int pin ...
Instance variables should be private
20
Accessor/Mutator methods
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.titletitle

21
public for classes
in each file
one public class with the same name as the
filename
any number of classes which are visible only to
classes in the file
22
Variables
int x
of primitive type of class type
ToyBankAccount johnsAccount johnsAccountnew
ToyBankAccount(John Smith,0,1234)
ToyBankAccount marysAccountjohnsAccount marysAcc
ount.depositMoney(1000) System.out.println(joh
nsAccount.balance())
23
Variables
ToyBankAccount johnsAccount johnsAccountnew
ToyBankAccount(John Smith,0,1234) ToyBankAccoun
t marysAccountjohnsAccount marysAccount.depositM
oney(1000) System.out.println(johnsAccount.bal
ance())
marysAccount
johnsAccount
int x x10 int yx y10
24
Privacy leaks
private Address ownersAddress public Address
getOwnersAddress() return ownersAddress

addressjohnsAccount.getOwnersAddress() address.
set(John Evil,....)
25
Privacy leaks
private Address ownersAddress public Address
getOwnersAddress() return new
Address(ownersAddress)
addressjohnsAccount.getOwnersAddress() address.
set(John Evil,....)
26
null
ToyBankAccount johnsAccountnull
does not point to any object...
27
null Pointer Exception error
johnsAccount.depositMoney(100)
28
, and equals
for variables of class type tests whether they
point to the same object.
String sabc, tabc if (st)
system.out.println(You win 1,000,000)
29
, and equals
for variables of class type tests whether they
point to the same object.
String sabc, tabc 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.
30
, and equals
boolean equals(Book s) return
author.equals(s.author)
title.equals(s.title) ...
You have to implement equals method if you want
deeper equality testing for your class.
31
Method overloading
public void depositMoney(double amount,Currency
currency) checkingBalanceCurrency.Convert(am
ount,currency,Currency.USD)
public void depositMoney(double amount)
checkingBalanceamount
JohnsAccount.depositMoney(100,Currency.SKK) Johns
Account.depositMoney(100)
32
Method overloading
public void depositMoney(double amount,Currency
currency) checkingBalanceCurrency.Convert(am
ount,currency,Currency.USD)
public void depositMoney(double amount)
checkingBalanceamount
public void depositMoney(double amountInSkk)
checkingBalanceamount50
33
Method overloading
public void depositMoney(double amount,Currency
currency) checkingBalanceCurrency.Convert(am
ount,currency,Currency.USD)
public void depositMoney(double amount)
checkingBalanceamount
public char depositMoney(double amountInSkk)
checkingBalanceamount50
cannot overload based on the returned type
34
Automatic conversion
public void depositMoney(double amount)
checkingBalanceamount
johnsAccount.depositMoney(500)
int
35
Class variables
public class ToyBankAccount private static
int totalAccounts0 private static
totalAccountBalance private String
ownersName private double savingsBalance,checki
ngBalance private double penalty private
int pin ...
36
Class variables
initializer block
public class ToyBankAccount private static
int totalAccounts0totalAccounts private
static totalAccountBalance private String
ownersName private double savingsBalance,checki
ngBalance private double penalty private
int pin ...
37
Class variables
public class ToyBankAccount private static
final int PENALTY_FOR_CHECK_OVER_LIMIT25
private static int totalAccounts0 private
static totalAccountBalance private String
ownersName private double savingsBalance,checki
ngBalance private double penalty private
int pin ...
38
Class methods
public class ToyBankAccount private static
final int PENALTY_FOR_CHECK_OVER_LIMIT25
private static int totalAccounts0 private
static double totalAccountBalance .....
public static AverageBalance() return
totalAccountBalance/totalAccounts
39
Class methods
public static AverageBalance()
public endOfMonthUpdate() if
(balancegtAverageBalance()) balanceBONUS
class methods cannot use instance fields or
methods
40
Class methods
public static AverageBalance()
class Bank public monthlyReport()
system.out.print(Account.AverageBalance())
41
Class methods example - Math
Math.sqrt()
can use to group methods which have something in
common
42
Instance vs Class methods
public boolean bigger(Account a) return
(balancegta.balance)
(a.bigger(b))
public static boolean bigger(Account a,Account b)
return (a.balancegtb.balance)
(Account.bigger(a,b))
43
main
public static void main(String args)
can be defined for any class, not just for the
one that is running (good for debugging)
44
Garbage collection
ToyBankAccount johnsAccount johnsAccountnew
ToyBankAccount(John Smith,0,1234)
johnsAccountnull
45
EXERCISE 1
public class ToyBankAccount private String
ownersName private double savingsBalance,checki
ngBalance private double penalty private
int pin ...
1a) implement method transferSavingToChecking
1b) implement method transferToAccount
1c) implement method transferBetweenAccounts
(takes two accounts and amount)
46
Default initialization
instance variables are initialized to the
default value (zero for numerical classes, false
for boolean)
(this is not true for local variables (i.e.
variables declared in methods))
47
EXERCISE 2
public class Runs public main(String args)
Test r,p rnew Test(2,3)
r.Values() pr pnew Test(3,4)
r.Values()
Class Test private int x,y public Test(int
x,int y) xx this.yy
System.out. println(x,,y) public
Values() System.out. println(x,y)

What is the output?
48
EXERCISE 3
public class Runs public main(String)
int x14,y7,z5,i for (i0ilt300i)
x xyz y x-y-z z
x-y-z x x-y-z
System.out(x,y,z)
What is the output?
49
EXERCISE 4
What is the output?
public class MyClass private int data
public boolean equals(MyClass a) return
(thisa) public MyClass(int x) datax
MyClass a,b anew MyClass(3)
ba System.out.println(a.equals(b)) bnew
MyClass(3) System.out.println(a.equals(b))
50
EXERCISE 5
What is the output?
public class MyClass private int data
public boolean equals(MyClass a) return
(this.dataa.data) public MyClass(int
data) datadata MyClass a,b anew
MyClass(3) ba System.out.println(a.equals(b))
bnew MyClass(4) System.out.println(a.equals(b))
51
EXERCISE 6
What is the output?
public class MyClass private String name
public boolean equals(MyClass a) return
(this.namea.name) public MyClass(String
name) this.namename MyClass a,b anew
MyClass(John) ba System.out.println(a.equals(
b)) bnew MyClass(John) System.out.println(a.e
quals(b))
52
EXERCISE 7
What is the output?
public class myInt public int x myInt a
new myInt() a.x 10 myInt b a
System.out.println("a "a.x" b "b.x)
b.x 20 System.out.println("a "a.x" b
"b.x)
53
EXERCISE 8
public class myInt public int x public
static void switch(myInt a,myInt b) int c
ca.x a.xb.x b.xc public static void
switch(int a,int b) int c ca ab bc
myInt a new myInt() a.x 10 myInt b
new myInt() b.x 20 myInt.switch(a.x,b.x) Sy
stem.out.println("a "a.x" b "b.x)
myInt.switch(a.b) System.out.println("a
"a.x" b "b.x)
What is the output?
54
EXERCISE 9
public class myString public String x
public static void switch(myInt a,myInt b)
String c ca.x a.xb.x b.xc public
static void switch(String a,String b)
String c ca ab bc myString a new
myString() a.x 10 myString b new
myString() b.x 20 myString.switch(a.x,b.x)
System.out.println("a "a.x" b "b.x)
myString.switch(a.b) System.out.println("a
"a.x" b "b.x)
What is the output?
55
EXERCISE 10
What is the output?
class Test public int x public static int
y public Test(int x,int y) this.xx
this.yy Test a,b anew
Test(3,4) System.out.println(""a.x","a.y)
bnew Test(5,6) System.out.println(""b.
x","b.y) System.out.println(""a.x","a.y)

56
EXERCISE 11
What is the output?
public class Test static Test firstnull
Test next public int x public Test(int x)
this.xx nextfirst firstthis public
static void list() for (Test
pfirstp!nullpp.next)
System.out.println(" "p.x) public static
void main(String args) Test anew
Test(2),bnew Test(3),cnew Test(4)
Test.list()
57
EXERCISE 12
Where is the error?
public class Test Test next public int
x public void print() int y
while (ylt10) System.out.println(xy)
y public static void
main(String args) Test anew Test()
a.print()
58
EXERCISE 13
Where are the errors
public class Test Test next public int
x public Test(int x) Test.xx
public static void main(String args)
double y1 Test anew Test(y)
Write a Comment
User Comments (0)
About PowerShow.com