( please the answer have to me in JAVA only and please provide explanation to ea
ID: 3735513 • Letter: #
Question
( please the answer have to me in JAVA only and please provide explanation to each method) Solve the following problems in Java. You may write all of the methods in the same class. All methods must be recursive. You may not use any loops. You must moo like a cow if you get the StackOverflowError. You must cluck like a chicken if you get the ArrayIndexOutOfBoundsException. Problem 2: Write a recursive method that computes the sum of a given array of integers. public static int sum(int[] a) f return sum(a, 0): private static int sum(int[] a, int i) [ // WRITE ME! Problem 1: Write a recursive method that returns the index of a given element in a given array (do not assume the elements are in order) Problem 3: Write a recursive method that finds the largest integer in a given array of integers. Problem 4: Write a recursive method that prints a given string backwards. Remember that you can use the charAt method to use a string as a character array. The only other String method you may use is length.Explanation / Answer
import java.util.Scanner;
public class Tester {
public static int sum(int[] a)
{
return sum(a, 0);
}
private static int sum(int[] a, int i)
{
if(a[i] != '')
{
return a[i] + sum(a, i+1);
}
else
{
return 0;
}
}
//search the index of givin number
public static int searchIndex(int[] a, int numberToSearch, int index)
{
if(a[index] != '')
{
if(a[index] == numberToSearch)
return index;
else
{
index++;
return searchIndex(a, numberToSearch, index);
}
}
else
{
return -1;
}
}
//find the largest Integer in the array
public static int largestInteger(int[] a, int currentIndex,int number)
{
if(a[currentIndex] != '')
{
int graterNum = a[currentIndex] > number ? a[currentIndex] : number;
currentIndex++;
return largestInteger(a, currentIndex, graterNum);
}
return number;
}
//print the string in reverse order
public static void printString(String givinString, int lastIndex)
{
if(lastIndex >= 0)
{
System.out.print(givinString.charAt(lastIndex));
lastIndex--;
printString(givinString, lastIndex);
}
else
{
return;
}
}
public static void main(String args[])
{
// Problem 2
int[] a = new int[100];
System.out.println("Enter the values for array(-1 to exit)");
Scanner sc = new Scanner(System.in);
int counter = 0;
while(true)
{
int i = sc.nextInt();
if(i == -1)
{
break;
}
else {
a[counter] = i;
}
counter++;
}
int resultOfSum = sum(a);
System.out.println("The result of sum = " + resultOfSum);
//Problem 1
System.out.println("Enter the number to find the index in given Array");
int indexSearch = sc.nextInt();
int foundIndex = searchIndex(a, indexSearch, 0);
System.out.println("Found index = " + foundIndex);
//Problem 3
int largestInteger = largestInteger(a, 0, a[0]);
System.out.println("Largest integar in the Array = " + largestInteger);
//Problem 4
System.out.println("Enter the string to reverse it");
String givinString = sc.next();
printString(givinString, givinString.length()-1);
}
}
//PLEASE PROVIDE FEEDBACH THUMBS UP
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.