Intro to JAVA please see below thank you! --------------------------------------
ID: 3913830 • Letter: I
Question
Intro to JAVA please see below thank you!
------------------------------------------------------------------------------------------
Now suppose we want to find the positions of all matches. Here is a plan, using the findNext method of the preceding problem:
Allocate a partially filled array result.
While findNext returns a valid position
Insert the position to the end of result.
Copy result into an array whose length equals the number of matches.
Return that copy.
Complete the following code:
import java.util.Arrays;
public class FindAll
{
/**
Finds the positions of all occurrences of an element in an array.
@param values an array of values
@param searchedValue the value to search for
@param the positions of all matches
*/
int[] findAll(int[] values, int searchedValue)
{
int[] result = new int[. . .];
int resultSize = 0;
int pos = -1;
do
{
pos = findNext(values, searchedValue, pos + 1);
if (. . .)
{
. . .
}
} while (. . .);
. . .
}
/**
Finds the next occurrence of an element in an array.
@param values an array of values
@param searchedValue the value to search for
@param start the position at which to start the search
@return the position of the first match at position >= start,
or -1 if the element was not found
*/
int findNext(int[] values, int searchedValue, int start)
{
for(int position = start; position<values.length; position++)
{
if(values[position]==searchedValue)
return position;
}
return -1;
}
}
Explanation / Answer
package classProg;
import java.util.Arrays;
import java.util.Scanner;
// Class FindAll definition
public class FindAll
{
/**
Finds the positions of all occurrences of an element in an array.
@param values an array of values
@param searchedValue the value to search for
@param the positions of all matches
*/
int[] findAll(int[] values, int searchedValue)
{
// Creates an array of array size values to store found index position
int[] result = new int[values.length];
// Found index position counter
int resultSize = 0;
// Loops till length of the result array
for(int c = 0; c < result.length; c++)
// Assigns -1 to current index position of the array
result[c] = -1;
// To store the starting index position of the array to search
int pos = -1;
// To store the previous found index position
int previous = -1;
// Loops till the length of the array values
do
{
// Stores the current starting index position as previous
previous = pos + 1;
// Calls the method to search the number from pos value plus one index position
// If found returns the found index position otherwise returns -1
pos = findNext(values, searchedValue, pos + 1);
// If pos value is not -1
if (pos != -1)
// Stores the pos value at resultSize index position of result array
result[resultSize++] = pos;
// Otherwise reset the pos value to previous index position
else
pos = previous;
} while (pos != values.length); // End of do - while loop
// Returns the result array
return result;
}// End of method
/**
Finds the next occurrence of an element in an array.
@param values an array of values
@param searchedValue the value to search for
@param start the position at which to start the search
@return the position of the first match at position >= start,
or -1 if the element was not found
*/
int findNext(int[] values, int searchedValue, int start)
{
// Loops from start to length of the array
for(int position = start; position < values.length; position++)
{
// Checks if current index position value is equals to searchedValue
if(values[position] == searchedValue)
// Returns the position
return position;
}// End of for loop
// Otherwise returns -1
return -1;
}// End of method
// main method definition
public static void main(String[] args)
{
// Scanner class object created
Scanner sc = new Scanner(System.in);
// To store found status
boolean foundStatus = false;
// Creates a object of class FindAll
FindAll fa = new FindAll();
// Creates an array
int numberArray [] = {10, 2, 5, 10, 4, 8, 2, 10, 33, 10, 9, 2, 78, 10};
// Displays the original array
System.out.println("Original Array");
// Loops till length of the array
for(int c = 0; c < numberArray.length; c++)
// Displays the value
System.out.print(numberArray[c] + " ");
// Accepts a number to search
System.out.print(" Enter a number to search: ");
int no = sc.nextInt();
// Calls the method and stores the return result array
int foundPositions [] = fa.findAll(numberArray, no);
// Displays the result array
System.out.println(" Found Index Positions");
// Loops till length of the array
for(int c = 0; c < foundPositions.length; c++)
{
// Checks if current index position value is not -1 then display the number
if(foundPositions[c] != -1)
{
// Displays the value
System.out.print(foundPositions[c] + " ");
// Sets the found status to true for found
foundStatus = true;
}// End of if condition
}// End of for loop
// Checks if the found status is false then display no such number found
if(foundStatus == false)
System.out.println(" ERROR: No such number found.");
// Closes the scanner class
sc.close();
}// End of main method
}// End of class
Sample Output 1:
Original Array
10 2 5 10 4 8 2 10 33 10 9 2 78 10
Enter a number to search: 99
Found Index Positions
ERROR: No such number found.
Sample Output 2:
Original Array
10 2 5 10 4 8 2 10 33 10 9 2 78 10
Enter a number to search: 10
Found Index Positions
0 3 7 9 13
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.