Related questions
Rewrite the private static int recurseMax method without using Java API
public class Recursion{
public static void main(String[] args){
// My tests (DO NOT MODIFY!)
int[] a = {3,2,6,4,15,7,9,8,6};
p(recurseSum(a)); // Sum should be 60
p(recurseMax(a)); // Max should be 15
// End of tests!
}
// Recursive methods below here...
public static int recurseSum(int[] a){
// TODO: This is the wrapper method. You must complete this AND implement helper method
int total = recurseSum(a, a.length);
return total;
}
private static int recurseSum(int[] a, int i){
if (i <= 0)
return 0;
return (recurseSum(a, i - 1) + a[i - 1]);
}
public static int recurseMax(int[] a){
int max = recurseMax(a, a.length);
return max;
}
private static int recurseMax(int[] a, int i){
if(i == 1)
return a[0];
return Math.max(a[i-1], recurseMax(a, i-1));
}
// End recursive methods
/* Method to make printing text easy */
public static <E> void p(E item){
System.out.println(item);
}
}
Trending nowThis is a popular solution!
Step by stepSolved in 2 steps with 1 images
- Complete the following program Multiply.java. This program uses recursion to multiply two numbers through repeated addition. The output should look exactly like what is pictured below. The code should be completed where it states "complete here"arrow_forwardWrite a recursive method called reverseString() that takes in a string as a parameter and returns the string in reversed order. The main method is provided to read a string from the user and call the reverseString() method. Ex: If the input of the program is: Hello the reverseString() method returns and the program outputs: Reverse of "Hello" is "olleH". Ex: If the input of the program is: Hello world! the reverseString() method returns and the program outputs: Reverse of "Hello, world!" is "!dlrow, olleH". Hint: Move the first character to the end of the returning string and pass the remaining sub-string to the next reverseString() method call.arrow_forwardimport java.util.Scanner; public class AverageWithSentinel{ public static final int END_OF_INPUT = -500; public static void main(String[] args) { Scanner in = new Scanner(System.in); // Step 2: Declare an int variable with an initial value // as the count of input integers // Step 3: Declare a double variable with an initial value // as the total of all input integers // Step 4: Display an input prompt // "Enter an integer, -500 to stop: " // Step 5: Read an integer and store it in an int variable // Step 6: Use a while loop to update count and total as long as // the input value is not -500. // Then display the same prompt and read the next integer // Step 7: If count is zero // Display the following message // "No integers were...arrow_forward
- Scala programmingarrow_forward1 public static int sum(int x, int y){ 2 int z = x +y; 3 return z; 4 } 5 public static void main(String[] args){ 6 ... 7 sum(a+2, b); 8 ... 9 } write the signature of the method sum: sum(int,int) *Method name/parameter list list local variables of sum: x, y, and y are local variables of the function. list the parameters of sum: int x, int y write the line number where a call to sum occurs 7 list the arguments for the above call list the return type of sum here ______________arrow_forwardGetting an error while running my code. import java.util.Scanner;import java.math.BigInteger; public class Main{Bigintege result;public static void computeFact(int n){BigInteger result = BigInteger.ONE;while (n>0){result = result.multiply(BigInteger.valueOf(n));n = n-1;}}public static void computeFact2(int n){BigInteger a = 4n;BigInteger fact = 1;while(n>0) {fact = fact.multiply(BigInteger.valueOf(4n));a = a - 1;}}BigInteger b=396, c, d;public static void compute(int n) {BigInteger sum = 0, final_result;public static void computeSummation() { while(n > 0) { sum = sum + (a/result) * (c); n = n-1; } final_result = d * sum;System.out.println("Final result is "+final_result);} public static void main(String[] args) { Scanner kybd = new Scanner(System.in); while ( true) { System.out.print("\n\tEnter an Integer Number e.g. 5 "); int num = kybd.nextInt(); computeFact(num); computeFact2(num); compute(num); computeSummation(num);} }}arrow_forward
- *JAVA* complete method Delete the largest valueremoveMax(); Delete the smallest valueremoveMin(); class BinarHeap<T> { int root; static int[] arr; static int size; public BinarHeap() { arr = new int[50]; size = 0; } public void insert(int val) { arr[++size] = val; bubbleUP(size); } public void bubbleUP(int i) { int parent = (i) / 2; while (i > 1 && arr[parent] > arr[i]) { int temp = arr[parent]; arr[parent] = arr[i]; arr[i] = temp; i = parent; } } public int retMin() { return arr[1]; } public void removeMin() { } public void removeMax() { } public void print() { for (int i = 0; i <= size; i++) { System.out.print( arr[i] + " "); } }} public class BinarH { public static void main(String[] args) { BinarHeap Heap1 = new BinarHeap();...arrow_forwardpublic class Main { static int findPosSum(int A[], int N) { if (N <= 0) return 0; { if(A[N-1]>0) return (findPosSum(A, N - 1) + A[N - 1]); else return findPosSum(A, N - 1); } } public static void main(String[] args) { int demo[] = { 11, -22, 33, -4, 25,12 }; System.out.println(findPosSum(demo, demo.length)); } } Consider the recursive function you wrote in the previous problem. Suppose the initial call to the function has an array of N elements, how many recursive calls will be made? How many statements are executed in each call? What is the total number of statements executed for all recursive calls? What is the big O for this function?arrow_forwardGiven string inputStr on one line and integers idx1 and idx2 on a second line, output "Match found" if the character at index idx1 of inputStr is equal to the character at index idx2. Otherwise, output "Match not found". End with a newline. Ex: If the input is: eerie 4 1 then the output is: Match found Note: Assume the length of string inputStr is greater than or equal to both idx1 and idx2.arrow_forward
- Consider the recursive method myPrint: public void myPrint (int n) if (n < 10) System.out.print(n); else { int m = n % 10; %3D System.out.print (m) ; myPrint (n / 10); What is printed for the call myPrint (10)?arrow_forwardpublic class ArraySection { static void arraySection(int a[], int b[]) { int k = 0; int [] c = new int[a.length]; for(int i = 0; i < a.length; i++) { for(int j = 0; j < b.length; j++) if(a[i] == b[j]) c[k++] = a[i]; } for(int i = 0; i < k; i++) System.out.println(c[i]); System.out.println(); } public static void main(String[] args) { int a[] = { 1, 2, 3, 4, 5 }; int b[] = { 0, 2, 4,5 }; arraySection(a,b); }} Calculate the algorithm step number and algorithm time complexity of the above program?arrow_forwardPROGRAMMING LANGUAGE: Javaarrow_forward
- Text book imageDatabase System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationText book imageStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONText book imageDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- Text book imageC How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONText book imageDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningText book imageProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education