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
Grabar microphone audio icon_person click each item to hear a list of vocabulary words from the lesson. then, record yourself sa
Vaselesa [24]

Because this is a on your own problem, it cannot be solved. You must speak to it after the person says something.

5 0
2 years ago
Read 2 more answers
Write a function string middle(string str) that returns a string containing the middle character in str if the length of str is
Katen [24]

Answer:

function getMiddle(s) {

return s.length % 2 ? s.substr(s.length / 2, 1) : s.substr((s.length / 2) - 1, 2);

}

// I/O stuff

document.getElementById("submit").addEventListener("click", function() {

input = document.getElementById("input").value;

document.getElementById("output").innerHTML = getMiddle(input);

});

Explanation:

// >>> is an unsigned right shift bitwise operator. It's equivalent to division by 2, with truncation, as long as the length of the string does not exceed the size of an integer in Javascript.

// About the ~ operator, let's rather start with the expression n & 1. This will tell you whether an integer n is odd (it's similar to a logical and, but comparing all of the bits of two numbers). The expression returns 1 if an integer is odd. It returns 0 if an integer is even.

// If n & 1 is even, the expression returns 0.

// If n & 1 is odd, the expression returns 1.

// ~n & 1 inverts those two results, providing 0 if the length of the string is odd, and 1 if the length of the sting is even. The ~ operator inverts all of the bits in an integer, so 0 would become -1, 1 would become -2, and so on (the leading bit is always the sign).

// Then you add one, and you get 0+1 (1) characters if the length of the string is odd, or 1+1 (2) characters if the length of the string is even.

6 0
1 year ago
Clunker Motors Inc. is recalling all vehicles from model years 1995-1998 and 2004-2006. A boolean variable named recalled has be
AlekseyPX

Answer:

boolean recalled;

((modelYear>=1995 && modelYear <=1998) || (modelYear>=2004 && modelYear<=2006)) ? recalled =true : recalled =false;

Explanation:

In the first line of the code we declare the variable of type boolean (values can only be true or false) then using the conditional expression operator (ternary operator) in place of an if statement, We state the conditions of the years that will return true and vice versa

3 0
2 years ago
A common fallacy is to use MIPS (millions of instructions per second) to compare the performance of two different processors, an
yulyashka [42]

The question is incomplete. It can be found in search engines. However, kindly find the complete question below:

Question

Cites as a pitfall the utilization of a subset of the performance equation as a performance metric. To illustrate this, consider the following two processors. P1 has a clock rate of 4 GHz, average CPI of 0.9, and requires the execution of 5.0E9 instructions. P2 has a clock rate of 3 GHz, an average CPI of 0.75, and requires the execution of 1.0E9 instructions. 1. One usual fallacy is to consider the computer with the largest clock rate as having the largest performance. Check if this is true for P1 and P2. 2. Another fallacy is to consider that the processor executing the largest number of instructions will need a larger CPU time. Considering that processor P1 is executing a sequence of 1.0E9 instructions and that the CPI of processors P1 and P2 do not change, determine the number of instructions that P2 can execute in the same time that P1 needs to execute 1.0E9 instructions. 3. A common fallacy is to use MIPS (millions of instructions per second) to compare the performance of two different processors, and consider that the processor with the largest MIPS has the largest performance. Check if this is true for P1 and P2. 4. Another common performance figure is MFLOPS (millions of floating-point operations per second), defined as MFLOPS = No. FP operations / (execution time x 1E6) but this figure has the same problems as MIPS. Assume that 40% of the instructions executed on both P1 and P2 are floating-point instructions. Find the MFLOPS figures for the programs.

Answer:

(1) We will use the formula:

                                       CPU time = number of instructions x CPI / Clock rate

So, using the 1 Ghz = 10⁹ Hz, we get that

CPU time₁ = 5 x 10⁹ x 0.9 / 4 Gh

                    = 4.5 x 10⁹ / 4 x 10⁹Hz = 1.125 s

and,

CPU time₂ = 1 x  10⁹ x 0.75 / 3 Ghz

                  = 0.75 x 10⁹ / 3 x 10⁹ Hz = 0.25 s

So, P2 is actually a lot faster than P1 since CPU₂ is less than CPU₁

(2)

     Find the CPU time of P1 using (*)

CPU time₁ = 10⁹ x 0.9 / 4 Ghz

                = 0.9 x 10⁹ / 4 x 10⁹ Hz = 0.225 s

So, we need to find the number of instructions₂ such that  CPU time₂ = 0.225 s. This means that using (*) along with clock rate₂ = 3 Ghz and CPI₂ = 0.75

Therefore,   numbers of instruction₂ x 0.75 / 3 Ghz = 0.225 s

Hence, numbers of instructions₂ = 0.225 x 3 x  10⁹ / 0.75  = 9 x 10⁸

So, P1 can process more instructions than P2 in the same period of time.

(3)

We recall  that:

MIPS = Clock rate / CPI X 10⁶

  So, MIPS₁ = 4GHZ / 0.9 X 10⁶ = 4 X 10⁹HZ / 0.9 X 10⁶ = 4444

        MIPS₂ = 3GHZ / 0.75 X 10⁶ = 3 x 10⁹ / 0.75 X 10⁶ = 4000

So, P1 has the bigger MIPS

(4)

  We now recall that:

MFLOPS = FLOPS Instructions / time x 10⁶

              = 0.4 x instructions / time x 10⁶ = 0.4 MIPS

Therefore,

                  MFLOPS₁ = 1777.6

                  MFLOPS₂ = 1600

Again, P1 has the bigger MFLOPS

3 0
1 year ago
Nancy would like to configure an automatic response for all emails received while she is out of the office tomorrow, during busi
Stella [2.4K]

she can appoints someone she trust to act on her behalf

4 0
2 years ago
Read 2 more answers
Other questions:
  • The ________ method is based on simple arithmetic. The process involves dividing the bits of a frame into equal segments, adding
    11·1 answer
  • You are attempting to open a file containing a list of integer numbers, each on a separate line, and read them into Python as in
    6·1 answer
  • There are no breakpoints in the "Access Customer Account" subpage however there is an error. What will happen if you choose to s
    11·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
  • Which of the following can you NOT apply for at any FLHSMV office? A. Certificate of title B. License plates C. Vehicle registra
    15·2 answers
  • ___________is used for drawing 3D objects in the field of Science and Engineering.
    12·2 answers
  • Through which of the devices listed are we able to connect to wireless networks? Check all that apply.
    14·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
  • Television broadcasts were originally delivered by using which technology
    14·1 answer
  • Chinh wants to have a program print, "Sorry, but that isn’t one of your options" until the user enters the correct information.
    13·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!