Getting the fullname

Report a typo

Here is the User class with two fields: firstName and lastName.

Implement two null-safety setters and one calculated getter in the following ways:

  • both setters should accept and set a value if it is not null or an empty string "";
  • the getter should return the full name of the user by concatenating firstName and lastName using a single space between them. If one of the parts was not set, the getter should return only the other part without additional spaces. If both parts are empty, the getter should return the word "Unknown".

There are two examples below:

User tim = new User();
tim.setFirstName("Tim");
tim.setLastName("Towler");
System.out.println(tim.getFullName()); // Tim Towler

User katie = new User();
katie.setFirstName("Katie");
katie.setLastName(null);
System.out.println(katie.getFullName()); // Katie (without additional spaces)
Write a program in Java 17
class User {
private String firstName;
private String lastName;

public User() {
this.firstName = "";
this.lastName = "";
}

public void setFirstName(String firstName) {
// write your code here
}

public void setLastName(String lastName) {
// write your code here
}

public String getFullName() {
return ""; // write your code here
}
}
___

Create a free account to access the full topic