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:
- Account (int a, int b) — to initialize acno = a, balance = b
- void withdraw(int w) — to maintain the balance with withdrawal (balance - w)
- 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:
- void accept(int x, int y) — to initialize r=x,t=y,amt=0
- void compute() — to find simple interest and amount
si = (balance * r * t) / 100;
a = a + si; - 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

Answered By
11 Likes
Related Questions
Write a program by using a class with the following specifications:
Class name — Salary
Data members — private int basic
Member functions:
- void input() — to input basic pay
- void display() — to find and print the following:
da = 30% of basic
hra = 10% of basic
gross = basic + da + hra
Use a main function to create an object and call member methods of the class.
Write a class program with the following specifications:
Class name — Matrix
Data members — int array m[][] with 3 rows and 3 columns
Member functions:
- void getdata() — to accept the numbers in the array
- void rowsum() — to find and print the sum of the numbers of each row
- void colsum() — to find and print the sum of numbers of each column
Use a main function to create an object and call member methods of the class.
Write a program by using class with the following specifications:
Class name — Sale
Data members/ Instance variables:
- String title, author,publication
- double price
Member methods:
- void input() — to accept title, author name and publication name and price of a book
- void display() — to display title, author name and publication name and price of a book
Now, create another class 'Purchase' that inherits class 'Sale' having the following specifications:
Class name — Purchase
Data members/ Instance variables:
- int noc
- int amount;
Member methods:
- void accept() — to enter the number of copies purchased
- void calculate( ) — to find the amount by multiplying number of copies ordered and price (i.e., noc * price)
- void show() — to display the elements describes in base class along with the number of copies purchased and amount to be paid to the shopkeeper
Write a program to define class with the following specifications:
Class name — Number
Data members/ Instance variables:
- int n — to hold an integer number
Member methods:
- void input() — to accept an integer number in n
- void display() — to display the integer number input in n
Now, inherit class Number to another class Check that is defined with the following specifications:
Class name — Check
Data members/ Instance variables:
- int fact
- int revnum
Member methods:
- void find() — to find and print factorial of the number used in base class
- void palindrome() — to check and print whether the number used in base class is a palindrome number or not
[A number is said to be palindrome if it appears the same after reversing its digits. e.g., 414, 333, 515, etc.]