SHARE
SPREAD
HELP

The Tradition of Sharing

Help your friends and juniors by posting answers to the questions that you know. Also post questions that are not available.


To start with, Sr2Jr’s first step is to reduce the expenses related to education. To achieve this goal Sr2Jr organized the textbook’s question and answers. Sr2Jr is community based and need your support to fill the question and answers. The question and answers posted will be available free of cost to all.

 

#
Authors:
Walter Savitch ,kenrick Mock
Chapter:
Getting Started
Exercise:
Programming Projects
Question:3 | ISBN:9780132830317 | Edition: 5

Question

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 lowercase. 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.


TextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbook

Answer

Program:

// PigLatinStrings.java
public class PigLatinStrings
{
	public static void main(String[] args)
	{
		String first;
		String last;
		String pigLatinFirst;
		String pigLatinLast;
		String pigLatinName;

		first = "walt";
		last = "savitch";

		pigLatinFirst = first.substring(1) + first.charAt(0) + "ay";
		pigLatinLast = last.substring(1) + last.charAt(0) + "ay";

		pigLatinFirst = pigLatinFirst.substring(0, 1).toUpperCase() 
						+ pigLatinFirst.substring(1);

		pigLatinLast = pigLatinLast.substring(0, 1).toUpperCase()
						+ pigLatinLast.substring(1);

		pigLatinName = pigLatinFirst + " " + pigLatinLast;

		System.out.println("First Name:    " + first);
		System.out.println("Last Name:     " + last);
		System.out.println("PigLatin Name: " + pigLatinName);
	}
}

Output:

First Name:    walt
Last Name:     savitch
PigLatin Name: Altway Avitchsay

 

0 0

Discussions

Post the discussion to improve the above solution.