Results 1 to 5 of 5
Thread: Problem with Objects
- 01-27-2011, 11:19 PM #1
Member
- Join Date
- Jan 2011
- Posts
- 2
- Rep Power
- 0
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
And here is the code for my Bank 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"); } } } }
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!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; }
- 01-27-2011, 11:22 PM #2
Have a look at your constructor. You have declared a local variable with the same name.
-
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! :)
- 01-27-2011, 11:30 PM #4
Member
- Join Date
- Jan 2011
- Posts
- 2
- Rep Power
- 0
It works! Thanks, I appreciate it!
- 01-27-2011, 11:39 PM #5
Similar Threads
-
Saving objects problem
By BennyJass in forum NetworkingReplies: 2Last Post: 01-15-2011, 10:49 PM -
Problem with Vector of Objects
By JavaClueless in forum New To JavaReplies: 2Last Post: 11-07-2010, 03:46 AM -
Hashset and Custom Objects problem! Wont add! :(
By maz09 in forum New To JavaReplies: 4Last Post: 03-20-2010, 03:22 AM -
Problem in collision between objects
By Chetans in forum Java GamingReplies: 3Last Post: 03-15-2010, 11:24 PM -
Problem Creating/implementing Objects
By ramathews in forum New To JavaReplies: 2Last Post: 02-25-2010, 03:42 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks