Question: JAVA: Answer the following questions for the following codes 1. Identify recursive methods. 2. Explain what each of those methods does. 3. How will you

JAVA:

Answer the following questions for the following codes

1. Identify recursive methods.

2. Explain what each of those methods does.

3. How will you solve the problem without using recursive methods.

CODE 1

//Define the class public class Main {

//Define the recursive method sum. public static int sum(int num) {

//If the number is 0 then return 0. if (num == 0)

return 0;

//Recursively call the method to //evaluate the sum else

//Return the sum. return num+sum(num-1); }

//Define the main method. public static void main(String[] args) {

//Call the function and display the result. System.out.println(sum(30)); } }

CODE 2

import java.util.Scanner;

public class CharacterCounter {

public static int countCharacter(char[] array, int start, char ch) { if (start < array.length) { if (array[start] == ch) { return 1 + countCharacter(array, start + 1, ch); } else { return countCharacter(array, start + 1, ch); } } return 0; }

public static void main(String[] args) { Scanner in = new Scanner(System.in); char[] test = {'T', 'h', 'i', 's', ' ', 'i', 's', ' ', 't', 'h', 'e', ' ', 's', 't', 'r', 'i', 'n', 'g'}; System.out.print("Enter a character: "); char ch = in.next().charAt(0); System.out.println("Character Count of " + ch + " is " + countCharacter(test, 0, ch)); } }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!