Question

In: Computer Science

PLESE CODE IN C# not java RECURSION Objectives • Learn the basics of recursion – Part...

PLESE CODE IN C# not java

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:

Write a driver program that calls this method from the Main program.

• •

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:

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")

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.

  

Page 1 of 2

//Call to recursive function isReverse ("Computer","retupmCo") false

//Call to recursive function isReverse ("Hello","olleh") true

Solutions

Expert Solution

Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. If not, PLEASE let me know before you rate, I’ll help you fix whatever issues. Thanks

/*Program 1*/

//Lab10a.cs

using System;

class Lab10a{
        //method to return a string containing s repeated n times
        public static String repeatNTimes(String s, int n){
                //if n is 0 or below, returning empty string
                if(n<=0){
                        return "";
                }else{
                        //else returning s concatenated with the value returned from 
                        //recursive call passing n-1 as new n
                        return s+repeatNTimes(s,n-1);
                }
        }
        //Main method for testing
        public static void Main(String[] args){
                Console.WriteLine(repeatNTimes("hello", 5));
                Console.WriteLine(repeatNTimes("Good morning!", 3));
                
                //wait for a key press to quit. remove if not needed.
                Console.ReadKey();
        }
}

/*OUTPUT */

hellohellohellohellohello
Good morning!Good morning!Good morning!

/*Program 2*/

//Lab10b.cs

using System;

class Lab10a{
        //method to check if s1 is same as s2, but in reverse
        public static bool isReverse(String s1, String s2){
                //if lengths of s1 an s2 mismatch, returning false
                if(s1.Length!=s2.Length){
                        return false;
                }
                //if strings are empty, returning true
                else if(s1.Length==0){
                        return true;
                }
                //comparing first char of s1 with last char of s2, after converting them to 
                //lower case
                if(Char.ToLower(s1[0])!=Char.ToLower(s2[s2.Length-1])){
                        //mismatch s1 is not reverse of s2
                        return false;
                }
                //otherwise removing first char from s1, last char from s2, calling method 
                //recursively passing new strings
                //note: s1.Substring(1) will return a s1 without first character
                // s2.Substring(0,s2.Length-1) will return s2 without last character
                return isReverse(s1.Substring(1),s2.Substring(0,s2.Length-1));
        }
        
        //Main method for testing
        public static void Main(String[] args){
                Console.WriteLine(isReverse ("Computer","retupmCo"));
                Console.WriteLine( isReverse ("Hello","olleh") );
                
                //wait for a key press to quit. remove if not needed.
                Console.ReadKey();
        }
}

/*OUTPUT*/

False
True

Related Solutions

IN C# WITH SCREENSHOTS OF THE CODE RECURSION Objectives • Learn the basics of recursion. Background...
IN C# WITH SCREENSHOTS OF THE CODE RECURSION Objectives • Learn the basics of recursion. Background There are many problems that loops simplify, such as displaying every pixel to a screen or receiving repetitive input. However, some situations that can be simplified with looping are not easily solvable using loops. This includes problems that require back tracking and being able to use information from previous iterations, which would normally be lost when using an iterative loop. In those cases, it...
JAVA CODE Learning objectives; File I/O practice, exceptions, binary search, recursion. Design and implement a recursive...
JAVA CODE Learning objectives; File I/O practice, exceptions, binary search, recursion. Design and implement a recursive version of a binary search.  Instead of using a loop to repeatedly check for the target value, use calls to a recursive method to check one value at a time.  If the value is not the target, refine the search space and call the method again.  The name to search for is entered by the user, as is the indexes that define the range of viable candidates...
I need this code translated from C++ to Java. Im personally still trying to learn Java,...
I need this code translated from C++ to Java. Im personally still trying to learn Java, so if you can include screenshots of your IDE/output that would be helpful. Much appreciated! #include <iostream> #include <string> using namespace std; class pizza { public:    string ingrediants, address;    pizza *next;    pizza(string ingrediants, string address)    {        this->address = address;        this->ingrediants = ingrediants;        next = NULL;    } }; void enqueue(pizza **head, pizza **tail, pizza...
write code in java and comment. thanks. the program is about interface . Implement the basics...
write code in java and comment. thanks. the program is about interface . Implement the basics of Fitness and types of Fitness: Aerobic. Implement specific Fitness types such as Swimming, Cycling, Fitness Task: public interface Fitness (10pts) This will be used as a starting point for deriving any specific Fitness type. Every fitness exercise type has one or more muscle group it affects. Therefor a Fitness has the following abstarct method (Note that all methods in an interface are abstract...
In this programming assignment, you will write C code that performs recursion. For the purpose of...
In this programming assignment, you will write C code that performs recursion. For the purpose of this assignment, you will keep all functions in a single source file main.c. Your main job is to write a recursive function that generates and prints all possible password combinations using characters in an array. In your main() function you will first parse the command line arguments. You can assume that the arguments will always be provided in the correct format. Remember that the...
Objectives To learn to code, compile, and run a program using file input and an output...
Objectives To learn to code, compile, and run a program using file input and an output file. Assignment Plan and code a program utilizing one file for input and one file for output to solve the following problem: Write a program to determine the highest number, the lowest number, their total, and the average of each line of numbers in a file. A file contains 7 numbers per line. How many lines a file contains is unknown. Note Label all...
Code using JAVA: must include a "Main Method" to run on "intelliJ". Hint: Use recursion "...
Code using JAVA: must include a "Main Method" to run on "intelliJ". Hint: Use recursion " /** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode() {} * TreeNode(int val) { this.val = val; } * TreeNode(int val, TreeNode left, TreeNode right) { * this.val = val; * this.left = left; * this.right = right; * } * } */ class Solution { public boolean isSymmetric(TreeNode...
Please take this c++ code and make it into java code. /* RecursionPuzzleSolver * ------------ *...
Please take this c++ code and make it into java code. /* RecursionPuzzleSolver * ------------ * This program takes a puzzle board and returns true if the * board is solvable and false otherwise * * Example: board 3 6 4 1 3 4 2 5 3 0 * The goal is to reach the 0. You can move the number of spaces of your * current position in either the positive / negative direction * Solution for this game...
Objectives: Learn to analyze a computational problem and construct an algorithm to solve it Learn to...
Objectives: Learn to analyze a computational problem and construct an algorithm to solve it Learn to use sequential statements to build a basic program to implement the algorithm, using correct programming format Learn to use the scanf function to read and store values entered on the keyboard by the user, and the printf function used to display output results on the computer monitor Learn to convert math formulas into correct arithmetic expressions using variables, constants, and library math functions Learn...
Create a new Java project called 1410_Recursion. Add a package recursion and a class Recursion. Include...
Create a new Java project called 1410_Recursion. Add a package recursion and a class Recursion. Include the following three static methods described below. However, don't implement them right away. Instead, start by returning the default value followed by a // TODO comment. public static int sumOfDigits(int n) This method returns the sum of all the digits. sumOfDigits(-34) -> 7 sumOfDigits(1038) -> 12 public static int countSmiles(char[] letters, int index) This method counts the number of colons followed by a closing...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT