Computer Applications
Define a class Benefit, described as below:
Data Members:
Salary, Bonus and Amount (double), Exp (float)
Member Methods:
Benefit (double, float) — Parameterized constructor to initialize Salary and Exp
Evaluate() — To evaluate the value of Amount using the formula:
Amount = Salary + Bonus, based on the experience of the employee:
| Experience in years | Bonus |
|---|---|
| More than 3 but less than 5 | 10% of Salary |
| More than equal to 5 but less than 10 | 15% of Salary |
| More than equal to 10 but less than 20 | 20% of Salary |
| More than equal to 20 | 30% of Salary |
Display () - To print all the data members.
Write a main method to create an object of the class Benefit and call the above methods.
Java
Java Constructors
3 Likes
Answer
import java.util.Scanner;
public class Benefit
{
double salary;
double bonus;
double amount;
float exp;
public Benefit(double s, float e) {
salary = s;
exp = e;
}
public void evaluate() {
if (exp <= 3)
bonus = 0;
else if (exp < 5)
bonus = 10.0 / 100.0 * salary;
else if (exp < 10)
bonus = 15.0 / 100.0 * salary;
else if (exp < 20)
bonus = 20.0 / 100.0 * salary;
else
bonus = 30.0 / 100.0 * salary;
amount = salary + bonus;
}
public void display() {
System.out.println("Experience: " + exp);
System.out.println("Salary: " + salary);
System.out.println("Bonus: " + bonus);
System.out.println("Amount: " + amount);
}
public static void main (String [] args) {
Scanner in = new Scanner(System.in);
System.out.print("Enter Salary: ");
double sal = in.nextDouble();
System.out.print("Enter Experience: ");
float ex = in.nextFloat();
Benefit obj = new Benefit(sal, ex);
obj.evaluate();
obj.display();
}
}Output

Answered By
1 Like