Write the code for the following UML diagram NameBuilder - firstName: String - m
ID: 643096 • Letter: W
Question
Write the code for the following UML diagram
NameBuilder
- firstName: String
- middleName: String
- lastName: String
+ NameBuilder (String first, String middle, String last):
+ full(): String
+ formal(): String
+ initials(): String
+ lastNameFirst(): String
+ pigLatin(): String
+ reverseLast(): String
Assume input of Eric Patrick Clapton
firstName stores a first name Eric .
middleName stores the middle name Patrick.
lastName stores the last name Clapton.
The constructor initializes all instance variables.
full() returns a new String of the full name. ->Eric Patrick Clapton
formal() returns a new String with first middle initial and last names. ->Eric P. Clapton
initials() returns a new String with all initials. ->E. P. C.
lastNameFirst() return a String with the last name first then comma first name. Clapton, Eric
pigLatin() returns a new String. Take the first letter and move to the end and add ay to both first and last names. -> ricEay laptonCay
reverseLast() returns a new String that reverses the letters in the last name. -> notpalC
Create a separate file for class NameBuilderDriver with a main method.
The main method should create an instance of a NameBuilder object called builder with your name passed into the constructor.
Call each method with builder and print out each resulting String.
Properly formatted code with indenting
NameBuilder
- firstName: String
- middleName: String
- lastName: String
+ NameBuilder (String first, String middle, String last):
+ full(): String
+ formal(): String
+ initials(): String
+ lastNameFirst(): String
+ pigLatin(): String
+ reverseLast(): String
Explanation / Answer
Note: I completed maximum method definitions.
//Create class name
public class NameBuilder
{
//firstName stores a first name Eric
private String firstName = "Eric";
//middleName stores the middle name Patrick
private String middleName = "Patrick";
//lastName stores the last name Clapton
private String lastName = "Clapton";
//Default constructor
public NameBuilder()
{
}
//The constructor initializes
//all instance variables.
public NameBuilder(String first, String middle, String last)
{
this.firstName=first;
this.middleName=middle;
this.lastName = last;
}
public NameBuilder withFirst(String value)
{
firstName = value;
return this;
}
public NameBuilder withMiddle(String value)
{
middleName = value;
return this;
}
public NameBuilder withLast(String value)
{
lastName = value;
return this;
}
//full() returns a new String of the full name
public NameBuilder full(String value)
{
NameBuilder name=new NameBuilder();
name.withFirst(value);
name.withMiddle(value);
name.withLast(value);
return this;
}
//formal() returns a new String with first
//middle initial and last names
public NameBuilder formal(String value)
{
NameBuilder name=new NameBuilder();
name.withFirst(value);
name.withMiddle(value.substring(0));
name.withLast(value);
return this;
}
//initials() returns a new String with
//all initials
public NameBuilder initials(String value)
{
NameBuilder name=new NameBuilder();
name.withFirst(value.substring(0));
name.withMiddle(value.substring(0));
name.withLast(value.substring(0));
return this;
}
//lastNameFirst() return a String with the last name
//first then comma first name
public NameBuilder lastNameFirst(String value)
{
NameBuilder name=new NameBuilder();
name.withLast(value);
name.withFirst(value);
return this;
}
}
//main class
public class BuilderDemo
{
//main method
public static void main(String[] args)
{
final String value="Eric Patrick Clapton";
NameBuilder builder=new NameBuilder();
builder.full(value);
builder.formal(value);
builder.initials(value);
builder.lastNameFirst(value);
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.