Intro to Java Programming Can you explain or comment on what\'s happening in the
ID: 3771543 • Letter: I
Question
Intro to Java Programming
Can you explain or comment on what's happening in the code below step-by-step? And the changes that occur in the stack and heap?
public class Trace {
public static void main(String[] args) {
printHelloWorld(3);
int sum = addNumbersTill(5);
printNumbersNSum(5, sum);
}
public static void printHelloWorld(int cnt) {
for(int i = 0; i < cnt; i++) {
System.out.println("Hello World");
}
}
public static int addNumbersTill(int maxNum) {
int sum = 0;
for(int num = 1; num <= maxNum; num++) {
sum = sum + num;
}
return sum;
}
public static void printNumbersNSum(int maxNum, intsum) {
for(int num = 1; num <= maxNum; num++) {
System.out.print(num + " ");
}
System.out.println(" Sum: " + sum);
}
}
Explanation / Answer
Ok. Let’s understand your program step-by-step.
This is the name of the class which contains the main function, and as per the java convention, name of the file you are going to put this code in, must be the same as the class name, in this case which is Trace.
Now, it’s the main function definition.
Next is the function call to a function named printHelloWorld, now look at the calling signature, it passes a value 3 as an argument, and does not ask for any value in return. So, the corresponding function must be having a void return type, and accepts an integer as a parameter. And voila!, we have got a function matching the name and signature.
public static void printHelloWorld(int cnt) {
for(int i = 0; i < cnt; i++) {
System.out.println("Hello World");
}
}
You can notice, public static before the function name, well public specifier in java simply tells that this function can be accessed from anywhere in the project.
Static keyword tells that this function doesn’t need a object to call it. It can be called without an object as well, which is what we are doing here.
As soon as a function is called, stack is filled with all its local variables. Now, it will look like as follows:
cnt, i
printHelloWorld(int cnt)
main()
And as all the instructions in the function finish executing, control is returned back to the main. All the local variables of the function are flushed out from the stack, and now it will look like as follows:
main()
Again we have got a function call, this time to a different function with a signature different than the previous one.
Notice, this function call also passes a integer value 5 as an argument, but unlike the previous one, it also asks for a integer value in return and collects in the variable sum. So, the called function accepts and returns as well, an integer value.
public static int addNumbersTill(int maxNum) {
int sum = 0; // initializing a local integer variable sum equal to zero
for(int num = 1; num <= maxNum; num++) { // for loop to iterate until maxNum
sum = sum + num; // adding each integer until maxNum
}
return sum; // returns the total sum of integers through 1 until maxNum
}
Stack again be filled with the function and its local variables until the control is returned back to the main after the return statement. It looks like as follows after the call:
maxNum, sum=0, num
int addNumbersTill(int maxNum)
main()
}
Now, I think you can figure it out for yourself, what this line of code will do, and what will happen to the stack.
public static void printNumbersNSum(int maxNum, intsum) {
for(int num = 1; num <= maxNum; num++) {
System.out.print(num + " ");
}
System.out.println(" Sum: " + sum);
}
}
cnt, i
printHelloWorld(int cnt)
main()
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.