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
frozen
1 month 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:
Harlamova29_29 [1K]1 month 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 examine the code from start to finish.

- Two variables are initialized: maxCharacterCount to keep track of the longest character sequence in the string, and longestSubString which holds the longest substring found so far. They start at 0 and the first character of the string, respectively.

- The purpose is to check for all substrings to find the longest one. Hence, a nested for loop is employed (a loop within another loop).

- The outer loop commences at the first character of the input string, extending to its end.

- Within this outer loop, currentMaxCharacterCount counts the currently observed maximum character repetitions.

- The inner loop kicks off from the next character and verifies if it matches the character currently being examined in the outer loop.

- The loop halts if there’s a difference.

- If they match, currentMaxCharacterCount increments by 1, suggesting a possible new longest substring

* Note: When i equals 0, j grows from 1 up to the length of the string. When i is 1, j goes from 2 to the string length, and this cycle continues for all i and j.

- If currentMaxCharacterCount surpasses maxCharacterCount, it indicates a new longest substring, and maxCharacterCount ought to reflect this new count.

- To proceed to look for the subsequent longest substring, it carries on with the next iteration of i (longestSubString = str.charAt(i);)

- Ultimately, once both loops are finished, longestSubString and maxCharacterCount should be displayed.

You might be interested in
A four-year-old laptop will not boot and presents error messages on screen. You have verified with the laptop technical support
8_murik_8 [964]
Consider whether the laptop is worth fixing. If it is, proceed with the repair, and if it isn’t, you know the next steps.
7 0
1 month ago
A network has N subscribers. Each subscriber is connected directly to all other subscribers.
maria [1035]
Ns me plz/..................
6 0
22 days ago
Which of the following is not true of how computers represent complex information
ivann1987 [1066]

Response: c. The same bit sequence may denote various types of information based on context.

Clarification:

The choices provided are:

A. Patterns of bits are utilized by computing devices to signify intricate information

B. Abstraction enables the representation of complex information by revealing hidden complexities

C. The same sequence of bits, based on context, might signify different categories of information

D. Numbers, characters, and colors are some standard abstractions represented by computing devices.

These statements accurately describe how computers handle complex information:

• Patterns of bits are employed by computing devices for representing complex information

• It helps depict complex information by exposing underlying complexities

• Standard abstractions represented by computing devices include numbers, characters, and colors.

Thus, the statement that is not accurate concerning how computers depict complex information is that "the same sequence of bits may represent different information depending on context."

5 0
1 month ago
Read 2 more answers
Computers represent color by combining the sub-colors red, green, and blue (rgb). Each sub-color's value can range from 0 to 255
Harlamova29_29 [1022]

Response:

Below is the code corresponding to this inquiry:

#include <iostream>//specifying a header file

using namespace std; //utilizing the namespace

int main() //defining the main function

{

int red,green,blue,s; //declaring integer variables

cout<<"Enter value: \n ";//displaying prompt message

cin>>red>>green>>blue; //receiving input values

if(red<green && red<blue)//conditional statement to assess red value

s=red;//assign red variable value to s

else if(green<blue)//conditional check if green is less than blue

s=green;//assign green variable value to s

else//else block

s=blue; //assign blue variable value to s

//computing values for red, green, blue

red=red-s;//adjusting red value

green=green-s;//adjusting green value

blue=blue-s;//adjusting blue value

cout<<red<<" "<<green<<" "<<blue;

}

Output:

Enter value:

130  

50

130

80 0 80

Explanation:

In the provided code, within the main method, four integers "red, green, blue, and s" are declared, where "red, green, and blue" receive user input values.

  • Subsequently, a conditional statement evaluates the red variable; if it holds true, its value gets assigned to "s". If not, it proceeds to the else if segment.
  • Within this segment, it assesses if the green variable is lesser than the blue variable; if this condition is fulfilled, the green value will be assigned to "s". Otherwise, it will move to the else segment.
  • In this segment, the blue variable value gets assigned to "s", and then the values of "red, green, and blue" are adjusted by subtracting "s", which is then printed at the end.
4 0
25 days ago
Dan needs to ensure that delegates as well as those looking at his shared calendar information are unable to see the details of
amid [951]

Answer:

To make the meeting details less visible, Dan can change the public access settings in the Google Calendar app to "private." This way, only he can view the specifics of the event. It is generally possible to conceal the list of attendees, but the meeting agenda is often still accessible.

Explanation:

3 0
1 month ago
Other questions:
  • A pedometer treats walking 2,000 steps as walking 1 mile. Write a program whose input is the number of steps, and whose output i
    15·1 answer
  • Write a function solution that returns an arbitrary integer which is greater than n.
    13·1 answer
  • Describe a strategy for avoiding nested conditionals. Give your own example of a nested conditional that can be modified to beco
    11·1 answer
  • OCR Land is a theme park aimed at children and adults. Entrance tickets are sold online. An adult ticket to OCR Land costs £19.9
    7·1 answer
  • To finish creating a design for the webpage, use one shape to cut away part of the other. Create a 700 pixel by 700 pixel square
    10·1 answer
  • Write a program with total change amount as an integer input, and output the change using the fewest coins, one coin type per li
    11·1 answer
  • Which kind of image is indispensable and needs added text to how with it A. a map B. a chart C. a graph or D. a photograph
    6·2 answers
  • The principal that users have access to only network resources when an administrator explicitly grants them is called __________
    6·1 answer
  • hard disk drive has 16 platters, 8192 cylinders, and 256 4KB sectors per track. The storage capacity of this disk drive is at mo
    13·1 answer
  • A ____ partition contains the data necessary to restore a hard drive back to its state at the time the computer was purchased an
    8·2 answers
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!