Results 1 to 4 of 4
  1. #1
    loftus727 is offline Member
    Join Date
    Feb 2013
    Posts
    24
    Rep Power
    0

    Default Opening and Closing Accounts

    I am having problems with both the class and the method of this assignment. errors that I cant figure out come up. Any help would be greatly appreciated!

    here is what I have so far...

    code for class:

    Java Code:
    //*******************************************************
    // 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 Account1
    {
      private double balance;
      private String name;
      private long acctNum;
       private static int numAccounts = 0;
    
      //----------------------------------------------
      //Constructor -- initializes balance, owner, and account number
      //----------------------------------------------
      public Account(double balance, String name, long acctNum)
      {
        this.balance = balance;
        this.name = name;
        this.acctNum = acctNum;
    	 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;
      }
    
      //----------------------------------------------
      // Returns a string containing the name, account number, and balance.
      //----------------------------------------------
      public String toString()
      {
    	return "Name: " + name + 
    "\nAccount Number: " + acctNum +
    "\nBalance: " + balance; 
      }
      
       //----------------------------------------------
       // Returns the number of instances of this class has been created 
       //----------------------------------------------
          public static int getNumAccounts()
          {
             return numAccounts;
          }
    }
    here is the error:
    ----jGRASP exec: javac -g Account1.java

    Account1.java:19: invalid method declaration; return type required
    public Account(double balance, String name, long acctNum)
    ^
    1 error

    ----jGRASP wedge2: exit code for process is 1.
    ----jGRASP: operation complete.

    and here is the code for the method:

    Java Code:
    //***********************************************************
    // TestAccounts1
    // A simple program to test the numAccts method of the 
    // Account class.  
    //***********************************************************
       import java.util.Scanner;
    
       public class TestAccounts1
       {
          public static void main(String[] args)
          {
             Account testAcct;
          
             Scanner scan = new Scanner(System.in);
          
             System.out.println("How many accounts would you like to create?");
             int num = scan.nextInt();
          
             for (int i=1; i<=num; i++)
             {
                testAcct = new Account(100, "Name", i);
                System.out.println("\nCreated account " + testAcct);
                System.out.println("Now there are " + Account.getNumAccounts() +   " accounts");
             }
          }
       }
    here is the error:
    ----jGRASP exec: javac -g TestAccounts1.java

    TestAccounts1.java:23: cannot find symbol
    symbol : method getNumAccounts()
    location: class Account
    System.out.println("Now there are " + Account.getNumAccounts() + " accounts");
    ^
    1 error

    ----jGRASP wedge2: exit code for process is 1.
    ----jGRASP: operation complete.

  2. #2
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    757
    Rep Power
    8

    Default Re: Opening and Closing Accounts

    There are some problems with your code:

    1. From the document in your class that your file name is Account.java. This mean what you want is to create a class called Account but you create a class with Account1 name instead. This gave you the first error regarding the constructor. The class expect your constructor name is Account1 following the class name.

    2. In the second error you are trying to access Account.getNumAccounts() method. But you are not having a varibale name Account there. Instead you have a variable named testAcct. To access an instance method like getNumAccounts() you have to use the instance of the class to access it, like testAcct. You can access it with the class name, unless the method marked static.

  3. #3
    loftus727 is offline Member
    Join Date
    Feb 2013
    Posts
    24
    Rep Power
    0

    Default Re: Opening and Closing Accounts

    Okay I fixed the first part I did not realize it was such a silly little mistake I thought it was something big that why I overlooked it.

    But for the second part can you be a bit clearer as to what you mean?

  4. #4
    loftus727 is offline Member
    Join Date
    Feb 2013
    Posts
    24
    Rep Power
    0

    Default Re: Opening and Closing Accounts

    nevermind did not realize the mistakes were the same. Thanks for everything.

    Also can you take a look at my other post?
    A Flexible Account Class

Similar Threads

  1. limit disk space for my customers accounts
    By mtz1406 in forum Java Servlet
    Replies: 6
    Last Post: 04-04-2010, 04:38 PM
  2. API to create user accounts
    By amitnme in forum Advanced Java
    Replies: 1
    Last Post: 02-05-2010, 07:08 PM
  3. Replies: 4
    Last Post: 10-07-2009, 09:14 AM
  4. Closing an Editor
    By janpol1 in forum Eclipse
    Replies: 0
    Last Post: 02-14-2009, 04:00 PM
  5. Accounts of users
    By Eric in forum Enterprise JavaBeans (EJB)
    Replies: 1
    Last Post: 06-27-2007, 04:20 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
  •