Question

In: Computer Science

JAVA Write a test program that prompts the user to enter a series of integers and...

JAVA

Write a test program that prompts the user to enter a series of integers and displays whether the series contains runLength consecutive same-valued elements. Your program’s main() must prompt the user to enter the input size - i.e., the number of values in the series, the number of consecutive same-valued elements to match, and the sequence of integer values to check. The return value indicates whether at least one run of runLength elements exists in values. Implement the test program as part of the NConsecutive class (the main() method is already in place at the end of the class).

I am looking for a fix of my current code for this question. My code executes the expected output, but it messes up with other outputs. For example, if I input this data:

Enter The Number of Values: 5
How many consecutive, same-valued elements should I look for?: 2
Enter 5 Numbers: 1 1 2 2 1 1
The list has no sequence of 2 consecutive same-valued elements

The output I should get is "The list has at least 1 sequence of 2 consecutive same-valued elements"

EXPECTED OUTPUT:

Enter the number of values: 3
How many consecutive, same-valued elements should I look for? 3
Enter 3 numbers: 5 5 5
The list has at least one sequence of 3 consecutive same-valued elements
Enter the number of values: 3
How many consecutive, same-valued elements should I look for? 4
Enter 3 numbers: 5 5 5
The list has no sequences of 4 consecutive same-valued elements

CURRENT CODE:

public static boolean hasNConsecutive( int[] values, int runLength ) {
       int count = 0;
       for (int i = 0; i < (values.length-1);i++)
       {
           if (values[i] == values[i + 1]) {
               count++;
               if(count == runLength)
                   break;
           }
           else {
               count = 0;
       }
   }
   if (count >= (runLength - 1))
       return true;
   else
   return false ;
}   // end hasNConsecutive()
   public static void main( String[] args ) {
       int x, runLength;
      
       Scanner input = new Scanner(System.in);
      
       System.out.printf("Enter The Number of Values: ");
       x = input.nextInt();
      
       int values[] = new int[x];
       System.out.printf("How many consecutive, same-valued elements should I look for?: ");
       runLength = input.nextInt();
       System.out.printf("Enter "+ x + " Numbers: ");
       for(int i = 0; i < x; i++) {
           values[i] = input.nextInt();
       }
       input.close();
       if(hasNConsecutive(values,runLength))
           System.out.println("The list has at least one sequence of " + runLength + " consecutive same-valued elements");
       else
           System.out.println("The list has no sequence of " + runLength + " consecutive same-valued elements");
       }

   }

Solutions

Expert Solution

If you have any doubts, please give me comment...

import java.util.Scanner;

public class NConsecutive {

    public static boolean hasNConsecutive(int[] values, int runLength) {

        int count = 0;

        for (int i = 0; i < (values.length - 1); i++) {

            if (values[i] == values[i + 1]) {

                count++;

                if (count == runLength-1)

                    break;

            } else {

                count = 0;

            }

        }

        if (count >= (runLength - 1))

            return true;

        else

            return false;

    } // end hasNConsecutive()

    public static void main(String[] args) {

        int x, runLength;

        Scanner input = new Scanner(System.in);

        System.out.printf("Enter The Number of Values: ");

        x = input.nextInt();

        int values[] = new int[x];

        System.out.printf("How many consecutive, same-valued elements should I look for?: ");

        runLength = input.nextInt();

        System.out.printf("Enter " + x + " Numbers: ");

        for (int i = 0; i < x; i++) {

            values[i] = input.nextInt();

        }

        input.close();

        if (hasNConsecutive(values, runLength))

            System.out.println(

                    "The list has at least one sequence of " + runLength + " consecutive same-valued elements");

        else

            System.out.println("The list has no sequence of " + runLength + " consecutive same-valued elements");

    }

}


Related Solutions

Write a program that prompts the user to enter a series of strings, but with each...
Write a program that prompts the user to enter a series of strings, but with each string containing a small integer. Use a while loop and stop the loop when the user enters a zero. When the loop has finished, the program should display: the number of user inputs (not counting the final zero input). the total of the integers in the strings entered. the average of the integers accurate to one decimal place. Any help is greatly appreciated, this...
Write a program that prompts user to enter integers one at a time and then calculates...
Write a program that prompts user to enter integers one at a time and then calculates and displays the average of numbers entered. Use a while loop and tell user that they can enter a non-zero number to continue or zero to terminate the loop. (Switch statement) Write a program that prompts user to enter two numbers x and y, and then prompts a short menu with following 4 arithmetic operations: Chose 1 for addition Chose 2 for subtraction Chose...
C++ Write a program that prompts the user to enter 50 integers and stores them in...
C++ Write a program that prompts the user to enter 50 integers and stores them in an array. The program then determines and outputs which numbers in the array are sum of two other array elements. If an array element is the sum of two other array elements, then for this array element, the program should output all such pairs separated by a ';'. An example of the program is shown below: list[0] = 15 is the sum of: ----------------------...
Write a JAVA program that prompts the user to enter a single name. Use a for...
Write a JAVA program that prompts the user to enter a single name. Use a for loop to determine if the name entered by the user contains at least 1 uppercase and 3 lowercase letters. If the name meets this policy, output that the name has been accepted. Otherwise, output that the name is invalid.
Write a Java program that prompts the user to enter a list of integer values and...
Write a Java program that prompts the user to enter a list of integer values and displays whether the list is sorted in increasing order or not. Here is a sample run. Note that the first number in the input indicates the number of the elements in the list. <Output> Enter list: 8 101516619111 The list is not sorted <End Output <Output> Enter list: 10 11344579 11 21 The list is already sorted <End Output Create a complete class for...
java language NetBeans Write a program that prompts the user to enter the weight of a...
java language NetBeans Write a program that prompts the user to enter the weight of a person in kilograms and outputs the equivalent weight in pounds. Output both the weights rounded to two decimal places. (Note that 1 kilogram = 2.2 pounds.) Format your output with two decimal places.
In Java: Write a program that prompts the user to enter a positive number until the...
In Java: Write a program that prompts the user to enter a positive number until the user input a negative number. In the end, the program outputs the sum of all the positive numbers. You should use do-while loop (not while, not for). You cannot use break or if statement in the lab. Hint: if the program adds the last negative number to your sum, you can subtract the last number from the sum after the loop.
Write a Java program that prompts a user for 10 integers. When completed it outputs the...
Write a Java program that prompts a user for 10 integers. When completed it outputs the highest number, the lowest number, the number of odd number, and the average of the numbers
C Program 1. Write a program that prompts the user to enter 3 integers between 1...
C Program 1. Write a program that prompts the user to enter 3 integers between 1 and 100 from the keyboard in function main and then calls a function to find the average of the three numbers. The function should return the average as a floating point number. Print the average from main.The function header line will look something like this:float average(int n1, int n2, int n3) STOP! Get this part working before going to part 2. 2. Create a...
Write a test program that prompts the user to enter a sequence of numbers ending with...
Write a test program that prompts the user to enter a sequence of numbers ending with 0, and invokes this method to return the largest number in the input. Use inheritance and polymorphism approach. in java please
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT