/
1 The copy constructor in the 1 The copy constructor in the

1 The copy constructor in the - PowerPoint Presentation

olivia-moreira
olivia-moreira . @olivia-moreira
Follow
397 views
Uploaded On 2016-11-06

1 The copy constructor in the - PPT Presentation

BankAccounts class Two alternatives here copy constructor public BankAccountsBankAccounts L theAccounts L theAccounts clone size L size alternative copy constructor ID: 485465

public arraylist size type arraylist public type size index base bankaccounts myaccounts1 slide bankaccount lecture int ads2 return newelement

Share:

Link:

Embed:

Download Presentation from below link

Download Presentation The PPT/PDF document "1 The copy constructor in the" is the property of its rightful owner. Permission is granted to download and print the materials on this web site for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.


Presentation Transcript

Slide1

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 10Slide2

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 11Slide3

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 12Slide4
Slide5

5

As a data structure, ArrayList is a generic storage device

think of as an extendible arrayimplemented by arraysWill 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<String> list = new ArrayList<String>(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 15Slide6

6

Some ArrayList methods

/** Create ArrayList of specified Base-Type and initialCapacity */public ArrayList<Base-Type>(int initialCapacity)/** Create ArrayList of specified Base-Type and init capacity 10 */public ArrayList<Base-Type

>()/** 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 16Slide7

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 17Slide8

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 BankAccountbankAccount1 .. bankAccount3 are added to the list

Set element at index 0 to bankAccount4 = (“1455”,”Edwin Moore”,600)Remove entry with index 2Contents of myAccounts printed to standard output.What is output?ADS2 Lecture 2SLIDE 19