Transcript balance
1
Chapter 3: An Introduction to Classes
Chapter 3
An Introduction to Classes
©2000, John Wiley & Sons, Inc.
Horstmann/Java Essentials, 2/e
2
Chapter 3: An Introduction to Classes
Object behavior
• Bank account operations
– deposit money
– withdraw money
– get the current balance
• Methods
– deposit
– withdraw
– getBalance
©2000, John Wiley & Sons, Inc.
Horstmann/Java Essentials, 2/e
3
Chapter 3: An Introduction to Classes
Applying methods
• Transfer balance
double amt = 500;
momsSavings.withdraw(amt);
harrysChecking.deposit(amt);
• Add interest
double rate = 5; // 5%
double amt = acct.getBalance()
* rate / 100;
acct.deposit(amt);
©2000, John Wiley & Sons, Inc.
Horstmann/Java Essentials, 2/e
4
Chapter 3: An Introduction to Classes
Constructing objects
• Construct an object
new BankAccount()
• Save object in object variable
BankAccount account
= new BankAccount();
• Apply methods
account.deposit(1000);
©2000, John Wiley & Sons, Inc.
Horstmann/Java Essentials, 2/e
5
Chapter 3: An Introduction to Classes
Figure 1
Creating a New
Object
©2000, John Wiley & Sons, Inc.
Horstmann/Java Essentials, 2/e
6
Chapter 3: An Introduction to Classes
Figure 2
Initializing an Object Variable
©2000, John Wiley & Sons, Inc.
Horstmann/Java Essentials, 2/e
7
Chapter 3: An Introduction to Classes
Class definition
•
class BankAccount
{ public void deposit(double amount)
{ method implementation
}
public void withdraw(double amount)
{ method implementation
}
public double getBalance()
{ method implementation
}
data
}
©2000, John Wiley & Sons, Inc.
Horstmann/Java Essentials, 2/e
8
Chapter 3: An Introduction to Classes
Method signatures
•
•
•
•
access specifier (such as public)
return type (such as double or void)
method name (such as deposit)
list of parameters (such as double
amount)
©2000, John Wiley & Sons, Inc.
Horstmann/Java Essentials, 2/e
9
Chapter 3: An Introduction to Classes
Instance Variables
• public class BankAccount
{ ...
private double balance;
}
• access specifier (such as private)
• type of variable (such as double)
• name of variable (such as balance)
©2000, John Wiley & Sons, Inc.
Horstmann/Java Essentials, 2/e
10
Chapter 3: An Introduction to Classes
Figure 3
Instance Variables
©2000, John Wiley & Sons, Inc.
Horstmann/Java Essentials, 2/e
11
Chapter 3: An Introduction to Classes
Private data
• You can't access private data:
harrysChecking.balance = 1000;
// ERROR
• Use the public interface for all access:
harrysChecking.deposit(1000);
• Hiding implementation = encapsulation
• Safe
• Makes it easy to change implementation
©2000, John Wiley & Sons, Inc.
Horstmann/Java Essentials, 2/e
12
Chapter 3: An Introduction to Classes
Implementing methods
•
public class BankAccount
{ public void deposit(double amount)
{ balance = balance + amount;
}
public void withdraw(double amount)
{ balance = balance - amount;
}
public double getBalance()
{ return balance;
}
private double balance;
}
©2000, John Wiley & Sons, Inc.
Horstmann/Java Essentials, 2/e
13
Chapter 3: An Introduction to Classes
Implicit parameter
•
public void withdraw(double amount)
{ balance
= balance - amount;
}
•
balance is the balance of the object to the left of the dot:
momsSavings.withdraw(500)
means
momsSavings.balance
= momsSavings.balance - amount;
©2000, John Wiley & Sons, Inc.
Horstmann/Java Essentials, 2/e
14
Chapter 3: An Introduction to Classes
Constructors
• A constructor initializes the instance
variables
• Constructor name = class name
• public class BankAccount
{ public BankAccount()
{ balance = 0;
}
. . .
©2000, John Wiley & Sons, Inc.
}
Horstmann/Java Essentials, 2/e
15
Chapter 3: An Introduction to Classes
Multiple constructors
• public class BankAccount
{ public BankAccount(double
initialBal)
{ balance = initialBal;
}
. . .
}
• new BankAccount(5000)
©2000, John Wiley & Sons, Inc.
Horstmann/Java Essentials, 2/e
16
Chapter 3: An Introduction to Classes
Tips
• Missing quality tip:
– Make all data private
– Make most methods public
• Productivity hint: Keyboard shortcuts
– Ctrl+C, Ctrl+V, Ctrl+X
– Alt+Tab, Ctrl+Esc
– Alt+letter accesses menu
©2000, John Wiley & Sons, Inc.
Horstmann/Java Essentials, 2/e
17
Chapter 3: An Introduction to Classes
Driver Program
• BankAccount class implements bank
account.
• Need a separate class to do something with
bank accounts
• public class BankAccountTest
{
public static void main
(String[] args)
{ do something with bank accounts
}
}
©2000, John Wiley & Sons, Inc.
Horstmann/Java Essentials, 2/e
18
Chapter 3: An Introduction to Classes
Class BankAccount.java
public class BankAccount
{ public BankAccount()
{ balance = 0;
}
public BankAccount(double initialBalance)
{
balance = initialBalance;
}
public void deposit(double amount)
{
balance = balance + amount;
}
public void withdraw(double amount)
{
balance = balance - amount;
}
©2000, John Wiley & Sons, Inc.
Horstmann/Java Essentials, 2/e
19
Chapter 3: An Introduction to Classes
public double getBalance()
{
return balance;
}
private double balance;
}
©2000, John Wiley & Sons, Inc.
Horstmann/Java Essentials, 2/e
20
Chapter 3: An Introduction to Classes
Program BankAccountTest.java
public class BankAccountTest
{ public static void main(String[] args)
{ BankAccount account = new BankAccount(10000);
final double INTEREST_RATE = 5;
double interest;
// compute and add interest for one period
©2000, John Wiley & Sons, Inc.
Horstmann/Java Essentials, 2/e
21
Chapter 3: An Introduction to Classes
interest = account.getBalance() * INTEREST_RATE / 100;
account.deposit(interest);
System.out.println("Balance after year 1 is $"
+ account.getBalance());
// add interest again
interest = account.getBalance() * INTEREST_RATE / 100;
account.deposit(interest);
System.out.println("Balance after year 2 is $"
+ account.getBalance());
}
}
©2000, John Wiley & Sons, Inc.
Horstmann/Java Essentials, 2/e
22
Chapter 3: An Introduction to Classes
Discovering Classes
• Nouns: candidates for classes
• Verbs: candidates for methods
• Example: Program asks a user to add coins
to a purse. Then get total amount of money
in the purse
• Classes: Coin, Purse
• Methods: addCoins, getTotal
©2000, John Wiley & Sons, Inc.
Horstmann/Java Essentials, 2/e
23
Chapter 3: An Introduction to Classes
Copying numbers
• double balance1 = 1000;
double balance2 = balance1;
balance2 = balance2 + 500;
• Change in balance2 does not affect
balance1
©2000, John Wiley & Sons, Inc.
Horstmann/Java Essentials, 2/e
24
Chapter 3: An Introduction to Classes
Figure 5
Copying Numbers
©2000, John Wiley & Sons, Inc.
Horstmann/Java Essentials, 2/e
25
Chapter 3: An Introduction to Classes
Copying Object References
• BankAccount account1
= new BankAccount(1000);
BankAccount account2
= account1;
account2.deposit(500);
• Change through account2 is also visible
through account1
• Object variables hold references, not objects
©2000, John Wiley & Sons, Inc.
Horstmann/Java Essentials, 2/e
26
Chapter 3: An Introduction to Classes
Copying Object References
©2000, John Wiley & Sons, Inc.
Horstmann/Java Essentials, 2/e
27
Chapter 3: An Introduction to Classes
Null Reference
•
•
•
•
•
account1 = null;
Now account1 refers to no account
Can't call methods on null
null is not the same as 0
Common error: A null string is not the
same as the empty string ""
©2000, John Wiley & Sons, Inc.
Horstmann/Java Essentials, 2/e
28
Chapter 3: An Introduction to Classes
Figure 7
A null Reference
©2000, John Wiley & Sons, Inc.
Horstmann/Java Essentials, 2/e
29
Chapter 3: An Introduction to Classes
Figure 8
String References
©2000, John Wiley & Sons, Inc.
Horstmann/Java Essentials, 2/e