الفريق العربي للبرمجةأرشيف المنتديات · 2000 – 2023
نسخة أرشيفية للقراءة فقط — التسجيل والمشاركة مغلقان، والمحتوى محفوظ كما كان.

خطأ في مناداة ميثود من كلاس

بدأه شادن..}~ في 12 أبريل 2011 · 9 رد · 1,324 مشاهدة · في JavaSE
مشاركة: واتساب X فيسبوك تيليجرام
#1 صاحب الموضوع

لسلام عليكم ...

انا عندي مشكله لما انادي للفنكشن في كلاس ثاني يطلع لي غلط وماني عارفه كيف اصلح ذا الغلط

هنا يطلعلي error

EmployeeList.setEmployee();

وفي ذا كمان ...

 EmployeeList.display();

وهذا الكود حقي الكلاس

package employee;


import java.util.Scanner;
/**
 *
 * @author Com net
 */
public class Employee {
    private String FirstName;
     private String LastName;
      private double MonthlySalary;
       private double Rate;
       private double[]nhours=new double[4];
//
//

public Employee()
    {
    FirstName=null;
    LastName=null;
    MonthlySalary=0.0;
    Rate=0.0;
    for( int j=0;j<4;j++)
        nhours[j]=0.0;
    }

void Employee (String firstName,String lastName,double monthlySalary,double hoursOfWork,double hourlyRate)
    {
     FirstName=firstName;
     LastName=lastName;
     MonthlySalary=monthlySalary;
     for(int j=0;j<4;j++)
     nhours[j]=hoursOfWork;
     Rate=hourlyRate;
        }
 double MonthlyHours=0.0;
public void setEmployee()
    {
    Scanner input =new Scanner(System.in);

    System.out.println("Enter First Name");
    FirstName=input.nextLine();
     System.out.println("Enter Last Name");
    LastName=input.nextLine();
     System.out.println("Enter Rate");
   Rate=input.nextDouble();

   for(int j=0;j<4;j++)
   {
       System.out.println("Enter Hours of week"+(j+1));
       nhours[j]=input.nextInt();    
        MonthlyHours+=nhours[j];
   }

    for(int j=0;j<4;j++){//calculat Monthly Salary
       if (nhours[j]<=40)
         {
           MonthlySalary+=(Rate*nhours[j]);
         }
       else
          {
         MonthlySalary+=(Rate*nhours[j])+((Rate*1.5)*(nhours[j]-40));
           }


 }
    }
    }

وذا المين ....

class Employee{
private String FirstName;//First Name
private String LastName;//Last Name
private double MonthlySalary;//Monthly Salary

// constructor that initializes the three instance variables
public Employee()
    {
    FirstName=null;
    LastName=null;
    MonthlySalary=0.0;
}

void Employee(String first ,String last ,double salary) {
    FirstName=first;
    LastName=last;
    MonthlySalary=salary;
}

// Provide a set and get method for each instance variable.
// set and get method for variable FirstName

public void  setFirstName(String first)
{
    FirstName=first;
}
public String  getFirstName()
{
   return FirstName;
}

//----------------------
//set and get method for variable LastName
public void  setLastName(String last)
{
     LastName=last;
}
public String  getLastName()
{
   return LastName;
}
//----------------------------

//set and get method for variable MonthlySalary

public void  setMonthlySalary(double salary)
{  do
    if(salary>0)
     MonthlySalary=salary;
    else
        System.out.print("The salary should be postive");
   while(salary<0);
}
public double getMonthlySalary() {
        return MonthlySalary;
    }


}

/**
 *
 * @author hp
 */

public class EmployeeTest {

    /**
     * @param args the command line arguments
     */
      public static void main(String[] args) {
        // TODO code application logic here

 Employee EmployeeList[]=new Employee[5];//consantrat list of employee
    for (int i=0;i<EmployeeList.length;i++)
        {
            EmployeeList=new Employee();
            EmployeeList.setEmployee();

        }
 //--------------------------------------------------------------------------
   System.out.print("\t\tFirstName\tLastName\tHoursOfWork\t\tHourlyRate\t\tMonthlySalary");
        for (int i=0;i<EmployeeList.length;i++)//print all the employee
   {
      EmployeeList.display();
   }

    }
}

بليز احد يقولي ايش تصحيحه....

#2

الكود الاول به كلاس اسمها Employee

والكود الثانى به نفس الكلاس اسمها Employee وفيها دوال غير الاخرى

وانتى مستخدمه Employee الموجود مع الكلاس Main

بالتالى الدالتين setEmployee ,display غير موجودين بها

وكمان الداله display غير موجوده فى كلا الكلاسين

Software Developer
Mahmoudkelany.com


 

