Question

In: Computer Science

Write a static method endsWith that inputs two Strings and returns a boolean. If the first...

Write a static method endsWith that inputs two Strings and returns a boolean. If the first input ends with the substring that is the second input, then the method returns true; otherwise, it returns false.

For example,

endsWith( "radar installation", "rad" )

returns

false

endsWith( "radar installation", "installation" )

returns

true

endsWith( "radar installation", "" )

returns

true

endsWith( "", "a" )

returns

false

endsWith( "", "" )

returns

true

Solutions

Expert Solution

public static boolean endsWith(String s1, String s2) {
    if (s2.length() <= s1.length()) {
        for (int i = 0; i < s2.length(); i++) {
            if (s1.charAt(s1.length() - i - 1) != s2.charAt(s2.length() - i - 1)) {
                return false;
            }
        }
        return true;
    }
    return false;
}

public class EndsWith {

    public static boolean endsWith(String s1, String s2) {
        if (s2.length() <= s1.length()) {
            for (int i = 0; i < s2.length(); i++) {
                if (s1.charAt(s1.length() - i - 1) != s2.charAt(s2.length() - i - 1)) {
                    return false;
                }
            }
            return true;
        }
        return false;
    }

    public static void main(String[] args) {
        System.out.println(endsWith("radar installation", "rad"));
        System.out.println(endsWith("radar installation", "installation"));
        System.out.println(endsWith("radar installation", ""));
        System.out.println(endsWith("", "a"));
        System.out.println(endsWith("", ""));
    }
}


Related Solutions

Write a static method startsWith that inputs two Strings and returns a boolean. If the first...
Write a static method startsWith that inputs two Strings and returns a boolean. If the first input starts with the substring that is the second input, then the method returns true; otherwise, it returns false. For example, startsWith( "radar installation", "rad" ) returns true startsWith( "radar installation", "installation" ) returns false startsWith( "radar installation", "" ) returns true startsWith( "", "a" ) returns false startsWith( "", "" ) returns true
Java Write a method, makeUserName, that is passed two Strings and an int: the first is...
Java Write a method, makeUserName, that is passed two Strings and an int: the first is a first name, the second is a last name, and the last is a random number. The method returns a user name that contains the last character of the first name, the first five characters of the last name (assume there are five or more characters in last name), and the random number. An example: Assume that “John”, “Smith”, 45, are passed when the...
Write code for a short method that does the following: accepts two strings as parameters, first...
Write code for a short method that does the following: accepts two strings as parameters, first name, and last name; Outputs the following message, concatenated together in one line of output:
Write a method public static boolean isPalindrome(String input) that uses one or more stacks to determine...
Write a method public static boolean isPalindrome(String input) that uses one or more stacks to determine if a given string is a palindrome. [A palindrome is a string that reads the same forwards and backwards, for example ‘racecar’, ‘civic’]. Make sure that your method works correctly for special cases, if any. (b) What is the big-O complexity of your method in terms of the list size n.
Write a method that takes four strings as parameter. The first string should be a pokemon...
Write a method that takes four strings as parameter. The first string should be a pokemon name, the second a pokemon type, the third a pokemon name, and the fourth a pokemon type. The method should print out which pokemon has the advantage over the other based on their type. In this case you can use fire, water, and leaf. Example outputs should be like: Example: Pokemon X (Water) has the advantage over Pokemon Y (fire) Pokemon X (Fire) has...
With four Boolean inputs mentioned in the first four columns of the following table, report the...
With four Boolean inputs mentioned in the first four columns of the following table, report the outputs from the circuits described by the Boolean expressions in the topmost row of the table in the 5th and 6th columns, in the corresponding rows in the 5th and 6th columns of the table. The circuit in the 5th column uses two AND gates, two NOT gates, and one OR gate. The circuit in the 6th column uses two AND gates and one...
Create a method:         Public Static boolean isSubArray489(int[] intArray) This method has an array of integers as its...
Create a method:         Public Static boolean isSubArray489(int[] intArray) This method has an array of integers as its input parameter, we'll say that a SubArray 9,8,7is that three integers, 9,8,7values appearing decreasing order one by one in the array. Return true if the array does contain any SubArray “987” Notice that any 3 integers that presenting a decreasing by one order must return True. (987, 876,765,654, 543, 432,321, 210 are all True) Call this method in your main function, and pass in...
Write a Python function that takes two parameters: the first a list of strings and the...
Write a Python function that takes two parameters: the first a list of strings and the second a single string. The function should return True or False depending on whether the string is in the list or not. For example, if the list contains eggs, milk, bananas, and the second parameter is pumpkin, the function should return False. Thank you.
Write a Python function that takes two parameters: the first a list of strings and the...
Write a Python function that takes two parameters: the first a list of strings and the second a single string. The function should return True or False depending on whether the string is in the list or not. For example, if the list contains eggs, milk, bananas, and the second parameter is pumpkin, the function should return False. Thank you.
Java please. Write a static method sqrt()that takes a double argument and returns the square root...
Java please. Write a static method sqrt()that takes a double argument and returns the square root of that number using newton's method to compute result.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT