我有两种方法
第一种方法具有对象作为返回类型,而
第二种方法具有整数作为返回类型。 />
我在第一种方法中有一些值,在第二种方法中有一些值
我要做的就是将所有这些值传递给单个对象。
但是问题是只有第一种方法的值正在传递,而第二种方法的值却没有传递。
我正在练习封装
下面是两个文件和OUTPUT的代码。
//Employee.java file
import java.util.*;
class Employee
{
private int employeeId;
private String employeeName;
private double salary;
private double netSalary;
/*public Employee()
{
}*/
public void setEmployeeId(int a)
{
employeeId = a;
}
public void setEmployeeName(String b)
{
employeeName = b;
}
public void setSalary(double c)
{
salary=c;
}
public int getEmployeeId()
{
return employeeId;
}
public String getEmployeeName()
{
return employeeName;
}
public double getSalary()
{
return salary;
}
public void calculateNetSalary(int pfpercentage)
{
netSalary = salary-((salary*pfpercentage)/100);
}
public double getNetSalary()
{
return netSalary;
}
}
import java.util.*;
class Main
{
public static Employee getEmployeeDetails(Employee e)//1st Method
{
Scanner sc=new Scanner (System.in);
try
{
System.out.println("Enter Id:");
e.setEmployeeId(sc.nextInt());
sc.nextLine();
System.out.println("Enter Name:");
e.setEmployeeName(sc.nextLine());
System.out.println("Enter salary:");
e.setSalary(sc.nextDouble());
sc.nextLine();
}catch(Exception e1){System.out.println("Invalid Input");}
return e;
}
public static int getPFPercentage()//2nd Method
{
Employee e = new Employee();
int pf=0;
Scanner sc1=new Scanner(System.in);
try
{
System.out.println("Enter PF percentage:");
pf=sc1.nextInt();
e.calculateNetSalary(pf);
}catch(Exception e1){System.out.println("Invalid Input");}
return pf;
}
public static void main(String args[])
{
Employee e = new Employee();
getEmployeeDetails(e);
getPFPercentage();
System.out.println();
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());
}
}
OUTPUT
Enter Id:
101
Enter Name:
Harry
Enter salary:
20000
-------------1st method is used to take above input
Enter PF percentage:
7
-------------2nd method is used to take only PF percentage input
Id : 101
Name : Harry
Salary : 20000.0
Net Salary : 0.0
净工资的计算在文件Employee.java函数名称“ calculateNetSalary”中
答案 0 :(得分:0)
在调用方法netSalary
之前尚未计算getNetSalary()
;
public void calculateNetSalary(int pfpercentage) //This function should be called first.
{
netSalary = salary-((salary*pfpercentage)/100);
}
public double getNetSalary()
{
return netSalary;
}
请看下面的代码以更好地理解:
System.out.println("Salary : "+e.getSalary());
e.calculateNetSalary(5); //Add this line and you will get the required output.
System.out.println("Net Salary : "+e.getNetSalary());
答案 1 :(得分:0)
在“ getPFPercentage”方法中,您正在创建一个新对象,但在main方法中,您正在使用另一个对象。
将在main方法中创建的对象传递给getPFPercentage方法
将您的getPFPercentage
签名更改为
'public static int getPFPercentage(Employee e)'
通过getPFPercentage
方法删除对象创建行
并从main方法调用此方法 -getPFPercentage(e)
答案 2 :(得分:0)
问题在于您正在创建新的Employee对象的getPFPercentage()方法内部。您应该使用在main方法中创建的相同对象。
public static int getPFPercentage(Employee e)//2nd Method
{
int pf=0;
Scanner sc1=new Scanner(System.in);
try
{
System.out.println("Enter PF percentage:");
pf=sc1.nextInt();
e.calculateNetSalary(pf);
}catch(Exception e1){System.out.println("Invalid Input");}
return pf;
}
public static void main(String args[])
{
Employee e = new Employee();
getEmployeeDetails(e);
getPFPercentage(e);
System.out.println();
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());
}