Question

17. For the following program, draw a stack to find the output. public class RecApp public static void main(String[] args) Sy
0 0
Add a comment Improve this question Transcribed image text
Answer #1

So this recursion function will go till n becomes -1 after that it will return 5

so the sum is 21

how?

Check this solution

Public_Static void main(String II wiges System.out.println (Sum(a)); I -Public static int Sum (int n) { if (n. 1 retwin 5 dis

Sum would be 7+ Sum (5) = 14 +7= 21 54 Sum (3) = 9+5=14 34 Sum (1) = 3+6 = 9 It sum(-1) 1+5=6 returns 5 total Som is 21 CamSc

if any doubt, comment below

Add a comment
Know the answer?
Add Answer to:
17. For the following program, draw a stack to find the output. public class RecApp public...
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. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • 1) Consider the following Java program: 1 public class HelloWorld { 2     // My first program!...

    1) Consider the following Java program: 1 public class HelloWorld { 2     // My first program! 3     public static void main(String[] args) { 4         System.out.println("Hello, World!"); 5     } 6 } What is on line 1? a. a variable declaration b. a statement c. a method (subroutine) definition d. a comment e. a class definition 2) Which one of the following does NOT describe an array? a. It can be used in a for-each loop. b. It has a numbered sequence...

  • E 4 (4 pts.Determine the output of the program helow. impon util. public class ForEachFun output private int public...

    E 4 (4 pts.Determine the output of the program helow. impon util. public class ForEachFun output private int public ForEach unir 5 % 2:1 public v number1 ng toStringO( return mum:) public static void main(String I 1 args) un 1 m-new ForlachFuni4); iE i 3 mil w ForachPun-5) else mf i1 new ForEachFun(8 + i): for (ForEachFun n:m) .number) for (ForEachFun n: m) System.out.printin(n): E 4 (4 pts.Determine the output of the program helow. impon util. public class ForEachFun output...

  • Help with a question in Java: What is the output from the following program? public class...

    Help with a question in Java: What is the output from the following program? public class Methods2 { public static void main(String[] args) { for (int i = 0; i < 3; i++) { for (int j = 0; j <= i; j++){ System.out.print(fun(i, j) + "\t"); } System.out.println(); } } static long fun(int n, int k) { long p = 1; while (k > 0){ p *= n; } return p; } }

  • What output is produced by the following program? public class MysteryNums public static void main(String[] args)...

    What output is produced by the following program? public class MysteryNums public static void main(String[] args) - int x = 12; int y = x - 3; 3, sentence (y, x + y); . public static void sentence (int numi, int num2) { 4: System.out.println(num1 + " + num2);

  • Java class quiz need help~ This is the Backwards.java code. public class Backwards { /** *...

    Java class quiz need help~ This is the Backwards.java code. public class Backwards { /** * Program starts with this method. * * @param args A String to be printed backwards */ public static void main(String[] args) { if (args.length == 0) { System.out.println("ERROR: Enter a String on commandline."); } else { String word = args[0]; String backwards = iterativeBack(word); // A (return address) System.out.println("Iterative solution: " + backwards); backwards = recursiveBack(word); // B (return address) System.out.println("\n\nRecursive solution: " +...

  • JAVA language Add a recursive method to the program shown in the previous section that states...

    JAVA language Add a recursive method to the program shown in the previous section that states how many times a particular value appears on the stack. Code: class Stack { protected Node top; Stack() { top = null; } boolean isEmpty() { return( top == null); } void push(int v) { Node tempPointer; tempPointer = new Node(v); tempPointer.nextNode = top; top = tempPointer; } int pop() { int tempValue; tempValue = top.value; top = top.nextNode; return tempValue; } void printStack()...

  • Complete the following Java program by writing the missing methods. public class 02 { public static...

    Complete the following Java program by writing the missing methods. public class 02 { public static void main(String[] args) { Scanner scan = new Scanner(System.in); System.out.println("Enter a number: int n = scan.nextInt(); if (isOdd (n)) { //Print n to 1 System.out.println("Print all numbers from "+n+" to 1"); printNumToOne (n); } else { System.out.println(n + " is //End of main()

  • How many lines of output are provided by this program? public class PrintLines { public static...

    How many lines of output are provided by this program? public class PrintLines { public static void main(String[ ] args) { System.out.println(“Two…”); System.out.println(“One…”); System.out.println(“Zero…”); System.out.println(“Liftoff!…”); System.out.println(“Houston, we have a problem…”); } } 2 4 3 5

  • What is the output for the following program codes? a) [ 0.5 Mark ] class A...

    What is the output for the following program codes? a) [ 0.5 Mark ] class A { int i; } class B extends A { int j; void display() { super.i = j + 1; System.out.println(j + " " + i); }} class inheritance { public static void main(String args[]) { B obj = new B(); obj.i=1; obj.j=2; obj.display(); }} OUTPUT: b) [ 0.5 Mark ] class Parent { public void getBike(){ System.out.println("Suzuki Bike"); }} class Child extends Parent {...

  • Get the file “HW4Part4a.java” from the repository. Modify the program class to match the new file...

    Get the file “HW4Part4a.java” from the repository. Modify the program class to match the new file name. Complete it by writing a recursive static int function named “pow2” with one parameter, an integer n. The function is defined as follows: if n ≤ 0 then pow2(n) = 1. Otherwise, pow2(n) = 2 · pow2(n − 1). public class HW4Part4a { public static void main(String[] args) { for (int i = 0; i <= 20; i++) { System.out.println("pow2("+i+") = "+pow2(i)); }...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT