Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Answers Viewqa Java-Beginners Person have name field of Name type..??

Have Programming Question? Ask it here!
 
 
 


chandumundlore
Person have name field of Name type..??
3 Answer(s)      a year and a month ago
Posted in : Java Beginners


Person have name field of Name type means i understood to create new Name class with fields firstname, middlename, and lastname but i dont know how to use this class in person to give him name?

can you please write code two two different class and show me how to use?


Advertisement
View Answers

June 4, 2013 at 11:32 AM


hi friend,

Try the following code may this will be helpful for you

package net.roseindia.simpleExample;

public class Name {

    String firstName;
    String middleName;
    String lastName;

    public Name()
    {       
    }   
    public Name(String firstName, String middleName, String lastName)
    {
        this.firstName = firstName;
        this.middleName = middleName;
        this.lastName = lastName;
    }
    public String getFirstName() {
        return firstName;
    }
    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }
    public String getMiddleName() {
        return middleName;
    }
    public void setMiddleName(String middleName) {
        this.middleName = middleName;
    }
    public String getLastName() {
        return lastName;
    }
    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public String displayName()
    {       
        return getFirstName()+" "+ getMiddleName()+" "+getLastName();
    }
}

Continue....



June 4, 2013 at 11:37 AM


package net.roseindia.simpleExample;

public class Person {

    Name name;

    public Person()
    {       
    }   
    public Person(Name name) {
        super();
        this.name = name;
    }   
    public void display()
    {
        System.out.println(name.displayName());     
    }
}

Continue...



June 4, 2013 at 11:37 AM


package net.roseindia.simpleExample;

public class Main {

    public static void main(String args[])
    {
        Name name = new Name();
        name.setFirstName("Bipul");
        name.setMiddleName("Kumar");
        name.setLastName("Choudhary");

        //Name name = new Name("Bipul", "Kumar", "Choudhary");

        Person person = new Person(name);
        person.display();       
    }
}


Related Tutorials/Questions & Answers:
Advertisements
Advertisements
 

 

 

DMCA.com