Background: 1. Consider the following phrase: I love this class! We could change
ID: 3621339 • Letter: B
Question
Background:
1. Consider the following phrase:
I love this class!
We could change some of the letters, one at a time, to transform the phrase to read:
I lave this class!
I late this class!
I hate this class!
In the example above, we changed the letters in the word “love”, the ‘o’ to ‘a’, then the
‘v’ to ‘t’, and finally the ‘l’ to ‘h’ to give us the new phrase.
Assignment:
1. Write a program that prompts the user to input a word, phrase, or sentence.
2. Your program should then prompt the user to choose an index value where they want
to change the letter. The user then enters the index and the letter. If the user inputs
the index outside of the range of indices, the user should be prompted until they get it
right. Keep in mind that the first letter has index 0.
3. Be sure to break your program up into pieces. For example, you could write a
method for the input of the phrase, and another for the loop of continually asking the
user for changes.
5. Be sure to include a comment block at the top of your programs, and indent properly.
Instructions:
1. Make sure that your output looks like the following 3 sample run outputs:
Every sample output is separated by a line.
Enter a phrase -> The blue moon
Enter an index value to change -> 4
Enter the new letter or character, to replace the 'b' -> g
Here is the new phrase:
The glue moon
Would you like to replace another letter? ( 'Y' for yes, 'N' for no ) y
Enter an index value to change -> 11
Enter the new letter or character, to replace the 'o' -> r
Here is the new phrase:
The glue morn
Would you like to replace another letter? ( 'Y' for yes, 'N' for no ) n
Goodbye!
Enter a phrase -> Nicely done!
Enter an index value to change -> 0
Enter the new letter or character, to replace the 'N' -> P
Here is the new phrase:
Picely done!
Would you like to replace another letter? ( 'Y' for yes, 'N' for no ) y
Enter an index value to change -> 1
Enter the new letter or character, to replace the 'i' -> o
Here is the new phrase:
Pocely done!
Would you like to replace another letter? ( 'Y' for yes, 'N' for no ) y
Enter an index value to change -> 2
Enter the new letter or character, to replace the 'c' -> o
Here is the new phrase:
Pooely done!
Would you like to replace another letter? ( 'Y' for yes, 'N' for no ) y
Enter an index value to change -> 3
Enter the new letter or character, to replace the 'e' -> r
Here is the new phrase:
Poorly done!
Would you like to replace another letter? ( 'Y' for yes, 'N' for no ) n
Goodbye!
Enter a phrase -> Home sweet home
Enter an index value to change -> 20
Enter an index value to change -> -3
Enter an index value to change -> 14
Enter the new letter or character, to replace the 'e' -> b
Here is the new phrase:
Home sweet homb
Would you like to replace another letter? ( 'Y' for yes, 'N' for no ) y
Enter an index value to change -> 11
Enter the new letter or character, to replace the 'h' -> c
Here is the new phrase:Home sweet comb
Would you like to replace another letter? ( 'Y' for yes, 'N' for no ) y
Enter an index value to change -> 2
Enter the new letter or character, to replace the 'm' -> n
Here is the new phrase:
Hone sweet combWould you like to replace another letter? ( 'Y' for yes, 'N' for no ) y
Enter an index value to change -> 0
Enter the new letter or character, to replace the 'H' -> L
Here is the new phrase:Lone sweet comb
Would you like to replace another letter? ( 'Y' for yes, 'N' for no ) n
Goodbye!
Explanation / Answer
please rate - thanks
import java.util.*;
public class Transform
{public static void main(String[] args)
{
char yorn='Y',letter;
int i;
String phrase;
System.out.print("Enter a phrase -> ");
Scanner in = new Scanner(System.in);
phrase = in.nextLine();
while(Character.toUpperCase(yorn)=='Y')
{do
{System.out.print("Enter an index value to change -> ");
i=in.nextInt();
}while(i<0||i>phrase.length());
System.out.print("Enter the new letter or character, to replace the '"+phrase.charAt(i)+"'-> ");
letter=in.next().charAt(0);
phrase=phrase.substring(0,i)+letter+phrase.substring(i+1);
System.out.println("Here is the new phrase:");
System.out.println(phrase);
System.out.print("Would you like to replace another letter? ( 'Y' for yes, 'N' for no )");
yorn=in.next().charAt(0);
}
System.out.println("Goodbye!");
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.