# files needeed endlessexample java import java io from stackoverflow web site with m 5188075

*Files Needeed*

EndlessExample.java

import java.io.*;

// From stackoverflow web site with modifications

public class EndlessExample {

static int counter = 0;

public static void endless() throws StackOverflowError

{

counter++;

endless();

}

public static void main(String args[]) {

try {

endless();

} catch(StackOverflowError t) {

// more general: catch(Error t)

// anything: catch(Throwable t)

System.out.println(“Caught “+t);

// t.printStackTrace();

}

System.out.println(“After the error…counter = ” + counter);

}

}

ProgChal_1_Partial

import java.io.*;

import java.util.Scanner;

public class ProgChal_1_Partial

{

public static void main(String[] args)

{

long val1, val2;

Scanner keyboard = new Scanner(System.in);

System.out.print(“Enter an integer value: “);

val1 = keyboard.nextLong();

System.out.println(“val1 = ” + val1);

System.out.print(“Enter another integer value: “);

val2 = keyboard.nextLong();

System.out.println(“val2 = ” + val2);

System.out.println(val1 + ” times ” + val2 + ” = “

}

public static long multiplyByAdding(long x, long y) throws StackOverflowError

{

if (y == 1)

return x;

else

}

}

The Question

Part-C

Here we consider Programming Challenge: 1. Recursive Multiplication. The solution is quite simple and it is useful for demonstrating what can unexpectedly go wrong with recursive solutions.

A partial solution is posted on our Omnivox dropbox as ProgChal_1.java.

This solution needs completion of the multiplyByAdding() method. Set this up as a Java program and complete the missing method.

Run your program. First enter the values: 123 and 321 when prompted for input. You should get the correct result as 39483.

Run it again and try the values: 12345 and 54321. You should now get the error: “java.lang.StackOverflowError

followed by repeated line number location (java.28 in our example) and no answer. This means that your program has run out of the “stack memory”. This memory was originally assigned when the program was loaded to run. This illustrates that Recursive solutions can unexpectedly run out of memory. Regular non‑recursive methods may require more complex programming and may take more time but, usually, do not simply run out of memory.

The part under is where I do not know what to do.

The program: EndlessExample.java is provided in the Dropbox entry for Asg-5. Set it up to run. When this program causes a StackOverflowError the catch block receives the error and displays a message about catching the error. It also displays a counter of how many times the “endless” method was called before the error occurred. A sample of this output window is shown below.

Repeat running EndlessExample a few more times to compare the values reported from the counter

The input was 12345 and 54321

*The output of the EndlessExample is*

“This program sets up an endless call

to itself and counts each time a call is made

Caugth java.lang.StackOverflowError

After the error, counter has a value :23659″