Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

import java.util.Scanner; public class Lab14 { public static void main(String[]

ID: 3643315 • Letter: I

Question

import java.util.Scanner;


public class Lab14
{
public static void main(String[] args)
{
Scanner stdIn = new Scanner(System.in);
double[] numbers = new double[100];
int numElements = 0;
String input;
double element;
int elementIndex;

do
{
System.out.print("Enter a decimal value (q to quit): ");
input = stdIn.next();
if(!input.equalsIgnoreCase("q")) {
numbers[numElements] = Double.parseDouble(input);
numElements++;
}
} while(!input.equalsIgnoreCase("q") && numElements < 99);

/*
* TODO:
* - prompt the user for a value to search for
* - call the getElementIndex method
* - inform the user as to whether or not the
* element was found in the array
*/
}

public static int getElementIndex(double[] numbers, int numElements,
double element)
{
/*
* TODO:
* - loop through the elements in the numbers array
* - if an element value matches the element parameter,
* then return its index
* - if no element value matches the element parameter,
* then return -1
*/
}
}
Problem Description

Copy the file.
Read through the provided code. It creates an array of doubles that stores decimal values entered by the user until a 'q' is entered.
Read through the TODO comment in the getElementIndex method and write the code to accomplish the tasks it describes.
Read through the TODO comment in the main method and write the code to accomplish the tasks it describes.
Compile and run your program. Test your program with the same input values used in the sample run below.
When you have verified that your program is working correctly, see your TA to receive credit for this lab.
Sample output

Enter a decimal value (q to quit): 10.1
Enter a decimal value (q to quit): 10.2
Enter a decimal value (q to quit): 10.3
Enter a decimal value (q to quit): 10.4
Enter a decimal value (q to quit): q
Enter a decimal value to search for: 10.3
10.3 is present in the array at index 2


Enter a decimal value (q to quit): 0.1
Enter a decimal value (q to quit): 0.2
Enter a decimal value (q to quit): 0.3
Enter a decimal value (q to quit): 0.4
Enter a decimal value (q to quit): 0.5
Enter a decimal value (q to quit): q
Enter a decimal value to search for: 0.6
0.6 is not present in the array

Explanation / Answer

Please rate do not forget to rate, thanks.

public class Lab14
{
public static void main(String[] args)
{
Scanner stdIn = new Scanner(System.in);
double[] numbers = new double[100];
int numElements = 0;
String input;
double element;
int elementIndex;

do
{
System.out.print("Enter a decimal value (q to quit): ");
input = stdIn.next();
if(!input.equalsIgnoreCase("q")) {
numbers[numElements] = Double.parseDouble(input);
numElements++;
}
} while(!input.equalsIgnoreCase("q") && numElements < 99);
System.out.print("Enter a decimal value to search for: ");
element = stdIn.nextDouble();
int numReturn = getElementIndex(numbers, numElements, element);
Arrays.sort(numbers);
elementIndex = Arrays.binarySearch (numbers, element);
if (numReturn == 1)
{
System.out.print(element + " is present in the array at index " + elementIndex);
}
else
{
System.out.print(element + " is not present in the array");
}
}

public static int getElementIndex(double[] numbers, int numElements,
double element)
{
for ( int i = 0; i <numbers.length; i++)

if (element == numbers[i])
{
return 1;
}
return -1;

}
}


Hope this helped