008-002-004

Complex Object Operations: Execute Methods Integrating Arguments, Return Values, and State Management in Bank Account Class

Hard

Problem Description

In this problem, you implement BankAccount operations as stateless methods. All data (balance, rate, amount) is passed as parameters, and computed results are returned as return values.

Explanation: Method Design with Parameters and Return Values

In this problem, you learn to write pure methods that take input as parameters and return computed results.

Key Learning Points

  1. State via parameters: Receive balance and rate as arguments
  2. Double calculations and return values: Return computed interest and balance
  3. Conditional return values: Return status string based on balance
  4. Stateless design: No instance variables; same inputs always produce same outputs

Code Example Explanation

public double calculateInterest(double balance, double rate) {
    if (rate <= 0 || rate > 1) {
        System.out.println("Error: Invalid interest rate");
        return 0;
    }
    return balance * rate;  // Return interest amount
}

This method receives balance and rate as arguments, computes the interest amount, and returns it. No state is stored in the class.

Status Determination Example

public String getAccountStatus(double balance) {
    if (balance >= 1000) {
        return "Premium";
    } else if (balance >= 500) {
        return "Standard";
    } else {
        return "Basic";
    }
}

What You Learned

In stateless method design, all required data is passed as arguments and only the computed result is returned. Because the same inputs always yield the same result, these methods are easy to test and reason about.

Ready to Try Running Code?

Log in to access the code editor and execute your solutions for this problem.

Don't have an account?

Sign Up