KnowledgeBoat Logo
|

Computer Applications

Write a program to use a class Account with the following specifications:

Class name — Account

Data members — int acno, float balance

Member Methods:

  1. Account (int a, int b) — to initialize acno = a, balance = b
  2. void withdraw(int w) — to maintain the balance with withdrawal (balance - w)
  3. void deposit(int d) — to maintain the balance with the deposit (balance + d)

Use another class Calculate which inherits from class Account with the following specifications:

Data members — int r,t ; float si,amt;

Member Methods:

  1. void accept(int x, int y) — to initialize r=x,t=y,amt=0
  2. void compute() — to find simple interest and amount
    si = (balance * r * t) / 100;
    a = a + si;
  3. void display() — to print account number, balance, interest and amount

main() function need not to be used

Java

Encapsulation & Inheritance in Java

30 Likes

Answer

public class Account
{
    protected int acno;
    protected float balance;
    
    public Account(int a, int b) {
        acno = a;
        balance = b;
    }
    
    public void withdraw(int w) {
        balance -= w;
    }
    
    public void deposit(int d) {
        balance += d;
    }
}
public class Calculate extends Account
{
    private int r;
    private int t;
    private float si;
    private float amt;
    
    public Calculate(int a, int b) {
        super(a, b);
    }
    
    public void accept(int x, int y) {
        r = x;
        t = y;
        amt = 0;
    }
    
    public void compute() {
        si = (balance * r * t) / 100.0f;
        amt = si + balance;
    }
    
    public void display() {
        System.out.println("Account Number: " + acno);
        System.out.println("Balance: " + balance);
        System.out.println("Interest: " + si);
        System.out.println("Amount: " + amt);
    }
}

Variable Description Table

Program Explanation

Output

BlueJ output of Write a program to use a class Account with the following specifications: Class name — Account Data members — int acno, float balance Member Methods: (a) Account (int a, int b) — to initialize acno = a, balance = b (b) void withdraw(int w) — to maintain the balance with withdrawal (balance - w) (c) void deposit(int d) — to maintain the balance with the deposit (balance + d) Use another class Calculate which inherits from class Account with the following specifications: Data members — int r,t ; float si,amt; Member Methods: (d) void accept(int x, int y) — to initialize r=x,t=y,amt=0 (e) void compute() — to find simple interest and amount si = (balance * r * t) / 100; a = a + si; (f) void display() — to print account number, balance, interest and amount main() function need not to be used

Answered By

11 Likes


Related Questions