Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "ACM/JETT Workshop - August 4-5, 2005 1 06: Defining Classes in Java."— Presentation transcript:

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

2 ACM/JETT Workshop - August 4-5, 2005 2 Topics In this Lecture, we will examine the details of a Java class definition: –Constructors –Access methods –Using Containment

3 ACM/JETT Workshop - August 4-5, 2005 3 What to expect After 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.

4 ACM/JETT Workshop - August 4-5, 2005 4 What are constructors Constructors 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; }

5 ACM/JETT Workshop - August 4-5, 2005 5 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); }

6 ACM/JETT Workshop - August 4-5, 2005 6 What is “this”? this” is a Java keyword that refers to the object being manipulated by a constructor or instance method.

7 ACM/JETT Workshop - August 4-5, 2005 7 When is a constructor invoked? public static void main(String [] args){ BankAccount a1 = new BankAccount (); BankAccount a2 = new BankAccount (“A12”,200.00) } Show class BankAccount Example1_6 Show class BankAccount Example1_6

8 ACM/JETT Workshop - August 4-5, 2005 8 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?

9 ACM/JETT Workshop - August 4-5, 2005 9 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 BankAccount With set and get methods Example2_6 Show class BankAccount With set and get methods Example2_6

10 ACM/JETT Workshop - August 4-5, 2005 10 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 BankAccount With deposit () and withdraw()

11 ACM/JETT Workshop - August 4-5, 2005 11 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);

12 ACM/JETT Workshop - August 4-5, 2005 12 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.

13 ACM/JETT Workshop - August 4-5, 2005 13 Class Bank We will add the following data members to class Bank. branchName and city (strings) accounts, an array to hold a number of BankAccounts.

14 ACM/JETT Workshop - August 4-5, 2005 14 Class Bank We 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

15 ACM/JETT Workshop - August 4-5, 2005 15 Add more meaning to BankAccount Now, we can show this relationship between a Bank and a BankAccount using a graphical notation as below:

16 ACM/JETT Workshop - August 4-5, 2005 16 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

17 ACM/JETT Workshop - August 4-5, 2005 17 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.


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

Similar presentations


Ads by Google