How to mock static method call by constructor?

Accessor Method - JAVA Programming?

  • I was asked to write a class that represents a bank account. The question is to write an accessor method so that the owner of the bank account can be viewed from another class. I have written the following codes but I am not sure how to write the accessor method. class Account { // Instance Variables // Money in the bank account, in dollars private double balance; // Full name of the person who owns the account private String owner; // Constructor /** * Creates a new Account belonging to 'name' */ public Account(String name) { owner = name; balance = 0; } // Instance Methods /** * Add 'amount' dollars into the account * If amount < 0, then nothing happens */ public void deposit(double amount) { if (amount >= 0) balance = balance + amount; } /** * Deduct 'amount' dollars from the account * If amount < 0, then nothing happens * If there is not enough money in the account, then nothing happens */ public void withdraw(double amount) { if (amount>= 0 && amount <= balance) balance = balance - amount; } public double getBalance() { return balance; } } After writing the accessor method, write another class which uses the above class. In the main method of the new class, do the following in this order: 1. Make two Account objects - each belongs to 2 different people. 2. Add some money into BOTH accounts. 3. Withdraw some money from ONE account. 4. For each account, print out onto the screen the owner's name and the amount of money in the account. You will need to call(use) the accessor methods in the previous class to do this. Check that the output makes sense. For example, if you created 2 accounts belonging to Adam and Betty, add $20 into both accounts, then withdraw $5 from Adam's account, the output should look something like this: Adam has $15.0 Betty has $20.0 The initial balance in both accounts are $0. Thank you very much!!!

  • Answer:

    You may think of an accessor method as a "get" method if you will. For example, if I have the simple Java class here: public class Foo { private int bar; public int getBar() { return bar; } } The accessor method is getBar(). I'm not going to do your homework for you, but this should point you in the right direction.

KelvinKC... at Yahoo! Answers Visit the source

Was this solution helpful to you?

Related Q & A:

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.