KnowledgeBoat Logo
|

Computer Applications

Write a program by using a class with the following specifications:

Class name — Salary

Data members — private int basic

Member functions:

  1. void input() — to input basic pay
  2. 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

BlueJ output of Write a program by using a class with the following specifications: Class name — Salary Data members — private int basic Member functions: (a) void input() — to input basic pay (b) 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.

Answered By

9 Likes


Related Questions