944,008 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 765
  • Java RSS
Nov 8th, 2009
0

Static Method Problems

Expand Post »
Could anyone tell me what is wrong this code please. I'm trying to do this.
Add a static method Account consolidate(Account acct1, Account acct2) to your Account class that creates a new account whose balance is the sum of the balances in acct1 and acct2 and closes acct1 and acct2. The new account should be returned. Two important rules of consolidation:
Starting at line 98 is where I'm starting to have problems .


Java Syntax (Toggle Plain Text)
  1. //*******************************************************
  2. // Account.java
  3. //
  4. // A bank account class with methods to deposit to, withdraw from,
  5. // change the name on, and get a String representation
  6. // of the account.
  7. //*******************************************************
  8.  
  9. public class Account
  10. {
  11. private double balance;
  12. private String name;
  13. private long acctNum;
  14. private static int numAccounts = 0;
  15.  
  16. //----------------------------------------------
  17. //Constructor -- initializes balance, owner, and account number
  18. //----------------------------------------------
  19. public Account(double initBal, String owner, long number)
  20. {
  21. balance = initBal;
  22. name = owner;
  23. acctNum = number;
  24. numAccounts++;
  25. }
  26.  
  27. //----------------------------------------------
  28. // Checks to see if balance is sufficient for withdrawal.
  29. // If so, decrements balance by amount; if not, prints message.
  30. //----------------------------------------------
  31. public void withdraw(double amount)
  32. {
  33. if (balance >= amount)
  34. balance -= amount;
  35. else
  36. System.out.println("Insufficient funds");
  37. }
  38.  
  39. //----------------------------------------------
  40. // Adds deposit amount to balance.
  41. //----------------------------------------------
  42. public void deposit(double amount)
  43. {
  44. balance += amount;
  45. }
  46.  
  47. //----------------------------------------------
  48. // Returns balance.
  49. //----------------------------------------------
  50. public double getBalance()
  51. {
  52. return balance;
  53. }
  54.  
  55. //----------------------------------------------
  56. // Returns account number.
  57. //----------------------------------------------
  58. public long getAcctNumber()
  59. {
  60. return acctNum;
  61. }
  62.  
  63. //-----------------------------------------------
  64. //getNumAccount: return the of accounts
  65. //-----------------------------------------------
  66.  
  67. public static int getNumAccounts()
  68. {
  69. return numAccounts;
  70. }
  71.  
  72. //----------------------------------------------
  73. // Returns a string containing the name, account number, and balance.
  74. //----------------------------------------------
  75. public String toString()
  76. {
  77. return "Name: " + name +
  78. "\nAccount Number: " + acctNum +
  79. "\nBalance: " + balance;
  80. }
  81. public void close ()
  82. {
  83. name = name + "CLOSED";
  84. balance = 0;
  85. numAccounts--;
  86. }
  87.  
  88. //Account consolidate- the sum of acct1 and acct2
  89. private static Account consolidate(Account acct2, Account acct3)
  90. {
  91. Account newAccount = null;
  92. if((acct2.name).equals(acct3.name))
  93. If(acct2.acctNum!=acct3.acctNum)
  94. {
  95. Consolidate acct1 and acct2 like newAccount = (name, balance of 2 accts, acctnum(random));
  96. acct1.close();
  97. acct2.close();
  98. }
  99.  
  100.  
  101. return newAccount;
  102.  
  103. }
  104.  
  105. }
Last edited by COKEDUDE; Nov 8th, 2009 at 7:19 pm.
Reputation Points: 17
Solved Threads: 1
Junior Poster in Training
COKEDUDE is offline Offline
82 posts
since Oct 2009
Nov 8th, 2009
0
Re: Static Method Problems
>>
Add a static method Account consolidate(Account acct1, Account acct2) to your Account class that creates a new account whose balance is the sum of the balances in acct1 and acct2 and closes acct1 and acct2. The new account should be returned.
<<

Break this down :

>>Add a static method Account consolidate(Account acct1, Account acct2) to your Account class
<<
Java Syntax (Toggle Plain Text)
  1. public Account consolidate(Account account1, Account account2)
  2. { /* Code */ }

Next part of the statement :
>>
Account class creates a new account
<<
Java Syntax (Toggle Plain Text)
  1. public Account consolidate(Account account1, Account account2)
  2. {
  3. Account accountSum;
  4. }

whose balance is the sum of the balances in acct1 and acct2 and closes acct1 and acct2. The new account should be returned.

Java Syntax (Toggle Plain Text)
  1. public Account consolidate(Account account1, Account account2)
  2. {
  3. Account accountSum;
  4. accountSum.balance = account1.getBalance() + account2.getBalance();
  5.  
  6. //make account1 and account close()
  7. }

