Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions