Question

In: Computer Science

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

Solutions

Expert Solution

/**
 * 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) {
    for (int i = 0; i < nums.size() - 2; i++) {
        if (nums.get(i) == num && nums.get(i + 1) != num && nums.get(i + 2) == num) {
            return true;
        }
    }
    return false;
}

import java.util.ArrayList;
import java.util.Arrays;

public class SandswitchedTest {

    /**
     * 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) {
        for (int i = 0; i < nums.size() - 2; i++) {
            if (nums.get(i) == num && nums.get(i + 1) != num && nums.get(i + 2) == num) {
                return true;
            }
        }
        return false;
    }

    public static void main(String[] args) {
        System.out.println(sandwiched(new ArrayList<>(Arrays.asList(4, 5, 4, 6, 7, 3)), 4));
        System.out.println(sandwiched(new ArrayList<>(Arrays.asList(2, 1, 2)), 2));
        System.out.println(sandwiched(new ArrayList<>(Arrays.asList(3, 3, 3)), 3));
        System.out.println(sandwiched(new ArrayList<>(Arrays.asList(4, 5, 6, 4)), 4));
        System.out.println(sandwiched(new ArrayList<>(Arrays.asList(1, 1, 2, 3, 1, 4, 1)), 1));
    }
}


Related Solutions

   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...
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 with no imports,    Method arrayOfSums returns an ArrayList of the same size as...
Using Java with no imports,    Method arrayOfSums returns an ArrayList of the same size as the largest ArrayList        This method needs to be overloaded so that it works for 2 to 4 sent Arraylists.        arrayOfSums([2,4,7], [3,7,14]) returns [5,11,21]        arrayOfSums([6,13,4,8], [7,5,9], [2,5,8,3]) returns [15, 23, 21, 11]        arrayOfSums([6,13,4,8], [7,5,9], [2,5,8,3], [3,5,4,1,2]) returns [18, 28, 25, 12, 2]        arrayOfSums([8,6], [2,4,10]) returns [10,10,10]        arrayOfSums([2,2,2,2,2], [3,2,5,2,3], [1,3,1,3,1]) returns [6, 7, 8,...
Using Java with NO imports,    Method arrayOfSums returns an ArrayList of the same size as...
Using Java with NO imports,    Method arrayOfSums returns an ArrayList of the same size as the largest ArrayList        This method needs to be overloaded so that it works for 2 to 4 sent Arraylists.        arrayOfSums([2,4,7], [3,7,14]) returns [5,11,21]        arrayOfSums([6,13,4,8], [7,5,9], [2,5,8,3]) returns [15, 23, 21, 11]        arrayOfSums([6,13,4,8], [7,5,9], [2,5,8,3], [3,5,4,1,2]) returns [18, 28, 25, 12, 2]        arrayOfSums([8,6], [2,4,10]) returns [10,10,10]        arrayOfSums([2,2,2,2,2], [3,2,5,2,3], [1,3,1,3,1]) returns [6, 7, 8,...
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...
Code in Java Write a recursive method, reverseString, that accepts a String and returns the String...
Code in Java Write a recursive method, reverseString, that accepts a String and returns the String reversed. Write a recursive method, reverseArrayList, that accepts an ArrayList of Strings and returns the ArrayList in reserve order in reserve order of the input ArrayList. Write a main method that asks the user for a series of Strings, until the user enters “Done” and puts them in an ArrayList. Main should make use to reverseArrayList and reverseString to reverse each String in the...
In Java Please: * posOfLargestElementLtOeT returns the position of the largest element in the array that...
In Java Please: * posOfLargestElementLtOeT returns the position of the largest element in the array that is * less than or equal to the limit parameter * if all values are greater than theVal, return -1; * * Precondition: the array is nonempty and all elements are unique. * Your solution must go through the array exactly once. * * <pre> * 0 == posOfLargestElementLtOeT(3, new double[] { -7 }) // value:-7 is in pos 0 * 5 == posOfLargestElementLtOeT(3,...
JAVA PLEASE!! Write a value-returning method, isVowel, that returns the value true if a given character...
JAVA PLEASE!! Write a value-returning method, isVowel, that returns the value true if a given character is a vowel, and otherwise returns false. Also write a program to test your method. 2) Write a program that prompts the user to input a sequence of characters and outputs the number of vowels. (Use the method isVowel written in Programming Exercise 1.)
how to write in java; Write a method int[] coPrime[int num, int[]numbers] { // instructions are...
how to write in java; Write a method int[] coPrime[int num, int[]numbers] { // instructions are that it returns an array of all the elements of the int[] array numbers which are coprime with x } Note that the array that is returned may be an empty array--you will have to count how many times gcf(x, numbers[i]) == 1. ASSUME numbers is not null and not empty.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT