3
टास्क
Java Syntax, पातळी 9, धडा 2
Returning a stack trace
The way a stack works is unfair and harsh. But sometimes it's so useful! Remember that the last function called is the first function to end. Let's practice and see what happens: we need to write five methods that call each other. Each method must return its stack trace.
6
टास्क
Java Syntax, पातळी 9, धडा 2
Stack trace revisited
Let's get right down to business: write 5 methods that call each other. Each method should return the name of the method that called it. Use the stack trace to obtain this information. This name seems so beyond our reach. But we'll figure it out. These are just methods that sequentially call each other, nothing more.
3
टास्क
Java Syntax, पातळी 9, धडा 2
Who called me?
The call stack may not be the most beautiful construct... But why not? It makes sense—you just need to get used to it. Let's get used to tasks! This time, you need to write five methods that call each other. Each method should return the line number of the code from which it was called. To do this, use the element.getLineNumber() method.
3
टास्क
Java Syntax, पातळी 9, धडा 2
Stack trace with 10 calls
I got lucky: My favorite elementary school, Mrs. Turing, always wrote this phrase on the blackboard: "The last function called is the first function to end". She made us memorize it and swore that one day we would understand. Thank you, Mrs. Turing. It seems the promised day has come. I can even write code to get a stack trace that is 10 calls deep. Can you?
3
टास्क
Java Syntax, पातळी 9, धडा 2
In the blue depths of the stack trace…
Remember the golden rule: function A calls function B, which calls function C. And to exit A, you must first exit B, and to do that you must exit C. Got it? Good. And now we'll write a method that returns the stack trace depth, i.e. the number of methods in the stack trace. And one more thing: display this number on the screen.
3
टास्क
Java Syntax, पातळी 9, धडा 2
Logging stack traces
If you're approaching something more or less seriously, you will eventually come to some form of logging. Don't hesitate. Let's implement the log method. The log method should display the name of the class and method in which it is called, as well as the passed message. For clarity, use a colon and space to separate the class name, method name, and message.