Question

   /**    * Returns an array of booleans that are set true or    *...

   /**
   * Returns an array of booleans that are set true or
   * false based on the associated values in the array
   * arr using the following rules:
   * 1. If arr[i] is divisible by three then the boolean
   * value in the the array returned at the same position
   * should be true
   * 2. Unless the values in arr[i] is also divisible by 5,
   * then the value returned in that position should be false.
   * 3. It should also be false for any values not divisible
   * by 3.
   * So for example, if arr = {3, 15, 21, 7, 10} then the
   * array returned would be {true, false, true, false, false}.
   * NOTE: The original array must NOT be changed by this method!
   *
   * @param arr array holding the int values to test
   * @return a boolean array as described above
   */
   public static boolean[] divByThree(int[] arr) {
       // TODO: Implement this method
      
       // TODO: remove the following line before completing
       // this line is only here to allow the template
       // to compile.
return new boolean[0];
   }

0 0
Add a comment Improve this question Transcribed image text
Answer #1
/**
 * Returns an array of booleans that are set true or
 * false based on the associated values in the array
 * arr using the following rules:
 * 1. If arr[i] is divisible by three then the boolean
 * value in the the array returned at the same position
 * should be true
 * 2. Unless the values in arr[i] is also divisible by 5,
 * then the value returned in that position should be false.
 * 3. It should also be false for any values not divisible
 * by 3.
 * So for example, if arr = {3, 15, 21, 7, 10} then the
 * array returned would be {true, false, true, false, false}.
 * NOTE: The original array must NOT be changed by this method!
 *
 * @param arr array holding the int values to test
 * @return a boolean array as described above
 */
public static boolean[] divByThree(int[] arr) {
    boolean[] result = new boolean[arr.length];
    for (int i = 0; i < arr.length; i++) {
        result[i] = arr[i] % 3 == 0 && arr[i] % 5 != 0;
    }
    return result;
}
Add a comment
Know the answer?
Add Answer to:
   /**    * Returns an array of booleans that are set true or    *...
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
  • Here is the indexOf method that I wrote: public static int indexOf(char[] arr, char ch) {...

    Here is the indexOf method that I wrote: public static int indexOf(char[] arr, char ch) {            if(arr == null || arr.length == 0) {                return -1;            }            for (int i = 0; i < arr.length; i++) {                if(arr[i] == ch) {                    return i;                }            }        return -1;       ...

  • Our 1st new array operation/method is remove. Implement as follows: public static boolean remove( int[] arr,...

    Our 1st new array operation/method is remove. Implement as follows: public static boolean remove( int[] arr, int count, int key ) { 1: find the index of the first occurance of key. By first occurance we mean lowest index that contains this value. hint: copy the indexOf() method from Lab#3 into the bottom of this project file and call it from inside this remove method. The you will have the index of the value to remove from the array 2:...

  • The following code skeleton contains a number of uncompleted methods. With a partner, work to complete...

    The following code skeleton contains a number of uncompleted methods. With a partner, work to complete the method implementations so that the main method runs correctly: /** * DESCRIPTION OF PROGRAM HERE * @author YOUR NAME HERE * @author PARTNER NAME HERE * @version DATE HERE * */ import java.util.Arrays; import java.util.Random; import java.util.Scanner; public class ArrayExercises { /** * Given a random number generator and a length, create a new array of that * length and fill it from...

  • Load to the IDEA the remaining classes from the provided Lab02.zip file. Repeat the previous project...

    Load to the IDEA the remaining classes from the provided Lab02.zip file. Repeat the previous project inside the ArraySetWithArray class. As shown in the UML diagram below ArraySetWithArray class does not utilize ResizableArrayBag object as its instance variable, it has setOfEntries defined as an array which should be dynamically resized if more room needed (double the size). displaySet method should check if the set is empty and display appropriate message; if the set is not empty should display the number...

  • Check if an array is a heap in Java. Complete the method isHeapTree Together, these methods...

    Check if an array is a heap in Java. Complete the method isHeapTree Together, these methods are meant to determine if an array of objects corresponds to a heap. The methods are generic, and they should work with an array of any type T that implement Comparable<T>. Implement the second method so that it uses recursion to process the complete tree/subtree whose root is at position i in the array arr. The method should return true if that tree/subtree is...

  • Hello, I've been working on this for a while and I can't figure the rest out....

    Hello, I've been working on this for a while and I can't figure the rest out. Need asap if anyone can help. maxBSt,minBST,isBST, and inOrder package lab7; import java.util.ArrayList; import java.util.LinkedList; import java.util.Queue; public class TreeExercise {    /*    * Construct BST from preorder traversal    */    public static Node<Integer> consBSTfromPreOrder(int[] arr, int start, int end)    {                       if(start > end) return null;               Node<Integer> root = new Node<Integer>(arr[start],...

  • 1- takes a 1D integer array arr as a parameter and returns a boolean value. 2-...

    1- takes a 1D integer array arr as a parameter and returns a boolean value. 2- The method returns true if the argument array contains two distinct values such that the sum of those two values is equal to the first element of the array arr. Otherwise, it returns false. 3- Assume that arr contains only positive integers (greater than 0). Sample runs provided below. 4- Sample runs provided below. 5- Complete and place your code in the Question 3...

  • Java: Return an array of booleans in a directed graph. Please complete the TODO section in...

    Java: Return an array of booleans in a directed graph. Please complete the TODO section in the mark(int s) function import algs13.Bag; import java.util.HashSet; // See instructions below public class MyDigraph { static class Node { private String key; private Bag<Node> adj; public Node (String key) { this.key = key; this.adj = new Bag<> (); } public String toString () { return key; } public void addEdgeTo (Node n) { adj.add (n); } public Bag<Node> adj () { return adj;...

  • k * *Given 4 String parameters, add a contact to the contacts array. Before adding a...

    k * *Given 4 String parameters, add a contact to the contacts array. Before adding a contact to the array, do a parameter sanity check in this method. If the array is full, or if first name or last name is null or an empty string, do *not add a contact but return false instead. Middle name can be left as null or an empty String. Note that we allow adding duplicate contacts If the name is acceptable, create a...

  • Please help JAVA Create the following in Stack.java: An array of ints set to size 10...

    Please help JAVA Create the following in Stack.java: An array of ints set to size 10 to represent the Stack, an int variable for the index A constructor that sets the index to -1 A method called isEmpty (returns a boolean) that has no parameters. This method returns true if the Stack is empty and false if the Stack is not empty. true A method called push(returns a Boolean) that takes an int as a parameter. This method pushes, or...

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