Task Description
I have this problem statement: Create a class Employee with the following private member variables.
int employeeId
String employeeName
double salary
double netSalary
Include appropriate getters and setters method in Employee class. Write the following method in the Employee class: public void calculateNetSalary(int pfpercentage) - This method should take PF percentage as argument. Deduct the PF amount from the salary and set the netSalary.
Create a Main class which has the main method which invokes the method to get the input and prints the details as shown in the sample.
Also write a method :
public static Employee getEmployeeDetails() - which gets the employee details - id, name and salary, and returns the employee object.
public static int getPFPercentage() - which gets the PF percentage and returns the same
In the main method invoke the above two methods, and then call the calculateNetSalary method in Employee class and print the output as shown below.
Sample Input 1:
Enter Id: 101 Enter Name: Vivek Enter salary: 20000 Enter PF percentage: 7
Sample Output 1:
Id : 101
Name : Vivek
Salary : 20000.0
Net Salary : 18600.0
What I have done
I wrote the getter & setters methods and calculateNetSalary() method in Employee.java. I am stuck in as what and how should I write in Main.java
Employee.java
public class Employee{
private int employeeId;
private String employeeName;
private double salary;
private double netSalary;
//setters
public void setEmployeeId(int employeeId){
this.employeeId=employeeId;
}
public void setEmployeeName(String employeeName){
this.employeeName=employeeName;
}
public void setSalary(double salary){
this.salary=salary;
}
public void netSalary(double netSalary){
this.netSalary=netSalary;
}
//getters
public int getEmployeeId(){
return employeeId;
}
public String getEmployeeName(){
return employeeName;
}
public double getSalary(){
return salary;
}
public double getNetSalary(){
return netSalary;
}
public void calculateNetSalary(int pfpercentage){
pfamount=salary*pfpercentage;
netSalary=salary-pfamount;
}
}
Main.java
import java.util.Scanner;
public class Main{
public staic void main(String[] args){
Scanner sc = new Scanner(System.in);
Employee emp = new Employee();
System.out.println("Enter Id:"+setEmployeeId(sc.nextInt()))
System.out.println("Enter Name:"+setEmployeeName(sc.next()));
System.out.println("Enter salary:"+setSalary(sc.nextDouble()));
System.out.println("Enter PF percentage:");
double pfpercentage = sc.nextDouble();
public static Employee getEmployeeDetails(){
}
public static int getPFPercentage(){
}
}
}
I am unable to complete Main.java as I am not sure what and how to write.
setEmployeeId(),setEmployeeName()andsetSalary()are methods of the Employee class, so they need to be called on an employee:emp.setEmployeeId(...). Don't try to print something, get an input, and set the employee ID in a single line. Those are three separate instructions, which should be on three separate lines: 1. print a message, 2. read the employee ID, 3. set the ID of the employee to what you have read at step 2. - JB Nizet