Question: Problem b (PA2b.java) You are to write a program that inputs an array of integers and then outputs the length of the longest sequence of
Problem b (PA2b.java) You are to write a program that inputs an array of integers and then outputs the length of the longest sequence of repeating values. For example Enter the number of values: 7 Enter the values: 3 3 5 5 5 5 4 The maximum length of consecutive values is 4. Enter the number of values: 9 Enter the values: 3 4 5 5 6 5 5 4 5 The maximum length of consecutive values is 2. The program will first prompt the user for integers to store an in array. This array will then be passed to a method in order to ascertain the longest repeating sequence. Before attempting the full program, you will first implement a method that checks whether a sequence of a certain length exists in an array. You should complete this method first, then proceed to the maximum length method, and then finally the main method.
starter code:
| public class PA2b { | |
| /** | |
| * Error to supply if input is not positive | |
| */ | |
| public static final String ERR_VALUES = "Number of values must be positive."; | |
| /** | |
| * Returns true if the supplied array has a | |
| * sequence of k consecutive values | |
| * | |
| * @param values input array | |
| * @param k sequence length for which to search | |
| * @return true if values has a consecutive sequence of at least k | |
| */ | |
| public static boolean hasConsecutive(int[] values, int k) { | |
| return false; | |
| } | |
| /** | |
| * Returns the length of the longest | |
| * consecutive sequence in the supplied | |
| * array | |
| * | |
| * @param values input array | |
| * @return length of the longest consecutive value sequence in values | |
| */ | |
| public static int maxConsecutive(int[] values) { | |
| // Hint: hasConsecutive could | |
| // be very useful here | |
| return 0; | |
| } | |
| /** | |
| * Inputs an array of numbers | |
| * and outputs the longest consecutive | |
| * sequence of values | |
| * | |
| * @param args command-line arguments, ignored | |
| */ | |
| public static void main(String[] args) { | |
| // Hint: useful methods and constants here | |
| // - maxConsecutive | |
| // - ERR_VALUES | |
| } | |
| } |
MAKE sure to
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
