The following question was taken from Absolute Java 5th ed. by Walter Savitch:
Write a program that starts with the string variable first set to your first name and the string variable last set to your last name. Both names should be all lower- case. Your program should then create a new string that contains your full name in pig latin with the first letter capitalized for the first and last name. Use only the pig latin rule of moving the first letter to the end of the word and adding “ay.” Output the pig latin name to the screen. Use the substring and toUpperCase methods to construct the new name.
For example, given
first = "walt";
last = "savitch";
the program should create a new string with the text “Altway Avitchsay” and print it.
This is the code that I have written:
public class Question3 {
private static String first;
private static String last;
private static String newName;
public static void main(String[] args) {
Question3 name = new Question3("walt", "savitch");
System.out.println(name.convertName(first) + " "
+ name.convertName(last));
}
public Question3(String lowerCaseFirstName, String lowerCaseLastName) {
first = lowerCaseFirstName;
last = lowerCaseLastName;
}
private String convertName(String originalName) {
String firstLetter = originalName.substring(0, 1);
newName = nameWithoutFirstLetter(originalName) + firstLetter; // move first letter to the end
newName = capitalizeFirstLetter() + nameWithoutFirstLetter(newName) // capitalize first letter and add "ay" to the end
+ "ay";
return newName;
}
private String capitalizeFirstLetter() {
String capitalFirstLetter = newName.substring(0, 1).toUpperCase();
return capitalFirstLetter;
}
private String nameWithoutFirstLetter(String name) {
String restOfName = name.substring(1);
return restOfName;
}
}