我有以下问题陈述: 使用以下私有成员变量创建Employee类。
int employeeId
String employeeName
double salary
double netSalary
在Employee类中包括适当的getter和setters方法。在Employee类中编写以下方法: public void computeNetSalary(int pfpercentage)-此方法应以PF百分比作为参数。从薪水中扣除PF金额并设置netSalary。
创建一个Main类,该类具有main方法,该方法调用该方法以获取输入并打印详细信息,如示例中所示。
还要编写一个方法:
公共静态Employee getEmployeeDetails()-获取雇员详细信息-id,姓名和薪水,并返回雇员对象。
public static int getPFPercentage()-获取PF百分比并返回相同的值
在main方法中,调用上述两个方法,然后在Employee类中调用calculateNetSalary方法,并打印输出,如下所示。
样本输入1:
输入ID: 101 输入名字: 维维克 输入薪水: 20000 输入PF百分比: 7
示例输出1:
Id:101
名称:Vivek
工资:20000.0
净薪金:18600.0
我在Employee.java中编写了getter&setters方法和calculateNetSalary()方法。我陷入了困境,我应该在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(){
}
}
}
我无法完成Main.java,因为不确定如何编写。
答案 0 :(得分:1)
这应该是您的代码:
public void main(String[] args){
Scanner sc = new Scanner(System.in);
Employee emp = new Employee();
emp.setEmployeeId(sc.nextInt());
emp.setEmployeeName(sc.next()) ;
emp.setSalary(sc.nextDouble());
System.out.println("Enter PF percentage:");
double pfpercentage = sc.nextDouble();
emp.calculateNetSalary(pfpercentage);
System.out.println("Salay is " + emp.getNetSalary());
}
还请注意,您尚未定义pfamount的类型:
public void calculateNetSalary(double pfpercentage){
double pfamount=salary*pfpercentage;
netSalary=salary-pfamount;
}
您不能在main()方法内定义其他方法。 您可以调用其中的其他方法(随意)。
答案 1 :(得分:0)
您的代码中有几个问题。首先查看 Employee.java ,有几个问题:
netSalary
的方法被声明为public void netSalary(double netSalary)
,而应该是public void SetNetSalary(double netSalary)
public void calculateNetSalary(int pfpercentage)
中的计算看起来不正确。如果您要传递双精度值(即2代表2%),则需要将其除以100,以将数字转换为百分比。pfamount
必须先声明为double
,然后才能为其赋值)。public String toString()
方法来打印出您的员工对象。所以您最终会得到这样的东西:
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 setNetSalary(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(double pfpercentage) {
double pfamount = salary * (pfpercentage / 100);
netSalary = salary - pfamount;
}
@Override
public String toString() {
String output = new StringBuffer()
.append("Id: ").append(employeeId)
.append(System.lineSeparator()).append("Name: ").append(employeeName)
.append(System.lineSeparator()).append("Salary: ").append(salary)
.append(System.lineSeparator()).append("Net Salary: ").append(netSalary).toString();
return output;
}
}
您的 Main.java 也有一些问题:
java.util.Scanner
... System.out.println("Enter Id:"+setEmployeeId(sc.nextInt()))
的正确方法。基本上,扫描仪部分需要与System.out
分开(请记住System.out
所做的只是打印出文本,它不等待扫描仪的输入)。getEmployeeDetails()
和getPFPercentage()
所以您最终会得到这样的东西:
import java.util.Scanner;
public class EmployeeSalaryCalculation {
private Scanner scanner;
public EmployeeSalaryCalculation() {
scanner = new Scanner(System.in);
}
public Employee getEmployeeDetails() {
Employee employee = new Employee();
System.out.println("Enter Id:");
employee.setEmployeeId(scanner.nextInt());
System.out.println("Enter Name:");
employee.setEmployeeName(scanner.next());
System.out.println("Enter salary:");
employee.setSalary(scanner.nextDouble());
return employee;
}
public double getPFPercentage(){
System.out.println("Enter PF percentage:");
return scanner.nextDouble();
}
public static void main(String[] args) {
EmployeeSalaryCalculation employeeSalaryCalculation = new EmployeeSalaryCalculation();
Employee employee = employeeSalaryCalculation.getEmployeeDetails();
employee.calculateNetSalary(employeeSalaryCalculation.getPFPercentage());
System.out.println(employee.toString());
}
}
答案 2 :(得分:0)
您已经有了一个好的开始。但是,存在一些语法错误,例如忘记用分号结束某些语句。另外,据我所知,Java不支持嵌套方法,因此您不应在getEmployeeDetails()
方法内部使用方法getPFPercentage()
和main
。我已经重新排列了代码以更正此问题。
我对您的Employee
类进行了其他更改,尤其是calculateNetSalary
方法。 pfPercentage
除以100后再乘以salary
。同样,在使用适当的setter方法计算实例变量netSalary
之后,将其设置为局部变量netSalary
。方法netSalary
也已重命名为setNetSalary
,因为它更能说明方法的作用。
除了根据规范完成Main
类之外,我没有对您的代码进行任何其他更改。如果代码中还有其他部分需要说明,您可以对此发表评论。
Main.java
import java.util.Scanner;
public class Main {
private static Scanner scanner = new Scanner(System.in);
public static void main(String[] args){
Employee newEmployee = getEmployeeDetails();
double pfPercentage = getPFPercentage();
System.out.println();
System.out.println("Confirm employee details: ");
System.out.println("Id : " + newEmployee.getEmployeeId());
System.out.println("Name : " + newEmployee.getEmployeeName());
System.out.println("Salary : " + newEmployee.getSalary());
newEmployee.calculateNetSalary(pfPercentage);
System.out.println("Net Salary : " + newEmployee.getNetSalary());
}
/**
* Gets the details of a new employee from user input
* @return the new {@link Employee}
*/
public static Employee getEmployeeDetails() {
Employee employee = new Employee();
System.out.println("Enter Id: ");
employee.setEmployeeId(scanner.nextInt());
System.out.println("Enter Name: ");
employee.setEmployeeName(scanner.next());
System.out.println("Enter salary: ");
employee.setSalary(scanner.nextDouble());
return employee;
}
/**
* Gets the PF percentage from user input
* @return the PF percentage
*/
public static double getPFPercentage(){
System.out.println("Enter PF percentage:");
double pfPercentage = scanner.nextDouble();
return pfPercentage;
}
}
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;
}
private void setNetSalary(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 (double pfPercentage){
double pfAmount = salary * (pfPercentage / 100);
double netSalary = salary - pfAmount;
this.setNetSalary(netSalary);
}
}