Problem Description 1.Read through the provided code. It creates an array of dou
ID: 3642777 • Letter: P
Question
Problem Description
1.Read through the provided code. It creates an array of doubles that stores decimal values entered by the user until a 'q' is entered.
2.Read through the TODO comment in the getElementIndex method and write the code to accomplish the tasks it describes.
3.Read through the TODO comment in the main method and write the code to accomplish the tasks it describes.
4.Compile and run your program. Test your program with the same input values used in the sample run below.
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
The program:
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
*/
}
}
Explanation / Answer
Here's another shot at it:
Thanks, Joe
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.