Presentation is loading. Please wait.

Presentation is loading. Please wait.

Implementing Classes Yonglei Tao.

Similar presentations


Presentation on theme: "Implementing Classes Yonglei Tao."— Presentation transcript:

1 Implementing Classes Yonglei Tao

2 Public Interface of Class Counter
/** This class models a tally counter. */ public class Counter { … public Counter() { … } public int getValue() { … } public void count() { … } }

3 Using a Class Counter tally = new Counter(); tally.count(); int result = tally.getValue();

4 Instance Variables Instance variables should be declared as private
public class Counter { private int value; } Instance variables should be declared as private

5 Instance Variables (cont.)

6 Constructors /** This class models a tally counter. */
public class Counter { private int value; Initialize a newly created Counter object. A default constructor. public Counter() { value = 0; }

7 Overloaded Constructors
public Counter(int init) { value = init; } public Counter(Counter other) { value = other.value; … Counter counter1 = new Counter(); Counter counter2 = new Counter(5); Counter counter3 = new Counter(counter2); Counter counter4 = new Counter(new Counter(10)); Create a copy of counter2

8 Methods of a Class /** This class models a tally counter. */ public class Counter { private int value; Gets the current value of this the current value public int getValue() { return value; }

9 Methods of a Class (cont.)
/** Advances the value of this counter by 1. */ public void count() { value = value + 1; }

10 Discussion Private instance variables can only be accessed by methods of the same class How to allow the client to reset a counter back to zero? public void reset() How to make Counter do countdown also? public void countdown()

11 Object-Oriented Programming
We need to represent a bank account in an application under development Its expected behaviors include deposit money withdraw money get balance

12 Specify the Public Interface
Methods of class Account deposit withdraw getBalance In order to support method calls such as harrysChecking.deposit(2000); harrysChecking.withdraw(500); System.out.println(harrysChecking.getBalance());

13 Method Declaration access specifier return type method name
public void deposit (double amount) { } public void withdraw (double amount) { } public double getBalance () { } access specifier return type method name list of parameters method body

14 Constructor Declaration
public BankAccount() { // body } public BankAccount (double initBalance) {

15 Public Interface for Class Account
public class BankAccount { // private variables public BankAccount() { // body } public BankAccount (double initialBalance) {

16 Public Interface for Account (cont.)
public void deposit(double amount) { // body } public void withdraw(double amount) { public double getBalance() {

17 Commenting the Public Interface
/** A bank account has a balance that can be changed by deposits and withdrawals. */ public class BankAccount { Constructs a bank account with a zero balance. public BankAccount() { … } Constructs a bank account with a given balance. @param initialBalance the initial balance public BankAccount(double initialBalance) { … }

18 Commenting the Public Interface (cont.)
/** Withdraws money from the bank account. @param amount the amount to withdraw */ public void withdraw(double amount) { //implementation filled in later } /** Gets the current balance of the bank account. @return the current balance */ public double getBalance() { //implementation filled in later }

19 Declaring Instance Variable
public class BankAccount { private double balance; }

20 Implementing Constructors
public BankAccount () { balance = 0; } public BankAccount (double initialBalance) { balance = initialBalance; BankAccount harrysChecking = new BankAccount(1000);

21 Implementing Methods public void deposit (double amount) {
balance = balance + amount; } harrysChecking.deposit (500);

22 BankAccount.java 1 /** 2 A bank account has a balance that can be changed by 3 deposits and withdrawals. 4 */ 5 public class BankAccount 6 { 7 private double balance; 8 9 /** Constructs a bank account with a zero balance. */ public BankAccount() { balance = 0; } 16 /** Constructs a bank account with a given balance. @param initialBalance the initial balance */ public BankAccount(double initialBalance) { balance = initialBalance; }

23 BankAccount.java (cont.)
/** Deposits money into the bank account. @param amount the amount to deposit */ public void deposit(double amount) { balance = balance + amount; } /** Withdraws money from the bank account. @param amount the amount to withdraw */ public void withdraw(double amount) { balance = balance - amount; } /** Gets the current balance of the bank account. @return the current balance */ public double getBalance() { return balance; } 52 }

24 Discussion How can you use the methods of the public interface to empty the tomsChecking bank account? tomsChecking.withdraw( tomsChecking.getBalance() ) Suppose you also want the ability to keep track of an account number in addition to the balance. What would you change the public interface to accommodate this enhancement? Add an accountNumber parameter to the constructors Add a getAccountNumber method No need for a setAccountNumber method – the account number never changes after construction Add an instance variable to store the number

25 Local Variables public void withdraw (double amount) {
double newBalance = balance - amount; if ( newBalance <= 0 ) return; balance = newBalance; }

26 Scope of Local Variables
public class RectangleTester { public static double area(Rectangle rect) { double r = rect.getWidth() * rect.getHeight(); return r; } public static void main(String[] args) { Rectangle r = new Rectangle(5, 10, 20, 30); double a = area(r); System.out.println(r); } }

27 Scope of Local Variables
if (x >= 0) { double r = Math.sqrt(x); } // Scope of r ends here else { Rectangle r = new Rectangle(5, 10, 20, 30); // OK - it is legal to declare another r here }

28 Overlapping Scope Which one to refer to if using value in this method?
public class Coin { public double getExchangeValue(double exchangeRate) { double value; return value; } private String name; private double value; } Which one to refer to if using value in this method? How to refer to instance variable value in this method?

29 Built-in Reference this
How does Java ensure that the proper object is referenced? public void deposit(double amount) { balance = balance + amount; } TomsSavings.deposit(500) MarksSavings.deposit(500) Keyword this refers to the current object, that is, the one on which the method is invoked In method deposit(), balance is an instance variable but which object it belongs to? How does Java know since there is only one copy of the code for method amount.

30 Built-in Reference this (cont.)
An instance variable in a method belongs to the object the this reference refers to automatically In other words balance = balance + amount; actually means this.balance = this.balance + amount; The same is true for a method call such as public class BankAccount { . . . public void monthlyFee() { withdraw(10); // withdraw $10 from this account }

31 Built-in Reference this (cont.)
public class MyClass { private int id; private String name; public MyClass (int id, String name) { this.id = id; this.name = name; }

32 Local Variables vs. Instance Variables
What are their differences? Lifetime Scope Initialization


Download ppt "Implementing Classes Yonglei Tao."

Similar presentations


Ads by Google