Question

Sometimes people have their initials printed on things like towels, shirts, and glassware. These initials are...

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).

  • public Monogram(String theFirst, String theMiddleInitial, String theLast) initializes the instance variables with the values of the parameters. The user is supposed to supply a single character for theMiddleInitial. But users often do not do what they should. A good program protects itself from user errors. So use the substring method to get just the first character of theMiddleInitial and store that in the instance variable. Now your program will work, even if the user makes a mistake by entering more than one character for theMiddleInitial.
  • public Monogram(String theFirst, String theLast) initializes the first and last names to the given parameters and initializes the middle initial to the empty String (two double quotes with no space like this "")

The class has the following methods

  • public String getFirstName()
  • public String getMiddleInitial()
  • public String getLastName()
  • public String getName() Gets the full name. There is a single space after both the first name and the middle initial. So there will be two spaces between first name and last name when there is no middle initial.
  • public String getMonogram() Gets the letters comprising the initials (first initial, middle initial, last initial) with no spaces.

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.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

// 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

Add a comment
Know the answer?
Add Answer to:
Sometimes people have their initials printed on things like towels, shirts, and glassware. These initials are...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT