Question: a static method called repeat that takes as its two parameters a string str and an integer n and returns a new string consisting of

a static method called repeat that takes as its two parameters a string str and an integer n and returns a new string consisting of n copies of str. You may assume that n is positive. For example:

  • repeat("Java", 3) should return the string "JavaJavaJava"

  • repeat("oh!", 7) should return the string "oh!oh!oh!oh!oh!oh!oh!"

This method should not do any printing; rather, it should construct and return the appropriate string.

Hints:

  • You will need to gradually build up the return value using string concatenation. To do so, we recommend that you use a variable for your return value and that you give it the empty string as its initial value:

    String result = ""; 

    This will allow you to gradually add to the result by using statements that look like the following:

    result = result + expr; 

    where expr is replaced by an appropriate expression.

  • You will need to use a for loop as part of your implementation of this method.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!