Question

In: Computer Science

VI. Answer part A, B, and C of question 5. 5a) Given a String str1 and...

VI. Answer part A, B, and C of question 5.

5a)

Given a String str1 and a String str2, return true if str1 contains any character in str2


anyChar("abc", "xyz") → false
anyChar("abc", "xya") → true
anyChar("abc", "xyzxyzbxyzxyz") → true

5b)

Given a String str1 and a String str2, return true if str1 does not contains any character in str2, and false otherwise.

noChar("abc", "xyz") → true
noChar("abc", "xya") → false
noChar("xyzxyzbxyzxyz", "xyzb") → false

5c)

Given a String str1 and a String str2, return true if str1 contains every character in str2. The empty string is contained in any other String, including the empty string itself.


everyChar("abc", "xyz") → false
everyChar("abc", "xya") → false
everyChar("xyzxyzbxyzxyz", "xyzb") → true

Solutions

Expert Solution

// 5a)
public static boolean anyChar(String s1, String s2) {
    for (int i = 0; i < s2.length(); i++) {
        if (s1.contains("" + s2.charAt(i))) {
            return true;
        }
    }
    return false;
}

// 5b)
public static boolean noChar(String s1, String s2) {
    for (int i = 0; i < s2.length(); i++) {
        if (s1.contains("" + s2.charAt(i))) {
            return false;
        }
    }
    return true;
}

// 5c)
public static boolean everyChar(String s1, String s2) {
    for (int i = 0; i < s2.length(); i++) {
        if (!s1.contains("" + s2.charAt(i))) {
            return false;
        }
    }
    return true;
}

public class StringMethods {

    // 5a)
    public static boolean anyChar(String s1, String s2) {
        for (int i = 0; i < s2.length(); i++) {
            if (s1.contains("" + s2.charAt(i))) {
                return true;
            }
        }
        return false;
    }

    // 5b)
    public static boolean noChar(String s1, String s2) {
        for (int i = 0; i < s2.length(); i++) {
            if (s1.contains("" + s2.charAt(i))) {
                return false;
            }
        }
        return true;
    }

    // 5c)
    public static boolean everyChar(String s1, String s2) {
        for (int i = 0; i < s2.length(); i++) {
            if (!s1.contains("" + s2.charAt(i))) {
                return false;
            }
        }
        return true;
    }

    public static void main(String[] args) {
        System.out.println(anyChar("abc", "xyz"));
        System.out.println(anyChar("abc", "xya"));
        System.out.println(anyChar("abc", "xyzxyzbxyzxyz"));

        System.out.println(noChar("abc", "xyz"));
        System.out.println(noChar("abc", "xya"));
        System.out.println(noChar("xyzxyzbxyzxyz", "xyzb"));

        System.out.println(everyChar("abc", "xyz"));
        System.out.println(everyChar("abc", "xya"));
        System.out.println(everyChar("xyzxyzbxyzxyz", "xyzb"));
    }
}


Related Solutions

