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
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...
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.
Write a function addStrings(string1, string2) that takes in two decimals as strings and returns a string...
Write a function addStrings(string1, string2) that takes in two decimals as strings and returns a string of their sum. *Simply converting strings to numbers and adding them together isn’t acceptable.* The program must be able to handle large decimals. Be sure to touch on these when solving for the solution: Pad the right side Pad the left side Make sure string is same length by putting 0’s where it was missing (be careful w/ decimal points) Make sure to remove...
Write a function addStrings(string1, string2) that takes in two decimals as strings and returns a string...
Write a function addStrings(string1, string2) that takes in two decimals as strings and returns a string of their sum. *Simply converting strings to numbers and adding them together isn’t acceptable.* The program must be able to handle large decimals. Be sure to touch on these when solving for the solution: Pad the right side Pad the left side Make sure string is same length by putting 0’s where it was missing (be careful w/ decimal points) Make sure to remove...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT