Question

Ch. 09: Exclusive find in Array (Arrays, conditional, counter) Write a method that will receive an...

Ch. 09: Exclusive find in Array (Arrays, conditional, counter)

Write a method that will receive an array of integers and an integer value as a parameter. The integer value received as the second parameter will be searched within the Array received as the other parameter. The method will return true if the value appears only once in the Array. Use the "documentation shown in the program as a guide".

As an example, if the array received was the sequence of [3, 5, 25, 5, 4, 1] and the value to search was 5 the result would be false and true if the value was 4 or 1.

Here is the default code ...

import java.util.*;

public class Main{

public static final Scanner CONSOLE = new Scanner(System.in);

// Declare your method here
public static ///...

}

public static void main(String[] args){
  
// Declaring and initializing the array
int[] values = {88, 25, 79, 84, 29, 45, 56, 23, 92, 100, 105, 28, 32, 25, 1, 2, 3, 1, 3};
  
int search = CONSOLE.nextInt();
  
if (exclusiveFind(values, search)) // If the method call returns true
System.out.println("The value " + search + " was exclusively only one time in the array");
else // When the method returns not true (false)
System.out.println("The value " + search + " was NOT exclusively only one time in the array");
  
  
}

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Program code to copy:-

import java.util.*;
public class Main {
   public static final Scanner CONSOLE = new Scanner(System.in);

   // This method receives an array of integers and an integer value as a parameter.
   //The integer value received as the second parameter will be searched within the Array received as the other parameter.
   //This method will return true if the value appears only once in the Array.
   public static Boolean exclusiveFind(int[] values, int search) {
      
       //Determining the length of array
       int len = values.length;
       //Variable to store the number of times an element to be searched repeated in an array
       int repeat=0;
      
       //Loop to search the element
       for(int i=0; i<len; i++) {
           if(values[i] == search)
               repeat++;
       }
       //Checking number of times an element is repeated
       if(repeat==1)
           return true; //Returns true when searched element is exclusively only one time in the array
       else
           return false; //Returns false when searched element is exclusively only one time in the array
   }
  
   public static void main(String[] args) {
       // Declaring and initializing the array
       int[] values = {88, 25, 79, 84, 29, 45, 56, 23, 92, 100, 105, 28, 32, 25, 1, 2, 3, 1, 3};
      
       System.out.print("Enter the integer number to be searched in an array: ");
       int search = CONSOLE.nextInt();
      
       if (exclusiveFind(values, search)) // If the method call returns true
           System.out.println("The value " + search + " was exclusively only one time in the array");
       else // When the method returns not true (false)
           System.out.println("The value " + search + " was NOT exclusively only one time in the array");
   }

}

Screenshot of output:-

Add a comment
Know the answer?
Add Answer to:
Ch. 09: Exclusive find in Array (Arrays, conditional, counter) Write a method that will receive 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
  • JAVA 1.Write a static method named getMaxEven(numbers) which takes an array of positive integers as a...

    JAVA 1.Write a static method named getMaxEven(numbers) which takes an array of positive integers as a parameter. This method calculates and returns the largest even number in the list. If there are no even numbers in the array, the method should return 0. You can assume that the array is not empty. For example: Test Result int[] values = {1, 4, 5, 9}; System.out.println(getMaxEven(values)); 4 System.out.println(getMaxEven(new int[]{1, 3, 5, 9})); 0 public static int --------------------------------------------------------------------------------- 2. Write a static method...

  • This is for Java. Create ONE method/function that will return an array containing the row and...

    This is for Java. Create ONE method/function that will return an array containing the row and column of the largest integer i the 2D array. If the largest number is located on row 2, column 1, the method needs to return row 2 and column one. Do not use more than one method. Use the code below as the main. Please comment any changes. in java Given the main, create a method that RETURNS the largest number found in the...

  • JAVA HELP: Directions Write a program that will create an array of random numbers and output...

    JAVA HELP: Directions Write a program that will create an array of random numbers and output the values. Then output the values in the array backwards. Here is my code, I am having a problem with the second method. import java.util.Scanner; import java.util.Random; public class ArrayBackwards { public static void main(String[] args) { genrate(); print(); } public static void generate() { Scanner scanner = new Scanner(System.in);    System.out.println("Seed:"); int seed = scanner.nextInt();    System.out.println("Length"); int length = scanner.nextInt(); Random random...

  • write a program which include a class containing an array of words (strings).The program will search...

    write a program which include a class containing an array of words (strings).The program will search the array for a specific word. if it finds the word:it will return a true value.if the array does not contain the word. it will return a false value. enhancements: make the array off words dynamic, so that the use is prompter to enter the list of words. make the word searcher for dynamic, so that a different word can be searched for each...

  • Check if an array is a heap in Java. Complete the method isHeapTree Together, these methods...

    Check if an array is a heap in Java. Complete the method isHeapTree Together, these methods are meant to determine if an array of objects corresponds to a heap. The methods are generic, and they should work with an array of any type T that implement Comparable<T>. Implement the second method so that it uses recursion to process the complete tree/subtree whose root is at position i in the array arr. The method should return true if that tree/subtree is...

  • Write a program that instantiates an array of integers named scores. Let the size of the...

    Write a program that instantiates an array of integers named scores. Let the size of the array be 10. The program then first invokes randomFill to fill the scores array with random numbers in the range of 0 -100. Once the array is filled with data, methods below are called such that the output resembles the expected output given. The program keeps prompting the user to see if they want to evaluate a new set of random data. Find below...

  • Searching an array: --Using the template provided, create a method called search that takes an int...

    Searching an array: --Using the template provided, create a method called search that takes an int and an array as parameters. The method search should traverse the array to look for the int. search should print whether it found the int or not, for example: found: 10 did not find: 11 examples (bold face is text entered by user) % java SearchArray 20 found: 20 % java SearchArray 13 did not find: 13 % java SearchArray 100 found: 100 %...

  • . In the method main, prompt the user for a non-negative integer and store it in...

    . In the method main, prompt the user for a non-negative integer and store it in an int variable num (Data validation is not required for the lab). Create an int array whose size equals num+10. Initialize the array with random integers between 0 and 50 (including 0 and excluding 50). Hint: See Topic 4 Decision Structures and File IO slides page 42 for how to generate a random integer between 0 (inclusive) and bound (exclusive) by using bound in...

  • I am currently using eclipse to write in java. A snapshot of the output would be...

    I am currently using eclipse to write in java. A snapshot of the output would be greatly appreciated to verify that the program is indeed working. Thanks in advance for both your time and effort. Here is the previous exercise code: /////////////////////////////////////////////////////Main /******************************************* * Week 5 lab - exercise 1 and exercise 2: * * ArrayList class with search algorithms * ********************************************/ import java.util.*; /** * Class to test sequential search, sorted search, and binary search algorithms * implemented in...

  • I need java code for the following problem. Lab 7: Methods 1. Write a Java program called Numbers that calls the following methods and displays the returned value: Write a method called cubelt that a...

    I need java code for the following problem. Lab 7: Methods 1. Write a Java program called Numbers that calls the following methods and displays the returned value: Write a method called cubelt that accepts one integer parameter and returns the value raised to the third power as an integer. o Write a method called randominRange that accepts two integer parameters representing a range. The method returns a random integer in the specified range inclusive. 2. o Write a method...

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