Computer Applications
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.
Java
Encapsulation & Inheritance in Java
21 Likes
Answer
import java.util.Scanner;
public class Salary
{
private int basic;
public void input() {
Scanner in = new Scanner(System.in);
System.out.print("Enter the basic pay: ");
basic = in.nextInt();
}
public void display() {
double da = basic * 0.3;
double hra = basic * 0.1;
double gross = basic + da + hra;
System.out.println("da=" + da);
System.out.println("hra=" + hra);
System.out.println("gross=" + gross);
}
public static void main(String args[]) {
Salary obj = new Salary();
obj.input();
obj.display();
}
}Variable Description Table
Program Explanation
Output

Answered By
9 Likes
Related Questions
Write a program by using a class with the following specifications:
Class name — Prime
Data members — private int n
Member functions:
- void input() — to input a number
- void checkprime() — to check and display whether the number is prime or not
Use a main function to create an object and call member methods of the class.
Write a program by using a class with the following specifications:
Class name — Factorial
Data members — private int n
Member functions:
- void input() — to input a number
- void fact() — to find and print the factorial of the number
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 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