84.7k views
5 votes
Consider the following class, which uses the instance variable balance to represent a bank account balance.

public class BankAccount {

private double balance;


public double deposit(double amount) {

/* missing code */

}

}
The deposit method is intended to increase the account balance by the deposit amount and then return the updated balance. Which of the following code segments should replace /* missing code */ so that the deposit method will work as intended?
a.
amount = balance + amount;
return amount;
b.
balance = amount;
return amount;
c.
balance = amount;
return balance;
d.
balance = amount;, , return balance;,
balance = balance + amount;
return amount;
e.
balance = balance + amount;
return balance;

1 Answer

0 votes

Answer:

e.

balance = balance + amount;

return balance;

Step-by-step explanation:

Required

Code to update account balance and return the updated balance

The code that does this task is (e).

Assume the following:


balance = 5000; amount = 2000;

So, code (e) is as follows:

balance = balance + amount;
\to


balance = 5000 + 2000
\to


balance = 7000

Here, the updated value of balance is 7000

So: return balance

will return 7000 as the updated account balance

Other options are incorrect

User Finiteautomata
by
3.5k points