使用以下私有成员变量创建Employee类。
int employeeId
String employeeName
double salary
double netSalary
在Employee类中包括适当的getter和setters方法。在Employee类中编写以下方法:
public void calculateNetSalary(int pfpercentage) // This method should take PF percentage as argument. Deduct the PF amount from the salary and set the netSalary.
创建一个Main类,该类具有main方法,该方法调用该方法以获取输入并打印详细信息,如示例中所示。
还要编写一个方法:
public static Employee getEmployeeDetails() // which gets the employee details and returns the employee object.
public static int getPFPercentage() // which gets the PF percentage and returns the same
我已经编写了这段代码。输出正确,但是显示一些错误,如何解决?
public class Employee {
private int employeeId;
private String employeeName;
private double salary;
private double netSalary;
public static Employee instances= new Employee();
public int getEmployeeId() {
return employeeId;
}
public String getEmployeeName() {
return employeeName;
}
public double getSalary() {
return salary;
}
public double getNetSalary() {
return netSalary;
}
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 setNetSalary(double netSalary) {
this.netSalary = netSalary;
}
public void calculateNetSalary(int pfpercentage) {
instances.netSalary=(instances.salary)*(1-(((double)pfpercentage)/100));
}
public static Employee getEmployeeDetails(int id, String name,double salary) {
instances.setEmployeeId(id);
instances.setEmployeeName(name);
instances.setSalary(salary);
return instances;
}
public static int getPFPercentage(int pfpercentage)
{ instances.calculateNetSalary(pfpercentage);
' return pfpercentage; '
}
}'
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter id");
int id=sc.nextInt();
System.out.println("Enter name");
sc.nextLine();
String name=sc.nextLine();
System.out.println("Enter salary");
double salary=sc.nextDouble();
Employee e1= Employee.getEmployeeDetails( id, name, salary );
System.out.println("Enter pfpercentage");
Employee.getPFPercentage(sc.nextInt());
System.out.println("Id :"+e1.getEmployeeId());
System.out.println("name :"+e1.getEmployeeName());
System.out.println("salary :"+e1.getSalary());
System.out.println("Net salary :"+e1.getNetSalary());
}
}
FAIL-1:
check the availaibility of getEmployeeDetails() in main method or check wether the signature(returntype/argument/acessspecifier/methodname)of the of the method getEmployeeDetails is correct.
check the availaibility of getPFPercentage() in main method or check wether the signature(returntype/argument/acessspecifier/methodname)of the of the method getPFPercentage() is correct
FAIL-1: check the logic of calculateNetSalary
FAIL-2: check the method of getPFPercentage
FAIL-2: check the method of getEmployeeDetails
答案 0 :(得分:1)
public class Employee {
private int employeeId;
private String employeeName;
private double salary;
private double netSalary;
public int getEmployeeId() {
return employeeId;
}
public String getEmployeeName() {
return employeeName;
}
public double getSalary() {
return salary;
}
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 calculateNetSalary(int pf) {
double temp=(pf/100.00);
setNetSalary(salary-(salary*(temp)));
}
public void setNetSalary(double netSalary) {
this.netSalary=netSalary;
}
public double getNetSalary() {
return netSalary;
}
}
public class Main{
public static Employee getEmployeeDetails(){
Scanner sc=new Scanner(System.in);
Employee e=new Employee();
System.out.println("Enter Id:");
e.setEmployeeId(sc.nextInt());
System.out.println("Enter Name:");
e.setEmployeeName(sc.next());
System.out.println("Enter salary:");
e.setSalary(sc.nextDouble());
return e;
}
public static int getPFPercentage(){
Scanner sc=new Scanner(System.in);
System.out.println("Enter PF percentage:");
int val=sc.nextInt();
return val;
}
public static void main(String arg[]){
Scanner sc=new Scanner(System.in);
Employee e=getEmployeeDetails();
int temp=getPFPercentage();
e.calculateNetSalary(temp);
System.out.println("Id : "+e.getEmployeeId());
System.out.println("Name : "+e.getEmployeeName());
System.out.println("Salary : "+e.getSalary());
System.out.println("Net Salary : "+e.getNetSalary());
}
}
答案 1 :(得分:0)
尝试此代码。
class Employee {
private int employeeId;
private String employeeName;
private double salary;
private double netSalary;
public Employee(int employeeId, String employeeName, double salary) {
this.employeeId = employeeId;
this.employeeName = employeeName;
this.salary = salary;
}
public int getEmployeeId() {
return employeeId;
}
public String getEmployeeName() {
return employeeName;
}
public double getSalary() {
return salary;
}
public double getNetSalary() {
return netSalary;
}
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 setNetSalary(double netSalary) {
this.netSalary = netSalary;
}
public void calculateNetSalary(double pfpercentage) {
this.netSalary = this.salary * (1 - (pfpercentage / 100));
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter id");
int id = sc.nextInt();
System.out.println("Enter name");
sc.nextLine();
String name = sc.nextLine();
System.out.println("Enter salary");
double salary = sc.nextDouble();
Employee e1 = new Employee(id, name, salary);
System.out.println("Enter pfpercentage");
e1.calculateNetSalary(sc.nextDouble());
System.out.println("Id :" + e1.getEmployeeId());
System.out.println("name :" + e1.getEmployeeName());
System.out.println("salary :" + e1.getSalary());
System.out.println("Net salary :" + e1.getNetSalary());
}
在您的代码中,您已声明一个静态Employee对象,并且还具有默认的构造函数。这不是一个好的编程习惯。类的一般目的是创建多个对象。如果需要一个对象,则应使用单例设计模式。
https://www.javatpoint.com/singleton-design-pattern-in-java
public static Employee instance;
public static Employee getInstance(){
if(instance != null){
instance = new Employee();
}
return instance;
}
//private constructor
private Employee(){};
我不了解此方法的目的。方法名称显示为getPFPercentage,而您的参数也是pfpercentage。
public static int getPFPercentage(int pfpercentage)
{ instances.calculateNetSalary(pfpercentage);
' return pfpercentage; '
}
我认为上述方法是多余的。我们可以调用calculateNetSalary方法,并通过其getter检索计算出的净工资。