SE251:Questions & Answers

From Marks Wiki
Jump to navigation Jump to search
  • please explain constructors

OK, there will be some coverage of constructor methods in the tutorial.


  • When attempting to use the "add( amount );" function from the "Money" class, the word "add" is highlighted in red with an error message saying "The method add(Money) is undefined for the type BankAccount". Please explain.

The add( ) method is defined by class Money, so you can call this method on a Money instance (object). From what you are saying, it sounds like you are actually attempting to call add( ) on a BankAccount object:

BankAccount myAccount = new BankAccount( ... );
myAccount.add( ... );

Since the BankAccount class definition does not include an add( ) method, you cannot call add( ) through a reference variable pointing to a BankAccount object.

  • Ohh!! Thanks! That helps a lot! :D.



  • For the assignment, in regards to handling the transaction history, you are populating an array of Transaction objects. Because arrays need to have a fixed size and there is no easy way to add objects without having to iterate through the elements, couldn't we just use an arraylist, such as List<Transaction>? It then becomes as easy as List.add() and List.get()

Yes, you can use any of the List implementation classes (e.g. ArrayList, LinkedList etc.) defined in package java.util. The List interface and its implementation classes are defined using Java's quite recently added support for generic types. Generic types are considered an advanced topic and will be covered later in the course. Using generic types in the assignment is fairly straightforward though:

List<Transaction> transactionHistory = new ArrayList<Transaction>();
transactionHistory.add( new Transaction( ... ));

The above statements create an ArrayList object that is constrained to storing references to Transaction objects, and add a Transaction object to the ArrayList instance.

List transactionHistory = new ArrayList();
transactionHistory.add( new Transaction( ... ));

These statements do a similar thing but the ArrayList object can store references to instances of any class. Without specifying a type in the <> brackets, an ArrayList can be thought of as a dynamic array of type Object. Java's polymorphic typing rules allow a reference variable of type Object to point to an instance of any class. The preferred style is to specify a constraining type - and this avoids compile time warnings. Much more on generic types to come.

Alternatively to using one of Java's Collection classes (e.g. ArrayList), you can use an array for the assignment. In addition to having the array as an instance variable in class BankAccount, you can also have an instance variable to keep track of the number of items in the array. Every time you add a new Transaction object to the array, increment the counter variable. You can then access the next free slot in the array without having to iterate through the elements to find the first null reference array element.:

private Transaction[] transactionHistory;
private int numberOfTransactions;
transactionHistory[ numberOfTransactions++ ] = new Transaction( ... );

Also, class Arrays in java.util provides a method (copyOf) that can be used to extend an array's capacity at run-time.

NOTE: Using Arrays may not be ideal in this situation. Refer to the discussion page (Thiranjith)

There's no one right answer as to how to store a collection of Transaction objects, so go with what you feel is best.