Results 1 to 5 of 5
  1. #1
    javadam is offline Member
    Join Date
    Jan 2011
    Posts
    2
    Rep Power
    0

    Default Problem with Objects

    I'm learning Java, which is my first programming language. I've been teaching myself using the Blue Pelican pdf, and I've caught on a bit of a snag. I'm doing my own variation of one of the projects suggested in the book, and I'm stuck. Any help would be much appreciated. Thanks!

    Here is the code for my Tester class


    Java Code:
    import java.io.*;
    import java.util.*;
    
    public class Tester
    {
        public static void main(String args[])
        {
            
            Scanner keyboard = new Scanner(System.in);
            
            System.out.print("What is the name of the client? ");
            String cn = keyboard.next();
            System.out.print("What is the starting balance of the account? ");
            int startBalance = keyboard.nextInt();
            bank bank1 = new bank(cn, startBalance)
            System.out.println("The client's name is " + cn + " and the current balance is " + startBalance);
            System.out.println();
            System.out.println();
            
            for (int status = 0; status <= 99; status++)
            {
            System.out.println("Would you like to:");
            System.out.println("1:    Make a deposit?");
            System.out.println("2:    Make a withdrawal?");
            System.out.println("3:    Check your current balance?");
            System.out.println("4:    Exit?");
            int choice = keyboard.nextInt();
            
            switch (choice)
            {
                case 1: 
                System.out.println();
                System.out.print("What is the amount of your deposit? ");
                int depositIn = keyboard.nextInt();
                bank1.deposit(depositIn);
                break;
                
                case 2:
                System.out.println();
                System.out.print("What is the amount of your withdrawal? ");
                int withdrawal = keyboard.nextInt();
                bank1.withdraw(withdrawal);
                break;
                
                case 3:
                System.out.println();
                System.out.println("Your current balance is " + bank1.bankBalance);
                break;
                
                case 4:
                status = 99;
                break;
                
                default:
                System.out.println("Illegal Operation");
            
            }
        }
    }
    }
    And here is the code for my Bank class.

    Java Code:
    public class bank
    {
    
        public bank(String name, int b)
        {
            int bankBalance = b;
            String clientName = name;
            
        }
        
        public void deposit(int depAmount)
        {
           
            bankBalance = bankBalance + depAmount;
        }
        
        public void withdraw(int withAmount)
        {
            bankBalance = bankBalance - withAmount;
     
        }
        
        public int bankBalance;
    }
    I know it is not the most elegant code, but here is my problem... the bankBalance used in my bank1 object is not affected by my startBalance integer. What is the problem with my code? Thanks for your help!

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,789
    Rep Power
    7

    Default

    Have a look at your constructor. You have declared a local variable with the same name.

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    You are re-declaring the bankBalance variable inside the Bank constructor, which is "shadowing" the variable declared in the class. The solution is not to redeclare it in the constructor. So, not this:

    Java Code:
    int bankBalance = b;

    but this:

    Java Code:
    bankBalance = b;

    edit: as Junky states above! :)

  4. #4
    javadam is offline Member
    Join Date
    Jan 2011
    Posts
    2
    Rep Power
    0

    Default

    It works! Thanks, I appreciate it!

  5. #5
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,789
    Rep Power
    7

Similar Threads

  1. Saving objects problem
    By BennyJass in forum Networking
    Replies: 2
    Last Post: 01-15-2011, 10:49 PM
  2. Problem with Vector of Objects
    By JavaClueless in forum New To Java
    Replies: 2
    Last Post: 11-07-2010, 03:46 AM
  3. Hashset and Custom Objects problem! Wont add! :(
    By maz09 in forum New To Java
    Replies: 4
    Last Post: 03-20-2010, 03:22 AM
  4. Problem in collision between objects
    By Chetans in forum Java Gaming
    Replies: 3
    Last Post: 03-15-2010, 11:24 PM
  5. Problem Creating/implementing Objects
    By ramathews in forum New To Java
    Replies: 2
    Last Post: 02-25-2010, 03:42 AM

Tags for this Thread

Posting Permissions

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