Question

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;
       }

Now I have to write this method:

import java.util.Arrays;
       import java.util.Random;
       import java.util.Scanner;

       * TODO Write a summary of the role of this class in the
       * MasterMind program.
  
       public class MasterMind {

/**
       * Checks whether the code is the correct length and only contains valid symbols.
       * Uses the indexOf method you wrote to check whether each character in the input is in the
       * symbols array. If code or symbols are null then false is returned.

       * For all arrays, don't assume a length but use the array .length attribute.
       *   
       * @param numPositions The required number of symbols in the code.
       * @param symbols The allowed symbols in the code.
       * @param code The code that is being checked.
       * @return true if the code is the correct length and has only valid symbols otherwise
       * returns false.
       */
       public static boolean isValidCode( int numPositions, char [] symbols, char [] code) {
       return false; //TODO replace
       }

0 0
Add a comment Improve this question Transcribed image text
Answer #1
// TODO file header

import java.util.Arrays;
import java.util.Random;
import java.util.Scanner;

/**
 * TODO Write a summary of the role of this class in the
 * MasterMind program.
 *
 * @author TODO add your name here
 */
public class MasterMind {
    /**
     * Returns the index within arr of the first occurrence of the specified character.
     * If arr is null or 0 length then return -1. For all arrays, don't assume a length
     * but use the array .length attribute.
     *
     * @param arr The array to look through.
     * @param ch  The character to look for.
     * @return The index within the array of the first occurrence of the specified
     * character or -1 if the character is not found in the array.
     */
    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;
    }

    /**
     * Checks whether the code is the correct length and only contains valid symbols.
     * Uses the indexOf method you wrote to check whether each character in the input is in the
     * symbols array. If code or symbols are null then false is returned.

     * For all arrays, don't assume a length but use the array .length attribute.
     *
     * @param numPositions The required number of symbols in the code.
     * @param symbols The allowed symbols in the code.
     * @param code The code that is being checked.
     * @return true if the code is the correct length and has only valid symbols otherwise
     * returns false.
     */
    public static boolean isValidCode( int numPositions, char [] symbols, char [] code) {
        for (int i = 0; i < code.length; i++) {
            if (indexOf(symbols, code[i]) == -1) {
                return false;
            }
        }
        return true;
    }
}
Add a comment
Know the answer?
Add Answer to:
Here is the indexOf method that I wrote: public static int indexOf(char[] arr, char ch) {...
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
  • must provide the following public interface: public static void insertSort(int [] arr); public static void selectSort(int...

    must provide the following public interface: public static void insertSort(int [] arr); public static void selectSort(int [] arr); public static void quickSort(int [] arr); public static void mergeSort(int [] arr); The quick sort and merge sort must be implemented by using recursive thinking. So the students may provide the following private static methods: //merge method //merge two sorted portions of given array arr, namely, from start to middle //and from middle + 1 to end into one sorted portion, namely,...

  • 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:...

  • Can someone help me with the writing of this code please? public static int promptInt(Scanner input,...

    Can someone help me with the writing of this code please? public static int promptInt(Scanner input, String prompt, int min, int max) { return 0; //TODO replace }    /**    * Returns the index within arr of the first occurrence of the specified character.    * If arr is null or 0 length then return -1. For all arrays, don't assume a length    * but use the array .length attribute.    * @param arr The array to look...

  • public static int max(int [] arr){ int max = arr[0]; for( int i = 1; i...

    public static int max(int [] arr){ int max = arr[0]; for( int i = 1; i < arr.length; i++ ) if( arr[i] > max ) max = arr[i]; return max; } 1) Provide an input value for arr that causes the method to throw an IndexOutOfBounds exception. 2) Provide an input value for arr that causes the method to throw a NullPointerException.

  • import java.util.Arrays; public class lab {    public static void main(String args[])    {    int...

    import java.util.Arrays; public class lab {    public static void main(String args[])    {    int arr[] = {10, 7, 8, 9, 1, 5,6,7};    int arr2[] = {9, 8, 7, 6, 5, 4, 3, 2, 1};    int arr3[] = {1, 3, 5, 3, 2, 6, 20};    quicksort(arr,0,arr.length-1);    quicksort(arr2,0,arr2.length-1);    quicksort(arr3,0,arr3.length-1);    System.out.println(Arrays.toString(arr));    System.out.println(Arrays.toString(arr2));    System.out.println(Arrays.toString(arr3));       }    private static int partition(int[] items,int low, int high)    {    int i=0;    int j=0;...

  • Question 9 0 Consider the following method. public static String[] strArrMethod(String] arr) String[] result = new...

    Question 9 0 Consider the following method. public static String[] strArrMethod(String] arr) String[] result = new String(arr.length]; for (int j - 0; j < arr.length; j++) String sm = arr[i]; for (int k = j + 1; k < arr.length; k++) if (arr[k].length() < sm.length) sm - arr[k]; // Line 12 result[j] = SM; return result; Consider the following code segment. String[] test Two - {"last", "day" of "the", school","year"); String[] resultTrostrar Method(test Two) How many times is the line...

  • 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],...

  • JAVA programming 9.9 Ch 9, Part 2: ArrayList Searching import java.util.ArrayList; public class ArrayListSet {    ...

    JAVA programming 9.9 Ch 9, Part 2: ArrayList Searching import java.util.ArrayList; public class ArrayListSet {     /**      * Searches through the ArrayList arr, from the first index to the last, returning an ArrayList      * containing all the indexes of Strings in arr that match String s. For this method, two Strings,      * p and q, match when p.equals(q) returns true or if both of the compared references are null.      *      * @param s The string...

  • /**    * Returns the sum of "black hits" and "white hits" between the hiddenCode and...

    /**    * Returns the sum of "black hits" and "white hits" between the hiddenCode and    * guess. A "black hit" indicates a matching symbol in the same position in the    * hiddenCode and guess. A "white hit" indicates a matching symbol but different    * position in the hiddenCode and guess that is not already accounted for with    * other hits.    *    * Algorithm to determine the total number of hits:    *   ...

  • I am unsure how to add the following methods onto this code?? please help - rowValuesIncrease(int[][]...

    I am unsure how to add the following methods onto this code?? please help - rowValuesIncrease(int[][] t) A method that returns true if from left to right in any row, the integers are increasing, otherwise false. - columnValuesIncrease(int[][] t) A method that returns true if from top to bottom in any column, the integers are increasing, otherwise false. - isSetOf1toN(int[][] t) A method that returns true if the set of integers used is {1, 2, . . . , n}...

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