Results 1 to 5 of 5
Thread: Beginner needs help
- 11-06-2011, 08:31 PM #1
Member
- Join Date
- Nov 2011
- Posts
- 18
- Rep Power
- 0
Beginner needs help
I am trying to create a new BankAccount object with an initial balance, the withdraw an amount from that object. I, then want to display the amount after the withdrawal using JOptionPane showMessageDialog method.
I have 2 files, the BankAccount class file(attached) which I compiled and another file which calls methods from the BankAccount file.
When I try to compile the second file I get an error saying "cannot find variable balance".
I have tried declaring a variable balance but then I get an error saying "variable balance might not have been initialised".
Can anyone point me in the right direction?? Any help would be much appriciated.
Thanks in advance.
import javax.swing.JOptionPane;
public class FixedStartingBankBalanceAccount
{
public static void main(String[] args)
{
//Constructor; calls constructor method from BankAccount class
BankAccount myFirstAccount = new BankAccount(5000);// Creates new BankAccount object with an initial balance
//***Methods***
//Calls withdraw method from BankAccount class
myFirstAccount.withdraw(200);//balance = balance - amount
//Calls getBalance method from BankAccount class
myFirstAccount.getBalance();//Retrieves balance from myFirstAccoun object
JOptionPane.showMessageDialog(null, "myFirstAccount contains:" + balance);
}
}
- 11-06-2011, 08:42 PM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,606
- Blog Entries
- 7
- Rep Power
- 17
Re: Beginner needs help
Look at your last two lines of code (ignore empty lines and curly brackets for now): what is getBalance() doing? Is it returning the balance value of the account? Why aren't you storing (assigning) it somewhere? And, what is that (undeclared) variable 'balance' doing in your last line of code?
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 11-06-2011, 08:55 PM #3
Member
- Join Date
- Nov 2011
- Posts
- 18
- Rep Power
- 0
Re: Beginner needs help
OK I understand the problem there, but if i put something like, "double balance;" into my code I still get an error saying variable balance may not be initialised.
I suppose where I am getting confused is when I call a method from the BankAccount class e.g myFirstAccount.withdraw(200); it goes to that class and does something like this, balance = balance - amount;
Do I need to declare all variables e.g balance and amount and if so how do I do it because double balance;
double amount; doesnt work.
Kind regards.
- 11-06-2011, 09:06 PM #4
Member
- Join Date
- Nov 2011
- Posts
- 18
- Rep Power
- 0
Re: Beginner needs help
I think i need to declare instance fields, where the declaration requires an access specifier, type and name of variable e.g private double balance.
I just don't know where to do this.
Can anyone assist?
Thanks in advance
- 11-07-2011, 12:45 AM #5
Senior Member
- Join Date
- Mar 2010
- Posts
- 953
- Rep Power
- 4
Similar Threads
-
beginner
By Eiolvit in forum New To JavaReplies: 4Last Post: 07-10-2011, 05:32 PM -
Beginner
By gozuhair in forum New To JavaReplies: 12Last Post: 07-10-2011, 03:14 PM -
JNI beginner
By M77 in forum Advanced JavaReplies: 2Last Post: 04-04-2011, 12:53 AM -
Beginner needs help!
By Polyy in forum New To JavaReplies: 1Last Post: 11-27-2008, 05:12 AM -
almost done...beginner needs help plz..
By shongo in forum New To JavaReplies: 15Last Post: 11-10-2008, 08:14 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks