Question

In: Computer Science

   Method sandwiched returns true if num is in the element before and after       ...

   Method sandwiched returns true if num is in the element before and after
           an element that is not equal to num

          sandwiched([4,5,4,6,7,3], 4) returns true
          sandwiched([2,1,2], 2) returns true
          sandwiched([3,3,3], 3) returns false
          sandwiched([4,5,6,4], 4) returns false
           sandwiched([1,1,2,3,1,4,1], 1) returns true
          @param nums Integer ArrayList
           @param num integer
          @return true if a single number is between elements equal to num
      */
      public static boolean sandwiched(ArrayList<Integer> nums, int num)
      {
          return false;
      }//end sandwiched

Solutions

Expert Solution

Hi,

Hope you are doing fine. I have coded the question in java keeping all the requirements in mind. I have tested the output with all the given inputs and the result seems positive. The code has been clearly explained using comments that have been highlighted in bold.

Program:

import java.util.ArrayList;

public class Sandwich {
  
   //method as per the question
   public static boolean sandwiched(ArrayList<Integer> nums, int num)
{
       //we traverse through the list using i
       //note that we only traverse up to size-2 elements as the elements beyond that cannot be sandwiched

       for(int i=0;i<nums.size()-2;i++)
       {
           //if the number in the list is equal to num and if it is not nums.size()-2
           if(nums.get(i)==num && i!=nums.size()-2)
           {
               //if the next number in list is not equal to current number and the number next to the next number is same as current number
               if(nums.get(i)!=nums.get(i+1) && nums.get(i)==nums.get(i+2))
                   //return True
                   return true;
           }
       }
       //if no numbers are sandwiched
return false;
}//end sandwiched

   //Main method is only used for testing
   public static void main(String[] args) {
      
       // Testing the method using the call sandwiched([1,1,2,3,1,4,1], 1). It must return true as 4 is sandwiched between 1's at the end of the list
       ArrayList<Integer> test=new ArrayList<Integer>();
       test.add(1);
       test.add(1);
       test.add(2);
       test.add(3);
       test.add(1);
       test.add(4);
       test.add(1);
       boolean result=sandwiched(test, 1);
       System.out.println(result);
      
       // Testing the method using the call sandwiched([3,3,3], 3). It must return false as all elements are equal
       ArrayList<Integer> test1=new ArrayList<Integer>();
       test1.add(3);
       test1.add(3);
       test1.add(3);
       result=sandwiched(test1, 3);
       System.out.println(result);

   }

}

Executable code snippet:

Output:


Related Solutions

Code with Java and no imports, Method sandwiched returns true if num is in the element...
Code with Java and no imports, Method sandwiched returns true if num is in the element before and after an element that is not equal to num sandwiched([4,5,4,6,7,3], 4) returns true sandwiched([2,1,2], 2) returns true sandwiched([3,3,3], 3) returns false sandwiched([4,5,6,4], 4) returns false sandwiched([1,1,2,3,1,4,1], 1) returns true @param nums Integer ArrayList @param num integer @return true if a single number is between elements equal to num */ public static boolean sandwiched(ArrayList nums, int num) { return false; }//end sandwiched
Write a Java method that returns the index of the largest element in an array of...
Write a Java method that returns the index of the largest element in an array of integers. If the number of such elements is greater than 1, return the smallest index. Use the following header: 
 public static int indexOfLargestElement(double[] array)
 Write a test program that prompts the user to enter ten numbers, invokes this
method to return the index of the largest element, and displays the index.
Using Java Write a method that returns the index of the smallest element in an array...
Using Java Write a method that returns the index of the smallest element in an array of integers. If the number of such elements is greater than 1, return the smallest index. Use the following header:   public static int indexOfSmallestElement (double[] array)
Write a method in JAVA, called binarySearch, that returns the index of the key element if...
Write a method in JAVA, called binarySearch, that returns the index of the key element if found in the array, otherwise it will return the value of minus(insertion point +1). In main, use an initializer list create an array of ints called nums holding the following values: 1, 4, 13, 43, -25, 17, 22, -37, 29. Test your binarySearch method on nums with a key value number in it (e.g. 4) and one that is not (e.g. 100). Ex. Given...
Write a method called mode that returns the most frequently occurring element of an array of...
Write a method called mode that returns the most frequently occurring element of an array of integers. Assume that the array has at least one element and that every element in the array has a value between 0 and 100 inclusive. Break ties by choosing the lower value. For example, if the array passed contains the values [27, 15, 15, 11, 27], your method should return 15. write a version of this method that does not rely on the values...
**Program #2: Working with Generics Implement the following method that returns the maximum element in an...
**Program #2: Working with Generics Implement the following method that returns the maximum element in an array. public static <E extends Comparable<E>> E max(E[] list) What should you do?      Write a program to test the method above with various types. Deliverables: There are 2 separate programs to complete. Place them both in the same package. Projects submitted with evidence of plagiarism will be given a score of 0. Java files (source code) Word document should include: Screen snapshots of...
(True or False) The following function will compile. void print(int num) { return num+1; } Group...
(True or False) The following function will compile. void print(int num) { return num+1; } Group of answer choices True False Flag this Question Question 2 10 pts (True or False) The following code will output "I was true". bool isGreater(string s1, string s2) { if(s1 > s2) { return true; } return false; } int main() { if(isGreater("before","back")) { cout << "I was true"; } else { cout << "I was false"; } return 0; } Group of answer...
write the “largerComponents” method that takes in two integer arrays and returns true or false if...
write the “largerComponents” method that takes in two integer arrays and returns true or false if the first array’s components are strictly greater than the second array’s components. The arrays must be the same size or else return false. Clarification Note: Components meaning each value at a specific index. For instance, if we had the arrays {5,2,7} and {1,3,1} then this method would return false as the value “2” is not greater than “3”. here is a provided code //Do...
Write recursive method to return true if a given array has element equal to employee emp,...
Write recursive method to return true if a given array has element equal to employee emp, or returns false otherwise. Array can be empty or not. //PRECONDITION: Varible n denotes the number of occupied positions in the array and must be non-negative. Employee class has method getSalary() that returns employee's salary, // and it has method boolean equals(Employee emp) that accept an employee object and returns true if employee calling the equals method is equal as employee emp, and returns...
The law of diminishing marginal returns starts: * a) Before minimum marginal cost b) After minimum...
The law of diminishing marginal returns starts: * a) Before minimum marginal cost b) After minimum marginal cost c) After minimum MPP d) When capital is a variable input Which of the following statement is not correct: Average variable costs of production a) Will fall than rise as more is produced. b) has a minimum c) graphs as a U-shaped curve. d) falls as long as output is increased. If marginal cost is above average total cost, then: * a)...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT