6.3k views
0 votes
Consider the following class, which models a bank account. The deposit method is intended to update the account balance by a given amount; however, it does not work as handed public class BankAccount 1 private String accountOwner Name: private double balance private int account number public BankAccount(String nane, double Initialbalance, Int acetum) account Owner Name - name: balance initialBalance account Number - acetum; ) public void deposit (double amount) double balance - balance + amount; 1 3 What is the best explanation of why the deposit method does not work as intended? O The variable balance must be padded to the deposit method. The deposit method must have a return statement. In the deposit method, the variable balance should be replaced by the variable initialbalance. In the deposit method, the variable balance is declared as a local variable and is different from the instance variable balance

1 Answer

2 votes

Answer:

the variable balance is declared as a local variable and is different from the instance variable balance.

Step-by-step explanation:

In this code, the deposit method is not working as intended because the variable balance is declared as a local variable and is different from the instance variable balance. This is occurring due to the keyword double being placed before the balance variable within the deposit method. This is basically creating a new variable called balance that is only targetable by the local scope within the deposit method. Removing this keyword would instead target the class instance variable balance which is what is needed. In this case, the balance local variable being created is always null and would cause the method to not function.

User BryanOfEarth
by
6.6k points