Explanation
This Java code demonstrates the fundamental concepts of Object-Oriented Programming (OOP), specifically classes and objects. It's designed to manage employee information, including their ID, name, and salary.
Here's a step-by-step breakdown:
-
EmployeeClass:- This is a blueprint for creating employee objects. It defines the characteristics (data members or attributes) and behaviors (methods or functions) that every employee will have.
- Attributes:
id: An integer to store the employee's unique identification number.name: A String to store the employee's name.basicSalary: A double to store the employee's base salary.
- Methods:
calculateSalaryWithBonus(): This method calculates the employee's salary after adding a 10% bonus to theirbasicSalary. It returns the calculated total salary.displayEmployeeDetails(): This method prints all the details of an employee object to the console, including their ID, name, basic salary, and the calculated salary with the bonus.inputEmployeeDetails(Scanner sc): This method is responsible for taking input from the user to populate the attributes (id,name,basicSalary) of anEmployeeobject. It uses aScannerobject (passed as an argument) to read data from the console.
-
progx4Class (Main Program):- This class contains the
mainmethod, which is the entry point of the program. mainMethod:- A
Scannerobject namedscis created to read user input from the console. - An
Employeeobject namedemp1is created using thenew Employee()statement. This creates an actual instance (an object) based on theEmployeeblueprint. - The program prompts the user to enter details for
emp1by calling theinputEmployeeDetails()method on theemp1object, passing thescobject. - Another
Employeeobject namedemp2is created. - The program prompts the user to enter details for
emp2by callinginputEmployeeDetails()on theemp2object. - Finally, the program displays the details for both
emp1andemp2by calling their respectivedisplayEmployeeDetails()methods. - The
Scannerobjectscis closed to release system resources.
- A
- This class contains the
In essence, the code defines a template for an employee and then uses that template to create two specific employee "instances," collects their information, and then displays that information in a formatted way.