Question

Please write in Java and Array list. 3 THE COUNT LIKE PROBLEM Start with 1 String...

Please write in Java and Array list.

3 THE COUNT LIKE PROBLEM

Start with 1 String array wordList and one String word. Your method should calculate and return the number of times that word appears in the array wordList.

countLike({"a", "a", "b", "c"}, "a") → 2

countLike({"a", "a", "b", "c"}, "c") → 1

countLike({"a", "a", "b", "c"}, "3") → 0

4 THE AVERAGE EVENS PROBLEM

Start with an int array named nums. Return the average of all the even numbers in the array. A number (n) is even if n % 2 == 0. If there are no even numbers in the array, return -1.

averageEvens({1, 2, 3}) → 2.0

averageEvens({1, 5}) → -1.0

averageEvens({2, 3, 4}) → 3.0

0 0
Add a comment Improve this question Transcribed image text
Answer #1
// 3
public int countLike(ArrayList<String> arr, String word) {
    int count = 0;
    for (int i = 0; i < arr.size(); i++) {
        if (arr.get(i).equals(word))
            ++count;
    }
    return count;
}

// 4
public double averageEvens(ArrayList<Integer> nums) {
    double sum = 0, count = 0;
    for (int i = 0; i < nums.size(); i++) {
        if (nums.get(i) % 2 == 0) {
            sum += nums.get(i);
            ++count;
        }
    }
    if (count == 0)
        return -1;
    else
        return sum/count;
}
Add a comment
Know the answer?
Add Answer to:
Please write in Java and Array list. 3 THE COUNT LIKE PROBLEM Start with 1 String...
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
  • Please write a Java program: Given an array of positive integers, return a count of the...

    Please write a Java program: Given an array of positive integers, return a count of the number of even integers. countEvens([2, 3, 5])-1 countEvens([4, 20]) - 2 countEvens([3, 7, 1, 11]) 0 Go Save, Compile, Run (ctrl-enter) int countEvens (int[] nums) {

  • Hello Sir, I need help with Java. Here is the problem below. Problem 1.Write a program...

    Hello Sir, I need help with Java. Here is the problem below. Problem 1.Write a program that prompts the user to enter the number of milliseconds and converts the milliseconds to a string hours:minutes:seconds. The program should use the convertMillismethod with the following header: public static String convertMillis(long millis) For example, convertMillis(5500) returns the string 0:0:5, convertMillis(100000) returns the string 0:1:40, and convertMillis(555550000) returns the string 154:19:10. Problem 2. (Count occurrence of numbers)Write a program that reads integers between 1...

  • I need help writing this code for java class. Starter file: Project3.java and input file: dictionary.txt...

    I need help writing this code for java class. Starter file: Project3.java and input file: dictionary.txt Project#3 is an extension of the concepts and tasks of Lab#3. You will again read the dictionary file and resize the array as needed to store the words. Project#3 will require you to update a frequency counter of word lengths every time a word is read from the dictionary into the wordList. When your program is finished this histogram array will contain the following:...

  • Execute your program like this: C:\> java Lab4 10000ints.txt 172822words.txt Be sure to put the ints...

    Execute your program like this: C:\> java Lab4 10000ints.txt 172822words.txt Be sure to put the ints filename before the words filename. The starter file will be expecting them in that order. Lab#4's main method is completely written. Do not modify main. Just fill in the methods. Main will load a large arrays of int, and then load a large array of Strings. As usual the read loops for each file will be calling a resize method as needed. Once the...

  • Write a function with two input parameters: an array of numbers and the size of the...

    Write a function with two input parameters: an array of numbers and the size of the array (the number of elements). The function should return the count of even numbers in the array. For example, if the input to the function is the array [3, 2, 45, 56, 12], the function would return the integer 3. Use the following function header: int countEvens(int nums[], int size) { }

  • 1) Write a method that will return the length of smallest string in an array of...

    1) Write a method that will return the length of smallest string in an array of strings. smallestLength ({“hola”, “word”, “night”, “boom”}) → 4 smallestLength ({“java”, “is”, “so”, “much”, “fun”}) → 2 smallestLength ({“i”, “love”, “java”}) → 1 //precondition: strs.length>0 public static int smallestLength(String[] strs) { }

  • package week_3; /** Write a method called countUppercase that takes a String array argument. You can...

    package week_3; /** Write a method called countUppercase that takes a String array argument. You can assume that every element in the array is a one-letter String, for example String[] test = { "a", "B", "c", "D", "e"}; This method will count the number of uppercase letters from the set A through Z in the array, and return that number. So for the example array above, your method will return 2. You will need to use some Java library methods....

  • Java 1. Write a getCount method in the IntArrayWorker class that returns the count of the...

    Java 1. Write a getCount method in the IntArrayWorker class that returns the count of the number of times a passed integer value is found in the matrix. There is already a method to test this in IntArrayWorkerTester. Just uncomment the method testGetCount() and the call to it in the main method of IntArrayWorkerTester. 2. Write a getLargest method in the IntArrayWorker class that returns the largest value in the matrix. There is already a method to test this in...

  • JAVA 1.Write a static method named getMaxEven(numbers) which takes an array of positive integers as a...

    JAVA 1.Write a static method named getMaxEven(numbers) which takes an array of positive integers as a parameter. This method calculates and returns the largest even number in the list. If there are no even numbers in the array, the method should return 0. You can assume that the array is not empty. For example: Test Result int[] values = {1, 4, 5, 9}; System.out.println(getMaxEven(values)); 4 System.out.println(getMaxEven(new int[]{1, 3, 5, 9})); 0 public static int --------------------------------------------------------------------------------- 2. Write a static method...

  • I need this in Visual Studio C++ Write a function that count the number of vowels,...

    I need this in Visual Studio C++ Write a function that count the number of vowels, the number of consonants and the average number of letters in each word. The function accept a C-String (char array) or a string object as an argument and return the number of vowels, number of consonants and the average number of letters in each word. Problem: Requirements: . Use pointers as part of the solution Read a string from a text file. . Write...

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