We read your photo as:

Which of the following design strategies most effectively preserves data integrity in a BankAccount class?

Computer Science

ScanSolve AI Answer

Here's an analysis of each design strategy:

  • A. Make the balance field public and allow direct modification.
    • This approach allows any part of the program to change the balance directly, without any checks or validation. This makes it very easy to introduce errors or invalid states (e.g., a negative balance without a proper overdraft mechanism), thus compromising data integrity.
  • B. Permit unrestricted changes to balance by external users.
    • This is similar to option A. "Unrestricted changes" means there are no controls or rules governing how the balance can be altered, which directly violates the principle of data integrity.
  • C. Store balance in a global variable accessible across the program.
    • Using a global variable for something as critical as a bank account balance is a poor design choice. It makes the balance susceptible to modification from anywhere in the program, making it difficult to track changes, enforce rules, and maintain data integrity.
  • D. Make balance private and use methods to update.
    • This strategy employs encapsulation, a core principle of object-oriented programming.
      • Making the balance field private restricts direct access from outside the BankAccount class.
      • Using public methods (e.g., deposit(amount), withdraw(amount)) to modify the balance means that all changes must pass through these controlled methods. Inside these methods, you can implement validation logic (e.g., ensuring deposit amounts are positive, checking for sufficient funds before a withdrawal). This ensures that the balance always remains in a valid and consistent state, thereby preserving data integrity.

Therefore, making the balance private and using methods to update it is the most effective strategy for preserving data integrity.

The final answer is D\boxed{\text{D}}.

Was this helpful?

Still stuck on this one?

Ask a follow-up, and the answer starts from this question.

Got a different question?Ask your own question
Handwritten step-by-step solution preview

Handwritten Step-by-Step Solution

Get this as a handwritten image on WhatsApp — like a tutor wrote it for you.

Try on WhatsApp