Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I\'m in intro to Java can you please write a program with the following requirem

ID: 3702117 • Letter: I

Question

I'm in intro to Java can you please write a program with the following requirements

use only printf for all printings, thanks in advance

(Pattern recognition: four consecutive equal numbers) Write a
method that tests whether the array has four consecutive numbers with the
same value.

public static boolean areFourConsecutives(int[] daRay)

Write a test program that prompts the user to enter a series of integers and
displays if the series contains four consecutive numbers with the same value.
The program should first prompt the user to enter the input (array) size.
Here are two sample runs:

Enter array size: 7
Enter the array values: 3 6 6 6 6 7 3
The array has four consecutive numbers.

Enter array size: 7
Enter the array values: 3 6 6 3 6 7 3
The array does not have four consecutive numbers.

only use printf for all printings, thanks in advance

Explanation / Answer

Executable code:

import java.util.*;
public class HelloWorld{
public static void main(String []args){
Scanner scan= new Scanner(System.in);
//prompts the user to size the array and stored in variable n
System.out.printf("Enter the size of the array: ");
int n=scan.nextInt();
//prompts the user to enter array elements and stored in array ar
System.out.printf("Enter the array elements: ");
int ar[]=new int[n];
for(int i=0;i<n;i++)
ar[i]=scan.nextInt();
//a boolean variable is set false to check if there are any consecutive numbers
boolean flag=false;
for(int i=0;i<n-3;i++){
//loops through all the elements and compares 4 at a time and if found consecutive prints out sets boolean variable flag to true that there are consecutive numbers and breaks out of the loop
if(ar[i]==ar[i+1]&&ar[i+1]==ar[i+2]&&ar[i+2]==ar[i+3])
{
System.out.printf("The array has four consecutive numbers.");
flag=true;
break;
}
}
//if no consecutive numbers are found the boolean variable will be equal to the intialized condition false and no ouput is displayed
if(!flag)
System.out.printf("The array does not have four consecutive numbers.");
}
}

sample input 1:

6 2 3 3 3 3 4

sample output 1:

sample input 2:

7 1 4 5 2 2 3 2

sample output 2:

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote