Results 1 to 15 of 15
  1. #1
    Manish87 is offline Member
    Join Date
    Aug 2010
    Posts
    29
    Rep Power
    0

    Default fibonnaci number between limits?

    hi there,
    i have been working on this applet program for the past 3-4 hours n i still cant figure out on what the error is! there appears to be an logical error in this programs(thought it generates few numberes between the limit).

    Java Code:
    import java.applet.*;
    import java.awt.*;
    import java.awt.event.*;
    
    public class Main extends Applet implements ActionListener
    {
    	TextField t1,t2,t3;
    	Label label1,label2,label3;
    	Button b;
    	public void init()
    	{
    		t1=new TextField(10);
    		t2=new TextField(10);
    		t3=new TextField(10);
    
    		label1=new Label("enter the lower limit");
    		label2=new Label("enter the upper limit");
    		label3=new Label("fibonnaci series are");
    		b=new Button("find");
    	
    		add(label1);
    		add(t1);
    		add(label2);
    		add(t2);
    		add(label3);
    		add(t3);
    		add(b);
    
    		b.addActionListener(this);
    	}
    	
    	public void actionPerformed(ActionEvent e)
    	{
    		String s1="",s2="",s3="",str="";
    		int lower=0,upper=0,fib1=0,fib2=0,fib3=0,fib4=0,i;	
    	
    		s1=t1.getText();		
    		s2=t2.getText();
    
    		lower=Integer.parseInt(s1);
    		upper=Integer.parseInt(s2);
    
    		fib1=lower;
    		fib2=lower+1;
    		
    		fib3=fib1+fib2;
    		
    		while(fib3<upper)
    		{
    		s2=str+" "+String.valueOf(fib3);
    		
    		fib1=fib2;
    		fib2=fib3;
    		fib3=fib1+fib2;
    		}
    		s1=String.valueOf(fib1)+" "+String.valueOf(fib2)+" "+s2;	
    	}
    }
    Last edited by Eranga; 09-25-2010 at 05:10 PM. Reason: code tags added

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,449
    Rep Power
    25

    Default

    Could you show the output for the program and explain what the problem with the output is and show what the output should be?
    Your program documentation doesn't explain anything about what the program is to do or how it is going to do it.

  3. #3
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    And also please use code tags when you are posting again. Unformated codes are really hard to read.

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Did you know how to calculate the Fibonacci serious? Seems to me the logic you've build bu in your code is incorrect.

  5. #5
    Manish87 is offline Member
    Join Date
    Aug 2010
    Posts
    29
    Rep Power
    0

    Default

    Hi,

    thanks for the reply guys,i had modified the program before i posted the code,hence when i clickd on the find button that was supposed to generate fibonaci series ,it doesnt dispay anythin! :( :(.the logic luks proper to me though,i still cant figure out where i am going wrong! :(





    Kind Regards
    Manish87

  6. #6
    Manish87 is offline Member
    Join Date
    Aug 2010
    Posts
    29
    Rep Power
    0

    Default

    Hi,

    thanks for the reply guys,i had modified the program before i posted the code,hence when i clickd on the find button that was supposed to generate fibonaci series ,it doesnt dispay anythin! :( :(.the logic luks proper to me though,i still cant figure out where i am going wrong! :(

    the ouput show be say like if i enter a lower limit say 6 and upper limit say 20,it should generate all the fibonaci numbers between 6 and 20

    i.e 6,7,13




    Kind Regards
    Manish87

  7. #7
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Fine, then you've set the text into the relevant control, after the completion of the calculation.

    Java Code:
    t3.setText(s1);

  8. #8
    Manish87 is offline Member
    Join Date
    Aug 2010
    Posts
    29
    Rep Power
    0

    Default

    hey,

    now the program displays the output but its incorrect

    output:

    lower limit : 1

    upper limit:10

    output: 5 8 8

  9. #9
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,449
    Rep Power
    25

    Default

    What should the output be?
    Have you tried debugging your code by adding print outs of the variables and all the intermediate values as it computes them?

  10. #10
    Manish87 is offline Member
    Join Date
    Aug 2010
    Posts
    29
    Rep Power
    0

    Default

    Hey,

    the output was supposed to be 1,2,3,5,8 where 1 and 8 are lower and upper limit.the preceding values are added!

  11. #11
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,449
    Rep Power
    25

    Default

    Have you tried debugging your code by adding print outs of the variables and all the intermediate values as it computes them?

  12. #12
    Manish87 is offline Member
    Join Date
    Aug 2010
    Posts
    29
    Rep Power
    0

    Default

    Hi,

    since i started my applet last week itself i have not gone much of its details,hence i have not tryed out any debugging methods

    i tryed to make the program more simplier.please check it but the problem still persist !

    fib1=lower;
    fib2=lower+1;

    s1=String.valueOf(fib1)+" "+String.valueOf(fib2);

    fib3=fib1+fib2;

    while(fib3<upper)
    {

    s2=s1+" "+String.valueOf(fib3);
    t3.setText(s1);
    fib1=fib2;
    fib2=fib3;
    fib3=fib1+fib2;
    }

  13. #13
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,449
    Rep Power
    25

    Default

    What are the all numbers that are generated by your code? Are they in the series?

    Add print outs as the variables change and copy and paste them here. Be sure to show the name of the variable as well as its value in the print out.

  14. #14
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

  15. #15
    bm0807 is offline Member
    Join Date
    Sep 2010
    Posts
    2
    Rep Power
    0

    Default

    try this.

    fib1=lower;
    fib2=lower+1;

    fib3=fib1+fib2;

    s1 = s1 + " " + String.valueOf(fib2);
    s2 = "";

    while(fib3<upper)
    {
    s2= s2+" "+String.valueOf(fib3);

    fib1=fib2;
    fib2=fib3;
    fib3=fib1+fib2;
    }
    s1=s1 +" "+s2;
    System.Out.Println(s1);

Similar Threads

  1. Finding a number in array close to another number
    By SteroidalPsycho in forum New To Java
    Replies: 2
    Last Post: 02-15-2010, 12:37 AM
  2. Printing the Number of Times a Number in a Range Shows up
    By space4rent00 in forum New To Java
    Replies: 1
    Last Post: 02-05-2010, 10:42 PM
  3. Java Limits?
    By TexanProgrammer in forum New To Java
    Replies: 10
    Last Post: 09-02-2009, 04:42 AM
  4. Replies: 1
    Last Post: 08-07-2009, 12:14 PM
  5. Replies: 1
    Last Post: 11-03-2008, 02:38 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
  •