Results 1 to 4 of 4
Thread: constructors
- 04-28-2011, 05:40 PM #1
Senior Member
- Join Date
- Feb 2011
- Posts
- 235
- Rep Power
- 3
constructors
there is a cash register code, and here is what i have so far:
to test the data, they use:Java Code:private double balance; /** Constructs a bank account with a zero balance. */ public double BankAccount() { balance = 0; return balance; } /** Constructs a bank account with a given balance. @param initialBalance the initial balance */ public double BankAccount(int initialBalance) { balance = initialBalance; return balance; } /** Deposits money into the bank account. @param amount the amount to deposit */ public void deposit(double amount) { double newBalance = balance + amount; balance = newBalance; } /** Withdraws money from the bank account. @param amount the amount to withdraw */ public void withdraw(double amount) { double newBalance = balance - amount; balance = newBalance; balance = balance-1; } /** Gets the current balance of the bank account. @return the current balance */ public double getBalance() { return balance; }
i get a error message when running it. i made sure that my constructor is able to take in the int, so i am not sure why it doesn't like it:Java Code:public class BankAccountTester { public static void main(String[] args) { BankAccount account1 = new BankAccount(1000); account1.withdraw(100); System.out.println(account1.getBalance()); System.out.println("Expected: 899"); BankAccount account2 = new BankAccount(); account2.deposit(1000); account2.withdraw(100); account2.withdraw(100); System.out.println("Balance: " + account2.getBalance()); System.out.println("Expected: 798"); } }
Java Code:symbol**:*constructor*BankAccount(int) location:*class*BankAccount ******BankAccount*account1*=*new*BankAccount(1000); *****************************^ 1*error
- 04-28-2011, 05:49 PM #2
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
That's not what a constructor should look like. A constructor does not have a return type.
is wrong, a constructor should look like thisJava Code:public double BankAccount()
so they just do initialization and they do not return anything. See if these changes help you out. Also, I consider you read the oracle tutorials on constructors.Java Code:public BankAccount()
Google the phrase. "java tutorial constructors"
- 04-28-2011, 07:53 PM #3
Senior Member
- Join Date
- Feb 2011
- Posts
- 235
- Rep Power
- 3
i got it. thanks. i will probably have some more questions on these since they will be getting harder.
Last edited by droidus; 04-28-2011 at 08:01 PM.
- 04-28-2011, 08:14 PM #4
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
Glad to have helped. Please mark your thread solved with the thread tools at the top of the page.
Similar Threads
-
Need help with constructors
By tpfaff in forum New To JavaReplies: 10Last Post: 10-22-2010, 04:33 AM -
Constructors?
By annna in forum New To JavaReplies: 3Last Post: 01-27-2010, 10:51 PM -
constructors?
By shroomiin in forum New To JavaReplies: 4Last Post: 10-13-2009, 02:14 PM -
Constructors
By new2java2009 in forum New To JavaReplies: 5Last Post: 08-18-2009, 06:46 AM -
constructors
By khamuruddeen in forum New To JavaReplies: 2Last Post: 12-01-2007, 03:15 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks