Question

I am having some trouble writing some java code involving strings. I have included the code...

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.
    
  }

}

Homework Answers

Answer #1

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.

  • stringVariable.endsWith("parameterString") -- checks if our string ends with the specified string.
  • stringVariable.startsWith("parameterString") -- checks if our string starts with the specified string.
  • stringVariable.equals("parameterString") -- compares the string charater by charater to find if they are equal or not
  • stringVariable.equalsIgnoreCase("parameterString") -- compares the string charater by charater to find if they are equal or not while ignoring the case of characters whether upper or lower.
  • stringVariable.indexOf("string/character") -- returns the first index in the string of character passed as parameter.
  • stringVariable.indexOf("string/character", int index) -- returns the first index in the string of character passed as parameter. It starts the search after the input index.
  • stringVariable.substring(beginIndex, endIndex) -- returns the part of whole string specified by the indices given in parameter. The endIndex is exclusive, meaning substring(3,8) returns the string from index 3 to 7.
  • stringVariable.compareTo("parameterString") -- Compares the strings lexicographically. The result is a negative integer if this 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.
Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Write Java code that attempts to call a method named bootUp() which takes a String parameter...
Write Java code that attempts to call a method named bootUp() which takes a String parameter and returns an integer value. The String parameter should be read from a file named "bootConfig.txt" and is the only value in the file. If a BootUpException is thrown, print the Exception object's message. If a DriverException occurs, call the reboot() method and rethrow the original DriverException object. If any other type of Exception is thrown, print a generic error message to the console....
[Java] I'm not sure how to implement the code. Please check my code at the bottom....
[Java] I'm not sure how to implement the code. Please check my code at the bottom. In this problem you will write several static methods to work with arrays and ArrayLists. Remember that a static method does not work on the instance variables of the class. All the data needed is provided in the parameters. Call the class Util. Notice how the methods are invoked in UtilTester. public static int min(int[] array) gets the minimum value in the array public...
is there anything wrong with the solution. the question are from java course Write a main...
is there anything wrong with the solution. the question are from java course Write a main method that will request the user to enter Strings using a JOptionPane input dialog. The method should continue accepting strings until the user types “STOP”.       Then, using a JOptionPane message dialog, tell the user how many of the strings begin and end with a digit. Answer: import javax.swing.*; public class IsAllLetters {     public static void main(String[] args) {         String input;         int count =...
In this Java programming assignment, you will practice using selection statements to determine whether a given...
In this Java programming assignment, you will practice using selection statements to determine whether a given year in the past or future qualifies as a “Leap Year”. I. Design a class called LeapYear in a file called LeapYear.java. This class will hold the main method and the class method that we will write in this assignment. II. Write an empty public static void main(String[] args) method. This method should appear inside the curly braces of the LeapYear class. III. Write...
Java: ModifyStudentList.javato use anArrayListinstead of an array In StudentList.java, create two new public methods: The addStudent...
Java: ModifyStudentList.javato use anArrayListinstead of an array In StudentList.java, create two new public methods: The addStudent method should have one parameter of type Student and should add the given Student to the StudentList. The removeStudent method should have one parameter of type String. The String is the email of the student to be removed from the StudentList. In Assign5Test.java do the following: Create a new StudentList containing 3 students. Print the info of all the Students in the StudentList using...
1. Suppose that the integer variables n and m have been initialized. Check all the correct...
1. Suppose that the integer variables n and m have been initialized. Check all the correct statements about the following code fragment. String s2 = (n>=m) ? "one" : ( (m<=2*n) ? "two": "three" ); Question 5 options: If m is strictly greater than 2n then the value of s2 is "two" If n is equal to m then the value of s2 is "two" If m is strictly greater than 2n then the value of s2 is "three" If...
THIS IS FOR JAVA I have to write a method for a game of Hangman. The...
THIS IS FOR JAVA I have to write a method for a game of Hangman. The word the user is trying to guess is made up of hashtags like so " ###### " If the user guesses a letter correctly then that letter is revealed on the hashtags like so "##e##e##" If the user guesses incorrectly then it increments an int variable named count " ++count; " String guessWord(String guess,String word, String pound) In this method, you compare the character(letter)...
Code in JAVA The requirements are as follows: The input will be in a text file...
Code in JAVA The requirements are as follows: The input will be in a text file whose name is given by arg[0] of main(). It will contain a fully-parenthesized infix expression containing only: "(", ")", "+", "-" and integers. Need help on the main and fixing the Queue. //Input: ( ( 1 + 2 ) - ( ( 3 - 4 ) + ( 7 - 2 ) ) ) ( ( 1 + 2 ) - ( 3 -...
In java create a dice game called sequences also known as straight shooter. Each player in...
In java create a dice game called sequences also known as straight shooter. Each player in turn rolls SIX dice and scores points for any sequence of CONSECUTIVE numbers thrown beginning with 1. In the event of two or more of the same number being rolled only one counts. However, a throw that contains three 1's cancels out player's score and they mst start from 0. A total of scores is kept and the first player to reach 100 points,...
this is the book name. Data Structures and Abstractions with Java 1) Description: The sample programs...
this is the book name. Data Structures and Abstractions with Java 1) Description: The sample programs in Chapter 1 of your textbook are not complete. They are used for illustration purpose only. The implementation of Listing 1-1 on page 39 is explained in Chapter 2. And, in order to see the result of using it, we will need the following set of files: i. BagInteface.java – the specification only. ii. ArrayBag.java – the implementation of BagInerface.java. iii. ArrayBagDemo.java – a...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT