Question

In: Computer Science

In java Write a static method named consecutiveDigits that accepts an integer n as a parameter...

In java Write a static method named consecutiveDigits that accepts an integer n as a parameter and that returns the highest number of consecutive digits in a row from n that have the same value. For example, the number 3777785 has four consecutive occurrences of the number 7 in a row, so the call consecutiveDigits(3777785) should return 4.

For many numbers the answer will be 1 because they don't have any adjacent digits that match. Below are sample calls on the method. You are not allowed to use a String to solve this problem. You may assume that the value passed to the method is greater than or equal to 0.

consecutiveDigits(99) will return 2

consecutiveDigits(1) will return 1

consecutiveDigits(11335577) will return 2

consecutiveDigits(12345) will return 1

consecutiveDigits(89995) will return 3

Solutions

Expert Solution

Sample output:

The screenshots are attached below for reference.

Please follow them for proper output.

Program to copy:

import java.util.*;
public class Main
{
public static int consecutiveDigits(int n){
String s;
s=Integer.toString(n);
int c=1,temp=1;
for(int i=0;i<s.length()-1;i++){//iterate through all the numbers
if(s.charAt(i)==s.charAt(i+1)){//check if same or not
temp++;//increment temp if same consecutiveDigits occurs
if(s.length()==2)
return temp;
}
else{
if(c<temp){//check if current count is maximum
c=temp;
temp=1;
}
else
temp=1;
}
}
return c;//return maximum coutn obtained
}
   public static void main(String[] args) {
       Scanner inp=new Scanner(System.in);
       int num;
       String n;
       System.out.println("Enter number:");//read number from user
       num=inp.nextInt();
       int res=consecutiveDigits(num);//call the function
       System.out.println("The maximum consecutive same value count is:"+res);//print the returned value
      
   }
}

Note:

Please let me know in case of any help needed in the comments section.

Plese upvote my answer. Thank you.


Related Solutions

Write a RECURSIVE method that receives as a parameter an integer named n. The method will...
Write a RECURSIVE method that receives as a parameter an integer named n. The method will output n # of lines of stars. For example, the first line will have one star, the second line will have two stars, and so on. The line number n will have "n" number of ****** (stars) so if n is 3 it would print * ** *** The method must not have any loops!
Write a method sumTo that accepts an integer parameter n and returns the sum of the...
Write a method sumTo that accepts an integer parameter n and returns the sum of the first n reciprocals. In other words: sumTo(n) returns: 1 + 1/2 + 1/3 + 1/4 + ... + 1/n For example, the call of sumTo(2) should return 1.5. The method should return 0.0 if passed the value 0 and should print an error message and return -1 if passed a value less than 0. Include a loop. Please help for Java programming.
Write a complete static method named addUp that accepts, in order, an integer number and an...
Write a complete static method named addUp that accepts, in order, an integer number and an integer thisMany as parameters and that prints a complete line of output reporting all the numbers that result from adding number to itself thisMany number of times (starting at 1 and ending at thisMany). For example, the following calls: addUp(3, 5); addUp(7, 3); should produce this output: Adding up 3 for 5 times gives: 3, 6, 9, 12, 15 Adding up 7 for 3...
Write, in Java, a recursive method countBinaryStrings that has one integer parameter n and returns the...
Write, in Java, a recursive method countBinaryStrings that has one integer parameter n and returns the number of binary strings of length n that do not have two consecutive 0’s. For example, for n = 4, the number of binary strings of length 4 that do not contain two consecutive 0’s is 8: 1111, 1110, 1101, 1011, 1010, 0111, 0110, 0101. For this problem, your method needs to return only the number of such strings, not the strings themselves. You...
Write Java code that accepts the integer input (from keyboard) in an arraylist named num1 and...
Write Java code that accepts the integer input (from keyboard) in an arraylist named num1 and stores the even integers of num1 in another arraylist named evennum.
a) Write a function drawShape() that accepts a parameter n, and: If n is odd, it...
a) Write a function drawShape() that accepts a parameter n, and: If n is odd, it constructs a pattern of a diamond of height n If n is even, it constructs an hourglass of length n b) What is the time complexity of the drawShape() function you created? C++ language with for loop
Write a recursive method repeatNTimes(String s, int n) that accepts a String and an integer as...
Write a recursive method repeatNTimes(String s, int n) that accepts a String and an integer as two parameters and returns a string that is concatenated together n times. (For example, repeatNTimes ("hello", 3) returns "hellohellohello") Write a driver program that calls this method from the Main program. Need code in c#.
Using C++ Write a template function that accepts an integer parameter and returns its integer square...
Using C++ Write a template function that accepts an integer parameter and returns its integer square root. The function should return -1, if the argument passed is not integer. Demonstrate the function with a suitable driver program .
(java) Part 1 Write a method named compare that accepts two String arrays as parameters and...
(java) Part 1 Write a method named compare that accepts two String arrays as parameters and returns a boolean. The method should return true if both arrays contain the same values (case sensitive), otherwise returns false. Note - the two arrays must be the same length and the values must be in the same order to return true. Part  2 Write a method named generateArray that accepts an int size as its parameter and returns an int[] array where each element...
Python: Write a function named calc_odd_sum that accepts a positive integer as the argument and returns...
Python: Write a function named calc_odd_sum that accepts a positive integer as the argument and returns the sum of all the odd numbers that are less than or equal to the input number. The function should return 0 if the number is not positive. For example, if 15 is passed as argument to the function, the function should return the result of 1+3+5+7+9+11+13+15. If -10 is passes, it shall return 0. You shall run the program test the result at...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT