Computer Applications
Design a class to represent a bank account. Include the following members:
Data members
- Name of the depositor
- Account number
- Type of account
- Balance amount in the account
Methods
- To assign initial values
- To deposit an amount
- To withdraw an amount after checking balance
- To display the name and balance
- Do write proper constructor functions
Answer
class BankAccount
{
private String name;
private long accno;
private char type;
private double bal;
BankAccount() {
name = "";
accno = 0;
type = '\0';
bal = 0.0d;
}
BankAccount(String n, long accnumber, char t, double b) {
name = n;
accno = accnumber;
type = t;
bal = b;
}
public void deposit(double amt) {
bal = bal + amt;
System.out.println("Amount deposited: Rs. " + amt);
System.out.println("Balance: Rs. " + bal);
}
public void withdraw(double amt) {
if(amt <= bal) {
bal = bal - amt;
System.out.println("Amount withdrawn: Rs. " + amt);
System.out.println("Balance: Rs. " + bal);
}
else {
System.out.println("Insufficient Balance");
}
}
public void display()
{
System.out.println("Name: " + name);
System.out.println("Balance: Rs. " + bal);
}
}
Related Questions
When a compiler can automatically generate a constructor if it is not defined then why is it considered that writing constructors for a class is a good practice ?
'Accessibility of a constructor greatly affects the scope and visibility of their class.' Elaborate this statement.
List some of the special properties of the constructor functions.
What is a parameterized constructor ? How is it useful ?