38.1k views
3 votes
Write a class named FullName containing: Two instance variables named given and family of type String. A constructor that accepts two String parameters. The value of the first is used to initialize the value of given and the value of the second is used to initialize family. A method named toString that accepts no parameters. toString returns a String consisting of the value of family, followed by a comma and a space followed by the value of given.

User Invariance
by
7.0k points

1 Answer

1 vote

Answer:

public class FullName {

private String givenName;

private String familyName;

//The constructor

public FullName(String givenName, String familyName) {

this.givenName = givenName;

this.familyName = familyName;

}

//Method toString

public String toString(){

String fullName = familyName+", "+givenName;

return fullName;

}

}

Step-by-step explanation:

As required by the question. A class is created with two fields

private String givenName;

private String familyName;

The constructor initializes the values for this fields

The method toString concatenates the familyName and givenName and returns the fullName

User Jernej K
by
7.3k points