Question

In Java, create a list that represents a sentence. Then: Write a method that counts and...

In Java, create a list that represents a sentence. Then:

  1. Write a method that counts and returns the number of punctuation in a given sentence. Express this as a higher-order function and implement it.

  2. Write a method that counts and returns the number of words in the sentence that have the letter ’z’ in them. Express this as a higher-order function and implement it.

0 0
Add a comment Improve this question Transcribed image text
Answer #1
Please find below code also added main function for testing
//Program.java

public class Program {
  
    public static int countPunctuation(String[] list) {
        int count = 0;
        // looping through list of words
        for (String word : list) {
            // looping through the each char of word
            for (int i = 0; i < word.length(); i++) {
                char c = word.charAt(i);
                // if char is whitespace or letter or alphabets
                if (Character.isWhitespace(c) || Character.isLetterOrDigit(c) || Character.isSpaceChar(c))
                    continue;
                // if punctuation increasing the count
                count++;
            }
        }
        // returning count
        return count;
    }

    public static int countZ(String list[]) {
        int count = 0;
        // looping through words
        for (String word : list) {
            // if z in words increasing the count
            if (word.contains("z")) count++;
        }
        return count;
    }

    public static void main(String[] args) {
        String list[] = {"this", "is", "a ", "list's", "of, words!"};
        System.out.println("Count of punctuation in \"this\", \"is\", \"a \" , \"list's\", \"of, words!\": " + countPunctuation(list));
        String list2[] = {"zoom", "the", "scene", "of", "zebra."};
        System.out.println("Count of 'z' in \"zoom\", \"the\",\"scene\",\"of\",\"zebra.\" in: " + countZ(list2));
    }
}


//OUTPUT

a the.scene.of. zebra. in: 2 Count of punctuation in this Count of z in zoom of, words!: 3 is lists,

Please do let me know if u have any concern...

Add a comment
Know the answer?
Add Answer to:
In Java, create a list that represents a sentence. Then: Write a method that counts and...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • USING JAVA You will be given a sentence and you will need to correct to proper...

    USING JAVA You will be given a sentence and you will need to correct to proper sentence. - Make sure first word and first letter is capitalized. - Make sure sentence ends with period. Create a class there should be NO field level variables In that class create a method that takes the sentence and returns corrected sentence. Create another method (only used by created class) that makes sure and corrects that first work first letter is capitalized public class...

  • write a program in java that asks the user for a sentence, and then returns that...

    write a program in java that asks the user for a sentence, and then returns that string with the words backwards. For example: Enter a sentence! Mary had a little lamb. Your sentence backwards: lamb. little a had Mary Write a method called reverse() that accepts a sentence as a string, and then returns that string with the words backwards. Write a main() method that gets the string from the user and then displays the string backwards. demonstrate program by...

  • Create a method in Java that adds a task to a list and returns false if...

    Create a method in Java that adds a task to a list and returns false if the task is already part of the list. Create another method to remove tasks from the list, and returns false if the task is not part of the list.

  • In java: A fun language: f you recently ate at Chipotle, you may have received a...

    In java: A fun language: f you recently ate at Chipotle, you may have received a brown paper bag with gibberish letters written on it: “AKINGMAY AWAY IGPAY EALDAY ABOUTWAY IGPAY ATINLAY”. In fact this message is written in Piglatin, a simple but fictitious language. Piglatin converts a word in English using the following two rules: If the letter begins with a vowel, then “WAY” is appended to the word. If the letter begins with a consonant (a letter other...

  • In Java: Let's create a method that has parameters. Write a method, called returnHours that returns...

    In Java: Let's create a method that has parameters. Write a method, called returnHours that returns a string. Make the string from a string representing your name and a number representing the hours you spend sleeping, both are values that you pass in from the main. Create the variables to pass into the method call in main. Call the method and print the return value in main. Run and test it.

  • Java Write a method countStairs that, given a number of blocks, counts and returns the number...

    Java Write a method countStairs that, given a number of blocks, counts and returns the number of stairs you could build with the blocks. Stairs are built in a staircase with one block for the first step, two for the second step, three for the third step, and so on, like this picture which shows * for each block in a staircase: * ** *** If you had 5 blocks, you could only build 2 stairs (which requires 1+2 =...

  • use java code Write a collection of array utility methods. •Write a method to create a...

    use java code Write a collection of array utility methods. •Write a method to create a list of duplicate numbers in an array. •Write a method to create a list of duplicate Strings in an array. •Convert to using generics.

  • Need help with this. Java please! Write a recursive method that counts the number of bowling...

    Need help with this. Java please! Write a recursive method that counts the number of bowling pins given the number of pins in the back row: 1. public static int numPins (int num) numPins(0)-> 0 numPins(1) → 1 numPins(2)-> 3

  • Python: Create a function count_target_in_list that takes a list of integers and the target value then...

    Python: Create a function count_target_in_list that takes a list of integers and the target value then counts and returns the number of times the target value appears in the list. Write program in that uses get_int_list_from_user method to get a list of 10 numbers, then calls the count_target_list method to get the count then prints the number of times the target was found in the list. def get_int_list_from_user(): lst=[] y = int(input("Enter number of numbers")) for x in range(y): lst.append(int(input("Enter...

  • java code Write a method that given a list of integers as a reference to its...

    java code Write a method that given a list of integers as a reference to its first node, determines if the list is sorted in non-decreasing order. Please select file(s) Select file(s)

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT