Create a Java program Named LB04 with a class name HardMonogram Input: fullName
ID: 3753029 • Letter: C
Question
Create a Java program Named LB04 with a class name HardMonogram
Input: fullName
Process:
getInitial(separateName): extract the first character string from each part of a full name and return it.
getSeparateName(fullName): separate a full name into first name, middle name, and last name.
setMonogram(): using getInitial(), generate the right monogram result, then display it.
main(String[] args): driver method
Output:
- Using JOptionPane to get a first name, middle name, and last name at once, then, on a JOptionPane dialog, show the monogram which is 3 initials extracted from the full name.
Ex) Johnny Mac Appleseed = JMC
Explanation / Answer
import javax.swing.*;
public class HardMonogram {
JFrame f;
String fullName;
HardMonogram(){
f=new JFrame();
fullName=JOptionPane.showInputDialog(f,"Enter fullName"); //getting name using JOptinPane
}
String[] getSeparateName(String name){
return name.split(" ",-1);// split on space
}
String getInitial(String[] S){
String initial = "";
for(String s :S){
initial = initial+Character.toString(s.charAt(0));
}
return initial;
}
void setMonogram(){
String[] listString = getSeparateName(fullName);
String initial = getInitial(listString);
String upper = initial.toUpperCase();
f = new JFrame();
JOptionPane.showMessageDialog(f,"The Monogram is " + upper);
}
public static void main(String[] args) {
HardMonogram Mo = new HardMonogram();
Mo.getSeparateName(Mo.fullName);
Mo.setMonogram();
}
}
Happy Chegging!