ACM/JETT Workshop - August 4-5, 2005 1 06: Defining Classes in Java.

17
1 ACM/JETT Workshop - August 4-5, 2005 06: Defining Classes in Java

Transcript of ACM/JETT Workshop - August 4-5, 2005 1 06: Defining Classes in Java.

Page 1: ACM/JETT Workshop - August 4-5, 2005 1 06: Defining Classes in Java.

1 ACM/JETT Workshop - August 4-5, 2005

06: Defining Classes in Java

Page 2: ACM/JETT Workshop - August 4-5, 2005 1 06: Defining Classes in Java.

2 ACM/JETT Workshop - August 4-5, 2005

TopicsIn this Lecture, we will examine the

details of a Java class definition:

–Constructors

–Access methods

–Using Containment

Page 3: ACM/JETT Workshop - August 4-5, 2005 1 06: Defining Classes in Java.

3 ACM/JETT Workshop - August 4-5, 2005

What to expectAfter this lecture, you should be able to use

the given code in BankAccount.java file and do the following:

–Add a new method to class BankAccount.

–Add an object instance as a data member into class BankAccount.

–Use the methods defined in BankAccount

–Compile and test your code.

Page 4: ACM/JETT Workshop - August 4-5, 2005 1 06: Defining Classes in Java.

4 ACM/JETT Workshop - August 4-5, 2005

What are constructorsConstructors are methods that initialize the

instance variables of an object.

• Constructors have the same name as the class name and they have no return type.

public BankAccount(String acctId, double balance){ // initialise instance variables

this.acctId = acctId;

this.balance = balance;}

Page 5: ACM/JETT Workshop - August 4-5, 2005 1 06: Defining Classes in Java.

5 ACM/JETT Workshop - August 4-5, 2005

What are constructors• A class can have multiple constructors

(method overloading). • A default constructor is a parameter-less

constructor.public BankAccount(){

this.acctId = “Not set”; this.balance = 0.0;}

public BankAccount(){// Call the two parameter constructor// that is defined this (“Not set”,0.0);

}

Page 6: ACM/JETT Workshop - August 4-5, 2005 1 06: Defining Classes in Java.

6 ACM/JETT Workshop - August 4-5, 2005

What is “this”?

• this” is a Java keyword that refers to the object being manipulated by a constructor or instance method.

Page 7: ACM/JETT Workshop - August 4-5, 2005 1 06: Defining Classes in Java.

7 ACM/JETT Workshop - August 4-5, 2005

When is a constructor invoked?

public static void main(String [] args){

BankAccount a1 = new BankAccount ();

BankAccount a2 = new BankAccount (“A12”,200.00) }

Show class BankAccountExample1_6

Show class BankAccountExample1_6

Page 8: ACM/JETT Workshop - August 4-5, 2005 1 06: Defining Classes in Java.

8 ACM/JETT Workshop - August 4-5, 2005

More methods We create an instance of BankAccount and store the

reference in a2 as below:

BankAccount a2 = new BankAccount (“A12”,200.00);

What if we want to change the acct id to “A13” or

What if we want to change the balance to “100”?

a2.acctId = “A13”

a2.balance = 100.00;

Will this work?

Page 9: ACM/JETT Workshop - August 4-5, 2005 1 06: Defining Classes in Java.

9 ACM/JETT Workshop - August 4-5, 2005

More methods We need to add some public access methods to set

and get the values of private data members.

Once the set and get methods are available in the class,

we can change the balance using the set method

a2.setBalance(100.00);

Show class BankAccountWith set and get methods

Example2_6

Show class BankAccountWith set and get methods

Example2_6

Page 10: ACM/JETT Workshop - August 4-5, 2005 1 06: Defining Classes in Java.

10 ACM/JETT Workshop - August 4-5, 2005

More methods Let us complete the definitions for the methods,

deposit () and withdraw() in BankAccount.

The deposit method should add an amount to the balance of a BankAccount object. The amount is passed in as a parameter.

The withdraw method should return the requested amount, if the balance >= requested amount;

the amount then should be subtracted from the balance.

Show class BankAccount

With deposit () and withdraw()

Show class BankAccountWith deposit () and withdraw()

Page 11: ACM/JETT Workshop - August 4-5, 2005 1 06: Defining Classes in Java.

11 ACM/JETT Workshop - August 4-5, 2005

Using an instance of BankAccount• Let us now see how we can create an

instance of BankAccount, deposit and withdraw money from it.

BankAccount ba1 = new BankAccount("A123",250.00);

ba1.deposit (100.00);

double amt = ba1.withdraw(50.00);

BankAccount ba2 = new BankAccount(“B222",amt);

Page 12: ACM/JETT Workshop - August 4-5, 2005 1 06: Defining Classes in Java.

12 ACM/JETT Workshop - August 4-5, 2005

Add more meaning to class BankAccount

Bank Accounts generally belong to specific banks.

How do we add this meaning to our class BankAccount?

Firstly, we need a class called Bank.

A Bank has a branch name and city.

In addition, a Bank maintains a list of BankAccounts.

Page 13: ACM/JETT Workshop - August 4-5, 2005 1 06: Defining Classes in Java.

13 ACM/JETT Workshop - August 4-5, 2005

Class BankWe will add the following data members to

class Bank.

• branchName and city (strings)

• accounts, an array to hold a number of BankAccounts.

Page 14: ACM/JETT Workshop - August 4-5, 2005 1 06: Defining Classes in Java.

14 ACM/JETT Workshop - August 4-5, 2005

Class BankWe will add a constructor to initialize the new

data member, parentBank.

How do we add a bank account to the list of accounts in Bank?

We will define a method called

add (BankAccount) in the class Bank.

Show class Bank Show class Bank

Page 15: ACM/JETT Workshop - August 4-5, 2005 1 06: Defining Classes in Java.

15 ACM/JETT Workshop - August 4-5, 2005

Add more meaning to BankAccountNow, we can show this relationship between

a Bank and a BankAccount using a graphical notation as below:

Page 16: ACM/JETT Workshop - August 4-5, 2005 1 06: Defining Classes in Java.

16 ACM/JETT Workshop - August 4-5, 2005

Connect Bank and BankAccount• Let us add

– a new data member, parentBank (of type Bank) to class BankAccount.

– Then we add one more constructor to initialize the new data member.

Show class BankAccount Show class BankAccount

Page 17: ACM/JETT Workshop - August 4-5, 2005 1 06: Defining Classes in Java.

17 ACM/JETT Workshop - August 4-5, 2005

The topics that you should be comfortable with at this time

Defining multiple constructors for a class.

Composing objects with other instances.

You can test your understanding of some of these topics in Lab 2.