Jump to content

please help me out with the code below

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
9 replies to this topic

#1
Ananta2010

Ananta2010

    Newbie

  • Members
  • Pip
  • 9 posts
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

#2
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
Why are you redeclaring balance in your deposit and withdraw methods?
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#3
abzero

abzero

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 217 posts
I've put some comments in line on your code.


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
Ananta2010

Ananta2010

    Newbie

  • Members
  • Pip
  • 9 posts
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));
}
}

#5
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
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?
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#6
Ananta2010

Ananta2010

    Newbie

  • Members
  • Pip
  • 9 posts
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
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
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).
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#8
tate

tate

    Learning Programmer

  • Members
  • PipPipPip
  • 90 posts
Here i will help you with one of your methods to help you understand. We will fix this method:

Ananta2010 said:

public void deposit(double dep)
{
double bal=balance+dep;
System.out.println("balance : "+bal);
}
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:
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
InfiniteRecursion

InfiniteRecursion

    Newbie

  • Members
  • Pip
  • 5 posts
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
abzero

abzero

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 217 posts
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.