Question
public class StringExercise { public static void main(String[] args) { String binary = 10101001; //Task 1: //Write a Java code snippet to sum up all
public class StringExercise { public static void main(String[] args) { String binary = "10101001"; //Task 1: //Write a Java code snippet to sum up all the '1's in the binary String. //Detail instructions: //Write a for loop to iterate through all the characters in the //binary String. In order to iterate through all the characters //in the String you will need the length. To get the length of the // String use the length() method. //e.g. binary.length(); // To get every character at an index use the getCharAt(int index) method. //e.g. char c = binary.getCharAt(i); //Task 2: //Write a Java code snippet to count the number of '0's in the binary String. //However, iterator backwards through the characters. //Detail instructions: //Write a for loop but have your index start at length() -1, and end //at 0 by decrmenting the index. //Similar to Task 1, use length() and getCharAt to help you. //Task 3: see instructions below. 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 //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. }
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started