please help!!

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • outofmymind
    New Member
    • Oct 2006
    • 45

    please help!!

    Hi,

    i've been trying to solve some java questions, to prepare for a test. I came across a question and im not sure if my answer is correct or not. Is it ok if you guys check it for me and give me your feedback?

    this is the question:

    What does the following code do with the array list accounts.

    Code:
    public class Bank
    {
        public BankAccount doSomething(int accountNumber)
       {
          for (BankAccount a : accounts)
          {
              if (a.getAccountNumber() == accountNumber) 
              return a;
           }
           return null; 
        }
           . . .
    }
    This is the answer i came up with:

    the for loop will check “for each BankAccount a in accounts.” Then it will check if (a.getAccountNu mber() is equal to accountNumber) then it return the value of that account.

    so what do you think?

    thanks!!
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Originally posted by outofmymind
    Hi,

    i've been trying to solve some java questions, to prepare for a test. I came across a question and im not sure if my answer is correct or not. Is it ok if you guys check it for me and give me your feedback?

    this is the question:

    What does the following code do with the array list accounts.

    Code:
    public class Bank
    {
    public BankAccount doSomething(int accountNumber)
    {
    for (BankAccount a : accounts)
    {
    if (a.getAccountNumber() == accountNumber) 
    return a;
    }
    return null; 
    }
    . . .
    }
    This is the answer i came up with:

    the for loop will check “for each BankAccount a in accounts.” Then it will check if (a.getAccountNu mber() is equal to accountNumber) then it return the value of that account.

    so what do you think?

    thanks!!
    Yes it returns that BankAccount. Also if no account is not found that has a.getAccountNum ber()equal to accountNumber then the null reference is returned.

    Comment

    • outofmymind
      New Member
      • Oct 2006
      • 45

      #3
      Originally posted by r035198x
      Yes it returns that BankAccount. Also if no account is not found that has a.getAccountNum ber()equal to accountNumber then the null reference is returned.

      thanks, i was worried that it might be wrong :s

      outofmymind ;)

      Comment

      • r035198x
        MVP
        • Sep 2006
        • 13225

        #4
        Originally posted by outofmymind
        thanks, i was worried that it might be wrong :s

        outofmymind ;)
        Anytime. If you are going for a test, might want to consider having a look at the java classes on this site as aonther revision point

        Comment

        Working...