Question

Write a recursive method repeatNTimes(String s, int n) that accepts a String and an integer as...

Write a recursive method repeatNTimes(String s, int n) that accepts a String and an integer as two parameters and returns a string that is concatenated together n times. (For example, repeatNTimes ("hello", 3) returns "hellohellohello") Write a driver program that calls this method from the Main program.

Program should ask for input and be stored in the string and should return n amount of times.

Homework Answers

Answer #1

HERE IS THE CODE...PLEADE GIVE AN UPVOTE

import java.io.*;

import java.util.Scanner;

public class Main {

// class declaration
public static String repeat(String str,int n)
{
if (n==1)
return str;
else
return str+repeat(str,(n-1));
}
public static void main(String args[]){

System.out.println("please enter the string and number of times");

Scanner sc= new Scanner(System.in);
String str;

int n;
str= sc.next();

n=sc.nextInt();

System.out.println("result : " +repeat(str,n)); // print the result
}
}

INPUT ----> String mon n=4

result : monmonmonmon

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
*In Java Please RECURSION Objectives • Learn the basics of recursion – Part II (last week...
*In Java Please RECURSION Objectives • Learn the basics of recursion – Part II (last week was Part I) Submission Guidelines: You will turn in 2 program files (one for each lab problem) Tasks This lab has two parts: 1. Write a recursive method repeatNTimes(String s, int n) that accepts a String and an integer as two parameters and returns a string that is concatenated together n times. (For example, repeatNTimes ("hello", 3) returns "hellohellohello") Write a driver program that...
Please code in Java Write a recursive method that takes a string and return a string...
Please code in Java Write a recursive method that takes a string and return a string where every character appears twice. For example, if the string is “HELLO”, it will return “HHEELLOO”. Write a program to test it.
Write a method called countChar which accepts a string parameter and a character parameter and returns...
Write a method called countChar which accepts a string parameter and a character parameter and returns an integer, that is: int countChar (String s, char c) This method should count the number of occurrences of the character c within the string s, and return the count to the caller. Also write a main method that tests countChar. All of the print statements and user interaction belong in the main method, not in countChar.
Write a recursive method public static int sumEveryOther(int n) that takes a positive int as an...
Write a recursive method public static int sumEveryOther(int n) that takes a positive int as an argument and returns the sum of every other int from n down to 1. For example, the call sumEveryOther(10) should return 30, since 10 + 8 + 6 + 4 + 2 = 30. The call sumEveryOther(9) should return 25 since 9 + 7 + 5 + 3 + 1 = 25. Your method must use recursion.
#include <iostream> #include <string> using namespace std; int pal(int n, int temp) {    // Using...
#include <iostream> #include <string> using namespace std; int pal(int n, int temp) {    // Using division of 10 trick    if(n == 0)        return temp;    // Build the number backs.    temp = (temp * 10) + (n % 10);    // Then advance through the number.    return pal(n / 10, temp); } int main() {    system("clear");    int num;        // Ask the user for an input.        cout << "\nEnter...
C++ Write a recursive function that reverses the given input string. No loops allowed, only use...
C++ Write a recursive function that reverses the given input string. No loops allowed, only use recursive functions. Do not add more or change the parameters to the original function. Do not change the main program. I had asked this before but the solution I was given did not work. #include #include using namespace std; void reverse(string &str) { /*Code needed*/ } int main() {    string name = "sammy";    reverse(name);    cout << name << endl; //should display...
Java Write a recursive boolean method named isMember. The method should accept two arguments: an int...
Java Write a recursive boolean method named isMember. The method should accept two arguments: an int array and an int value. The method should return true if the value is found in the array, or false if the value is not found in the array. Demonstrate the method in a program that takes input from the user. First, the program should prompt the user to enter the length of the array. Then, it should prompt the user for every member...
Write a recursive method that takes as input a string s and returns a list containing...
Write a recursive method that takes as input a string s and returns a list containing all the anagrams of the string s. An anagram is a word formed by rearranging the letters of a different word. For example, the word ‘binary’ is an anagram of ‘brainy’. Note that the output list should not contain duplicates. Java
Write a recursive method “int sumPos(int a[], int n)” to calculate the sum of positive integers...
Write a recursive method “int sumPos(int a[], int n)” to calculate the sum of positive integers in an array of n integers, a[]. No global variables are allowed. No other method is allowed to be called by sumPos() except itself. (Hint: The integers in an array a[] of n integers are stored in a[0], a[1], ... a[n-1].)
Write a function that accepts an int array arr, the array’s size sz and integer n,...
Write a function that accepts an int array arr, the array’s size sz and integer n, the function should create a new array that it’s n times the array arr. The fucntion copy the content of arr to the new array n times. The Example arr = {5, 2, 1} n=3 Newarr = {5, 2, 1, 5, 2, 1, 5, 2, 1} (c++)