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
Related Questions
The basic salary of employees is undergoing a revision. Define a class called Grade_Revision with the following specifications:
Data Members Purpose String name to store name of the employee int bas to store basic salary int expn to consider the length of service as an experience double inc to store increment double nbas to store new basic salary (basic + increment) Member Methods Purpose Grade_Revision() constructor to initialize all data members void accept() to input name, basic and experience void increment() to calculate increment based on experience as per the table given below void display() to print all the details of an employee Experience Increment Up to 3 years ₹1,000 + 10% of basic 3 years or more and up to 5 years ₹3,000 + 12% of basic 5 years or more and up to 10 years ₹5,000 + 15% of basic 10 years or more ₹8,000 + 20% of basic Write the main method to create an object of the class and call all the member methods.
Consider the given program and answer the questions given below:
class temp { int a; temp() { a=10; } temp(int z) { a=z; } void print() { System.out.println(a); } void main() { temp t = new temp(); temp x = new temp(30); t.print(); x.print(); } }
(a) What concept of OOPs is depicted in the above program with two constructors?
(b) What is the output of the method main()?
Name the following:
(a) Method with the same name as of the class and is invoked every time an object is created.
(b) Keyword to access the classes of a package.Why do we need a constructor as a class member?