Question: language: Java Sometimes people have their initials printed on things like towels, shirts, and glassware. These initials are called a monogram. You are to write
language: Java
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. No period), and last name.
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 error. 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 it the user makes a mistake.
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 "")
Letters class has these methods
public String getFirstName()
public String getMiddleInitial()
public String getLastName()
public String getName() gets the full name with spaces separating the names. Note: When the there is no middle initial, there will be 2 spaces between first and last name. That is okay.
public String getMonogram() gets the letters comprising the initials (first initial, middle initial, last initial) with no spaces. Do not use an if statement. You do not need it. The middle initial is either a string of one character of the empty string.
Provide Javadoc (the class, the constructors, and the methods)
To get the first letter of a string, you can use substring
String firstLetter = word.substring(0, 1);
Use the following file:
MonogramTester.java
/** * Tests the Monogram class * * @author Kathleen O'Brien */ public class MonogramTester { public static void main(String[] args) { Monogram name = new Monogram("Sergey", "Mikhaylovich", "Brin"); System.out.println("Name: " + name.getName()); System.out.println("Expected: Sergey M Brin"); System.out.println("Monogram: " + name.getMonogram()); System.out.println("Expected: SMB"); name = new Monogram("Larry", "Page"); System.out.println("First: " + name.getFirstName()); System.out.println("Expected: Larry"); System.out.println("Middle Initial: " + name.getMiddleInitial()); System.out.println("Expected: "); System.out.println("Last: " + name.getLastName()); System.out.println("Expected: Page"); System.out.println("Name: " + name.getName()); System.out.println("Expected: Larry Page"); System.out.println("Monogram: " + name.getMonogram()); System.out.println("Expected: LP"); } } Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
