Ask Question
3 February, 17:56

Assume the existence of an interface, Account, with the following methods: deposit: accepts an integer parameter and returns an integer withdraw: accepts an integer parameter and return a boolean Define a class, BankAccount, that implements the above interface and has the following members: an instance variable named balance a constructor that accepts an integer that is used to initialize the instance variable an implementation of the deposit method that adds its parameter to the balance variable. The new balance is returned as the value of the method. an implementation of the withdraw method that checks whether its parameter is less than or equal to the balance and if so, decreases the balance by the value of the parameter and returns true; otherwise, it leaves the balance unchanged and returns false.

+2
Answers (1)
  1. 3 February, 19:40
    0
    The code to this question can be given as:

    Code:

    public class BankAccount implements Account / /define class that inherit interface Account.

    {

    private int balance; / /define integer variable.

    public BankAccount (int x) / /define parameterized constructor.

    {

    balance = x;

    }

    public int deposit (int x) / /define function deposit.

    {

    balance = balance + x;

    return balance;

    }

    public boolean withdraw (int x) / /define function withdraw.

    {

    //conditional statements

    if (x < = balance) / /if block

    {

    balance = balance - x; / /calculate value.

    return true; / /return value.

    }

    else / /else block

    {

    return false; / /return value

    }

    }

    }

    Explanation:

    The description of the above code can be given as:

    In this code we define a class that is "BankAccount" this class inherits the interface that is "Account". for inheriting the class to the interface we use implements keyword to inherit class to interface. In this class we also define an integer variable that is balance. Then we define a parameterized constructor. In this constructor, we pass an integer variable that is "x" as a parameter. In this constructor, we use the balance variable to hold the constructor parameter value. Then we define a function that is a deposit () and withdraw (). In both functions, we pass an integer variable that is "x". In the deposit () function we use a balance variable for calculating value and return its value. and in the withdraw () function we use conditional statement. In this, we check if function parameter value that is x is less then equal to balance so we calculate the balance and return true. else we return false.
Know the Answer?