اذهب إلى المحتوى
  • 0

كتابة دوال في جافا

Tamer Abo Ali

السؤال

Recommended Posts

  • 1

إذا أردنا أن تكون صفات المدرس خاصة ولا يمكن التعديل عليها بسهوولة فيمكننا اعتبار المتغيرات التي ذكرتها من نوع private وإنشاء دوال getter & setter لكل خاصية بهذا المعلم ويمكن إضافة دالة tostring لتطبع بيانات المدرس على شكل نص فالنتيجة تكون كالتالي... إن كنت تقصد دوال أخرى معينة أرجو أن توضح قصدك



import java.util.*;

public class Teacher {
	
     private int id;
     private String name;
     private String dateOfBirth;
     private String specialization;
     private String crs_name;

    //من هنا تبدأ الدوال الخاصة بالمدرس
    
    public Teacher(int id, String name, String dateOfBirth, String specialization, String crs_name ){
        this.id = id;
        this.name = name;
        this.dateOfBirth = dateOfBirth;
        this.specialization = specialization;
        this.crs_name = crs_name;
    }
    
    public Teacher(int id, String name){
        this.id = id;
        this.name = name;
        this.dateOfBirth = " ";
        this.specialization = " ";
        this.crs_name = " ";
    }
    
    public String toString()
    {
        return this.getId()+ "\t" + this.getName()+ "\t"+ this.getDateOfBirth() +"\t"+ this.getSpecialization() +"\t"+ this.getCrs_name() ;
    }
    
    
    public int getId()
    {
        return this.id;
    }
    
    
    public String getName()
    {
        return this.name;
    }
    
    public String getDateOfBirth()
    {
        return this.name;
    }
    
    public String getSpecialization()
    {
        return this.name;
    }
    
    public String getCrs_name()
    {
        return this.name;
    }
    
    
    public void setID(int id)
    {
        this.id=id;
    }
    
    
    public void setName(String name)
    {
         this.name=name;
    }
    
    public void setDateOfBirth(String dateOfBirth)
    {
         this.dateOfBirth=dateOfBirth;
    }
    
    public void setSpecialization(String specialization)
    {
         this.specialization=specialization;
    }
    
    public void setCrs_name(String crs_name)
    {
         this.crs_name=crs_name;
    }
    
    // انتهت الدوال الخاصة بالمدرس
     
    public static void main(String[] args) {
        List<Teacher> teachers = new ArrayList<Teacher>();
        Scanner input = new Scanner(System.in);
        
        System.out.println ("Enter the name of the teacher");
        String teacherName= input.next();
        
        System.out.println ("Enter the id of the teacher");
        int teacherId= input.nextInt();
        
        
        Teacher t = new Teacher(teacherId, teacherName);
        teachers.add(t);

        for (Teacher teacher : teachers) { 		      
           System.out.println(teacher.toString()); 		
      }
    }

}

 

رابط هذا التعليق
شارك على الشبكات الإجتماعية

انضم إلى النقاش

يمكنك أن تنشر الآن وتسجل لاحقًا. إذا كان لديك حساب، فسجل الدخول الآن لتنشر باسم حسابك.

زائر
أجب على هذا السؤال...

×   لقد أضفت محتوى بخط أو تنسيق مختلف.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   جرى استعادة المحتوى السابق..   امسح المحرر

×   You cannot paste images directly. Upload or insert images from URL.

  • إعلانات

  • تابعنا على



×
×
  • أضف...