Question

In: Computer Science

Java. If possible please put explanations for each line of code. Write a test program that...

Java. If possible please put explanations for each line of code.

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).

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

Solutions

Expert Solution

Here is the code:

import java.util.Scanner;

public class Main
{
   public static void main(String[] args) {
   //Scanner Object for input
       Scanner sc = new Scanner(System.in);
      
       //Taking number of values as input
       System.out.print("Enter the number of values: ");
       int n = sc.nextInt();
      
       //Array declaration for storinf values
       int arr[] = new int[n];
      
       //Taking input of How many consecutive elements to find
       System.out.print("How many consecutive, same-valued elements should I look for?: ");
       int c = sc.nextInt();
      
       System.out.print("Enter "+n+" numbers: ");
      
       //Taking the numbers as input and storing them to the array
       for(int i=0; i<n; i++)
       {
       arr[i] = sc.nextInt();
       }
      
       //Condition if the number of consecutive elements to be found are less than or equal to total elements
       if(c<=n)
       {
      
       //Flag variable
       int f=0;
      
       //Checking each consecutive array possible
       for(int i=0; i<=(n-c); i++)
       {
       //If all the elements are not same, turn f=1
       f=0;
       for(int j=i; j<i+c-1; j++)
       {
       if(arr[j] != arr[j+1])
       f=1;
       }
      
       //If f remains zero, that means, we found a pattern
       if(f==0)
       break;
       }
      
       //Output based on flag variable
       if(f==0)
       {
       System.out.println("The list has at least one sequence of "+c+" consecutive same-valued elements");
       }
       else
       {
       System.out.println("The list has no sequences of "+c+" consecutive same-valued elements");
       }
       }
       else
       {
       System.out.println("The list has no sequences of "+c+" consecutive same-valued elements");
       }
   }
}

Output:

PLEASE UPVOTE IF YOU FOUND IT HELPFUL!


Related Solutions

write this program in java... don't forget to put comments. You are writing code for a...
write this program in java... don't forget to put comments. You are writing code for a calendar app, and need to determine the end time of a meeting. Write a method that takes a String for a time in H:MM or HH:MM format (the program must accept times that look like 9:21, 10:52, and 09:35) and prints the time 25 minutes later. For example, if the user enters 9:21 the method should output 9:46 and if the user enters 10:52...
Java programming Write the max-heapify code and test it and then write the program to find...
Java programming Write the max-heapify code and test it and then write the program to find the three largest values of the array without sorting the entire array to get values.
Please write the code JAVA Write a program that allows the user to enter the last...
Please write the code JAVA Write a program that allows the user to enter the last names of five candidates in a local election and the number of votes received by each candidate. The program should then output each candidate’s name, the number of votes received, and the percentage of the total votes received by the candidate. Your program should also output the winner of the election. A sample output is: Candidate      Votes Received                                % of Total Votes...
PLEASE CODE IN JAVA In this assignment you will write a program in that can figure...
PLEASE CODE IN JAVA In this assignment you will write a program in that can figure out a number chosen by a human user. The human user will think of a number between 1 and 100. The program will make guesses and the user will tell the program to guess higher or lower.                                                                   Requirements The purpose of the assignment is to practice writing functions. Although it would be possible to write the entire program in the main function, your...
Write a Java test program, all the code should be in a single main method, that...
Write a Java test program, all the code should be in a single main method, that prompts the user for a single character. Display a message indicating if the character is a letter (a..z or A..Z), a digit (0..9), or other. Java's Scanner class does not have a nextChar method. You can use next() or nextLine() to read the character entered by the user, but it is returned to you as a String. Since we are only interested in the...
Please write a java code. Write a generic program for New Home Construction Pricing with the...
Please write a java code. Write a generic program for New Home Construction Pricing with the following specifications. Note, each one of the 2, 3 or 4 bedroom homes are priced with standard type bathrooms. Update to deluxe or premium choice of bathrooms can be ordered by paying the difference in prices.    Types of homes Price 2 bedroom, 2 bathroom (standard type) and 1 car garage home = $350,000 3 bedroom, 2 bathroom (standard type) and 2 car garage...
Java please! Write the code in Exercise.java to meet the following problem statement: Write a program...
Java please! Write the code in Exercise.java to meet the following problem statement: Write a program that will print the number of words, characters, and letters read as input. It is guaranteed that each input will consist of at least one line, and the program should stop reading input when either the end of the file is reached or a blank line of input is provided. Words are assumed to be any non-empty blocks of text separated by spaces, and...
Write a java code that gets student test scores from the user. Each test score will...
Write a java code that gets student test scores from the user. Each test score will be an integer in the range 0 to 100. Input will end when the user enters -1 as the input value. After all score have been read, display the number of students who took the test, the minimum score, the maximum score, the average score (with decimal point, and the number of A where an A is a score in the range 90-100.
As in previous labs, please write the Java code for each of the following exercises in...
As in previous labs, please write the Java code for each of the following exercises in BlueJ. For each exercise, write a comment before the code stating the exercise number. Make sure you are using the appropriate indentation and styling conventions Exercise 1 (15 Points) In BlueJ, create a new project called Lab6 In the project create a class called Company Add instance data (fields) for the company name (a String) and the sales for the current period (a double)...
JAVA CODE FOR BEGINNERS!! DON'T USE FOR OR WHILE METHODS PLEASE! Write a program that reads...
JAVA CODE FOR BEGINNERS!! DON'T USE FOR OR WHILE METHODS PLEASE! Write a program that reads three strings from the keyboard. Although the strings are in no particular order, display the string that would be second if they were arranged lexicographically.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT