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....
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....
[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...
JAVA please Arrays are a very powerful data structure with which you must become very familiar....
JAVA please Arrays are a very powerful data structure with which you must become very familiar. Arrays hold more than one object. The objects must be of the same type. If the array is an integer array then all the objects in the array must be integers. The object in the array is associated with an integer index which can be used to locate the object. The first object of the array has index 0. There are many problems where...
IN JAVA!! You may be working with a programming language that has arrays, but not nodes....
IN JAVA!! You may be working with a programming language that has arrays, but not nodes. In this case you will need to save your BST in a two dimensional array. In this lab you will write a program to create a BST modelled as a two-dimensional array. The output from your program will be a two-dimensional array.   THEN: practice creating another array-based BST using integers of your choice. Once you have figured out your algorithm you will be able...
My assignment: Triplet Template Class Directions: Define a template class for a generic triplet. The private...
My assignment: Triplet Template Class Directions: Define a template class for a generic triplet. The private data member for the triplet is a generic array with three elements. The triplet ADT has the following functions:  default constructor  explicit constructor: initialize the data member using parameters  three accessors (three get functions) which will return the value of each individual element of the array data member  one mutator (set function) which will assign values to the data member...
**please write code with function definition taking in input and use given variable names** for e.g....
**please write code with function definition taking in input and use given variable names** for e.g. List matchNames(List inputNames, List secRecords) Java or Python Please Note:    * The function is expected to return a STRING_ARRAY.      * The function accepts following parameters:      *  1. STRING_ARRAY inputNames      *  2. STRING_ARRAY secRecords      */ Problem Statement Introduction Imagine you are helping the Security Exchange Commission (SEC) respond to anonymous tips. One of the biggest problems the team faces is handling the transcription of the companies reported...
You will write a program that loops until the user selects 0 to exit. In the...
You will write a program that loops until the user selects 0 to exit. In the loop the user interactively selects a menu choice to compress or decompress a file. There are three menu options: Option 0: allows the user to exit the program. Option 1: allows the user to compress the specified input file and store the result in an output file. Option 2: allows the user to decompress the specified input file and store the result in an...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT