Page 1 of 2 12 LastLast
Results 1 to 15 of 16

Programming help please! UPDATED!

This is a discussion on Programming help please! UPDATED! within the Off Topic forums, part of the Entertainment category; My friend is stuck with this...and the ko4life has lots of programmers (I think) Can you guys help us ? ...
Page: 1


  1. #1
    Xtr3m3
    Guest

    Default

    My friend is stuck with this...and the ko4life has lots of programmers (I think) Can you guys help us ?

    Much thanks is advance

    Friend quote:



    I created a class called Bank;
    And the corresponding testclass to work with the first class.
    But when i run the program it does not do what i want it to do.

    i want it to return the deposit amount. And apparently it returns some sort of default value, that is equal to zero.

    This is the class Bank:

    //create a class called Bank
    public class Bank
    {
    //Declaration of class: field variables
    private double balance;
    private String account;
    public double amount;
    public double newBalance;

    //build constructor
    public Bank(double initialBalance, String AccountNumber)
    {
    balance=initialBalance;
    account=AccountNumber;
    }

    //mutator methods: operate by redifining a given variable
    public void deposit(double amount)//this method needs to add to the balance
    {
    double newBalance=balance+amount;
    balance=newBalance;
    }
    public void withdraw(double amount)
    {
    double newBalance=balance-amount;
    balance=newBalance;
    }

    //accessor methods: operate via the return statement
    //accessing the balance
    public double getBalance()
    {
    return balance;
    }
    //accessing the account number
    public String getAccountNumber()
    {
    return account;
    }
    // need to build the getTransaction method??????
    public double getTransaction()
    {

    return amount;
    }

    }


    This is the testclass:

    //create testclass called "TestBank"
    class TestBank
    { //create main method:
    public static void main(String[] arg)
    { //create new object within the class "Bank" with reference variable "b"
    Bank b= new Bank(100,"098-055-9325");
    //print line with accessor methods retrieving the information via the return statement
    System.out.println("Account: "+b.getAccountNumber()+"\tOpening balance $"+b.getBalance());
    //make a deposit
    b.deposit(50);
    System.out.println("Deposit $"+b.getTransaction()+"\t\tNew balance $"+b.getBalance());

    }

    }

    This is the corresponding output of the program:

    Account: 098-055-9325 Opening balance $100.0
    Deposit $0.0 New balance $150.0

    Once again, i can successfully add the $50,0 to the account and fetch the new balance;
    but i cannot fetch correctly the deposit amount.

    This is where i am stuck.

  2. #2
    Senior Member VictoryAkara's Avatar
    Join Date
    Nov 2007
    Location
    Florida
    Posts
    2,361

    Default

    Hm, Weird - //make a deposit
    b.deposit(50);

    Tell's it that it had a deposit of 50, And your getting the correct after and it's not displaying... hm, Weird :/ I don't see anything wrong with the script however I am slightly tipsy so yea.

  3. #3
    Xtr3m3
    Guest

    Default

    Anybody else? :unsure:

    He says that when he prints it out is not displaying...it displays as 0

  4. #4
    Senior Member
    Join Date
    Mar 2006
    Location
    Middle Earth
    Posts
    1,017

    Default

    u got nothing that set amount

  5. #5
    Xtr3m3
    Guest

    Default

    Huh? Can you go into more details please I'm dumb with this

  6. #6
    Xtr3m3
    Guest

    Default

    UPDATED!

    I am having the problem with the following part of the program:

    // need to build the getTransaction method??????

    public double getTransaction()
    {
    return amount;
    }


    I need to build a method to return the "amount" that was just deposited;

    I am successfully depositing the amount, and fetching the new balance, but i cannot fetch the amount that i just added via the return statement. I think it is because amount is not a field, rather a parameter inside the a method, but i do not know how to fetch a parametric value rather than a field variable.

  7. #7
    evilwevel
    Guest

    Default

    add following lines (in red) :

    public void deposit(double amount)//this method needs to add to the balance
    {
    double newBalance=balance+amount;
    balance=newBalance;
    this.amount = amount;
    }
    public void withdraw(double amount) {
    double newBalance=balance-amount;
    balance=newBalance;
    this.amount = amount;
    }

    should work like a charm after that.

  8. #8
    Xtr3m3
    Guest

    Default

    add following lines (in red) :

    public void deposit(double amount)//this method needs to add to the balance
    {
    double newBalance=balance+amount;
    balance=newBalance;
    this.amount = amount;
    }
    public void withdraw(double amount) {
    double newBalance=balance-amount;
    balance=newBalance;
    this.amount = amount;
    }

    should work like a charm after that.[/b]
    OMG!!!!! It works great he says "That guy of those forums rocks!!!!! This is so sweet!!!!!"

    Thanks evilwevel

  9. #9
    evilwevel
    Guest

    Default

    glad i could help, wasn't a really complicated problem but i know how frustrating it can be to stare at the code while not knowing why it won't work

  10. #10
    Xtr3m3
    Guest

    Default

    EDIT:

    Thanks evilwevel for the help...disregard my pm as well

  11. #11
    Senior Member
    Join Date
    Mar 2006
    Location
    Middle Earth
    Posts
    1,017

    Default

    this is the simpleast form of java

    and yet.. i failed it 1st year

  12. #12
    evilwevel
    Guest

    Default

    this is the simpleast form of java

    and yet.. i failed it 1st year [/b]
    i'm about to go down the same road :lol:

    but this is indeed the very beginning of java no graphic interface whatsoever, just plain text.

  13. #13
    Senior Member ChaosFactor's Avatar
    Join Date
    Aug 2006
    Location
    Canada
    Posts
    332

    Default

    i'm about to go down the same road :lol:

    but this is indeed the very beginning of java no graphic interface whatsoever, just plain text.[/b]
    i work with the new ide 6.0 (or 6.whatever the new one w/ graphic interface)
    omg thats rough

    just trying 2 align a simple row of lbl/txtBox will make you cry

    VS ftw

  14. #14
    evilwevel
    Guest

    Default

    our shool teaches in 5.5 so i adapted to that.

    the hardest things we had to do so far was to establish a connection with a database and make it communicate. i forgot how many times i wanted to smash my screen, but it happened alot.

  15. #15
    Speed
    Guest

    Default

    any of you tried eclipse europa by any chance... its pretty good, and user friendly

Page 1 of 2 12 LastLast

Bookmarks

Posting Permissions

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