FullName.java A full name has three components - the given name, the middle init
ID: 3623430 • Letter: F
Question
FullName.java A full name has three components - the given name, the middle initial, and the family name. Typically, a full name, when entered in standard form, will be in the format given_name middle_initial (period) family_name. The full name "Jose Maria G. Mariano" therefore yields a given name of "Jose Maria", a middle initial of "G" and a family name of "Mariano". Write a FullName class that should have a constructor that will extract the first name, middle initial, and family name from a full name in standard form as described above. It should also have a method called familyFirst() which returns the full name in the format family_name, given_name middle_initial (period); e.g. "Mariano, Jose Maria G." Test it by creating a main method that will ask the user to enter his/her full name and your program should be able to print the given name, the middle initial and family name individually. It should also print out the return value of the familyFirst() method.
Sample run:
> java FullName
Please enter your full name: Juan Paolo N. Dela Cruz
Given name: Juan Paolo
Middle initial: N
Family name: Dela Cruz
Dela Cruz, Juan Antonio N.
Explanation / Answer
Here you go import java.io.*; public class FullName { public String name, first, family_name, middle; public FullName(String full_name) { int position = 0; name = full_name; position = name.indexOf("."); first = name.substring(0,position-2); middle = name.substring(position-1,position); family_name = name.substring(position+2, name.length()); } public String familyFirst() { return family_name + ", " + first + " " + middle + "."; } public static void main(String[] args) { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); String name; try { System.out.print("Enter your full name: "); name = in.readLine(); FullName person = new FullName(name); System.out.println("Given Name: " + person.first); System.out.println("Middle Inital: " + person.middle); System.out.println("Family Name: " + person.family_name); System.out.println(person.familyFirst()); } catch(Exception e) { e.printStackTrace(); } } }Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.