public class BankAccount
{
public double balance;
String name;
public BankAccount(double bal,String nam)
{
balance=bal;
name=nam;
}
public void deposit(double dep)
{
double depo=dep;
double balance=depo+balance;
}
public void withdraw(double out)
{
double outa=out;
double balance=balance-outa;
}
}
class Tester
{
public static void main(String[]args)
{
BankAccount myAccount=new BankAccount(1000.00,"Sam");
myAccount.deposit(505.22);
System.out.println("Balance : "+balance);
myAccount.withdraw(100.00);
System.out.println("the Sam account balance is ,"+balance);
}
}
it throws errors like 'balance 'may not be initialised....and symbol not resolved in class tester.....please help...i m stuck in it for too long:p
please help me out with the code below
Started by Ananta2010, Jun 18 2010 04:26 AM
9 replies to this topic
#1
Posted 18 June 2010 - 04:26 AM
|
|
|
#2
Posted 18 June 2010 - 04:34 AM
Why are you redeclaring balance in your deposit and withdraw methods?
#3
Posted 18 June 2010 - 04:40 AM
I've put some comments in line on your code.
In short, your deposit&withdraw methods won't do what you want todo (there are probably the cause of the uninitlised variable messages.) Balance shouldn't be public, you need an accessor method on it.
Rewrite those bits and it should work.
public class BankAccount
{
public double balance; //Do you really want this to be public???
String name; //This should have an access qualifier;
public BankAccount(double bal,String nam)
{
balance=bal;
name=nam;
}
public void deposit(double dep)
{
double depo=dep; //What's the point of this line?
double balance=depo+balance; //Here you declaring a local variable,
// balance, and then trying to use the class value
// I don't think you want that
}
public void withdraw(double out)
{
double outa=out; //Again this line surves no purpose
double balance=balance-outa; //Again balance is locally defined here.
}
}
class Tester
{
public static void main(String[]args)
{
BankAccount myAccount=new BankAccount(1000.00,"Sam");
myAccount.deposit(505.22); //Note the balance will be 1000 still
System.out.println("Balance : "+balance); //Where is this balance declared?
myAccount.withdraw(100.00); //Note the balance will be 1000 still
System.out.println("the Sam account balance is ,"+balance); //Again this isn't declared.
}
}
In short, your deposit&withdraw methods won't do what you want todo (there are probably the cause of the uninitlised variable messages.) Balance shouldn't be public, you need an accessor method on it.
Rewrite those bits and it should work.
#4
Posted 18 June 2010 - 07:08 AM
i got most of the parts but i need that the methods remain of the type 'void' ....then how do i make them return a value???i wrote the code this way:
public class BankAccount
{
double balance;
String name;
public BankAccount(double bal,String nam)
{
balance=bal;
name=nam;
}
public void deposit(double dep)
{
double bal=balance+dep;
System.out.println("balance : "+bal);
}
public void withdraw(double out)
{
double bal=balance-out;
System.out.println("balance :"+bal);
}
}
class Tester
{
public static void main(String[]args)
{
BankAccount myAccount=new BankAccount(1000.00,"Sam");
System.out.println(myAccount.deposit(505.22));
System.out.println(myAccount.withdraw(100.00));
}
}
public class BankAccount
{
double balance;
String name;
public BankAccount(double bal,String nam)
{
balance=bal;
name=nam;
}
public void deposit(double dep)
{
double bal=balance+dep;
System.out.println("balance : "+bal);
}
public void withdraw(double out)
{
double bal=balance-out;
System.out.println("balance :"+bal);
}
}
class Tester
{
public static void main(String[]args)
{
BankAccount myAccount=new BankAccount(1000.00,"Sam");
System.out.println(myAccount.deposit(505.22));
System.out.println(myAccount.withdraw(100.00));
}
}
#5
Posted 18 June 2010 - 10:19 AM
Methods of type void do NOT return a value, by definition. Also, you're STILL declaring new variables in your methods for no apparent reason. Why?
#6
Posted 18 June 2010 - 07:54 PM
actually i need to find the balance after an amount has been deposited and withdrawn. so i need my methods to return a value.but the problem is i have been asked to do so with the methods returning nothing.And i am trying to check the programs accuracy through another class by passing some values to the methods ...so i think i need to declare those variables...after this still i am left with no sulution...huh:cursing:
#7
Posted 19 June 2010 - 04:10 AM
1) do NOT declare new variables in your methods. Anything you do with those will not survive once method execution ends.
2) You will need to create new methods to return the current balance (or the default toString method).
2) You will need to create new methods to return the current balance (or the default toString method).
#8
Posted 19 June 2010 - 01:57 PM
Here i will help you with one of your methods to help you understand. We will fix this method:
When you initialize "double bal=balance+dep" it will be gone forever once it hits the end of the method block "}". You have a "balance" variable in your class already that you declared so you probably want to use it like so:
Ananta2010 said:
public void deposit(double dep)
{
double bal=balance+dep;
System.out.println("balance : "+bal);
}public void deposit(double dep){
balance=balance+dep; // balance is the class variable so we can use it without initializing a new variable
System.out.println("balance: "+balance);
}Now your class variable balance has the balance + the deposit. Now if you want to return the balance every time you deposit instead of printing it out inside the method you can do something like this:public double deposit(double dep){ // notice the "double" instead of the "void" in the method declaration. This is the return type.
balance+=dep; // this is the exact same thing as "balance=balance+dep;"
return balance; // this will return the balance
}Now that the method returns a double when you make the call "System.out.println(myAccount.deposit(505.22));" it will print out 1505.22. Hope this helps.
twas brillig
#9
Posted 21 June 2010 - 05:53 AM
If i'm not mistaken, Ananta want a void method that will do a return, but functionally that is impossible. However, within that particular method you can have it evoke another method that doesn't have a void signature and do the return operation.
#10
Posted 21 June 2010 - 08:14 AM
You need to think about data encapsulation in the object. I think these particular constrants might have been imposed to force you to do this.
The object itself must contain the balance, this should not be public. That is no outsider can see it. So in order to look at the balance you need to provide a (read-only) method to access it. So this would copy the current balance and return it to the caller. Then you don't need to use withdraw or deposit methods to look at the balance.
The object itself must contain the balance, this should not be public. That is no outsider can see it. So in order to look at the balance you need to provide a (read-only) method to access it. So this would copy the current balance and return it to the caller. Then you don't need to use withdraw or deposit methods to look at the balance.


Sign In
Create Account

Back to top









