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...
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...
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
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...
The Woods Company and the Spieth have both announced an initial public offering (IPO) at $40...
The Woods Company and the Spieth have both announced an initial public offering (IPO) at $40 per share. One of these is undervalued by $9, and the other is overvalued by $4, but you have no way of knowing which company is undervalued and which company is overvalued. You plan to buy 1,000 shares in Woods and 1,000 shares in Spieth. If an issue is undervalued, and only half of your shares will be filled. If you could get 1,000...
public class SumMinMaxArgs { private int[] array; // You will need to write the following: //...
public class SumMinMaxArgs { private int[] array; // You will need to write the following: // // 1. A constructor that takes a reference to an array, // and initializes an instance variable with this // array reference // // 2. An instance method named sum, which will calculate // the sum of the elements in the array. If the array // is empty (contains no elements), then this will // will return 0. You will need a loop for...
import java.util.Scanner; class Factorial { public static int calc_factorial(int f) { int myint= 1; // put...
import java.util.Scanner; class Factorial { public static int calc_factorial(int f) { int myint= 1; // put code here return myint; } public int getInt() { int f = 0; // put code here return f; } } public class Assignment06 { public static void main(String args[]){ System.out.println("Assignmemnt 06 Written by Matt Weisfeld"); int myInt = 0; // create a Factorial object Factorial factorial = new Factorial(); // get a number from the console myInt = factorial.getInt(); System.out.println("Factorial of " +...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT