Write a java program PlayWithName.java that should print the output as shown bas
ID: 3670004 • Letter: W
Question
Write a java program PlayWithName.java that should print the output as shown based on the name specification given for the code. The name will be entered by the user from the keyboard. Say your name is John Omega Phi, the output of PlayWithName.java should be as follows, with the user input after the colon.
What is your first name: John
What is your middle name: Omega
What is your last name: Phi
Name = John Omega Phi
Initials = JOP
Short name = John O Phi
Shorter name = J O Phi
UpperCase name = JOHN OMEGA PHI
Compute the output values using methods available to objects in the class String. Please don’t code in the output Strings.
Note: s.substring(0,1) returns a String which is the first character of the String s.
Explanation / Answer
// header files
import java.io.*;
public class HelloWorld
{
public static void main(String args[])
{
//Instantiation
String first = "Ram";
String middle = ""saran"";
String last = "chandran";
String initials = first.substring (0,1) + middle.substring (1,2) +
last.substring (0,1);
//Implementation
System.out.println("Name = " + first + " "+"Middle " + middle + " "+"Last " + last);
System.out.println("Initials = " + initials);
System.out.println("Short Name = " + first + " " +
initials.substring(1,2) + " " + last);
System.out.println("Shorter Name = " + initials.substring(0,1) + " "
+ initials.substring (1,2) + " " + last);
System.out.println("UpperCase name = " + (first + " " + middle + " "
+ last).toUpperCase());
}
}
output
Name = Ram Middle "saran" Last chandran
Initials = Rsc
Short Name = Ram s chandran
Shorter Name = R s chandran
UpperCase name = RAM "SARAN" CHANDRAN
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.