See how that works. Break down the question, and it becomes simpler.
So you need to sum the balance, and close account1 and account2.
By the way, You should make account1 and account2 = null when its closed.
Reputation Points: 840
Solved Threads: 594
Senior Poster
firstPerson is offline Offline
3,864 posts
since Dec 2008
Nov 8th, 2009
0
Re: Static Method Problems
1)You should always post error you getting
2)Line 93 if statement is with capital "i"
3)You are missing brackets for your first if statement on line 92
4)Line 95 is not a Java expression. Haven't you been given rules about consolidation?
Moderator
Featured Poster
Reputation Points: 2786
Solved Threads: 874
Code tags enforcer
peter_budo is offline Offline
6,658 posts
since Dec 2004
Nov 8th, 2009
0
Re: Static Method Problems
Click to Expand / Collapse  Quote originally posted by peter_budo ...
1)You should always post error you getting
2)Line 93 if statement is with capital "i"
3)You are missing brackets for your first if statement on line 92
4)Line 95 is not a Java expression. Haven't you been given rules about consolidation?
Is there a particular program that you know of that gives good error codes? I use a combination of Jgrasp and netbeans. Thank you for the figuring out the "I" and brackets.
Could you explain the consolidation rules please. I really don't understand them.
Reputation Points: 17
Solved Threads: 1
Junior Poster in Training
COKEDUDE is offline Offline
82 posts
since Oct 2009
Nov 8th, 2009
0
Re: Static Method Problems
>>


Java Syntax (Toggle Plain Text)
  1. public Account consolidate(Account account1, Account account2)
  2. {
  3. Account accountSum;
  4. accountSum.balance = account1.getBalance() + account2.getBalance();
  5.  
  6. //make account1 and account close()
  7. }

See how that works. Break down the question, and it becomes simpler.
So you need to sum the balance, and close account1 and account2.
By the way, You should make account1 and account2 = null when its closed.
Ok this looks better. For some reason it doesn't think accountSum has been initialized so my program won't run .
Here's what my program looks like so far.

Java Syntax (Toggle Plain Text)
  1. //*******************************************************
  2. // Account.java
  3. //
  4. // A bank account class with methods to deposit to, withdraw from,
  5. // change the name on, and get a String representation
  6. // of the account.
  7. //*******************************************************
  8.  
  9. public class Account
  10. {
  11. private double balance;
  12. private String name;
  13. private long acctNum;
  14. private static int numAccounts = 0;
  15.  
  16. //----------------------------------------------
  17. //Constructor -- initializes balance, owner, and account number
  18. //----------------------------------------------
  19. public Account(double initBal, String owner, long number)
  20. {
  21. balance = initBal;
  22. name = owner;
  23. acctNum = number;
  24. numAccounts++;
  25. }
  26.  
  27. //----------------------------------------------
  28. // Checks to see if balance is sufficient for withdrawal.
  29. // If so, decrements balance by amount; if not, prints message.
  30. //----------------------------------------------
  31. public void withdraw(double amount)
  32. {
  33. if (balance >= amount)
  34. balance -= amount;
  35. else
  36. System.out.println("Insufficient funds");
  37. }
  38.  
  39. //----------------------------------------------
  40. // Adds deposit amount to balance.
  41. //----------------------------------------------
  42. public void deposit(double amount)
  43. {
  44. balance += amount;
  45. }
  46.  
  47. //----------------------------------------------
  48. // Returns balance.
  49. //----------------------------------------------
  50. public double getBalance()
  51. {
  52. return balance;
  53. }
  54.  
  55. //----------------------------------------------
  56. // Returns account number.
  57. //----------------------------------------------
  58. public long getAcctNumber()
  59. {
  60. return acctNum;
  61. }
  62.  
  63. //-----------------------------------------------
  64. //getNumAccount: return the of accounts
  65. //-----------------------------------------------
  66.  
  67. public static int getNumAccounts()
  68. {
  69. return numAccounts;
  70. }
  71.  
  72. //----------------------------------------------
  73. // Returns a string containing the name, account number, and balance.
  74. //----------------------------------------------
  75. public String toString()
  76. {
  77. return "Name: " + name +
  78. "\nAccount Number: " + acctNum +
  79. "\nBalance: " + balance;
  80. }
  81. public void close ()
  82. {
  83. name = name + "CLOSED";
  84. balance = 0;
  85. numAccounts--;
  86. }
  87.  
  88. //Account consolidate- the sum of acct1 and acct2
  89. public Account consolidate(Account account1, Account account2)
  90. {
  91. Account newAccount = null;
  92. if((account1.name).equals(account2.name))
  93. {
  94. if(account1.acctNum!=account2.acctNum)
  95. {
  96. Account accountSum;
  97. accountSum.balance = account1.getBalance() + account2.getBalance();
  98. //Consolidate acct1 and acct2 like newAccount = (name, balance of 2 accts, acctnum(random));
  99. account1.close();
  100. account2.close();
  101. }}
  102.  
  103.  
  104. return newAccount;
  105.  
  106. }
  107.  
  108. }
Reputation Points: 17
Solved Threads: 1
Junior Poster in Training
COKEDUDE is offline Offline
82 posts
since Oct 2009
Nov 8th, 2009
0
Re: Static Method Problems
>>or some reason it doesn't think accountSum has been initialized so my program won't run
<<

Yea its right, sorry I am in c++ mode.

try :
Java Syntax (Toggle Plain Text)
  1. Account accountSum = new Account();
Reputation Points: 840
Solved Threads: 594
Senior Poster
firstPerson is offline Offline
3,864 posts
since Dec 2008
Nov 9th, 2009
-1
Re: Static Method Problems
What exactly the issue ?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
skoiloth is offline Offline
8 posts
since Oct 2005
Nov 9th, 2009
-1

Try this :)

Java Syntax (Toggle Plain Text)
  1. //Account accountSum;
  2. Account accountSum=new Account();
Click to Expand / Collapse  Quote originally posted by skoiloth ...
What exactly the issue ?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
skoiloth is offline Offline
8 posts
since Oct 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: TestConsolidation
Next Thread in Java Forum Timeline: File Download





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC