findRange
Category: Programming
Author: Stuart Reges
Book Chapter: 5.4
Problem: findRange
Write a static method called findRange that takes a console Scanner as a parameter and that prompts the user for a sequence of integers, reporting the greatest difference among the integers. The method should prompt for integers until the user enters a 0 and it should then report the greatest difference between a pair of numbers from the sequence (i.e., the largest value for x - y where x and y are numbers from the sequence). For example, the following call: Scanner console = new Scanner(System.in); findRange(console); would generate an interaction like this: number (0 to quit)? 45 number (0 to quit)? 3 number (0 to quit)? 8 number (0 to quit)? 54 number (0 to quit)? 0 range = 51 You must exactly reproduce the format of this sample execution. The method reports 51 as the range because the greatest difference between two of these numbers is obtained when you take 54 minus 3. Notice that the sentinel value of 0 is not considered part of the sequence. The sequence might include negative values, as in the following interaction: number (0 to quit)? 8 number (0 to quit)? -17 number (0 to quit)? 5 number (0 to quit)? 0 range = 25 The method reports a range of 25 because the greatest difference is obtained by taking 8 minus -17. You may assume that the user types at least two nonzero values. The range would be 0 if all numbers in the sequence are the same.