Write a Java program based on the design you created in Exercise 1a. Enter the p
ID: 3926534 • Letter: W
Question
Write a Java program based on the design you created in Exercise 1a. Enter the program, saving it as Words.java, and then execute the program. Copy the output and save it according to your instructor's requirements. Text output can be saved in a block comment at the end of your program. Save your source code to the device or location specified your instructor. Following is a copy of the screen results that should appear. This program demonstrates commonly used string methods. The output displayed uses the string values "Hi" and "Hello World" The length of the string "Hello World" is 11 The index of the first character 'o' in "Hello World" is 4 The index of the second character 'o' in "Hello World" is 7 The concatenation of "Hi" and "World" is "Hi World" The string value "Hello World" in all lowercase is hello world The string value "Hello World" in all uppercase is HELLO WORLDExplanation / Answer
import java.util.*;
import java.lang.String;
public class Words {
public static void main(String[] args) {
String str1="Hi";
String str2="Hello World";
/*
methods that i have used:
length() for finding string length ex: string.length()
string.indexOf('') here need to supply char it will return first index of char
string.indexOf('o',5));//returns the o of is substring after 5th o
we can Concatenation strings using +
string.toLowerCase()// to change lowercase
string.toUpperCase()// to change all char to uppercase
*/
System.out.println("The length of the string"+str2+" is "+str2.length());
System.out.println("The index of the first character 'o' in "+str2+" is "+str2.indexOf('o'));
System.out.println("The index of the second character 'o' in "+str2+" is "+str2.indexOf('o',5));
System.out.println("The Concatenation of 'H' and 'Hello World' is "+str1+str2);
System.out.println("The String value 'Hello World' in all lower case' is +"+str2.toLowerCase());
System.out.println("The String value 'Hello World' in all upper case' is +"+str2.toUpperCase() );
}
}
/******* output ********************
The length of the stringHello World is 11
The index of the first character 'o' in Hello World is 4
The index of the second character 'o' in Hello World is 7
The Concatenation of 'H' and 'Hello World' is HiHello World
The String value 'Hello World' in all lower case' is +hello world
The String value 'Hello World' in all upper case' is +HELLO WORLD
*/
import java.util.*;
import java.lang.String;
public class Words {
public static void main(String[] args) {
String str1="Hi";
String str2="Hello World";
/*
methods that i have used:
length() for finding string length ex: string.length()
string.indexOf('') here need to supply char it will return first index of char
string.indexOf('o',5));//returns the o of is substring after 5th o
we can Concatenation strings using +
string.toLowerCase()// to change lowercase
string.toUpperCase()// to change all char to uppercase
*/
System.out.println("The length of the string"+str2+" is "+str2.length());
System.out.println("The index of the first character 'o' in "+str2+" is "+str2.indexOf('o'));
System.out.println("The index of the second character 'o' in "+str2+" is "+str2.indexOf('o',5));
System.out.println("The Concatenation of 'H' and 'Hello World' is "+str1+str2);
System.out.println("The String value 'Hello World' in all lower case' is +"+str2.toLowerCase());
System.out.println("The String value 'Hello World' in all upper case' is +"+str2.toUpperCase() );
}
}
/******* output ********************
The length of the stringHello World is 11
The index of the first character 'o' in Hello World is 4
The index of the second character 'o' in Hello World is 7
The Concatenation of 'H' and 'Hello World' is HiHello World
The String value 'Hello World' in all lower case' is +hello world
The String value 'Hello World' in all upper case' is +HELLO WORLD
*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.