Question

*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 calls this method from the Main program.
2. Write a recursive method called isReverse(String s1, String s2) that accepts two
strings as parameters and returns true if the two strings contain the same sequence of characters as
each other but in the opposite order and false otherwise.
• The recursive function should ignore capitalization. (For example, the call of
isReverse("hello", "eLLoH") would return true.)
• The empty string, as well as any one letter string, should be its own reverse.
Write a driver program that calls this method from the Main program.
Note: Your program name should match the name of your java / C# class exactly.
e.g Lab10a.java / Lab10a.cs
Sample Output:
LAB 1:
//A call to recursive function repeatNTimes(“hello”, 5)
hellohellohellohellohello
// A call to recursive function repeatNTimes(“Good morning!”, 3)
Good morning!Good morning!Good morning!
LAB 2:
Page 2 of 2
//Call to recursive function isReverse ("Computer","retupmCo")
false
//Call to recursive function isReverse ("Hello","olleh")
true
Grading:
● 100 %: Attempted lab and submitted fully functioning lab exercises with complete
headers and clear I/O statements and
● 95 %: Attempted lab and submitted fully functioning lab exercises but incomplete
headers and/or unclear I/O statements before due date.
● 90%: All but one item are correct
● 80%: At least two more items are correct
● 70%: Program compiles and methods are correct
● 0%: Did not attempt lab or did not submit it before the due date

Homework Answers

Answer #1

1. Java code for repeating the string n times.

import java.util.Scanner;
class StringRepeat{
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.print("Enter a string:");
String str = in.nextLine();
System.out.print("Enter how many times to be repeated:");
int n = in.nextInt();
//Recursive function calling
System.out.println(repeatNTimes (str,n));
}
//Recursive function definition
public static String repeatNTimes(String s, int n){
if(n==0){
return "";
}
return s+repeat(s,n-1);
}
}

Output Screen

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
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.
THIS IS A JAVA PROGRAM THAT NEEDS TO BE WRITTEN Write a recursive method void reverse(ArrayList<Object>...
THIS IS A JAVA PROGRAM THAT NEEDS TO BE WRITTEN Write a recursive method void reverse(ArrayList<Object> obj) that reverses an ArrayList of any type of object. For example, if an ArrayList held 4 strings: "hi", "hello", "howdy", and "greetings" the order would become "greetings", "howdy", "hello", and "hi". Implement a recursive solution by removing the first object, reversing the ArrayList consisting of the remaining Objects, and combining the two. Use the following class ArrayListReverser to write and test your program....
Please do it in Python Write the simplest program that will demonstrate iteration vs recursion using...
Please do it in Python Write the simplest program that will demonstrate iteration vs recursion using the following guidelines - Write two primary helper functions - one iterative (IsArrayPrimeIter) and one recursive (IsArrayPrimeRecur) - each of which Take the array and its size as input params and return a bool. Print out a message "Entering <function_name>" as the first statement of each function. Perform the code to test whether every element of the array is a Prime number. Print out...
You need to write a permute class that will take first and second strings to rearrange...
You need to write a permute class that will take first and second strings to rearrange letters in first, followed by second. For example, if the first is “CAT” string and second is “MAN” string, then the program would print the strings TACMAN, ATCMAN, CTAMAN, TCAMAN, ACTMAN, and CATMAN. The first and second strings can be any length of string or a null. The permute class uses a Node class as link list node to link all letters arrangement. The...
Please follow ALL the instructions and solve it by C++. Please and thank you! There are...
Please follow ALL the instructions and solve it by C++. Please and thank you! There are two ways to write loops: (1) iterative, like the for-loops we're used to using, and (2) recursive. Your prerequisite preparation for this course should have exposed you to both, although your working knowledge of recursive loops may not be as strong as that of iterative loops. Consider the following iterative function that prints an array of characters backward: #include <iostream> #include <cstring> // print...
Write the Game class, Java lanuage. A Game instance is described by three instance variables: gameName...
Write the Game class, Java lanuage. A Game instance is described by three instance variables: gameName (a String), numSold (an integer that represents the number of that type of game sold), and priceEach (a double that is the price of each of that type of Game). I only want three instance variables!! The class should have the following methods: A constructor that has two parameter – a String containing the name of the Game and a double containing its price....
Using Java language.... Polymorphism of Interfaces : Take the Account class from the last chapter and...
Using Java language.... Polymorphism of Interfaces : Take the Account class from the last chapter and make a java program for your bank. However, there is another type of customer that may not have money in the bank and may not in fact have a back account with this bank at all, but may have a loan here. Make another class in the application you are making called CarLoan. Carloan's should have a name, amount owed, a rate, and a...
[Java] I'm not sure how to implement the code. Please check my code at the bottom....
[Java] I'm not sure how to implement the code. Please check my code at the bottom. In this problem you will write several static methods to work with arrays and ArrayLists. Remember that a static method does not work on the instance variables of the class. All the data needed is provided in the parameters. Call the class Util. Notice how the methods are invoked in UtilTester. public static int min(int[] array) gets the minimum value in the array public...
PLEASE USING C# TO SOLVE THIS PROBLEM. THANK YOU SO MUCH! a. Create a project with...
PLEASE USING C# TO SOLVE THIS PROBLEM. THANK YOU SO MUCH! a. Create a project with a Program class and write the following two methods (headers provided) as described below: - A Method, public static int InputValue(int min, int max), to input an integer number that is between (inclusive) the range of a lower bound and an upper bound. The method should accept the lower bound and the upper bound as two parameters and allow users to re-enter the number...
I need this before the end of the day please :) In Java 10.13 Lab 10...
I need this before the end of the day please :) In Java 10.13 Lab 10 Lab 10 This program reads times of runners in a race from a file and puts them into an array. It then displays how many people ran the race, it lists all of the times, and if finds the average time and the fastest time. In BlueJ create a project called Lab10 Create a class called Main Delete what is in the class you...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT