thank you, any help would be great Write a completed java program in a source fi
ID: 3541967 • Letter: T
Question
thank you, any help would be great
Write a completed java program in a source file to be named Assignment3.java. The program asks the user for their full name (first middle last-using nextLine method) as one string in any combination of upper and lower case. Your task is to output the name in the following variations: Print just the initials in upper case letters Print the last name in upper case followed by a comma and the first name in lower case with the first letter capitalized and the middle initial capitalized followed by a period Print the last name comma first name and middle name A- all names with first letter capitalized. Note: Your program has to check for middle name and if the user does not have a middle name your program has to print the variations without the middle name. See the sample output below Important: You will not get points if you do not read the string as one line (Use scan.nextLine()). You cannot use the Scanner class for extracting different parts of the name. You have to use the methods in class String Here is the output your program should produce when the user enters the string shown in bold: Sample Output: (the user enters the string shown in bold) What are your first, middle, and last names? david john smith Your initials are: DJS Variation one: SMITH, David J. Variation two: Smith, David JohnExplanation / Answer
Save it as MyClass.java
import java.util.Scanner;
public class MyClass
{
public static String initCap(String str)
{
return str.substring(0, 1).toUpperCase()+str.substring(1, str.length());
}
public static String getInit(String str)
{
return (""+str.charAt(0)).toUpperCase();
}
public static void main(String[] a)
{
Scanner s=new Scanner(System.in);
System.out.println("What are your First, Middle and Last Names");
String name=s.nextLine();
String[] nameParts=name.split(" ");
String init="";
if(nameParts.length==2)
{
System.out.println("Your initials are : ");
System.out.println(getInit(nameParts[0])+getInit(nameParts[1]));
System.out.println("Variation One : ");
System.out.println(nameParts[1].toUpperCase()+", "+initCap(nameParts[0]));
System.out.println("Variation Two : ");
System.out.println(initCap(nameParts[1])+", "+initCap(nameParts[0]));
}
else if(nameParts.length==3)
{
System.out.println("Your initials are : ");
System.out.println(getInit(nameParts[0])+getInit(nameParts[1])+getInit(nameParts[2]));
System.out.println("Variation One : ");
System.out.println(nameParts[2].toUpperCase()+", "+initCap(nameParts[0])+" "+getInit(nameParts[1])+".");
System.out.println("Variation Two : ");
System.out.println(initCap(nameParts[2])+", "+initCap(nameParts[0])+" "+initCap(nameParts[1]));
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.