Page 3 of 3 FirstFirst 123
Results 41 to 55 of 55

Thread: Help?

  1. #41
    wdlax11 is offline Member
    Join Date
    Feb 2011
    Posts
    25
    Rep Power
    0

    Default

    Quote Originally Posted by milovan View Post
    Ok, you must learn some basics as Fubarale said.

    Your program will not work until you rewrite it.

    I guess that your intention was to have a global varible called balance.

    After you declare it in your class you can't make some local variable with the same name, so this is not going to work:
    Java Code:
    public class BankAccount
    {
       
        /**
         * Constructor for objects of class BankAccount
         */
        double balance;
          String name;
    
          public BankAccount(int x){
    
            
        }
    
        public void deposit(double deposit, double current,double balance)
        {
        
              balance = deposit + current; // you hide your global variable with this one 
    
        }


    so I have to do this?

    Java Code:
    public class BankAccount
    {
       
        /**
         * Constructor for objects of class BankAccount
         */
           double balance;
        String name;
    
          public BankAccount(int x){
    
      
        }
    
        public void deposit(double deposit, double current)
        {
        
              balance = deposit + current; // you hide your global variable with this one 
    
        }
    Last edited by wdlax11; 02-06-2011 at 08:49 PM.

  2. #42
    milovan is offline Senior Member
    Join Date
    Jan 2011
    Location
    Belgrade, Serbia
    Posts
    301
    Rep Power
    4

    Default

    Very good, getting somewhere. Now, pay attention, I think you don't need constructor with int in this case, and remeber to provide right number of parameters to your deposit() from main method.
    You should follow the same logic for other method...
    Last edited by milovan; 02-06-2011 at 08:56 PM.

  3. #43
    wdlax11 is offline Member
    Join Date
    Feb 2011
    Posts
    25
    Rep Power
    0

    Default

    No I need it with the int because if I don't it wont allow me to enter the initial balance number. Also I followed the format with 2 doubles. However it still says cannot find symbol for balance.

  4. #44
    wdlax11 is offline Member
    Join Date
    Feb 2011
    Posts
    25
    Rep Power
    0

    Default

    Here is my code for both of them. I ran the BankAccount one and it worked fine. But when I try to compile and run the tester (which is the whole point of the assignment) it cant find the balance symbol...

    BankAccount
    Java Code:
    public class BankAccount
    {
       
        /**
         * Constructor for objects of class BankAccount
         */
        
        public double balance;
        String name;
    
          public BankAccount(int x){
          double balance;
          String name;
            
        }
    
        /**
         * deposit method returns nothing, accepts a double that is the amount of money being withdrawn.  This will be subtracted to the balance.
         * 
         * @param      double deposit
         * @return     nothing
         */
        public void deposit(double deposit, double current)
        {
            
            balance = current + deposit;
    
        }
        
         /**
         * withdraw method returns nothing, accepts a double that is the amount of money being withdrawn.  This will be subtracted ded to the balance.
         * 
         * @param      double withdrawn
         * @return     nothing
         */
        public void withdraw(double withdrawn, double current)
        {
            
            balance = current - withdrawn;
    
        }
        }
    Tester
    Java Code:
    public class Tester{
    
       /**
       * MAIN METHOD
       */
       public static void main(String [ ] args)
       {
         int x =1000;
         String name = "The Sally Jones account balance is,";
         BankAccount myAccount = new BankAccount (x);
         
         myAccount.deposit(505.22, x);
         System.out.println(balance);
         
         myAccount.withdraw(100, x);
         System.out.println(name + x);
         
         
       }
    
    }

  5. #45
    milovan is offline Senior Member
    Join Date
    Jan 2011
    Location
    Belgrade, Serbia
    Posts
    301
    Rep Power
    4

    Default

    Well I guess you are trying to print value of balance from your other class with main method (not possible at this point) but to be sure please post your last main method again...

  6. #46
    wdlax11 is offline Member
    Join Date
    Feb 2011
    Posts
    25
    Rep Power
    0

    Default

    BankAccount
    Java Code:
    public class BankAccount
    {
       
        /**
         * Constructor for objects of class BankAccount
         */
        
        public double balance;
        String name;
    
          public BankAccount(int x){
          double balance;
          String name;
            
        }
    
        /**
         * deposit method returns nothing, accepts a double that is the amount of money being withdrawn.  This will be subtracted to the balance.
         * 
         * @param      double deposit
         * @return     nothing
         */
        public void deposit(double deposit, double current)
        {
            
            balance = current + deposit;
    
        }
        
         /**
         * withdraw method returns nothing, accepts a double that is the amount of money being withdrawn.  This will be subtracted ded to the balance.
         * 
         * @param      double withdrawn
         * @return     nothing
         */
        public void withdraw(double withdrawn, double current)
        {
            
            balance = current - withdrawn;
    
        }
        }
    Tester / Main Method
    Java Code:
    public class Tester{
    
       /**
       * MAIN METHOD
       */
       public static void main(String [ ] args)
       {
         int x =1000;
         String name = "The Sally Jones account balance is,";
         BankAccount myAccount = new BankAccount (x);
         
         myAccount.deposit(505.22, x);
         System.out.println(balance);
         
         myAccount.withdraw(100, x);
         System.out.println(name + x);
         
         
       }
    
    }

  7. #47
    milovan is offline Senior Member
    Join Date
    Jan 2011
    Location
    Belgrade, Serbia
    Posts
    301
    Rep Power
    4

    Default

    you are quick..

  8. #48
    wdlax11 is offline Member
    Join Date
    Feb 2011
    Posts
    25
    Rep Power
    0

    Default

    ^^ yeah i need to get it done i have been doing this for like 2 hours haha I have to study for tests and stuff

  9. #49
    milovan is offline Senior Member
    Join Date
    Jan 2011
    Location
    Belgrade, Serbia
    Posts
    301
    Rep Power
    4

    Default

    You are trying to print balance from Tester right?
    But do you have a variable named balance in your Tester class?
    What you want to do is to print BackAccount's balance, so you must tell java where to find it:
    Java Code:
    myAccount.balance
    will do the job...

  10. #50
    milovan is offline Senior Member
    Join Date
    Jan 2011
    Location
    Belgrade, Serbia
    Posts
    301
    Rep Power
    4

    Default

    Is it working?

  11. #51
    wdlax11 is offline Member
    Join Date
    Feb 2011
    Posts
    25
    Rep Power
    0

    Default

    ... im about to punch a hole in my monitors..i spent like an hour trying to print something out and all i needed was myAccount.

  12. #52
    milovan is offline Senior Member
    Join Date
    Jan 2011
    Location
    Belgrade, Serbia
    Posts
    301
    Rep Power
    4

    Default

    Believe me, this is the best way of learning things.
    If you have it working and you rewrite withdraw method in the same way, and also declared String name as public I think we are done here, or this is not a solution you are looking for?

  13. #53
    milovan is offline Senior Member
    Join Date
    Jan 2011
    Location
    Belgrade, Serbia
    Posts
    301
    Rep Power
    4

    Default

    There is one more thing. Take a look at this part of your code and read my comments


    Java Code:
    public BankAccount(int x){
          double balance; // you don't need this local variable since it is never used
          String name;     // you don't need this local variable since it is never used
    
          // int x  is never used in this method so there is no need for it
          // you just can have blank BankAccount() with nothing inside its body
          // and your app is still going to work, in fact you can skip this whole
          // method since it does nothing (javac will provide generic one when compiling)
        }

  14. #54
    wdlax11 is offline Member
    Join Date
    Feb 2011
    Posts
    25
    Rep Power
    0

    Default

    yeah i fixed that. Thanks a lot for the help!

  15. #55
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,375
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by wdlax11 View Post
    ... im about to punch a hole in my monitors..i spent like an hour trying to print something out and all i needed was myAccount.
    It took you that long because you never tried to answer my two questions in reply #22; a.a.m.o.f. you completely ignored them.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Page 3 of 3 FirstFirst 123

Posting Permissions

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