#3

كيف غير موجوده وانا مستخدمتها في الكلاس الاول

وسويت اوبجيكت وناديت الميثود

مافهمت عليك ممكن توضح اكثر

#4

الداله غير موجوده

تحققى من الكود مره اخرى ستجدى انها غير موجوده فى الكلاسين التى اضفتهما

ثانى شىء كما قولت لك ان هناك 2 كلاس بنفس الاسم

واحده فى ملف خارجى وهى اول كود وضعتيه

وواحده مع الـTest class

اتمنى ان تدققى النظر فى الاثنين

Software Developer
Mahmoudkelany.com


 

#5

عدلته في الكود الثاني للمين وصار اسم الكلاس EmployeeTest

public class EmployeeTest {

    private String FirstName;//First Name
private String LastName;//Last Name
private double MonthlySalary;//Monthly Salary

// constructor that initializes the three instance variables
public EmployeeTest()
    {
    FirstName=null;
    LastName=null;
    MonthlySalary=0.0;
}

void EmployeeTest(String first ,String last ,double salary) {
    FirstName=first;
    LastName=last;
    MonthlySalary=salary;
}

// Provide a set and get method for each instance variable.
// set and get method for variable FirstName

public void  setFirstName(String first)
{
    FirstName=first;
}
public String  getFirstName()
{
   return FirstName;
}

//----------------------
//set and get method for variable LastName
public void  setLastName(String last)
{
     LastName=last;
}
public String  getLastName()
{
   return LastName;
}
//----------------------------

//set and get method for variable MonthlySalary

public void  setMonthlySalary(double salary)
{  do
    if(salary>0)
     MonthlySalary=salary;
    else
        System.out.print("The salary should be postive");
   while(salary<0);
}
public double getMonthlySalary() {
        return MonthlySalary;
    }
    /**
     * @param args the command line arguments
     */
      public static void main(String[] args) {
        // TODO code application logic here

 Employey[] EmployeeList=new Employee[5];//consantrat list of employee
    for (int i=0;i<EmployeeList.length;i++)
        {
            EmployeeList=new Employee();
            EmployeeList.setEmployee();

        }
 //--------------------------------------------------------------------------
   System.out.print("\t\tFirstName\tLastName\tHoursOfWork\t\tHourlyRate\t\tMonthlySalary");
        for (int i=0;i<EmployeeList.length;i++)//print all the employee
   {
      EmployeeList.display();
   }

    }
}

دحين الغلط اللي طالع هنا

 Employey[] EmployeeList=new Employee[5];

وفي الكلاس طالع الغلط في داله display

package employee;


import java.util.Scanner;
/**
 *
 * @author Com net
 */
public class Employee {
    private String FirstName;
     private String LastName;
      private double MonthlySalary;
       private double Rate;
       private double[]nhours=new double[4];
//
//

public Employee()
    {
    FirstName=null;
    LastName=null;
    MonthlySalary=0.0;
    Rate=0.0;
    for( int j=0;j<4;j++)
        nhours[j]=0.0;
    }

void Employee (String firstName,String lastName,double monthlySalary,double hoursOfWork,double hourlyRate)
    {
     FirstName=firstName;
     LastName=lastName;
     MonthlySalary=monthlySalary;
     for(int j=0;j<4;j++)
     nhours[j]=hoursOfWork;
     Rate=hourlyRate;
        }
 double MonthlyHours=0.0;
public void setEmployee()
    {
    Scanner input =new Scanner(System.in);

    System.out.println("Enter First Name");
    FirstName=input.nextLine();
     System.out.println("Enter Last Name");
    LastName=input.nextLine();
     System.out.println("Enter Rate");
   Rate=input.nextDouble();

   for(int j=0;j<4;j++)
   {
       System.out.println("Enter Hours of week"+(j+1));
       nhours[j]=input.nextInt();    
        MonthlyHours+=nhours[j];
   }

    for(int j=0;j<4;j++){//calculat Monthly Salary
       if (nhours[j]<=40)
         {
           MonthlySalary+=(Rate*nhours[j]);
         }
       else
          {
         MonthlySalary+=(Rate*nhours[j])+((Rate*1.5)*(nhours[j]-40));
           }





    void display()
    {
         System.out.printf("%s \t %s \t %f lt %f \t %f",FirstName,LastName,MonthlyHours,Rate,MonthlySalary);
    }
        }
    }
#6

الغلط في Employee بس في السبيلينق كتبتها غلط

دحين داله الست صارت مزبوطه بس هي display ماني عارفه ايش المشكلة

#7

بليز احد يقولي كيف اصحح ذا الغلط

المشكلة دحين عندي في داله display

#8

أضيفي إلى متغيرات الفئة:

private double MonthlyHours=0.0;

أضيفي الدوال :

public String get_First(){return this.FirstName;}
public String get_Last(){return this.lastName;}
public double get_monthly_sal(){return this.MonthlySalary;}
public double get_Rate(){return this.Rate;}
public double get_MonthlyHours(){return this.MonthlyHours;}

اكتبي هذا السطر في دالة display

System.out.printf("%s \t %s \t %f lt %f \t %f",this.get_First(),this.get_Last(),this.get_MonthlyHours(),this.get_Rate(),this.get_monthly_sal());

لم أطلع على كامل الكود .. ولكن

جربي وبلغينا بالنتائج

عفواً أحبتي انقطاعي بسبب الدراسة دعواتكم بالتوفيق..

{ لا ينال العلم مستحٍ ولا مستكبر }

#9

تفضلى

public class EmployeeTest {

