Assignment 1 Questions

From Marks Wiki
Jump to navigation Jump to search

3. (How come question 1 is at the bottom?) I'm working on the deposit method and my "if" statement does not work. I first tried this code:

if (amount == zero || amount < zero) {}

and the error that I get is "The operator < is undefined for the argument type(s) Money, int" and also "Incompatible operand types Money and int". I would appreciate any help, thanks.

ANS::What is Zero.The important this is amount (I beleive) belongs to Money class.Unfortunately logical operators will not work on class.Logical operators only work on primitive types.SO how do u solve this issue.Call either isNegative() or compareTo() method from money class , and then use or logic.I used toString method.It is not recommended that you do the same.Upar006 19:40, 25 March 2008 (NZST)

2. I am working on implementing the withdraw method... I have written some code and one part of it is giving me an error. This part here:

if(transactionHistory.lastElement() == Transaction.INTEREST) || transactionHistory.lastElement() == Transaction.DEPOSIT){

    balance = balance.subtract(amount);

}

the conditions in my if statement are undelined in red and the error messgae is: "incompatible operand types Object and int"

can i please get some direction on how to fix this????

For your if condition "transactionHistory.lastElement() == Transaction.INTEREST", the left side *transactionHistory.lastElement()* returns an object, the right side "Transaction.INTEREST" is an int. The error message tells you that you cannot try to compare whether an object equals to an integer.

I guess you want to compare the type of a transaction with the Transaction constant (e.g. INTEREST, DEPOSIT etc.). If so, you can downcast the transactionHistory's last element to Transaction, and get the type value (type is an int), then do the comparison.


1. Do we have to make any new classes in the program. And how do we run the program, what is the starter method.

You need to add one class to the package, and this should be a subclass of TestCase that implements the tests for class BankAccount. Other than adding this class, all that is required for part A is to flesh out the methods in the BankAccount class, whose skeleton is supplied in the banking package.

For part B, you will need to make changes that will involve introducing a couple of BankAccount subclasses.

There is no main program - the assignment is concerned with completing the set of building blocks (i.e. classes) and ensuring that they meet their specifications. You could, if you wanted to, write a main program to create some BankAccount objects and use them to fulfil some sort of scenario.

5. How can we test the transaction history method, whenever i try to compare the 2 transactions using assertequals, it compares the memory value, and not the values that are stored by the array.

assertEquals(x,y) compares x and y using the equals() method of Object, i.e. x.equals(y). By default Object's equals() method compares by the hascode of the object as you correctly observed. So there are two ways to get around this: one is to compare individual parts of the Transaction, that is to say do an assertEquals() for type(), another one for amount() and another for account(). The other way is to override the equals() method inside Transaction, like:

public boolean equals(Object other) {
  //first check if other is instanceof Transaction. If not, return false.
  //then return whether my type equals other's type && my amountInvolved equals other's amountInvolved && etc.
}