Static Method Problems

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Oct 2009
Posts: 31
Reputation: COKEDUDE is an unknown quantity at this point 
Solved Threads: 1
COKEDUDE COKEDUDE is offline Offline
Light Poster

Static Method Problems

 
0
  #1
26 Days Ago
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 .


  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; 26 Days Ago at 7:19 pm.
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 1,265
Reputation: firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice 
Solved Threads: 157
firstPerson's Avatar
firstPerson firstPerson is offline Offline
Nearly a Posting Virtuoso
 
0
  #2
26 Days Ago
>>
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
<<
  1. public Account consolidate(Account account1, Account account2)
  2. { /* Code */ }

Next part of the statement :
>>
Account class creates a new account
<<
  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.

  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.
1) What word becomes shorter if you add a letter to it? [ Solved by : niek_e, Paul Thompson]
2) What does this sequence  equal to :  (.5u - .5a)(.5u-.5b)(.5u-.5c) ...[*]
      [*solved by : murtan]
3) What is the 123456789 prime numer?
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 4,205
Reputation: peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of 
Solved Threads: 486
Moderator
Featured Poster
peter_budo's Avatar
peter_budo peter_budo is offline Offline
Code tags enforcer
 
0
  #3
26 Days Ago
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?
Learn to see in another's calamity the ills which you should avoid.
Publilius Syrus
(~100 BC)

LJC - London Java Community, Graduate & Undergraduate Software Development Community, JAVAWUG (Java Web User Group), The London Android Group
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 31
Reputation: COKEDUDE is an unknown quantity at this point 
Solved Threads: 1
COKEDUDE COKEDUDE is offline Offline
Light Poster
 
0
  #4
26 Days Ago
Originally Posted by peter_budo View Post
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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 31
Reputation: COKEDUDE is an unknown quantity at this point 
Solved Threads: 1
COKEDUDE COKEDUDE is offline Offline
Light Poster
 
0
  #5
26 Days Ago
Originally Posted by firstPerson View Post
>>


  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.

  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. }
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 1,265
Reputation: firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice 
Solved Threads: 157
firstPerson's Avatar
firstPerson firstPerson is offline Offline
Nearly a Posting Virtuoso
 
0
  #6
26 Days Ago
>>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 :
  1. Account accountSum = new Account();
1) What word becomes shorter if you add a letter to it? [ Solved by : niek_e, Paul Thompson]
2) What does this sequence  equal to :  (.5u - .5a)(.5u-.5b)(.5u-.5c) ...[*]
      [*solved by : murtan]
3) What is the 123456789 prime numer?
Reply With Quote Quick reply to this message  
Join Date: Oct 2005
Posts: 7
Reputation: skoiloth is an unknown quantity at this point 
Solved Threads: 0
skoiloth's Avatar
skoiloth skoiloth is offline Offline
Newbie Poster
 
-1
  #7
26 Days Ago
What exactly the issue ?
Reply With Quote Quick reply to this message  
Join Date: Oct 2005
Posts: 7
Reputation: skoiloth is an unknown quantity at this point 
Solved Threads: 0
skoiloth's Avatar
skoiloth skoiloth is offline Offline
Newbie Poster

Try this :)

 
-1
  #8
26 Days Ago
  1. //Account accountSum;
  2. Account accountSum=new Account();
Originally Posted by skoiloth View Post
What exactly the issue ?
Reply With Quote Quick reply to this message  
Reply

Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC