By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,034 Members | 820 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,034 IT Pros & Developers. It's quick & easy.

please help!!

P: 45
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.

Expand|Select|Wrap|Line Numbers
  1. public class Bank
  2. {
  3.     public BankAccount doSomething(int accountNumber)
  4.    {
  5.       for (BankAccount a : accounts)
  6.       {
  7.           if (a.getAccountNumber() == accountNumber) 
  8.           return a;
  9.        }
  10.        return null; 
  11.     }
  12.        . . .
  13. }
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.getAccountNumber() is equal to accountNumber) then it return the value of that account.

so what do you think?

thanks!!
Jan 5 '07 #1
Share this Question
Share on Google+
3 Replies


10K+
P: 13,264
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.

Expand|Select|Wrap|Line Numbers
  1. public class Bank
  2. {
  3. public BankAccount doSomething(int accountNumber)
  4. {
  5. for (BankAccount a : accounts)
  6. {
  7. if (a.getAccountNumber() == accountNumber) 
  8. return a;
  9. }
  10. return null; 
  11. }
  12. . . .
  13. }
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.getAccountNumber() 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.getAccountNumber()equal to accountNumber then the null reference is returned.
Jan 5 '07 #2

P: 45
Yes it returns that BankAccount. Also if no account is not found that has a.getAccountNumber()equal to accountNumber then the null reference is returned.

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

outofmymind ;)
Jan 5 '07 #3

10K+
P: 13,264
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
Jan 5 '07 #4

Post your reply

Sign in to post your reply or Sign up for a free account.