Question

In: Computer Science

public boolean areArrays(int[] arr1, int[] arr2){ // 1. initial check: both arrays need to have the...

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 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] != arr2[i]){

}
}
return true;
}

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

public boolean arrayContainsWord(String[] arr, String word){
// return true if array contains specific word, false otherwise
for(int i = 0; i < arr.length; i++){
if(arr[i].equals(word));
return true;
}
return false;
}

--- Scanner ------

Solutions

Expert Solution

PLEASE GIVE IT A THUMBS UP, I SERIOUSLY NEED ONE, IF YOU NEED ANY MODIFICATION THEN LET ME KNOW, I WILL DO IT FOR YOU

class A {

  public static 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 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] != arr2[i]) {
        return false;
      }
    }
    return true;
  }

  public static int getIndexOfWord(String[] arr, String word) {
    // if found, return the index of word, otherwise return -1
    for (int i = 0; i < arr.length; i++) {
      if (arr[i].equals(word)) {
        return i;
      }
    }
    return -1;
  }

  public static boolean arrayContainsWord(String[] arr, String word) {
    // return true if array contains specific word, false otherwise
    for (int i = 0; i < arr.length; i++) {
      if (arr[i].equals(word)) return true;
    }
    return false;
  }

  public static void main(String[] args) {
    int a[] = { 1, 2, 3 };
    int b[] = { 1, 2, 3 };
    int c[] = { 5, 42, 4 };
    String arr[] = {"HI","MY","name","is","Nikhil"};
    System.out.println(areArrays(a,b));
    System.out.println(areArrays(a,c));
    System.out.println(getIndexOfWord(arr, "is"));
    System.out.println(arrayContainsWord(arr, "ayush"));
  }
}

Related Solutions

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 =...
Consider the following two methods: public static boolean isTrue(int n){ if (n <= 1)          return false;...
Consider the following two methods: public static boolean isTrue(int n){ if (n <= 1)          return false;        for (int i = 2; i < n; i++){          if (n % i == 0)             return false; }        return true; } public static int Method(int[] numbers, int startIndex) { if(startIndex >= numbers.length) return 0; if (isTrue(numbers[startIndex]))      return 1 + Method(numbers, startIndex + 1); else      return Method(numbers, startIndex + 1); } What is the final return value of Method() if it is called with the...
Add a recursive Boolean function called bool isGreater(int compare) to class IntegerLinkedList to check if any of...
Add a recursive Boolean function called bool isGreater(int compare) to class IntegerLinkedList to check if any of the linked list data values is greater than the given parameter, compare. For example, isGreater(25) should return true and isGreater(100) should return false for the the linked list shown below. A main function (prob3.cpp) is given to you to add data values to the linked list and test your function. Other examples are given in the main function. // ADD ANSWER TO THIS FILE...
Public static boolean isPalindrome(String word) * Check to see if a word is a palindrome. Should...
Public static boolean isPalindrome(String word) * Check to see if a word is a palindrome. Should be case-independent. * @param word a String without whitespace * @return true if the word is a palindrome    Public static int longestWordLength(String words) * Returns length of the longest word in the given String using recursion (no loops). * Hint: a Scanner may be helpful for finding word boundaries. After delimiting by space, * use the following method on your String to remove...
import javax.swing.JOptionPane; public class Animal {    private int numTeeth = 0;    private boolean spots...
import javax.swing.JOptionPane; public class Animal {    private int numTeeth = 0;    private boolean spots = false;    public int weight = 0;       public Animal(int numTeeth, boolean spots, int weight){        this.numTeeth =numTeeth;        this.spots = spots;        this.weight =weight;    }       public int getNumTeeth(){        return numTeeth;    }    public void setNumTeeth(int numTeeth) {        this.numTeeth = numTeeth;    }       public boolean getSpots() {       ...
Create a method:         Public Static boolean isSubArray489(int[] intArray) This method has an array of integers as its...
Create a method:         Public Static boolean isSubArray489(int[] intArray) This method has an array of integers as its input parameter, we'll say that a SubArray 9,8,7is that three integers, 9,8,7values appearing decreasing order one by one in the array. Return true if the array does contain any SubArray “987” Notice that any 3 integers that presenting a decreasing by one order must return True. (987, 876,765,654, 543, 432,321, 210 are all True) Call this method in your main function, and pass in...
You will need to use only two arrays:  One array of type int to store...
You will need to use only two arrays:  One array of type int to store all the ID's  One two‐dimensional array of type double to store all the scores for all quizzes Note: The two dimensional array can be represented also with the rows being the students and the columns being the quizzes. How to proceed: 1. Declare the number of quizzes as a constant, outside the main method. (Recall that identifiers for constants are all in CAPITAL_LETTERS.)...
1. Which of the following can be treated as a Boolean expression? A. an int expression...
1. Which of the following can be treated as a Boolean expression? A. an int expression B. any of these C. the result of a comparison(such as <or>) D. a float expression 2. Assuming the variable x contains an integer value what will the result of the following statement be: if x<0 or x>=0. A. True B. False C. Sometimes true and sometimes false D. A "math domain" error will occur
(JAVA) Balanced Class diagram: Balanced + swing(int[] a) : boolean For this exercise, you will have...
(JAVA) Balanced Class diagram: Balanced + swing(int[] a) : boolean For this exercise, you will have to implement the class diagram above. Basically, the objective of this exercise is to develop the class and the previous method, in such a way that it tells us if an array is balanced or not. We say that an array is balanced if the sum of the elements belonging to the first half of the array (not including the element in the middle),...
public static int countSubstrings​(java.lang.String t, java.lang.String s, boolean allowOverlap) Counts the number of times that one...
public static int countSubstrings​(java.lang.String t, java.lang.String s, boolean allowOverlap) Counts the number of times that one string occurs as a substring in another, optionally allowing the occurrences to overlap. For example: countSubstrings("aa", "aaaaa", false) returns 2 countSubstrings("aa", "aaaaa", true) returns 4 countSubstrings("aa", "ababab", true) returns 0 Parameters: t - string we are looking for ("target") s - string in which we are looking ("source") allowOverlap - true if occurrences of t are allowed to overlap Returns: number of times t...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT