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

بناء كلاس Author بسيط بلغة جافا

Alaa Emam

السؤال

Write a Java class Author with following features:
• Instance variables :
o firstName for the author’s first name of type String.
o lastName for the author’s last name of type String.
• Constructor:
o public Author (String firstName, String lastName): A constructor with parameters, it
creates the Author object by setting the two fields to the passed values.
• Instance methods:
o public void setFirstName (String firstName): Used to set the first name of author.
o public void setLastName (String lastName): Used to set the last name of author.
o public double getFirstName(): This method returns the first name of the author.
o public double getLastName(): This method returns the last name of the author.
o public String toString(): This method printed out author’s name to the screen
 

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

Recommended Posts

  • 0

مرحبًا @Alaa Emam,

يمكن عمل المطلوب بهذه الشيفرة :

public class Author {
    String firstName ; //متغير الاسم الاول 
    String lastName ; // متغير الاسم الاخير
    
    public Author(String first_name,String last_name) {
      //  Constructor
      firstName = first_name; // تعين قيمة الاسم الاول
      lastName = last_name;// تعين قيمة الاسم الاخير
   }
   
   //Method تعين الاسم الاول
   public void setFirstName ( String first_name ) {
      firstName = first_name; 
   }

// Method تعين الاسم الاخير
    public void setLastName ( String last_name ) {
      lastName = last_name;
   }
   
   // Method جلب الاسم الاول
   public String getFirstName( ) {
      return firstName;
   }
   
   // Method جلب الاسم الاخير
   public String getLastName( ) {
      return lastName;
   }
   
   //Method طباعىة الاسم كاملا
    public String toString(){
        System.out.println(firstName + " " + lastName);
        return firstName + " " + lastName;
    }
    
     public static void main(String []args){
         
         
        Author myObj = new Author("Adam","Ali");
        System.out.println(myObj.getFirstName());
        
     }
     
}

 

 

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

  • 0

مرحباً @Alaa Emam

يُمكنك عمل البرنامج بلغة جافا بالشكل التالي فهو عبارة عن إنشاء كلاس يتضمن خاصيتين (properties) من النوع النصي ( string ) و 5 توابع (methods) أي getters and setters و إعادة تعريف للتابع toString : 

class Author  {

    // Instance variables
    private String firstName, lastName;

    // Constructor
    public Author(String firstName, String lastName) {
        this.firstName = firstName;
        this.lastName = lastName;
    }


    /*
        Instance methods
     */
    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    @Override
    public String toString() {
        return this.firstName + " " + this.lastName;
    }
}

ويُمكنك إستخدام الكلاس Author في الميثود main بالشكل التالي:

public class Main {

    public static void main(String[] args) {

        // create instance
        Author author = new Author("Alaa", "Emam");
        System.out.println(author); // print author => call toString method
    }
}

بإمكانك تجربة المثال من خلال الرابط التالي: إضغط هنا

بالتوفيق

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

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

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

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

×   لقد أضفت محتوى بخط أو تنسيق مختلف.   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.

  • إعلانات

  • تابعنا على



×
×
  • أضف...