Skip to content

CodeQuiz 4

In this code note, we will explore the sample answers to the seven methods implementation task from codquiz project. Make sure to watch and try out the code for each of the challenges.

List of challenge methods:

  • valueAt
  • find
  • frequency
  • allEven



› Solution

Please watch the following explainer video for each method's implementation detail:

Please expand to see instructor's implementation of above methods:

Expand to see the code
package challenges.warmup;

import stayaway.verify.easy.LevelThreeTests;

public class EasyLevel3 {

    // Run main method to verify your
    // challenge method implementations
    public static void main(String[] args) {
        LevelThreeTests levelThree = new LevelThreeTests();
        // Comment out to isolate the method run
        levelThree.valueAt();
        levelThree.find();
        levelThree.frequency();
        levelThree.allEven();
    }


    /**
     * INSTRUCTIONS: [READY]
     * -------------
     * Given String array and integer index, return the element
     * found from the array at given index.
     *
     * EXAMPLE:
     * -------------
     * valueAt(["ab", "bc", "cd"], 2) ➞ "cd"
     * valueAt(["ab", "bc", "cd"], 4) ➞ "ERROR"
     * valueAt(["xy", "md"], 0) ➞ "xy"
     *
     * RULE:
     * -------------
     *  > Do not use sout
     *  > If user provides invalid index return "ERROR" String.
     */
    public String valueAt(String[] arr, int index) {
        boolean badIndex = (index < 0 || index > arr.length-1);
        if(badIndex) return "ERROR";
        return arr[index];
    }


    /**
     * INSTRUCTIONS: [READY]
     * -------------
     * Given String array and target text, return true if the String array contains
     * the target text as one of its element. Otherwise, just return false.
     *
     * EXAMPLE:
     * -------------
     * find(["ab", "bc", "cd"],  "ab") ➞ true
     * find(["ab", "bc", "cd"],  "kc") ➞ false
     *
     * RULE:
     * -------------
     *  > Do not use sout
     *  > Search is case-sensitive, "EX" and "ex" is not the same
     */
    public boolean find(String[] arr, String target) {

        for(int i = 0; i < arr.length; i++) {
            String curr = arr[i];
            boolean found = curr.equals(target);
            if(found) return true;
        }
        return false;
    }


    /**
     * INSTRUCTIONS: [READY]
     * -------------
     * Given an integer array and integer value, count how many times the integer
     * value appears in the array and return to the user. If the value does not
     * appear in the array, just return 0.
     *
     * EXAMPLE:
     * -------------
     * frequency([34,2,5,34,65,2,52],  34) ➞ 2
     * frequency([34,2,5,34,65,2,52],   5) ➞ 1
     * frequency([34,2,5,34,65,2,52],  27) ➞ 0
     *
     * RULE:
     * -------------
     *  > Do not use sout
     */
    public int frequency(int[] scores, int value) {
        int count = 0;
        for(int i = 0; i < scores.length; i++) {
            int current = scores[i];
            if(current ==  value) count++;
        }

        return count;
    }


    /**
     * INSTRUCTIONS: [READY]
     * -------------
     * Given an integer array, determine whether all of its element are even
     * number or not. If all the element in the array is even number, return true
     * otherwise return false.
     *
     * EXAMPLE:
     * -------------
     * allEven( [28,50,34,20,52] ) ➞ true
     * allEven( [28,51,34,20,52] ) ➞ false
     *
     * RULE:
     * -------------
     *  > Do not use sout
     */
    public boolean allEven(int[] arr) {
        for(int i = 0; i < arr.length; i++) {
            int curr = arr[i];
            if(curr%2 != 0) return false;
        }
        return true;
    }

}//end::class



valueAt

Given String array and integer index, return the element found from the array at given index. If user provides invalid index return "ERROR" String.

Example:

1
2
3
valueAt(["ab", "bc", "cd"], 2) ➞ "cd"
valueAt(["ab", "bc", "cd"], 4) ➞ "ERROR"
valueAt(["xy", "md"], 0)       ➞ "xy"

Sample Answer:

1
2
3
4
5
public String valueAt(String[] arr, int index) {
    boolean badIndex = (index < 0 || index > arr.length-1);
    if(badIndex) return "ERROR";
    return arr[index];
}



find

Given String array and target text, return true if the String array contains the target text as one of its element. Otherwise, just return false. Note that search is case-sensitive, meaning "EX" and "ex" is not the same.

Example:

find(["ab", "bc", "cd"],  "ab") ➞ true
find(["ab", "bc", "cd"],  "kc") ➞ false

Sample Answer:

1
2
3
4
5
6
7
8
9
public boolean find(String[] arr, String target) {

    for(int i = 0; i < arr.length; i++) {
        String curr = arr[i];
        boolean found = curr.equals(target);
        if(found) return true;
    }
    return false;
}


frequency

Given an integer array and integer value, count how many times the integer value appears in the array and return to the user. If the value does not appear in the array, just return 0.

Example:

1
2
3
frequency([34,2,5,34,65,2,52],  34) ➞ 2
frequency([34,2,5,34,65,2,52],   5) ➞ 1
frequency([34,2,5,34,65,2,52],  27) ➞ 0

Sample Answer:

1
2
3
4
5
6
7
8
9
public int frequency(int[] scores, int value) {
    int count = 0;
    for(int i = 0; i < scores.length; i++) {
        int current = scores[i];
        if(current ==  value) count++;
    }

    return count;
}



allEven

Given an integer array, determine whether all of its element are even number or not. If all the element in the array is even number, return true otherwise return false.

Example:

allEven( [28,50,34,20,52] ) ➞ true
allEven( [28,51,34,20,52] ) ➞ false

Sample Answer:

1
2
3
4
5
6
7
public boolean allEven(int[] arr) {
    for(int i = 0; i < arr.length; i++) {
        int curr = arr[i];
        if(curr%2 != 0) return false;
    }
    return true;
}