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...

  • JAVA~ 1. Write a static method named countOdd(my_array) that returns the number of odd integers in...

    JAVA~ 1. Write a static method named countOdd(my_array) that returns the number of odd integers in a given array. If there are no odd numbers in the array, the method should return 0. If the array is empty, the method should also return 0. For example: Test Result System.out.println(countOdd(new int[]{2, 3, 5, 6})); 2 System.out.println(countOdd(new int[]{2})); 0 System.out.println(countOdd(new int[]{})); 0 System.out.println(countOdd(new int[]{-7, 2, 3, 8, 6, 6, 75, 38, 3, 2})); 4 public static int ----------------------------------------------------------------------------------------------------------- 2. Write a static...

  • Write a program that stores a phrase as an array of words, and then prints it...

    Write a program that stores a phrase as an array of words, and then prints it backwards. The main method calls method getInput , which asks the user how many words there are, stores them in an array, and returns this array. printBackwards then takes this array of words, and prints it in reverse. Code Example: import java.util.Scanner; public class L17Num1{    public static void main(String[] args) {    String[] sArray=getInput(); printBackwards(sArray); } public static String[] getInput() { System.out.println("How many...

  • 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...

  • Write a java program that has a method called sameArrayBackwards. The method takes an array of...

    Write a java program that has a method called sameArrayBackwards. The method takes an array of integers, and checks if the numbers in the array are the same going forward as going backwards and will return a boolean (true or false) depending on the result. You can assume that there is at least one element in the array. Method Header: public static boolean sameArrayBackwards (int [] arr) You will test your method in the main method of your program by...

  • 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 Java method that should take an ArrayList as a parameter, print its element in...

    Write a Java method that should take an ArrayList as a parameter, print its element in the reverse order. The main function that calls the method is the following: import java.util.*; public class ReverseGen {   public static void main(String[] args) {     Scanner input = new Scanner(System.in);       System.out.println("How many numbers do you want to input?");       int num = input.nextInt();       ArrayList<Double> d = new ArrayList<Double>(num);       for(int i = 0 ; i < num; i++){           System.out.print("Enter...

  • 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...

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