	private String FirstName;//First Name
	private String LastName;//Last Name
	private double MonthlySalary;//Monthly Salary

// constructor that initializes the three instance variables
	public EmployeeTest() {
    	FirstName = null;
    	LastName = null;
    	MonthlySalary = 0.0;
	}

	void EmployeeTest(String first, String last, double salary) {
    	FirstName = first;
    	LastName = last;
    	MonthlySalary = salary;
	}

// Provide a set and get method for each instance variable.
// set and get method for variable FirstName
	public void setFirstName(String first) {
    	FirstName = first;
	}

	public String getFirstName() {
    	return FirstName;
	}

//----------------------
//set and get method for variable LastName
	public void setLastName(String last) {
    	LastName = last;
	}

	public String getLastName() {
    	return LastName;
	}
//----------------------------

//set and get method for variable MonthlySalary
	public void setMonthlySalary(double salary) {
    	do {
        	if (salary > 0) {
            	MonthlySalary = salary;
        	} else {
            	System.out.print("The salary should be postive");
        	}
    	} while (salary < 0);
	}

	public double getMonthlySalary() {
    	return MonthlySalary;
	}

	/**
 	* @param args the command line arguments
 	*/
	public static void main(String[] args) {
    	// TODO code application logic here

    	Employee[] EmployeeList = new Employee[2];//consantrat list of employee
    	for (int i = 0; i < EmployeeList.length; i++) {
        	EmployeeList = new Employee();
        	EmployeeList.setEmployee();

    	}
    	//--------------------------------------------------------------------------
    	System.out.println("FirstName\tLastName\tHoursOfWork\tHourlyRate\tMonthlySalary");
    	for (int i = 0; i < EmployeeList.length; i++)//print all the employee
    	{
        	EmployeeList.display();
    	}

	}
}

public class Employee {

	private String FirstName;
	private String LastName;
	private double MonthlySalary;
	private double Rate;
	private double[] nhours = new double[4];
//
//

	public Employee() {
    	FirstName = null;
    	LastName = null;
    	MonthlySalary = 0.0;
    	Rate = 0.0;
    	for (int j = 0; j < 4; j++) {
        	nhours[j] = 0.0;
    	}
	}

	void Employee(String firstName, String lastName, double monthlySalary, double hoursOfWork, double hourlyRate) {
    	FirstName = firstName;
    	LastName = lastName;
    	MonthlySalary = monthlySalary;
    	for (int j = 0; j < 4; j++) {
        	nhours[j] = hoursOfWork;
    	}
    	Rate = hourlyRate;
	}
	double MonthlyHours = 0.0;

	public void setEmployee() {
    	Scanner input = new Scanner(System.in);

    	System.out.println("Enter First Name");
    	FirstName = input.nextLine();
    	System.out.println("Enter Last Name");
    	LastName = input.nextLine();
    	System.out.println("Enter Rate");
    	Rate = input.nextDouble();

    	for (int j = 0; j < 4; j++) {
        	System.out.println("Enter Hours of week" + (j + 1));
        	nhours[j] = input.nextInt();
        	MonthlyHours += nhours[j];
    	}

    	for (int j = 0; j < 4; j++) {//calculat Monthly Salary
        	if (nhours[j] <= 40) {
            	MonthlySalary += (Rate * nhours[j]);
        	} else {
            	MonthlySalary += (Rate * nhours[j]) + ((Rate * 1.5) * (nhours[j] - 40));
        	}
    	}

	}

	public void display() {
    	System.out.printf("%s \t %s \t %f \t %f \t %f%n", FirstName, LastName, MonthlyHours, Rate, MonthlySalary);
	}
}

Software Developer
Mahmoudkelany.com


 

#10

شكرا اخوي يعطيك الف عافيه :)

مواضيع مشابهة