Question

In: Computer Science

IV. Answer parts A,B, and C 4a) Return true if the letter 'a' occurs in the...

IV. Answer parts A,B, and C

4a)

Return true if the letter 'a' occurs in the given String, and false otherwise.


containsA("abc") → true
containsA("wxyz") → false
containsA("bcaca") → true

4b)

Return true if the letter 'a' does not occur in the given String, and false otherwise.


notContainsA("abc") → false
notContainsA("wxyz") → true
notContainsA("bcaca") → false

4c)

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) → 1

Solutions

Expert Solution

// 4a)
public static boolean containsA(String s) {
    return s.contains("a");
}

// 4b)
public static boolean notContainsA(String s) {
    return !s.contains("a");
}

// 4c)
public static int countCharsInRange(String s, String ch, int start, int end) {
    int count = 0;
    for (int i = start; i < end; i++) {
        if (s.charAt(i) == ch.charAt(0))
            ++count;
    }
    return count;
}

public class StringMethods {

    // 4a)
    public static boolean containsA(String s) {
        return s.contains("a");
    }

    // 4b)
    public static boolean notContainsA(String s) {
        return !s.contains("a");
    }

    // 4c)
    public static int countCharsInRange(String s, String ch, int start, int end) {
        int count = 0;
        for (int i = start; i < end; i++) {
            if (s.charAt(i) == ch.charAt(0))
                ++count;
        }
        return count;
    }

    public static void main(String[] args) {
        System.out.println(containsA("abc"));
        System.out.println(containsA("wxyz"));
        System.out.println(containsA("bcaca"));

        System.out.println(notContainsA("abc"));
        System.out.println(notContainsA("wxyz"));
        System.out.println(notContainsA("bcaca"));

        System.out.println(countCharsInRange("java", "v", 1, 2));
        System.out.println(countCharsInRange("java", "v", 0, 2));
        System.out.println(countCharsInRange("java", "v", 0, 3));
    }
}


Related Solutions

III. Answer part A,B, and C. (in JAVA) 3a) Return the number of times the letter...
III. Answer part A,B, and C. (in JAVA) 3a) Return the number of times the letter 'a' occurs in a String. countA("abc") → 1 countA("wxyz") → 0 countA("bcaca") → 2 3b) Return the number of times the given letter ch occurs in the given String str. countNumChar("abc", "a") → 1 countNumChar("wxyz", "b") → 0 countNumChar("bcaca", "c") → 2 3c) Count the number of characters that are either a or b in a String. Try to use only one for loop....
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...
Answer the questions in the context of price elasticity. Answer parts A, B and C independently....
Answer the questions in the context of price elasticity. Answer parts A, B and C independently. The price elasticity of demand of product X is 0.6. The equilibrium price of product X is P1 and the equilibrium quantity is Q1. A.   The economy is very weak recently and the suppliers of product X are making less profit than before and some of the suppliers are even having a loss. As a result, many suppliers of product X are closing down...
8. Use the following table to answer questions I, ii, iii and iv A B C...
8. Use the following table to answer questions I, ii, iii and iv A B C Initial cost $15,000 $9,000 $12,000 Annual benefit $8,000 $2,000 $1,800 Salvage value $5,000 $9,000 0 Life in years 2 years 3 Years Infinity MARR 10% i. The NPW of alt. A is __________________. A) $13,420 B) $17,380 C) $11,000 D) $6,00 ii. The NPW of alt. B is __________________. A) $13,420 B) $17,380 C) $11,000 D) $6,000 iii. The NPW of alt. C is...
Answer ALL parts. (a) The reaction B + C → D just becomes spontaneous at a...
Answer ALL parts. (a) The reaction B + C → D just becomes spontaneous at a temperature of 78.1 °C. When the temperature is increased to 220 °C: (i) Calculate Gibbs free energy (ii) Calculate equilibrium constant (iii) State whether the products or reactants are favoured at this temperature and explain your reasoning (iv) State the assumptions associated with your calculations. Useful data for this question: ΔSreaction (B + C → D) = 259.4 J K–1 mol–
Answer all parts (a), (b), and (c) of this question. [10 marks] Define and explain the...
Answer all parts (a), (b), and (c) of this question. [10 marks] Define and explain the concepts of moral hazard and adverse selection. Illustrate each concept with two examples: one in the context of new technology sale, the other in the context of new technology funding. [20 marks] Explain why asymmetric information may cause a market failure in the markets for selling new technology and the market for funding new technological development.    [20 marks] Propose and defend a solution to...
2. Answer all parts (a), (b), and (c) of this question. (a) [10 marks] Define and...
2. Answer all parts (a), (b), and (c) of this question. (a) [10 marks] Define and explain the concepts of moral hazard and adverse selection. Illustrate each concept with two examples: one in the context of new technology sale, the other in the context of new technology funding. (b) [20 marks] Explain why asymmetric information may cause a market failure in the markets for selling new technology and the market for funding new technological development. (c) [20 marks] Propose and...
(6ptseach)Let A={a,b,c},B={b,c,d},C ={b,c,e}. (a) Explicitly find (i) A∪(B∩C), (ii)(A∪B)∩C, and (iii)(A∪B)∩(A∪C). (iv)Which of these sets are...
(6ptseach)Let A={a,b,c},B={b,c,d},C ={b,c,e}. (a) Explicitly find (i) A∪(B∩C), (ii)(A∪B)∩C, and (iii)(A∪B)∩(A∪C). (iv)Which of these sets are equal? (b) Explicitly find (i) A∩(B∪C), (ii)(A∩B)∪C, and (iii)(A∩B)∪(A∩C). (iv)Which of these sets are equal? (c) Explicitly find (i)(A−B)−C and (ii) A−(B−C). (iii)Are these sets equal?
PLEASE ANSWER PARTS A, B, C, D, AND E. A rocket is launched straight up with...
PLEASE ANSWER PARTS A, B, C, D, AND E. A rocket is launched straight up with a constant acceleration of 25 m/s2. Six seconds after liftoff, a bolt falls off the side of the rocket. (a) Determine the velocity of the bolt just before it falls off the side of the rocket. (b) Determine the altitude of the bolt (measured from the ground) just before it falls off the side of the rocket. (c) How long is the bolt in...
Answer all parts (a), (b), and (c) of this question. [10 marks] Illustrate and explain what...
Answer all parts (a), (b), and (c) of this question. [10 marks] Illustrate and explain what is meant by a “double marginalisation problem” in technology licensing. [20 marks] A single (monopoly) upstream licensor proposes to supply an input under license to a single (monopoly) downstream licensee. This input has the effect of reducing the marginal cost of production of the downstream firm. The licensor proposes a contract whereby the licensee pays an up-front fixed fee but no recurrent royalty for...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT