Question

CMSC 200 (Computer Programming - Java 10.0.2) We'll say that a value is "everywhere" in an...

CMSC 200 (Computer Programming - Java 10.0.2)

We'll say that a value is "everywhere" in an array if for every pair of adjacent elements in the array, at least one of the pair is that value. Return true if the given value is everywhere in the array.

Examples:

isEverywhere( [1, 2, 1, 3], 1 ) -> true

isEverywhere( [1, 2, 1, 3], 2 ) -> false

isEverywhere( [1, 2, 1, 3, 4], 1 ) -> false

0 0
Add a comment Improve this question Transcribed image text
Answer #1
public class Everywhere {

    public static boolean isEverywhere(int[] arr, int num) {
        for(int i = 0; i < arr.length-1; i++) {
            if(arr[i] != num && arr[i+1] != num) {
                return false;
            }
        }
        return true;
    }

    public static void main(String[] args) {
        System.out.println(isEverywhere(new int[]{1, 2, 1, 3}, 1));
        System.out.println(isEverywhere(new int[]{1, 2, 1, 3}, 2));
        System.out.println(isEverywhere(new int[]{1, 2, 1, 3, 4}, 1));
    }

}

\color{blue}code\;is\;bold\;font\;is\;the\;method\;you\;need..

Add a comment
Know the answer?
Add Answer to:
CMSC 200 (Computer Programming - Java 10.0.2) We'll say that a value is "everywhere" in an...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Has to be in Java programming language Java Python mirrorLength We'll say that a "mirror" section...

    Has to be in Java programming language Java Python mirrorLength We'll say that a "mirror" section in an array is a group of contiguous elements such that somewhere in the array, the same group appears in reverse order. For example, the largest mirror section in {1, 2, 3, 8, 9, 3, 2, 1} is length 3 (the {1, 2, 3} part). Return the size of the largest mirror section found in the given array. 3 mirrorLength([1, 2, 3, 8, 9,...

  • Course: CMSC 200 (Computer Programming - Java) CMSC200 HOMEWORK 9 Graded against 3 test cases totaling...

    Course: CMSC 200 (Computer Programming - Java) CMSC200 HOMEWORK 9 Graded against 3 test cases totaling 30 points Description: Suppose the weekly hours for all employees are stored in a two-dimensional array. Each row records an employee's seven-day work hours with seven columns. For example, the following array stores the work hours for eight employees. Write a program that displays employees and their total hours in decreasing order of the total hours. See sample output below Required Method public static...

  • the programming language is in java Problem 2 You are given an array A with n...

    the programming language is in java Problem 2 You are given an array A with n distinct elements. Implement an (n log n)-time algorithm that creates an array B where all elements are in range from 0 to n - 1 and where the order of elements is the same as in A. That is, 0 has the same index in B as the smallest element in A, 1 has the same index in B as the second smallest element...

  • QUESTION 2: Elementary Java Programming (a) Explain what a Wrapper class is in Java and provide...

    QUESTION 2: Elementary Java Programming (a) Explain what a Wrapper class is in Java and provide an example of one and what it [06] "wraps". (b) You have been given an array of elements to search through for a particular value. [02] Which loop would you choose between a for loop and a for-each loop and why? (c) In Java what is the difference between a StringBuilder and StringBuffer? What [02] are they used for? Total: 10

  • 4 CS136: Computer Science II-Spring 2019 [10 points] Problem #4 A run is a sequence of...

    4 CS136: Computer Science II-Spring 2019 [10 points] Problem #4 A run is a sequence of adjacent repeated values. write a Java class (RunsPrinter. java) with one main method that generates a sequence of 20 random die tosses in an array (and not an ArrayList) and then prints the die values, marking the runs by including them in parentheses, like this: 1 2 (5 5) 3 124 3 (2 2 2 2) 3 6 (5 5) 6 3 1 Examples...

  • I am working on my java but I keep getting this one wrong. Method Name: arrayContains...

    I am working on my java but I keep getting this one wrong. Method Name: arrayContains Access modifier: Private Parameters: 1 integer named number Return type: boolean Purpose: This method returns a true or a false indicating if the number is present in an array or not. In order for this method to work, there must be an array already declared. Therefore, in the CLASS BLOCK, declare an array as follows: static int[] listof Numbers = {1, 2, 3, 4,...

  • Hi, I have Java programming problem: Please solve using Java. Thanks. Best Regards. In the army,...

    Hi, I have Java programming problem: Please solve using Java. Thanks. Best Regards. In the army, each soldier has an assigned rank. A soldier of rank X has to report to (any) soldier of rank X + 1. Many soldiers can report to the same superior. Write a function: class Solution { public int solution(int[] ranks); } that, given an array ranks consisting of soldiers' ranks, returns the number of soldiers who can report to some superior. Examples: 1. Given...

  • this java! a response as soon as possible would be appreciated. thanks again. please say a...

    this java! a response as soon as possible would be appreciated. thanks again. please say a brief statement as to why u chose that answer. Indicate TRUE OR FALSE for each of the following statements concerning search and sort algorithms 1) Linear search is guranteed to locate 5 in the array [1, 3, 2, 6, 5, 9, 4 ] 2) Binary search is guranteed to locate 5 in the array [1, 3, 2, 6, 5, 9, 4 ] 3) Linear...

  • Programming in java In this part of this Lab exercise, you will need to create a...

    Programming in java In this part of this Lab exercise, you will need to create a new class named ArrayShiftMult. This class carries out simple manipulation of an array. Then you should add a main method to this class to check that your code does what it is supposed to do. 1. Create a BlueJ project named LAB06 as follows: a. Open the P drive and create a folder named Blue), if it does not exist already. Under this folder,...

  • Please use java code. Implement a test class named BatArray1Test that tests all the functionality of...

    Please use java code. Implement a test class named BatArray1Test that tests all the functionality of the following given 3 methods, including the invalid arguments: 1) commonEnd Given two arrays of ints, a and b, return true if they have the same first element or they have the same last element. This method should return false if either array is empty or null. 2) midThree Given an array of integers, return a new array of length 3 containing the elements...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT