Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 15 of 15

Thread: Frog jumping code (AP comp science help!)

  1. #1
    Junior Member
    Join Date
    Sep 2011
    Posts
    8
    My Mood
    Stressed
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Frog jumping code (AP comp science help!)

    My computer science teacher told my class to write a code where a frog jumps a staircase to eat a fly. The frog can only jump 3 stairs at once and the frog must jump any number of stairs input. The program must compute the number of possible ways the frog can get to the top of the staircase and also spit out the paths it can take. s= # of stairs
    V My code so far has errors in the jumpsPossible method and I really don't understand why can anyone help?
    class frog {
    static int totalPaths=0;
    	public static void jumpsPossible(int i, int s, String path){
    		if (i==s){
    			totalPaths++;
    			System.out.println (path);
    		}
    		if (i<s){
    			jumpsPossible(i+1,s,path+1);
    			jumpsPossible(i+2,s,path+2);
    			jumpsPossible(i+3,s,path+3);
    		}
    	}
    	public static void main (String [] args) {
    		Integer s = Integer.valueOf(args [0]);
    		System.out.println ("Freddie the Frog will now jump" + s + " stairs to catch a fly!");
    		jumpsPossible(0,s,"");
    		System.out.println ("Freddie can get to the top of the staircase in " + jumpsPossible + " ways.");
    	}
    }
    Last edited by CompScienceStudent1; September 20th, 2011 at 08:35 PM.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Frog jumping code (AP comp science help!)

    Can you post the output from the program and add comments to show what it should be.

    Please edit your post and wrap the code in code tags.
    Press the Go Advanced button, select the code and press the #icon.

  3. #3
    Junior Member
    Join Date
    Sep 2011
    Posts
    8
    My Mood
    Stressed
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Frog jumping code (AP comp science help!)

    the output should show (lets say the input is 5 stairs so. . .)
    Freddie the Frog will now jump 5 stairs to catch a fly!
    12345
    1245
    1345
    1235
    135
    145
    125
    2345
    245
    235
    25
    345
    35
    Freddie can get to the top of the staircase in 13 ways.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Frog jumping code (AP comp science help!)

    Is that the output from the program?
    Can you copy the console for when you execute the program?
    To copy the contents of the command prompt window:
    Click on Icon in upper left corner
    Select Edit
    Select 'Select All' - The selection will show
    Click in upper left again
    Select Edit and click 'Copy'

    Paste here.

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Frog jumping code (AP comp science help!)

    Please edit your post and wrap the code in code tags.
    Press the Go Advanced button, select the code and press the #icon.

  6. #6
    Junior Member
    Join Date
    Sep 2011
    Posts
    8
    My Mood
    Stressed
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Frog jumping code (AP comp science help!)

    No that is what should be the output
    when i try to compile i get an error:
    kulbir-sabharwals-imac:Raina kulbirsabharwal$ javac -classpath . frog.java
    frog.java:18: cannot find symbol
    symbol : variable jumpsPossible
    location: class frog
    System.out.println ("Freddie can get to the top of the staircase in " + jumpsPossible + " ways.");
    ^
    1 error

  7. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Frog jumping code (AP comp science help!)

    Where is the variable: jumpsPossible defined? The compiler can not find its definition.
    You have a method by that name. Do you another variable with a new name?

  8. #8
    Junior Member
    Join Date
    Sep 2011
    Posts
    8
    My Mood
    Stressed
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Frog jumping code (AP comp science help!)

    The method jumpsPossible is determining the number of possible combinations and the ways in which the frog can jump, so I am trying to print the results of that method in main. . .

  9. #9
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Frog jumping code (AP comp science help!)

    System.out.println ("Freddie can get to the top of the staircase in " + jumpsPossible + " ways.");
    The above is coded as a variable, not a method. A method call ends with ()s like this:
    jumpsPossible(0,s,"");

  10. #10
    Junior Member
    Join Date
    Sep 2011
    Posts
    8
    My Mood
    Stressed
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Frog jumping code (AP comp science help!)

    but i wrote that o.o so confused x.x i did call the method

  11. #11
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Frog jumping code (AP comp science help!)

    System.out.println ("Freddie can get to the top of the staircase in " + jumpsPossible + " ways.");
    Where do you define the variable (red) used in the above println statement?

  12. #12
    Junior Member
    Join Date
    Sep 2011
    Posts
    8
    My Mood
    Stressed
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Frog jumping code (AP comp science help!)

    but its not a variable I'm trying to call the jumpsPossible method

  13. #13
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Frog jumping code (AP comp science help!)

    A method has () after it. Look at your code again. There are no ()s after the name in this statement:
    System.out.println ("Freddie can get to the top of the staircase in " + jumpsPossible + " ways.");
    Without ()s the compiler thinks its a variable. This is NOT a call to a method.

  14. #14
    Junior Member
    Join Date
    Sep 2011
    Posts
    8
    My Mood
    Stressed
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Frog jumping code (AP comp science help!)

    so if i change jumpsPossible in that line to jumpsPossible (0,s,"")
    this is the error:
    frog.java:18: 'void' type not allowed here
    System.out.println ("Freddie can get to the top of the staircase in " + jumpsPossible(0,s,"") + " ways.");

  15. #15
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Frog jumping code (AP comp science help!)

    : 'void' type not allowed here
    That is because the jumpsPossible() method does NOT return any value. It is a void type.

    Where is the number of jumps value stored? Do you have a class variable that has it?

    Should the jumpsPossible() method return a value instead of being void?

Similar Threads

  1. need help in AP Computer Science!
    By eechord in forum Member Introductions
    Replies: 3
    Last Post: October 7th, 2011, 09:57 AM
  2. Replies: 1
    Last Post: March 11th, 2011, 02:11 PM
  3. Cant get my code to stop jumping
    By Mob31 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 2nd, 2011, 07:03 AM
  4. computer science help
    By hairyjewbear in forum Algorithms & Recursion
    Replies: 6
    Last Post: November 4th, 2010, 04:05 PM