Part 1: answer (a), (b), (c), and (d). Part 2: answer (a), (b), (c), and (d)....
Part 1: answer (a), (b), (c), and (d). Part 2: answer (a), (b), (c), and (d). Godspeed, and good luck!!! CC11 Cookie Creations Natalie and her friend Curtis Lesperance decide that they can benefit from joining Cookie Creations and Curtis’s coffee shop. In the first part of this problem, they come to you with questions about setting up a corporation for their new business. In the second part of the problem, they want your help in preparing financial information following...
I need the answer for PART B and PART C of this question: You have recently...
I need the answer for PART B and PART C of this question: You have recently been appointed management accountant for Rugby Coffee Mugs Pty Ltd. The company commenced its operations on 1 July 2019 manufacturing one size coffee mugs with individual club names and club logos of rugby union clubs playing in the New South Wales, Queensland, Victoria and Western Australia local rugby union competition. The company currently does not have any management accounting controls and part of your...
I. Answer part A, B, and C 1a) Return true if the given int parameter is...
I. Answer part A, B, and C 1a) Return true if the given int parameter is both positive and less than 10, and false otherwise. Remember that 0 is not positive! positiveLessThan10(10) → false positiveLessThan10(9) → true positiveLessThan10(11) → false 1b) Return true if the parameters are in ascending order, from left to right. Two values that are equal are considered to be in ascending order. Remember, you can just type return true or return false to return true or...
I. Answer part A,B, C and D. 1a) Count the number of times a given char...
I. Answer part A,B, C and D. 1a) Count the number of times a given char occurs in a given range of a String parameter (i.e. starting at a given start index, and up to but not including and end index). If end is greater than the length of the String, then the method should stop looking at the end of the String. countCharsInRange("java", "v", 1, 2) → 0 countCharsInRange("java", "v", 0, 2) → 0 countCharsInRange("java", "v", 0, 3) →...
Question 3: C-Strings and pointers (10 pts) [5] The following function appends C-string str2, to C-string...
Question 3: C-Strings and pointers (10 pts) [5] The following function appends C-string str2, to C-string str1. Complete the function using array access, i.e. str1[i], but no pointer access. Note: For both part a) and part b) you may not use any library function calls (e.g. you cannot use strlen, strcat, etc.) // Append strt2 to str1 void my_strcat(char str1[], char str2[]) { //YOUR CODE HERE   } // example of using my_strcat() #include <stdio.h> int main(void) { char my_str1[50] =...
Please answer part B question c. Pete Sandstrom is the CEO of WeRHere4U Ltd. He is...
Please answer part B question c. Pete Sandstrom is the CEO of WeRHere4U Ltd. He is speaking with you having shown you these financial statements for the years 2016 through 2019. He is extremely proud of his results; Net profit has steadily increased from $67,200 in 2017 to $73,500 in 2018 and in 2019, $83,300. He also highlights to you that the Return on Equity (ROE) at WeRHere4U Ltd has also improved substantially form 51% in 2017 to 62% in...
Part VI Push-Down Accounting (continued) Question B: Assume that Pop Corporation acquires a 90 percent interest...
Part VI Push-Down Accounting (continued) Question B: Assume that Pop Corporation acquires a 90 percent interest in Son Corporation for $225,000 cash on January 1, 2016. Comparative balance sheets of the two companies immediately before the acquisition are as follows (in thousands):                                                             Pop                                                          Son____________                                                 Book Value      Fair Value                    Book Value      Fair Value Cash                                         $300                $300                            $ 10               $ 10 Accounts Receivable, net        100                100                                35                     40 Inventories                              110                140                                45                    ...
C question Using a for-loop: (a) calculate the length of a string, (b) find a word...
C question Using a for-loop: (a) calculate the length of a string, (b) find a word in a string
PART 1 The weighted voting systems for the voters A, B, C, ... are given in...
PART 1 The weighted voting systems for the voters A, B, C, ... are given in the form {q: w1, w2, w3, w4, ..., wn}. The weight of voter A is w1, the weight of voter B is w2, the weight of voter C is w3, and so on. Consider the weighted voting system {78: 4, 74, 77}. (a) Compute the Banzhaf power index for each voter in this system. (Round your answers to the nearest hundredth.) BPI(A) = BPI(B)...
23. Given a = 5, b = 4, c = 2, evaluate the following: a) a//c...
23. Given a = 5, b = 4, c = 2, evaluate the following: a) a//c b) a % b c) b **c d) b *= c 27. Given the following var_1 = 2.0 var_2 = "apple" var_3 = 'orange' var_4 = 4 Predict the output of the following statements or indicate that there would be an error. a) print (var_1) b) print (var_2) c) print ("var_3") d) print (var_1 / var_4) e) print (var_4 + var_3) f) print (var_2...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT