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:
Console Input And Output
Exercise:
Programming Projects
Question:2 | ISBN:9780132830317 | Edition: 5

Question

(This is a version with input of an exercise from Chapter 1.) Write a program that inputs two string variables, first and last , which the user should enter with his or her name. First, convert both strings to all lowercase. Your program should then create a new string that contains the 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, if the user inputs “Walt” for the first name and “Savitch” for the last name, then the program should create a new string with the text “Altway Avitchsay” and print it.


TextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbook

Answer

import java.util.*;
public class PigLatin {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
 Scanner input=new Scanner(System.in);
 String firstname,lastname,lower_first,lower_last,latin_first,latin_last;
 System.out.println("Enter the First Name:");
 firstname=input.next();
 System.out.println("Enter the Last Name:");
 lastname=input.next();
 System.out.println("Full Name before PigLatin:"+ firstname+" "+ lastname);
 
 lower_first=firstname.toLowerCase();
 lower_last=lastname.toLowerCase();
 
 latin_first=lower_first.substring(1,2).toUpperCase()+lower_first.substring(2)+lower_first.substring(0,1)+"ay";
 latin_last=lower_last.substring(1,2).toUpperCase()+lower_last.substring(2)+lower_last.substring(0,1)+"ay";
 
 System.out.println("Full Name after modifying PigLatin:"+ latin_first +" "+ latin_last);
    }

}

0 0

Discussions

Post the discussion to improve the above solution.