1answer.
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
iogann1982 [59]
3 years ago
10

Write a static method called bothStart that allows the user to input two Strings and returns the String that is the longest subs

tring s such that both inputs start with s.
For example,


bothStart( "radar dish", "radical food" ) returns "rad"


bothStart( "radar disk", "disk radar" ) returns ""


bothStart( "radar installation", "" ) returns ""


bothStart( "Mendacious", "Mendicant" ) returns "Mend"


bothStart( "FRANKLY", "frank" ) returns ""
Computers and Technology
1 answer:
marishachu [46]3 years ago
5 0

Answer:

  1.    public static String bothStart(String text1, String text2){
  2.        String s = "";
  3.        if(text1.length() > text2.length()) {
  4.            for (int i = 0; i < text2.length(); i++) {
  5.                if (text1.charAt(i) == text2.charAt(i)) {
  6.                    s += text1.charAt(i);
  7.                }else{
  8.                    break;
  9.                }
  10.            }
  11.            return s;
  12.        }else{
  13.            for (int i = 0; i < text1.length(); i++) {
  14.                if (text1.charAt(i) == text2.charAt(i)) {
  15.                    s += text1.charAt(i);
  16.                }else{
  17.                    break;
  18.                }
  19.            }
  20.            return s;
  21.        }
  22.    }

Explanation:

Let's start with creating a static method <em>bothStart()</em> with two String type parameters, <em>text1 </em>&<em> text2</em> (Line 1).  

<em />

Create a String type variable, <em>s,</em> which will hold the value of the longest substring that both inputs start with the same character (Line 2).

There are two possible situation here: either <em>text1 </em>longer than<em> text2 </em>or vice versa. Hence, we need to create if-else statements to handle these two position conditions (Line 4 & Line 13).

If the length of<em> text1</em> is longer than <em>text2</em>, the for-loop should only traverse both of strings up to the length of the <em>text2 </em>(Line 5). Within the for-loop, we can use<em> charAt()</em> method to extract individual character from the<em> text1</em> & <em>text2 </em>and compare with each other (Line 15). If they are matched, the character should be joined with the string s (Line 16). If not, break the loop.

The program logic from (Line 14 - 20) is similar to the code segment above (Line 4 -12) except for-loop traverse up to the length of <em>text1 .</em>

<em />

At the end, return the s as output (Line 21).

You might be interested in
Read the paragraph.
aleksandrvk [35]

Answer:

I think sentence 3 bc it doesn't really make any sense Ik it's explaining it but it doesnt connect with the whole story as much I think it has to have more details

5 0
3 years ago
Read 2 more answers
What is the central idea of the second section:
Ivanshal [37]

Answer:

The second is that laughter prepares us for tough conditions. And it does always ensure good things for us, and there are many real advantages of the laughter that we can trace out. And all those can be parsed from our real life. We just need to think of a sad situation and try to realize how laughter helps us get rid of that sad situation. And that is what is the central idea of the second section.

Explanation:

Please check the answer section.

7 0
2 years ago
Task 04
aleksley [76]

Answer:

are u sure this is the right question?

Explanation:

7 0
1 year ago
The four smallest numbers (40 points) Write an ANNA assembly program (smallest_four.ac) that finds the four smallest numbers ent
kari74 [83]

Answer:

the answer is -25

Explanation:

6 0
2 years ago
What is a lan adapter
ryzh [129]

Answer:

A LAN adapter is an expansion board that is used to connect a computer or computing device to a network. LAN adapter is the most common and generic alternate name for a Network Interface Card (NIC).

4 0
3 years ago
Read 2 more answers
Other questions:
  • Which is an advantage the electronic scheduling tools have over paper calenders?​
    11·1 answer
  • A rectangular range of cells with headings to describe the cells' contents is referred to as a
    9·1 answer
  • @anthonydboss23<br><br> it’s goldielove6 <br><br> .....this is not a question everyone ignore this
    11·2 answers
  • Wired network are the most reliable and provide the highest speed?
    7·1 answer
  • For some reason, Danica's classmate George could not find the registered symbol in the symbol gallery. He is selling
    14·1 answer
  • Implement a metho d to nd the k-th largest element in an array of size N using a minimum priority queue (MinPQ). Assume that the
    8·1 answer
  • Identify 5 products/services needed by the people in our current situation.<br>​
    6·1 answer
  • When looking at security standard and compliance, which three (3) are characteristics of best practices, baselines and framework
    8·1 answer
  • PLS HURRY<br> Look at the image below
    8·1 answer
  • What instructions would a computer have the hardest time completing correctly
    12·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!