السلام عليكم ورحمة الله وبركاته :) والصلاة والسلام على نبينا محمد واله الطيبين الطاهرين
عضوة جديده ... لديها مشكله في الكود / أتمنى ايضاح خطأ هذا الكود في أقرب فرصه :)
وأعدكم بالمساعده في هذا المنتدى الجميل قدر الإستطاعه :)
الكود :
1. الكلاس الأول ..
public Emplooyee (String first ,String last, double HoursNumber
,double rate )
{
FirstName = first ; // intinal value for the variable FirstName
LastName = last ; //intinal value for the variable LastName
// initial value for the variable NumberOfHoursWorkedLastWeek
NumberOfHoursWorkedLastWeek = HoursNumber ;
HourlyRate = rate ;// initial value for the variable rate
}
public Emplooyee()
{
}
// done with the Construcor
Scanner input = new Scanner( System.in );
// set methods :
// Done with set method
public void SetEmployee()
{
Scanner input = new Scanner( System.in );
System.out.println( "Please enter Employee first name :" );//ask the user to inpute the first name
FirstName = input.next();//store whatever the user input
System.out.println( "Please enter Employee Last name :" );//ask the user to inpute the last name
LastName = input.next();//store whatever the user input
System.out.println( "Please enter number of hours worked last week by Employee :" );//ask the user to inpute the hours worked last week by Employee
NumberOfHoursWorkedLastWeek = input.nextDouble();//store whatever the user input
System.out.println( "Please enter the Hourly Rate for Employee:" );//ask the user to inpute the Hourly Rate
HourlyRate = input.nextDouble ();//store whatever the user input
}
// get method
public String getFirstName () // get method for the FirstName Variable
{
return FirstName ; // return the value which method set store
}
public String getLastName ()// get method for the LasttName Variable
{
return LastName ; // return the value which method set store
}
// get method for the NumberOfHoursWorkedLastWeek Variable
public double getNumberOfHoursWorkedLastWeek ()
{
// eturn the value which method set store
return NumberOfHoursWorkedLastWeek ;
}
public double getHourlyRate () // get method for the HourlyRate Variable
{
return HourlyRate ;// rerurn the Hourly Rate input by the user
}
public double MonthlySalary ()// function to calculate the Salary
{
// if the employee worked extra hours .. he/she will has extra money :D
if (NumberOfHoursWorkedLastWeek > 40)
// Calculate the Salary
MonthlySalary = (40*HourlyRate)+ (NumberOfHoursWorkedLastWeek - 40 *HourlyRate*1.5 );
else
MonthlySalary = (HourlyRate*NumberOfHoursWorkedLastWeek);
return MonthlySalary ; // return the salary :)
}
// Done with the get method
public void printEmployee()//function to print out all the info. for the employee :)
{
System.out.println(""+FirstName+"|/t/t"); // print the first name
System.out.println(""+LastName+"|/t/t");// print the last name
System.out.println(""+NumberOfHoursWorkedLastWeek+"|/t/t");// print the number of hours worked last week
System.out.println(""+HourlyRate+"|/t/t");// print the Hourly Rate
System.out.println(""+MonthlySalary+"|/t/t");// print out the Monthly Salary
System.out.println("-------------------"); // mshan al.jdwl !
}
} // done wuth the class2. الكلاس الثاني
public class EmplooyeeTest { // new class :)
public static void main(String[]args)
{
Emplooyee Employeelist[] = new Emplooyee[5]; // making an array ... with size 5 and name Employeelist :)
/* a for loop from one to the size which is 5 to store the info. input by the user for each employee */
for (int i=0 ; i<Employeelist.length ; i++)
Employeelist.SetEmployee();// call the SetEmployee method for the object Employeelist " which is an array and the object type is the "CLASS" Employee
System.out.println( "#| First Name | Last Name | Number of hours worked last week | Hourly Rate | MonthlySalary" );// header for the schedul :)
// a for loop from one to the size of the array which is 5
for (int j=0 ; j<Employeelist.length ; j++)
Employeelist[j].printEmployee();// call the print function to print all the info input by the user for each employeee
} // end the main
}// end the classاستخدم برنامج : NetBeans 6.9.1
الخطأ الذي يظهر :
Exception in thread "main" java.lang.NullPointerException
at EmplooyeeTest.main(EmplooyeeTest.java:12)
Java Result: 1
BUILD SUCCESSFUL (total time: 0 seconds)
شكراً جزيلاً مقدماً :) لا عدمناكم !