Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

stuck on pa2b cant pass has consecutive stuck on it You are to write a program that inputs an array of integers and then outputs

stuck on pa2b cant pass has consecutive stuck on it

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; import java.util.Arrays; //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; int consecutive = 0; int value; if (values.length != 0) { consecutive = 1; if (consecutive >= k && k>0) { return true; } for(int i = 0;i

} }

/** * 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 if (values.length == 0) return 0; else if (values.length == 1) return 1; //maximum count of consecutive integers int max_count = 1; //sequence count of the current integer int curr_count = 1; int previous = values[0]; for (int i = 1; i < values.length; i++) { if (values[i] == previous) { curr_count++; max_count = Math.max(curr_count, max_count); } else { //update to 1 because a new integer not in sequence is encountered curr_count = 1; } //update previous value previous = values[i]; } return max_count; } /** * 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 sc = new Scanner(System.in); System.out.println("Enter the number of values: "); int n = sc.nextInt(); int arr[] = new int[n]; System.out.println("Enter the values: "); for (int i = 0; i < n; i++) { arr[i] = sc.nextInt(); } System.out.println("The maximum length of consecutive values is: " + maxConsecutive(arr)); System.out.println(hasConsecutive(arr, 2)); }

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

SQL Server Query Performance Tuning

Authors: Sajal Dam, Grant Fritchey

4th Edition

1430267429, 9781430267423

More Books

Students also viewed these Databases questions

Question

FindSpaces.java using single return statement

Answered: 1 week ago