Results 1 to 4 of 4

Thread: constructors

  1. #1
    droidus is offline Senior Member
    Join Date
    Feb 2011
    Posts
    244
    Rep Power
    4

    Default constructors

    there is a cash register code, and here is what i have so far:

    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;
       }
    to test the data, they use:

    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");      
       }
    }
    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:
    symbol**:*constructor*BankAccount(int)
    location:*class*BankAccount
    ******BankAccount*account1*=*new*BankAccount(1000);
    *****************************^
    1*error

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    That's not what a constructor should look like. A constructor does not have a return type.
    Java Code:
    public double BankAccount()
    is wrong, a constructor should look like this
    Java Code:
    public 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.

    Google the phrase. "java tutorial constructors"

  3. #3
    droidus is offline Senior Member
    Join Date
    Feb 2011
    Posts
    244
    Rep Power
    4

    Default

    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 09:01 PM.

  4. #4
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Glad to have helped. Please mark your thread solved with the thread tools at the top of the page.

Similar Threads

  1. Need help with constructors
    By tpfaff in forum New To Java
    Replies: 10
    Last Post: 10-22-2010, 05:33 AM
  2. Constructors?
    By annna in forum New To Java
    Replies: 3
    Last Post: 01-27-2010, 11:51 PM
  3. constructors?
    By shroomiin in forum New To Java
    Replies: 4
    Last Post: 10-13-2009, 03:14 PM
  4. Constructors
    By new2java2009 in forum New To Java
    Replies: 5
    Last Post: 08-18-2009, 07:46 AM
  5. constructors
    By khamuruddeen in forum New To Java
    Replies: 2
    Last Post: 12-01-2007, 04:15 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •