I need to create a print statement as "n" is being popped from the stack, just like I have one for when it is being pushed on to the stack. I understand what is happening on that final return when in the base case, but I don't know how to show the items being popped, how would I do this? Thanks in advance.

/**
 * 
 * @author harryluthi
 * @assignment 3
 * @precoditions none
 * 
 * SumOfSquares will calculate and return the sum of squares
 * while also telling the user know when an item is pushed
 * and popped from the stack.
 *
 */

public class SumOfSquares {
	
	private static int square;
	
	public static int ss(int n) {		
		if (n == 0) {
			System.out.println("Base case: n = 0");
			System.out.print("Sum of squares is "+square);
			return square;
		}
		
		else {
			while (n > 0) {
				System.out.println("Pushing call onto stack. n = " + n);
				square = n*n + square;
				return square + ss(n-1);
			}
		}
		return -1;
	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		ss(4);
	}

}

Here is the console display after code runs, followed by what I need it to ultimately display.

Pushing call onto stack. n = 4
Pushing call onto stack. n = 3
Pushing call onto stack. n = 2
Pushing call onto stack. n = 1
Base case: n = 0
Sum of squares is 30


Need:
Pushing call onto stack. n = 4
Pushing call onto stack. n = 3
Pushing call onto stack. n = 2
Pushing call onto stack. n = 1
Base case: n = 0
Popping previous call from stack. n = 1
Popping previous call from stack. n = 2
Popping previous call from stack. n = 3
Popping previous call from stack. n = 4
Sum of squares is 30

you don't have a stack.
first thing to do is add one.
a stack is like an array, you can add things to them, take them out of it again, step by step.

for instance: if you add four elements to a stack,
and you take three elements away, without knowing exactly what you take out of it, the last element will always be equal to what you stored in there as first element.

in your code above, you may be able to 'simulate' it a bit with fixed values, but it still won't be a stack. a stack would be able to do the same if you add random values, that is something your code is not able to do.

Oh of course I should do the obvious and create a stack. Thank for the quick help

if you *need* a stack for your assignement then go for it, but as far as i can see the recursive solution that you are using is fine, the thing is you are printing out "(Sum of squares is "+square)" inside your ss() method while really you would want to print this when the function is over, since it returns a int. The problem with doing this with your current code is that your function operates on a global variable, so the recursive work isn't really used corectly.

first, drop the while, its useless, as you use a "return" statement in it, it never loops, and you do not want to loop in a recursive solution.

if you want the output to look like the one you ased for, drop the while, drop the "square" variable, simply work with n within the method, and in your main use "System.out.println("Sum of squares is " + ss(4));"

lastly, in the "else" part of your method, after printing the "push" call the function again with n-1 , store that call's return value , print "poping : (that value you jsut stored)" then return it.

here is a correct recursive version of your code, without a stack.

public class SumOfSquares {
    public static int ss(int n) {
        System.out.println("Beginning ss(" + n + ").");		
        if( n < 0 ) return -1;

        if (n == 0) {
            System.out.println("Base case : n = 0");
            System.out.println("ss(" + n +") returning : " + n);
            return n;
        }
        else {
            int x;
            System.out.println("Calling ss(" + (n-1) + ").");
            x = n*n + ss(n-1);
            System.out.println("ss(" + n +") returning : " + x);

            return x;
        }
    }

    public static void main(String[] args) {
        System.out.println("Sum of squares is : " + ss(4));
    }
}

output will be :


Beginning ss(4).
Calling ss(3).
Beginning ss(3).
Calling ss(2).
Beginning ss(2).
Calling ss(1).
Beginning ss(1).
Calling ss(0).
Beginning ss(0).
Base case: n = 0
ss(0) returning : 0
ss(1) returning : 1
ss(2) returning : 5
ss(3) returning : 14
ss(4) returning : 30
Sum of squares is : 30

Don't need a stack, just figured it out.

import java.util.Scanner;

/**
 * 
 * @author harryluthi
 * @assignment 3
 * @precoditions none
 * 
 * SumOfSquares will calculate and return the sum of squares
 * while also telling the user know when an item is pushed
 * and popped from the stack.
 *
 */

class SumOfSquares {
	
	private static int square;
	
	public static void ss(int n) {
		
		if (n == 0) {
			System.out.println("Base case: n = 0");
			System.out.println("Sum of squares is " + square);
		}
		
		else {
			System.out.println("Pushing item on to stack. n = "+n);
			square = n*n + square;
			ss(n-1);
			System.out.println("Popping previous item from stack. n = "+n);
			
		}
	}
	
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		System.out.println("Running test driver...");
		System.out.println("Enter number to compute the sum of squares on: ");
		Scanner scan = new Scanner(System.in);
		int n = scan.nextInt();
		
		ss(n);
	}
	
}

Philipe, thanks for your help though!! That is basically what I was looking for.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.