Download presentation
Presentation is loading. Please wait.
Published byAron Clark Modified over 8 years ago
1
1 The copy constructor in the BankAccounts class. Two alternatives here: /** copy constructor */ public BankAccounts(BankAccounts L){ theAccounts = L.theAccounts.clone(); size=L.size; } /** alternative copy constructor */ public BankAccounts(BankAccounts L){ theAccounts = L.theAccounts; size = L.size; } or Will examine their effects using TestBankAccounts programme: ADS2 Lecture 2 (a) (b) Handout for Lecture 2. SLIDE 10
2
2 public class TestBankAccounts { public static void main(String[] args) { // Test BankAccount Class BankAccount bankAccount1=new BankAccount("1456","Michael Smith",300); BankAccount bankAccount2=new BankAccount("1457","Jane Edwards",1000); BankAccount bankAccount3=new BankAccount("1458","Sam Forge",2000); BankAccounts myAccounts1 = new BankAccounts(3); myAccounts1.setAccount(0,bankAccount1); myAccounts1.setAccount(1,bankAccount2); myAccounts1.setAccount(2,bankAccount3); BankAccounts myAccounts2=new BankAccounts(myAccounts1); myAccounts1.withdraw(0,250.0); myAccounts1.withdraw(1,250.0); myAccounts1.withdraw(2,250.0); * (ignore asterisks for now) ADS2 Lecture 2 SLIDE 11
3
3 System.out.println("The final bank accounts are: "); System.out.println("myAccounts1: "); System.out.println(myAccounts1); System.out.println("myAccounts2: "); System.out.println(myAccounts2); } Exercise: What is the output if we use the first copy constructor on slide 10? What is the output if we use the second copy constructor on slide 10? ** ADS2 Lecture 2 SLIDE 12
5
5 As a data structure, ArrayList is a generic storage device think of as an extendible array implemented by arrays Will see later arraylist ADT – don’t assume implemented using array. ArrayList is class in standard Java – need import java.util.ArrayList ; For an array, size is fixed at declaration: theAccounts = new BankAccount[3]; Initialises an array of size 3 of BankAccounts objects For an ArrayList specify type of entry and initial size at declaration: ArrayList list = new ArrayList (20); If capacity exceeds 20, new array is built. Note base type must be reference type (not primitive type). If you want to use a primitive type, must use wrapper class (e.g. Integer for int). ADS2 Lecture 2 SLIDE 15
6
6 Some ArrayList methods /** Create ArrayList of specified Base-Type and initialCapacity */ public ArrayList (int initialCapacity) /** Create ArrayList of specified Base-Type and init capacity 10 */ public ArrayList () /** Return element at specified index */ public Base-Type get(int index) /** Set the element at specified index to newElement and return * the value previously at that position; (often used as if void)*/ public Base-Type set(int index, Base-Type newElement) /** Add newElement at position index, moves elements to make room, * and increments size;increases capacity if necessary */ public void add(int index, Base-Type newElement) /** Add newElement at position size and increments size; * (increases capacity if necessary) */ public void add(Base-Type newElement) From JP2 ADS2 Lecture 2 SLIDE 16
7
7 /** Remove and return element at specified index; move elements * down to close the gap */ public Base-Type remove(int index) /** return the number of elements in the ArrayList*/ public int size() /** return true if the ArrayList is empty, and false otherwise*/ public boolean isEmpty() /** make the ArrayList empty */ public void clear() /** trim the capacity of the ArrayList to its current size */ public void trimToSize() /** Return the index of the first occurrence of o in the ArrayList, or -1 if this list does not contain the element. */ public int indexOf(Object o) /** Remove the first occurrence of o from the ArrayList, if it is present; returns true if it was removed, false otherwise */ public boolean remove(Object o) ADS2 Lecture 2 SLIDE 17
8
8 Example for you: replace code in Bank Accounts example between * and ** with code in which: ArrayList myAccounts of size 3 is initialised to take data of type BankAccount bankAccount1.. bankAccount3 are added to the list Set element at index 0 to bankAccount4 = (“1455”,”Edwin Moore”,600) Remove entry with index 2 Contents of myAccounts printed to standard output. What is output? ADS2 Lecture 2 SLIDE 19
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.