Question: need help need to pass junit quickly. Problem b (PA2b.java) You are to write a program that inputs an array of integers and then outputs
need help need to pass junit quickly.
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.
package edu.wit.cs.comp1050; import java.util.Scanner; //TODO: document this class 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) { int count = 1; for(int i = 0; i < values.length - 1; i++) { if(values[i] == values[i + 1]) { count++; } else{ if(count >= k) { count = 1; return true; } } }
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 int count = 1; int max = 1; for(int i = 0; i < values.length - 1; i++) { if(values[i] == values[i + 1]) { count++; } else{ if(count > max) { max = count; count = 1; } } } 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 Scanner s = new Scanner(System.in); System.out.print("Enter the number of values : "); int n = s.nextInt(); // read the size int a[] = new int[n]; int flag = 0; System.out.print("Enter the values : "); for(int i = 0; i < n; i++) { a[i] = s.nextInt(); // read the Array if(a[i] == -1) /// if negative elements { flag = 1; } } if(flag == 1) { System.out.println(ERR_VALUES); } else { System.out.print("Enter the length to be there : "); int length = s.nextInt(); // enter the desired length boolean result = hasConsecutive(a, length); if(result == true) // if consecutive of the desired length { System.out.println("Array has the consecutive length " + length); } else{ System.out.println("Array does not has the consecutive length " + length); } // printing the maximum consecutive length System.out.println("Maximum consecutive length is : " + maxConsecutive(a)); } }
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
