Results 1 to 3 of 3
  1. #1
    Coukapecker is offline Member
    Join Date
    Mar 2010
    Posts
    2
    Rep Power
    0

    Default Stack Overflow work around?

    Hey,

    Recently my CS teacher (highschool G12 Computer Science) has given out an assignment with a few subparts to it, one of them being to create a fibonacii sequence application. The assignment is worth a good percentage of our mark so I'd like to do well on it.

    We are not allowed to use imports and must use the basic java functions to write all our apps.

    My current application:

    Java Code:
    public class Part2 {
    	public static void main(String[] args) throws Exception {
    		long fib1 = 0, fib2 = 1;
    		int n1, n2, n;
    
    		System.out.println("Enter number of fibonacii values to compute: ");
    		n1 = System.in.read();
    		n2 = System.in.read();
    		System.in.skip(2);
    		n1 -= 48;
    		n2 -= 48;
    		n = n1 * 10 + n2;
    
    		for (int i = 0; i < n; i++) {
    			long temp = fib1;
    			fib1 = fib2;
    			fib2 += temp;
    			System.out.print(fib1 + ", ");
    		}
    	}
    }
    I'm pretty satisfied with it and its pretty efficient, but it has some obvious limmitations.

    1. the nth number entered has to be a 2 digit number or a single digit with a 0 in front of it.
    2. Because of the size limmit of the long datatype it cannot compute after around the 93rd sequence number as it stack overflows.

    Are they're any ways that I can get around these two problems without using imports? I was thinking of going into the java library and putting the code they use for inputting data directly into my code, but im not sure how to access the library to view the source code.

    any help would be appriciated,


    ~Coukapecker~

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,526
    Blog Entries
    7
    Rep Power
    20

    Default

    I don't see how a stack overflow error can occur; I understand that you can have int overflows because those fibonacci numbers can get huge. btw, please get rid of that 48, it's a magic number, use the symbolic '0' instead, the compiler knows how to deal with it and it'll translate it for you. Also what is that 'skip()' doing there?

    kind regards,

    Jos
    Last edited by JosAH; 03-14-2010 at 08:26 PM.

  3. #3
    Coukapecker is offline Member
    Join Date
    Mar 2010
    Posts
    2
    Rep Power
    0

    Default

    Quote Originally Posted by JosAH View Post
    I don't see how a stack overflow error can occur; I understand that you can have int overflows because those fibonacci numbers can get huge. btw, please get rid of that 48, it's a magic number, use the symbolic '0' instead, the compiler knows how to deal with it and it'll translate it for you. Also what is that 'skip()' doing there?

    kind regards,

    Jos
    My mistake, int overflow. (the part where it starts giving me the wrong answers in the sequence, as in negative numbers?, how can i fix this?)

    The 48 is how we were taught to do it; how do you use the symbolic '0' instead?

    The skip(2) is so it doesn't read the "ENTER" button pressed when the user inputs as part of the variable.

Similar Threads

  1. heap overflow
    By is0dvil in forum Advanced Java
    Replies: 1
    Last Post: 11-17-2009, 10:43 PM
  2. Java Stack Overflow?
    By fullmetaljacket in forum New To Java
    Replies: 0
    Last Post: 05-19-2009, 07:49 PM
  3. [SOLVED] overflow when calculate factorial
    By ravinda in forum New To Java
    Replies: 6
    Last Post: 05-05-2009, 05:07 PM
  4. Graphics2D: stack overflow error
    By rosh72851 in forum New To Java
    Replies: 11
    Last Post: 10-15-2008, 09:01 PM
  5. How to set a stack size. -Xss doesn't work
    By protonus in forum New To Java
    Replies: 4
    Last Post: 06-27-2008, 06:59 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •