In: Computer Science
Write a Java program to do the following with your name. This can all be done in the main() method.
1. Create a String variable called myName and assign your personal name to it. Use proper capitalization for a legal name. I.e. String myName = "Billy Bob";
2. Load myName with the upper case version of itself and display the result.
3. Load myName with the lower case version of itself and display the result.
4. Capitalize the first letter of each given name in the lowercased name and display the result. Load the result into myName. Hint: This is not a single method call. You will need a loop to perform this. Hint: The substring method will be very helpful as you should likely build a new string result. Avoid magic number references. This solution should work with any proper name.
5. Using the value of the variable myName from step 4:
a.Display the Initials of the name. ( This may require a
Loop)
b.Use the trim methd on myName and store the results into
myName.
c.Display the size of the name
// Using Billy Bob as the name - here are the expected results:
My name in upper case is BILLY BOB.
My name in lower case is billy bob.
My name capitalized is Billy Bob.
My initials are BB.
The length of my name is 9.
if you have any doubts, please give me comment...
public class NameExercise{
public static void main(String[] args) {
String myName = "Billy Bob";
myName = myName.toUpperCase();
System.out.println("My name in upper case is "+myName+".");
myName = myName.toLowerCase();
System.out.println("My name in lower case is "+myName+".");
String result = "";
for(String temp: myName.split(" ")){
result += (temp.charAt(0)+"").toUpperCase()+temp.substring(1)+" ";
}
myName = result.trim();
System.out.println("My name capitalized is "+myName+".");
result = "";
for(String n: myName.split(" ")){
result += (myName.charAt(0)+"");
}
System.out.println("My initials are "+result+".");
System.out.println("The length of my name is "+myName.length()+".");
}
}
