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]
2 years 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]2 years 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
given the numerical value 1010101.11, which of the following number systems is most likely represented.
nasty-shy [4]

Answer:

u have to give me more points

Explanation:

sorry

7 0
2 years ago
In order to protect your computer from the newest viruses, which of the following should you do after you’ve installed virus sca
GuDViN [60]
Scan it then check it
3 0
2 years ago
In computing, a(n) _____ is an attack on an information system that takes advantage of a particular system vulnerability. Select
tensa zangetsu [6.8K]

Answer: d) Exploit

Explanation: Exploit is a type computer attack that successful when the computer system of an user is vulnerable and attacker can do the exploitation. This happens due to the weakness of the system, applications software, network etc.

Other given option are incorrect because exit door,glitch and bad are not any type of attack in the computer field that causes harm to the system.Thus the correct option is option(d).

4 0
2 years ago
Type "Alley RB" into the author space on the Web of Science. You will find lots of pages of refereed scientific literature that
agasfer [191]

Answer / Explanation:

Coordination numbers, grain growth in ice, and till deformation.

The Web of Science lists 5 papers that Dr. Alley helped write and that have 1986 publication dates. Dr. Alley was studying why some parts of the Antarctic ice sheet move rapidly, and helped learn that deformation of a special type of mud beneath, called till, was involved. He also was studying the physical properties of ice cores, including why and how some crystals or grains in the ice get bigger over time. Some of the physical properties of the ice cores are related to how many different grains are touching each other, which is the coordination number.

Later, Dr. Alley used his knowledge of physical properties in ice to learn how old ice cores are, and to help learn about climate history from them. The other possibilities listed are all things that he studied, some rather closely related, but that were not published in 1986.

7 0
2 years ago
Which describes a query? A query organizes, summarizes, and presents data in an easy-to-view format. A query allows for retrieva
notka56 [123]
There can be multiple definitions of Query in a database, however, If we go as per the definitions given above, i think most suited description is that the Query allows retrieval of information from multiple tables based on the criteria specified by the user.
0 0
2 years ago
Other questions:
  • In your own words, what is pair-programming? What is the role of the driver? What is the role of the navigator? What are some be
    15·1 answer
  • g Suppose the information content of a packet is the bit pattern 1110 0110 1001 1101 and an even parity scheme is being used. Wh
    8·1 answer
  • How would GIS, GPS, or remote sensing technology be used to evaluate the destruction caused by a tornado in Oklahoma?
    13·1 answer
  • Given an alphabet, print all the alphabets up to and including the given alphabet.
    15·2 answers
  • If, instead, charge 3 is located to the left of charge 1 at a point (on the x axis) that satisfies the conditions given in the p
    7·1 answer
  • Insert the missing code in the following code fragment. This fragment is intended to call the Vehicle class's method. public cla
    14·1 answer
  • Sara is using her personal laptop (which is password protected) at a "hotspot" at a local cafe with wifi access. She is in the m
    7·1 answer
  • A machine is having issues, so the user is responsible for getting the machine serviced. Which stage of the hardware lifecycle d
    13·1 answer
  • On a webpage, a _____ provides supplemental material such as social networking feeds and ads. Group of answer choices footer sid
    9·1 answer
  • Write any 5 activities that help to remove bad events from the society?​
    13·2 answers
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!