answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
JulijaS [17]
1 year ago
7

JAVAThe method longestStreak is intended to determine the longest substring of consecutive identical characters in the parameter

str and print the result.For example, the call longestStreak("CCAAAAATTT!") should print the result "A 5" because the longest substring of consecutive identical characters is "AAAAA".
Complete the method below. Your implementation should conform to the example above.
public static void longestStreak(String str)
Computers and Technology
1 answer:
Butoxors [25]1 year ago
5 0

Answer:

public static void longestStreak(String str){

       int maxCharacterCount = 0;

       char longestSubString = str.charAt(0);

       

       for(int i=0; i<str.length(); i++){

           

           int currentMaxCharacterCount = 1;

           

           for(int j=i+1; j<str.length(); j++){

               if(str.charAt(i) != str.charAt(j)){

                   break;

               }

               else {

                   currentMaxCharacterCount++;

               }

           }  

           if (currentMaxCharacterCount > maxCharacterCount) {

                   maxCharacterCount = currentMaxCharacterCount;

                   longestSubString = str.charAt(i);

           }

       }

       System.out.println(longestSubString + " "+ maxCharacterCount);

   }



Explanation:

Let's start from the top of the code and go all the way down.

- Two variables created; <em>maxCharacterCount</em> to count the longest character in the string, and <em>longestSubString</em> to save the current longest substring. Initially they are set to 0 and the first character of given string respectively.

- We need to check all the substrings to find longest one. That is why a <u>nested for loop</u> (for loop inside a for loop) is created.

- The first loop starts from the first character of the given string and goes until the end of the given string.

- Inside the first loop, <em>currentMaxCharacterCount</em> is created to hold the current value of the maximum character in the given string.

- The second loop starts from the second character of the string, and checks if the character is same as the one that is being checked by the first loop.

- If they are not same, then the loop stops.

- If they are the same characters, <em>currentMaxCharacterCount</em> is incremented by 1. (That means we may find our new longest substring)

* Reminder: When the <em>i</em> is equal to 0, <em>j </em>is increasing from 1 to given string length. When <em>i</em> is equal to 1 <em>j</em> is increasing from 2 to given string length and so on. This process repeats for all <em>i</em> and <em>j</em> values to find out the longest substring.

- If <em>currentMaxCharacterCount </em>is greater than<em> maxCharacterCount, </em>that means we have a new longest substring, and <em>maxCharacterCount </em>should be equal to this new substring's count.

- Then, to be able to check for the next possible longest substring, it should continue from the next iteration of the <em>i </em>(longestSubString = str.charAt(i);)

- Finally, when the loops are done checking, <em>longestSubString</em> and <em>maxCharacterCount</em> need to be printed.

You might be interested in
This program finds the sum and average of three numbers. What are the proper codes for Lines a and b?
Gennadij [26K]

Answer:

The Proper codes in Line a and Line b is given below

average=Sum/3  

print (" Average is = ", average)

Explanation:

In the given question it calculated the sum but the program does not calculate the average of the 3 numbers.The average of the 3 number is calculated by using average=Sum/3   statement so we add this code in Line a then After that print the value of average by using the print function so we add this code in Line b.

4 0
1 year ago
Read 2 more answers
When long labels are required, which of these commands can you use to improve readability of a worksheet?
Morgarella [4.7K]
I believe it is wrap text! Hope this helped
6 0
2 years ago
Read 2 more answers
Where does an MPLS label go in a PDU?
Natalija [7]

Answer: Between Layers 2 and 3

Explanation:

In between Layer 2 and Layer 3 the MPLS header is present and is known as Shim header. It is also said to be in 2.5.

6 0
1 year ago
Two devices are connected to the Internet and communicating with one another. A squirrel chews through one of the wires in the n
bearhunter [10]

Answer:

Fault-Tolerance

Explanation:

Fault tolerance refers to the ability of a system (computer, network, cloud cluster, etc.) to continue operating without interruption when one or more of its components fail.

5 0
2 years ago
Which element of the word program window contains buttons for saving a document and for undoing, redoing, and repeating a change
miskamm [114]
The answer is the Quick Access Toolbar. However, it is not only for saving files or undoing your work. Containing a set of commands that are independent, this toolbar is actually customizable wherein you could change these icons to the ones you really need and frequently use. By tweaking the settings, you can even add commands to the Quick Access Toolbar that are not in the ribbon (like New, Open and Print).
7 0
2 years ago
Other questions:
  • Which of the following is not a benefit of active listening?
    8·1 answer
  • ____ refers to typing your entire e-mail message or discussion group post using only capital letters.
    15·1 answer
  • Write a program that prompts the user to enter the month and year and displays the number of days in the month. For example, if
    13·1 answer
  • When an author produce an index for his or her book, the first step in this process is to decide which words should go into the
    8·1 answer
  • A(n) ____ attack is when a system is compromised and used to attack other systems. a. indirect b. direct c. object d. subject
    13·1 answer
  • Define a method named roleOf that takes the name of an actor as an argument and returns that actor's role. If the actor is not i
    8·1 answer
  • Write a program that calculates an adult's fat-burning heart rate, which is 70% of 220 minus the person's age. Complete fat_burn
    13·1 answer
  • Q3** Write a query to create a new price list for books written by the same author. Allow the user to enter only the first 4 let
    15·1 answer
  • Information systems cannot solve some business problems. Give three examples and explain why technology cannot help.
    11·1 answer
  • Explain the history of computing of mechanical era
    14·2 answers
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!