| | |
Static Method Problems
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Oct 2009
Posts: 31
Reputation:
Solved Threads: 1
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
.
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)
//******************************************************* // Account.java // // A bank account class with methods to deposit to, withdraw from, // change the name on, and get a String representation // of the account. //******************************************************* public class Account { private double balance; private String name; private long acctNum; private static int numAccounts = 0; //---------------------------------------------- //Constructor -- initializes balance, owner, and account number //---------------------------------------------- public Account(double initBal, String owner, long number) { balance = initBal; name = owner; acctNum = number; numAccounts++; } //---------------------------------------------- // Checks to see if balance is sufficient for withdrawal. // If so, decrements balance by amount; if not, prints message. //---------------------------------------------- public void withdraw(double amount) { if (balance >= amount) balance -= amount; else System.out.println("Insufficient funds"); } //---------------------------------------------- // Adds deposit amount to balance. //---------------------------------------------- public void deposit(double amount) { balance += amount; } //---------------------------------------------- // Returns balance. //---------------------------------------------- public double getBalance() { return balance; } //---------------------------------------------- // Returns account number. //---------------------------------------------- public long getAcctNumber() { return acctNum; } //----------------------------------------------- //getNumAccount: return the of accounts //----------------------------------------------- public static int getNumAccounts() { return numAccounts; } //---------------------------------------------- // Returns a string containing the name, account number, and balance. //---------------------------------------------- public String toString() { return "Name: " + name + "\nAccount Number: " + acctNum + "\nBalance: " + balance; } public void close () { name = name + "CLOSED"; balance = 0; numAccounts--; } //Account consolidate- the sum of acct1 and acct2 private static Account consolidate(Account acct2, Account acct3) { Account newAccount = null; if((acct2.name).equals(acct3.name)) If(acct2.acctNum!=acct3.acctNum) { Consolidate acct1 and acct2 like newAccount = (name, balance of 2 accts, acctnum(random)); acct1.close(); acct2.close(); } return newAccount; } }
Last edited by COKEDUDE; 27 Days Ago at 7:19 pm.
0
#2 27 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
<<
Next part of the statement :
>>
Account class 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.
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.
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)
public Account consolidate(Account account1, Account account2) { /* Code */ }
Next part of the statement :
>>
Account class creates a new account
<<
Java Syntax (Toggle Plain Text)
public Account consolidate(Account account1, Account account2) { Account accountSum; }
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)
public Account consolidate(Account account1, Account account2) { Account accountSum; accountSum.balance = account1.getBalance() + account2.getBalance(); //make account1 and account close() }
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?
0
#3 27 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?
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
Publilius Syrus
(~100 BC)
LJC - London Java Community, Graduate & Undergraduate Software Development Community, JAVAWUG (Java Web User Group), The London Android Group
•
•
Join Date: Oct 2009
Posts: 31
Reputation:
Solved Threads: 1
0
#4 27 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?
Could you explain the consolidation rules please. I really don't understand them.
•
•
Join Date: Oct 2009
Posts: 31
Reputation:
Solved Threads: 1
0
#5 27 Days Ago
•
•
•
•
>>
Java Syntax (Toggle Plain Text)
public Account consolidate(Account account1, Account account2) { Account accountSum; accountSum.balance = account1.getBalance() + account2.getBalance(); //make account1 and account close() }
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.
. Here's what my program looks like so far.
Java Syntax (Toggle Plain Text)
//******************************************************* // Account.java // // A bank account class with methods to deposit to, withdraw from, // change the name on, and get a String representation // of the account. //******************************************************* public class Account { private double balance; private String name; private long acctNum; private static int numAccounts = 0; //---------------------------------------------- //Constructor -- initializes balance, owner, and account number //---------------------------------------------- public Account(double initBal, String owner, long number) { balance = initBal; name = owner; acctNum = number; numAccounts++; } //---------------------------------------------- // Checks to see if balance is sufficient for withdrawal. // If so, decrements balance by amount; if not, prints message. //---------------------------------------------- public void withdraw(double amount) { if (balance >= amount) balance -= amount; else System.out.println("Insufficient funds"); } //---------------------------------------------- // Adds deposit amount to balance. //---------------------------------------------- public void deposit(double amount) { balance += amount; } //---------------------------------------------- // Returns balance. //---------------------------------------------- public double getBalance() { return balance; } //---------------------------------------------- // Returns account number. //---------------------------------------------- public long getAcctNumber() { return acctNum; } //----------------------------------------------- //getNumAccount: return the of accounts //----------------------------------------------- public static int getNumAccounts() { return numAccounts; } //---------------------------------------------- // Returns a string containing the name, account number, and balance. //---------------------------------------------- public String toString() { return "Name: " + name + "\nAccount Number: " + acctNum + "\nBalance: " + balance; } public void close () { name = name + "CLOSED"; balance = 0; numAccounts--; } //Account consolidate- the sum of acct1 and acct2 public Account consolidate(Account account1, Account account2) { Account newAccount = null; if((account1.name).equals(account2.name)) { if(account1.acctNum!=account2.acctNum) { Account accountSum; accountSum.balance = account1.getBalance() + account2.getBalance(); //Consolidate acct1 and acct2 like newAccount = (name, balance of 2 accts, acctnum(random)); account1.close(); account2.close(); }} return newAccount; } }
0
#6 27 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 :
<<
Yea its right, sorry I am in c++ mode.
try :
Java Syntax (Toggle Plain Text)
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?
![]() |
Similar Threads
- non-static method cannot be referenced from a static context (Java)
- OOP Fatal error: Non-static method (PHP)
- Using a static method to read data from text file (Java)
- Trouble with a static method (Java)
- Non-Static method cannot be referanced from a Static context (Java)
- Non-static method can't be referenced from a static context (Java)
Other Threads in the Java Forum
- Previous Thread: Sorting Phone numbers? no code just a questions
- Next Thread: File Download
| Thread Tools | Search this Thread |
actuate android api applet application applications array arrays automation balls bank binary bluetooth business chat class classes clear client code codesnippet collections component database defaultmethod development dice dragging ebook eclipse error event exception formatingtextintooltipjava fractal froglogic game givemetehcodez graphics gui hql html ide image infinite input integer intersect invokingapacheantprogrammatically j2me java javaprojects jni jpanel julia linux list loop looping map method methods mobile mysql netbeans newbie numbers openjavafx parameter php print problem program programming project recursion repositories scanner screen scrollbar server set size sms sort sorting sql sqlserver state storm string sun superclass swing swt text-file threads time tree windows






