Practice manipulating an object by calling its methods.
Complete the class Homework1 class using String methods. Remember that all the String methods are accessors. They do not change the original String. If you want to apply multiple methods to a String, you will need to save the return value in a variable.
Complete the class by doing the
following:
+ Print the word in lowercase
+ Replace "e" with "3" (Use the unmodified variable word)
+ Print the changed word
+ In the changed word, replace: "t" with "7"
+ Print the changed word
+ In this newest changed word, replace: "L" with "1" (uppercase L
with the number 1). Then, print the final changed word (with all
the replacements)
+Print the length of the word
The code to print the original word is already included for you. Do not change that statement. You will need to use the replace() method multiple times. Then print the final String. Remember that replace is an accessor method; It returns the changed version of the String. If you want to do something with the returned String - like use replace() on it again - you need to save the return value in a variable.
Given code is below:
public class Homework1
{
public static void main(String[] args)
{
String word = "CoLton";
// Add your code here
System.out.println("original word: "
+ word);
}
}
Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. If not, PLEASE let me know before you rate, I’ll help you fix whatever issues. Thanks
// Homework1.java
public class Homework1 {
public static void main(String[] args) {
String word = "CoLton";
System.out.println("original word: " + word);
// printing word in lower case without saving
System.out.println("word in lower case: " + word.toLowerCase());
// replacing 'e' with '3' and storing in word itself
word = word.replace('e', '3');
// printing updated word. since 'CoLton' does not have 'e', this would
// print 'CoLton' itself
System.out.println("changed word: " + word);
// changing 't' with '7' and storing updated value in word itself
word = word.replace('t', '7');
// will print 'CoL7on'
System.out.println("changed word: " + word);
// changing 'L' to '1'
word = word.replace('L', '1');
// printing word for one last time
System.out.println("final word: " + word); // Co17on
// displaying length of the word
System.out.println("length of the word: " + word.length());
}
}
/*OUTPUT*/
original word: CoLton
word in lower case: colton
changed word: CoLton
changed word: CoL7on
final word: Co17on
length of the word: 6
Get Answers For Free
Most questions answered within 1 hours.