Question

In: Computer Science

public int getIndexOfWord(String[] arr, String word){ // if found, return the index of word, otherwise return...

public int getIndexOfWord(String[] arr, String word){
// if found, return the index of word, otherwise return -1

}

public boolean areArrays(int[] arr1, int[] arr2){
// 1. initial check: both arrays need to have the same length, return false if not the same
// 2. return true if both given arrats are equals(same values in the same indices), false otherwise

}


public boolean areArraysEqual(String[] arr1, String[] arr2){
// 1. initial check: both arrays need to have the same length, return false if not the same
// 2. return true if both given arrays are equals(same values in the same indices), false otherwise

}

Solutions

Expert Solution

public int getIndexOfWord(String[] arr, String word) {
        for (int i = 0; i < arr.length; i++) {
            if (arr[i].equals(word))
                return i;
        }
        return -1;

    }

    public boolean areArrays(int[] arr1, int[] arr2) {
        if (arr1.length != arr2.length)
            return false;
        for (int i = 0; i < arr1.length; i++)
            if (arr1[i] != arr2[i])
                return false;
        // 1. initial check: both arrays need to have the same length, return false if
        // not the same
        // 2. return true if both given arrats are equals(same values in the same
        // indices), false otherwise
        return true;
    }

    public boolean areArraysEqual(String[] arr1, String[] arr2) {
        // 1. initial check: both arrays need to have the same length, return false if
        // not the same
        // 2. return true if both given arrays are equals(same values in the same
        // indices), false otherwise
        if (arr1.length != arr2.length)
            return false;
        for (int i = 0; i < arr1.length; i++)
            if (!arr1[i].equals( arr2[i]))
                return false;
        // 1. initial check: both arrays need to have the same length, return false if
        // not the same
        // 2. return true if both given arrats are equals(same values in the same
        // indices), false otherwise
        return true;

    }

Related Solutions

class Arrays1{ public int getSumOfValues(int[] arr){ // return the sum of values of array elements int...
class Arrays1{ public int getSumOfValues(int[] arr){ // return the sum of values of array elements int sum = 0; int i; for(i = 0; i < arr.length; i++){ sum += arr[1]; } return sum; } public int getAverageValueInArray(int[] arr){ // return the average value of array elements int value = 0; for(int i = 0; i < arr.length; i++){ double average = value/ arr.length; } return value; } public int getNumberOfEvens(int[] arr){ //return the number of even values found in...
public static int example3 (int [] arr ) { int n = arr.length , total =...
public static int example3 (int [] arr ) { int n = arr.length , total = 0; for (int j = 0; j < n ; j += 2) for (int k = 0; k <= j ; k ++) total += arr [ j ]; return total ; } what is the running time of this method?
public boolean isPrime(int num){ // return true if the number is prime, false otherwise } public...
public boolean isPrime(int num){ // return true if the number is prime, false otherwise } public boolean isOdd(int num){ // return true if the number is odd, false otherwise } public String reverseMyString(String input){ // using a loop, reverse a string // i.e. input = "hello", reversed answer: "olleh" // i.e. input = "bye", reversed answer: "eyb" } public int pow(int base, int power){ // using for loop, calculate base raised to power // i.e. base = 2, power =...
class ArrayReverse1{ public static void reverse(int[] a, int index) { if (index >0) { index= index...
class ArrayReverse1{ public static void reverse(int[] a, int index) { if (index >0) { index= index - 1; // Decrementing the index System.out.printf("%d%n", a[index]); reverse(a, index); // Recursive call } return; } public static void main (String args[]) { int [] array = { 1, 2, 3, 4, 5 }; int n=array.length; reverse(array,n); // function call } } Write a generic version of the corrected recursive reverse method that could be used to print any of the following arrays (or...
public class Book{     public String title;     public String author;     public int year;    ...
public class Book{     public String title;     public String author;     public int year;     public String publisher;     public double cost;            public Book(String title,String author,int year,String publisher,double cost){        this.title=title;         this.author=author;         this.year=year;         this.publisher=publisher;         this.cost=cost;     }     public String getTitle(){         return title;     }         public String getAuthor(){         return author;     }     public int getYear(){         return year;     }     public String getPublisher(){         return publisher;...
Consider the following code: void swap(int arr[], int i, int j) {        int temp = arr[i];...
Consider the following code: void swap(int arr[], int i, int j) {        int temp = arr[i];        arr[i] = arr[j];        arr[j] = temp; } void function(int arr[], int length) {        for (int i = 0; i<length / 2; i++)               swap(arr, i, (length / 2 + i) % length); } If the input to the function was int arr[] = { 6, 1, 8, 2, 5, 4, 3, 7 }; function(arr,8); What values would be stored in the array after calling the...
public int static mirroringIt(String str){ return n; } implement this function so its takes ONE string...
public int static mirroringIt(String str){ return n; } implement this function so its takes ONE string and remove the characters so it becomes a mirrored word. example: Input: qswwbi Output: 4 remove q s b i "wow" is a mirror word. output : 0 Input : "if" output : 1 remove either I or f because it's two words. don't use 3rd parties library or java.util.
JAVA please write this method public static void recursiveSelectionSort(int[] arr) { }
JAVA please write this method public static void recursiveSelectionSort(int[] arr) { }
JAVA please write this method public static void recursiveMergeSort(int[] arr) { }
JAVA please write this method public static void recursiveMergeSort(int[] arr) { }
Swift Code 4 only please var arr: [String] = ["a","b","s","c"] var dictionary : [String : Int]...
Swift Code 4 only please var arr: [String] = ["a","b","s","c"] var dictionary : [String : Int] = ["a":1, "b":2, "c":3, "d":4] for i in arr if(dict[i] = even) print ("even number" if else (dict[i] =odd) print("odd number") else print("Does not exist") Is there a way to compare the string array with the dictionary keys. Use the array to see if string exists as a key in the dictionary. Depending on the value of the key, it will print a specific...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT