I am having some trouble writing some java code involving strings. I have included the code provided by my professor below. that has the instructions in it as well
public class StringExercise { public static void main(String[] args) { String given = "The quick brown fox jumped over the moon!"; String given2 = "mary"; String given3 = "D"; //Write a Java code snippet to check (print a boolean) whether the given String ends with the contents of "oon!", see endsWith method //Write a Java code snippet to check (print a boolean) whether the given String starts with the contents of "Th", see startsWith method //Write a Java code snippet to check (print a boolean) whether the given2 String has the same contents as the String "mark", see equals method //Write a Java code snippet to check (print a boolean) whether the given2 String has the same contents, ignoring case, as the String "MARY", see equalsIgnoreCase method //Write a Java code snippet to print all the indices where the letter 'o' is located at in the given String. See indexOf method and use a while loop. //Write a Java code snippet to print the substring of the given String from indices 3 through 8. See substring method. //Write a Java code snippet to compare the given3 String with the String "Z" using the compareTo method. Print the integer that is //returned from the compareTo method. //Write a Java code snippet to compare the given3 String with the String "A" using the compareTo method. Print the integer that is //returned from the compareTo method. //Write a Java code snippet to compare the given3 String with the String "D" using the compareTo method. Print the integer that is //returned from the compareTo method. } }
Code:
public class StringExercise {
public static void main(String[] args) {
String given =
"The quick brown fox jumped over the moon!";
String given2 = "mary";
String given3 = "D";
// Write a Java code snippet to check (print a boolean) whether the given String
// ends with the contents of "oon!", see endsWith method
System.out.println(given.endsWith("oon!"));
// Write a Java code snippet to check (print a boolean) whether the given String
// starts with the contents of "Th", see startsWith method
System.out.println(given.startsWith("Th"));
// Write a Java code snippet to check (print a boolean) whether the given2
// String has the same contents as the String "mark", see equals method
System.out.println(given2.equals("mark"));
// Write a Java code snippet to check (print a boolean) whether the given2
// String has the same contents, ignoring case, as the String "MARY", see
// equalsIgnoreCase method
System.out.println(given2.equalsIgnoreCase("MARY"));
// Write a Java code snippet to print all the indices where the letter 'o' is
// located at in the given String. See indexOf method and use a while loop.
int index = given.indexOf('o');
while (index >= 0) {
System.out.println(index);
index = given.indexOf('o', index + 1);
}
// we get the first index of 'o' and then iterate to find the
next occurences in the remaining string.
// .indexOf('o',index) begins the search from index, so the
previous one is not counted.
// Write a Java code snippet to print the substring of the given String from
// indices 3 through 8. See substring method.
System.out.println(given.substring(3, 9));
//9 is exculsive. So, to get the substring from 3 to 8, we pass 3 and 9 as parameters.
// Write a Java code snippet to compare the given3 String with the String "Z"
// using the compareTo method. Print the integer that is
// returned from the compareTo method.
System.out.println(given3.compareTo("Z"));
// Write a Java code snippet to compare the given3 String with the String "A"
// using the compareTo method. Print the integer that is
// returned from the compareTo method.
System.out.println(given3.compareTo("A"));
// Write a Java code snippet to compare the given3 String with the String "D"
// using the compareTo method. Print the integer that is
// returned from the compareTo method.
System.out.println(given3.compareTo("D"));
}
}
Reference screenshot:
Output of the code:
Explanation:
The string object in java comes with many predefined functions to perform operations on strings. In this exercise you are being asked to use some of them.
String
object lexicographically precedes the argument
string. The result is a positive integer if this
String
object lexicographically follows the argument
string. The result is zero if the strings are equal.Get Answers For Free
Most questions answered within 1 hours.