Sometimes people have their initials printed on things like towels, shirts, and glassware. These initials are called a monogram. You are to write a class Monogram which represents a person's initials. A Monogram class has String instance variables of a first name, middle initial (one letter without period), and last name.
There is no starter file. You will write the whole class and provide Javadoc for the class, the constructors and the methods.
The Monogram class has two constructors (This is called overloading).
The class has the following methods
Requirement: Do not use an if statement inside method getMonogram. The middle initial is either a string of one character of the empty string.
To get the first letter of a string, you can use substring
String firstLetter = word.substring(0, 1);
For the draft, provide the instance variables and the first constructor. Implement these methods: getFirstName, getMiddleInitial, and getLastName methods. Implement the other methods as stubs. Provide Javadoc. For the stubs, indicate what will be returned in the final not null.
// Monogram class
public class Monogram {
private String firstName;
private String middleInitial;
private String lastName;
/**
* constructor that initializes firstName,
middleInitial and lastName to theFirst, theMiddleInitial and
theLast respectively
* @param theFirst, first name
* @param theMiddleInitial, middle initial
* @param theLast, last name
*/
public Monogram(String theFirst, String
theMiddleInitial, String theLast)
{
firstName = theFirst;
middleInitial =
theMiddleInitial.substring(0,1); // get the first character from
theMiddleInitial and assign it to middleInitial
lastName = theLast;
}
/**
* constructor that initializes firstName and lastName
to theFirst and theLast respectively and middleInitial is
initialized to empty string
* @param theFirst, first name
* @param theLast, last name
*/
public Monogram(String theFirst, String theLast)
{
this(theFirst,"",theLast); // calls
the other constructor to initailize the values by passing empty
string for middle initial
}
/**
* method that returns the firstName
* @return String, firstName
*/
public String getFirstName()
{
return firstName;
}
/**
* method that returns the middle initial
* @return String, middleInitial
*/
public String getMiddleInitial()
{
return middleInitial;
}
/**
* method that returns the lastName
* @return String, lastName
*/
public String getLastName()
{
return lastName;
}
/**
* method that returns the full name with space between
firstName and middleInitial and middleInitial and lastName
* @return String, firstName+" "+middleInitial+"
"+lastName
*/
public String getName()
{
return firstName+"
"+middleInitial+" "+lastName;
}
/**
* method that returns the String comprising of the
initial letters (first initial, middle initial, last initial) with
no spaces.
* @return String, firstInitial + middleInitial +
lastInitial
*/
public String getMonogram()
{
return
firstName.substring(0,1)+middleInitial+lastName.substring(0,1);
}
}
//end of Monogram class
Sometimes people have their initials printed on things like towels, shirts, and glassware. These initials are...