Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Help with Assignment

  1. #1
    Junior Member
    Join Date
    Oct 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Help with Assignment

    This is a Java program consisting of two classes. The basic idea is a bank account. I am having trouble calling a method called 'mystery' from the Account to the Tester. Program being used is BlueJ, if that helps to explain the code layout.

    Account:

    public class BankAccount
    {  
       private double balance;
     
       public BankAccount()
       {   
          balance = 0;
       }
       public BankAccount(double initialBalance)
       {   
          balance = initialBalance;
       }
       public void deposit(double amount)
       {  
          double newBalance = balance + amount;
          balance = newBalance;
       }
       public void withdraw(double amount)
       {   
          double newBalance = balance - amount;
          balance = newBalance;
       }
       public double getBalance()
       {   
          return balance;
       }
       public void mystery(BankAccount that, double amount)
       {
           this.balance = this.balance - amount;
           that.balance = that.balance + amount;
        }
    }
    Tester:

    public class BankAccountTester
    {
    public static void main(String[] args)
       {
          //Initial balance
          BankAccount bankAcct = new BankAccount(10000);
          System.out.println("Balance should be equal to: 10000");
          System.out.println("Initial Balance: " + bankAcct.getBalance());
          //Withdraw 200
          bankAcct.withdraw(2000);
          System.out.println("Balance should be equal to: 8000");
          System.out.println("Actual balance: " + bankAcct.getBalance());
          //Deposit 100
          bankAcct.deposit(100);
          System.out.println("Balance should be equal to: 8100");
          System.out.println("Actual balance: " + bankAcct.getBalance());
          //Withdraw 1000
          bankAcct.withdraw(1000);
          System.out.println("Balance should be equal to: 7100");
          System.out.println("Actual balance: " + bankAcct.getBalance());
          //'mystery' method result
     
          System.out.println("Mystery Method result: ");
       }
    }

    Thanks in advance for any help. I'm a Web Design major that somehow ended up in a programming class, so it's a bit over my head and I just need a little guidance now and then.


  2. #2
    Member
    Join Date
    Jul 2011
    Posts
    33
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Help with Assignment

    Start commenting out lines of code from main program until it runs. Then try to uncomment the last line you commented and try to figure out what you did wrong.

    Maybe it's unfair,but when I see a whole broken program delivered to me with the demand "fix it", it kind of feels like I'm dealing with a toddler.

    Perhaps you can contact classmates and make a study group where you collectively try to figure out what's wrong in each other's code.

    Good luck

  3. #3
    Junior Member
    Join Date
    Oct 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help with Assignment

    The program as it is written compiles and runs with the correct results (for deposit, withdraw, balance, etc). I am not asking you to 'fix it' so much as help me figure out how to make the 'mystery' method show a result via the Tester that can be shown with a println.

    The ones I have tried in Tester to run it are as follows (showing underneath the // explanation for mystery):
    bankAcct.this();
    bankAcct.that();
    bankAcct.mystery();

    The class itself consists of seven people, all of which are confused how to do exactly what I am asking. The only clue our teacher has given is: Private instance variables can only be accessed by methods of the same class.

  4. #4
    Junior Member
    Join Date
    Oct 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help with Assignment

    Ah, I get what your asking!
    Learning Java my self so was interested to see what would happen..

    The signature for mystery() is:

    public void mystery(BankAccount that, double amount)

    So you need to pass it a BankAccount and a value.

    try:

    bankAcct.mystery(bankAcct, 100.00);

    though that doesn't explain what it does!

    Terry

Similar Threads

  1. assignment troubles polymorphism (guide for assignment included)
    By tdawg422 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 8th, 2011, 11:01 AM
  2. please help me in my assignment :(
    By asdfg in forum What's Wrong With My Code?
    Replies: 5
    Last Post: May 18th, 2010, 08:59 AM
  3. need help on an assignment :(
    By gamfreak in forum What's Wrong With My Code?
    Replies: 6
    Last Post: February 23rd, 2010, 04:20 PM
  4. Replies: 1
    Last Post: February 22nd, 2010, 08:20 AM
  5. Need help with assignment
    By TonyL in forum Loops & Control Statements
    Replies: 2
    Last Post: February 20th, 2010, 09:44 PM