Question

In: Computer Science

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.

countAorB("abc") → 2
countAorB("xyz") → 0
countAorB("ab") → 2

Solutions

Expert Solution

public class Main{
        // it counts number of a
        public int countA(String s){
                int i, count=0;
                for(i=0;i<s.length();i++){
                        if(s.charAt(i)=='a')
                                count++;
                }
                return count;
        }
        
        // it counts number of a charater 
        public int countNumChar(String s, char x){
                int i, count=0;
                for(i=0;i<s.length();i++){
                        if(s.charAt(i)==x)
                                count++;
                }
                return count;
        }
        
        // it counts chracters either a or b
        public int countAorB(String s){
                int i, count=0;
                for(i=0;i<s.length();i++){
                        if(s.charAt(i)=='a'|| s.charAt(i)=='b')
                                count++;
                }
                return count;
        }
        
        
        public static void main(String args[]){
                // declare object of class
                Main obj=new Main();
                
                // call the functions and display the result
                System.out.println("Number of `a' in "+"abc"+" is: "+obj.countA("abc"));
                System.out.println("Number of `a' in "+"wxyz"+" is: "+obj.countA("wxyz"));
                System.out.println("Number of `a' in "+"bcaca"+" is: "+obj.countA("bcaca")+"\n");
                
                System.out.println("Number of `a' in "+"abc"+" is: "+obj.countNumChar("abc",'a'));
                System.out.println("Number of `b' in "+"wxyz"+" is: "+obj.countNumChar("wxyz",'b'));
                System.out.println("Number of `c' in "+"bcaca"+" is: "+obj.countNumChar("bcaca",'c')+"\n");
                
                System.out.println("Number of `a' or `b' in "+"abc"+" is: "+obj.countAorB("abc"));
                System.out.println("Number of `a' or `b' in "+"xyz"+" is: "+obj.countAorB("xyz"));
                System.out.println("Number of `a' or `b' in "+"ab"+" is: "+obj.countAorB("ab"));
        }
}

___________________________________________________________________

Number of `a' in abc is: 1
Number of `a' in wxyz is: 0
Number of `a' in bcaca is: 2

Number of `a' in abc is: 1
Number of `b' in wxyz is: 0
Number of `c' in bcaca is: 2

Number of `a' or `b' in abc is: 2
Number of `a' or `b' in xyz is: 0
Number of `a' or `b' in ab is: 2

___________________________________________________________________


Note: If you have queries or confusion regarding this question, please leave a comment. I would be happy to help you. If you find it to be useful, please upvote.


Related Solutions

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...
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. 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...
Part A Calculate the number of C atoms in 0.602 mole of C. Part B Calculate...
Part A Calculate the number of C atoms in 0.602 mole of C. Part B Calculate the number of SO2 molecules in 6.82 moles of SO2. Part C Calculate the moles of Fe in 2.34x1022 atoms of Fe. Part D Calculate the moles of C2H6O in 3.97x1024 molecules of C2H6O.
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...
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...
String search   Describe the brute force solution to counting the number of times the letter “a”...
String search   Describe the brute force solution to counting the number of times the letter “a” occurs in a text. Outline a divide-and-conquer algorithm for counting the number of times the letter “a” occurs in a text. Analyze each approach and compare the efficiencies.
Create an Java application that uses a class to convert number grades to letter grades and...
Create an Java application that uses a class to convert number grades to letter grades and another class for data validation. Specifications The Grade class should have only one Instance Variable of type int. Use a class named Grade to store the data for each grade. This class should include these three methods:   public void setNumber(int number)   public int getNumber()   public String getLetter() The Grade class should have two constructors. The first one should accept no parameters and set the...
(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 all the questions with the question number and the correct answer (letter) next to...
Please answer all the questions with the question number and the correct answer (letter) next to it :) 27-Some of the best ways to learn about small business include: A Learn from successful entrepreneurs in the field. B College classes on small business. C Working in the field either in as an intern or employee. D All of the above. 28- By becoming certified as a welder, a manager can go a long way toward acquiring the _________ skills managers...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT