Question
import java.util.ArrayList; import java.util.List; import java.util.Random; import java.util.Scanner; /**A demonstration of the class IntervalFinder. @author Charles Hoot @version 4.0 */ public class Driver { public
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.Scanner;
/**A demonstration of the class IntervalFinder.
@author Charles Hoot
@version 4.0
*/
public class Driver
{
public static void main(String args[])
{
Random generator = new Random();
final int N = 15;
// Generate a random array of integers
Integer sortedData[] = new Integer[N];
List targets = null;
for(int i = 0; i < N; i++)
{
Integer value = generator.nextInt(10);
sortedData[i] = value;
// Guarantee is larger than the last value
if (i > 0 && sortedData[i] < sortedData[i-1])
sortedData[i] += sortedData[i-1];
} // end for
System.out.println("The sorted data is:");
for (int i = 0; i < N; i++)
System.out.print(sortedData[i] + " ");
System.out.println();
targets = getTargetValues();
boolean done = (targets.size() == 0);
while (!done)
{
Interval result = IntervalFinder.findInterval(sortedData, targets);
System.out.println("The pair of indices that bound the interval " +
"containing the given values is " + result);
System.out.println();
targets = getTargetValues();
done = (targets.size() == 0);
} // end while
System.out.println("Goodbye!");
} // end main
/** Initializes the list of ints by reading them from the keyboard.
@return A list of integers. */
public static List getTargetValues()
{
List result = new ArrayList<>();
System.out.println("Enter the list of integer values (all on one line), " +
"or just press enter if you are done.");
Scanner fromKeyboard = new Scanner(System.in);
String values = fromKeyboard.nextLine();
Scanner dataValues = new Scanner(values);
while (dataValues.hasNextInt())
{
int data = dataValues.nextInt();
result.add(new Integer(data));
} // end while
return result;
} // end getTargetValues
} // end Driver
/*
The sorted data is:
2 6 6 6 9 10 14 21 26 27 33 40 46 46 52
Enter the list of integer values (all on one line), or just press enter if you are done.
2 52
The pair of indices that bound the interval containing the given values is (0, 14)
Enter the list of integer values (all on one line), or just press enter if you are done.
7 42 20
The pair of indices that bound the interval containing the given values is (3, 12)
Enter the list of integer values (all on one line), or just press enter if you are done.
Goodbye!
*/
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started