Page 1 of 2 12 LastLast
Results 1 to 20 of 21
Like Tree1Likes

Thread: Java Assignment Problem.

  1. #1
    Southpaw is offline Member
    Join Date
    Dec 2012
    Posts
    24
    Rep Power
    0

    Default Java Assignment Problem.

    I cant get the sum to return any results in the jdialog program. Also on a side note i have to limit number n to the value 40 maximum. Im also required to use long as a value for the returned results.
    The coding that i have so far is:
    public class Fibonacci
    {
    public long Fib(long n)
    {
    long in1=1,in2=1;
    long sum=0;//initial value
    long index=1;

    while (index<n)
    {
    // sum=the sum of 2 values;
    // in1 gets in2
    // in2 gets sum
    // increment index
    }
    return sum;
    }
    }
    The input Jdialog Program is:
    import javax.swing.JOptionPane;

    public class FibonacciJDialog
    {

    public static void main(String[] args)
    {

    String input = JOptionPane.showInputDialog("Enter n: "); // getting user number input.
    long n = long.parseInt(input);


    Fibonacci box = new Fibonacci(); // Creating new Fibonacci object.


    {
    JOptionPane.showMessageDialog(null,"That Fibonacci Number is" ); // Display results in dialog box.
    return sum;

    }
    System.exit(0); // Terminate

    }
    }
    Thanks for any help.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Java Assignment Problem.

    Quote Originally Posted by Southpaw View Post
    I cant get the sum to return any results in the jdialog program. Also on a side note i have to limit number n to the value 40 maximum. Im also required to use long as a value for the returned results.
    You're asking this question as if we know what you're trying to do, and what's not working. Please give more information and assume that we are completely ignorant as to your assignment and your problems. Also, please wrap your formatted code in [code] [/code] tags when posting it here so that it's readable.

    Luck.

  3. #3
    Zyril is offline Senior Member
    Join Date
    Oct 2011
    Location
    Sweden
    Posts
    124
    Rep Power
    0

    Default Re: Java Assignment Problem.

    Fub, beat me to it! *edited*

  4. #4
    Southpaw is offline Member
    Join Date
    Dec 2012
    Posts
    24
    Rep Power
    0

    Default Re: Java Assignment Problem.

    I have to design and write a Dialog box for Input/Ouput that allows the users to input a number like 5 and the program will produce an output of 8, which is the fibonacci of 5 because the fib(5)=8.

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Java Assignment Problem.

    Again, please edit your first post and place the code tag, [code] above your code and the tag [/code] under your posted code.

  6. #6
    Southpaw is offline Member
    Join Date
    Dec 2012
    Posts
    24
    Rep Power
    0

    Default Re: Java Assignment Problem.

    The first Java coding is Fibonacci.java
    Java Code:
    public class Fibonacci
    {
    public long Fib(long n)
    {
    long in1=1,in2=1;
    long sum=0;//initial value
    long index=1;
    
    while (index<n)
    {
    // sum=the sum of 2 values;
    // in1 gets in2
    // in2 gets sum
    // increment index
    }
    return sum;
    }
    }
    This is the FibonacciJdialog.java code for input and output.
    Java Code:
    import javax.swing.JOptionPane;
    
    public class FibonacciJDialog
    {
    
    public static void main(String[] args)
    {
    
    String input = JOptionPane.showInputDialog("Enter n: "); // getting user number input.
    long n = long.parseInt(input);
    
    
    Fibonacci box = new Fibonacci(); // Creating new Fibonacci object.
    
    
    {
    JOptionPane.showMessageDialog(null,"That Fibonacci Number is" ); // Display results in dialog box.
    return sum;
    
    }
    System.exit(0); // Terminate
    
    }
    }
    Is this what you needed?

  7. #7
    Zyril is offline Senior Member
    Join Date
    Oct 2011
    Location
    Sweden
    Posts
    124
    Rep Power
    0

    Default Re: Java Assignment Problem.

    I made some comments in your code that you can think about.

    Java Code:
    import javax.swing.JOptionPane;
    
    public class FibonacciJDialog
    {
    
    	public static void main(String[] args)
    	{
    
    		String input = JOptionPane.showInputDialog("Enter n: "); // getting user number input.
    		long n = long.parseInt(input); //This will give you an error. The name of the class "Long" is "Long". It will not parse an integer either..
    
    
    		Fibonacci box = new Fibonacci(); // Creating new Fibonacci object.
    
    		//What are the brackets below for? This is not a method body, nor is a body needed in any way. Also, what are you trying to return with sum?
    		{
    			JOptionPane.showMessageDialog(null,"That Fibonacci Number is" ); // Display results in dialog box.
    			return sum;
    
    		}
    		System.exit(0); // Terminate
    
    	}

  8. #8
    Southpaw is offline Member
    Join Date
    Dec 2012
    Posts
    24
    Rep Power
    0

    Default Re: Java Assignment Problem.

    I appreciate the help but the comments are cut off early so i can't see everything your saying.

  9. #9
    Southpaw is offline Member
    Join Date
    Dec 2012
    Posts
    24
    Rep Power
    0

    Default Re: Java Assignment Problem.

    Nvm when i reloaded the page again everything showed up.

  10. #10
    Zyril is offline Senior Member
    Join Date
    Oct 2011
    Location
    Sweden
    Posts
    124
    Rep Power
    0

    Default Re: Java Assignment Problem.

    I understand that you want to run your Fib(long n) method but you have to call it with the object that you created of that class. You also have to make the method return something, since you right now has commented that code out, it will return nothing.

  11. #11
    Southpaw is offline Member
    Join Date
    Dec 2012
    Posts
    24
    Rep Power
    0

    Default Re: Java Assignment Problem.

    I'm sorry i'm very confused by in this class right now. I had a really easy time with this class till my last two assignments. Now I don't have a clue what to do.

  12. #12
    Southpaw is offline Member
    Join Date
    Dec 2012
    Posts
    24
    Rep Power
    0

    Default Re: Java Assignment Problem.

    Ok i've re-written my Dialog code and im still getting a few errors:
    Java Code:
    import javax.swing.JOptionPane;
    
    public class FibonacciJDialog
    {
    
        public static void main(String[] args)
        {
    	 	String num;
    		int n;
    
    		num =
              JOptionPane.showInputDialog("Enter n: "); // getting user number input.
              n = Integer.parseInt(num);
    
    
            Fibonacci box = new Fibonacci(); // Creating new Fibonacci object.
    
    
            JOptionPane.showMessageDialog(null,"That Fibonacci Number is" ); // Display results in dialog box.
            return sum;
    
    
            System.exit(0); // Terminate
    
        }
    }

  13. #13
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,453
    Rep Power
    20

    Default Re: Java Assignment Problem.

    Southpaw, go through the Forum Rules -- particularly the second and third paragraphs.

    I've closed the other two threads you started with the same or a follow-on question. Any more multiposting or starting threads with inane subject lines and you are liable to be banned for a period.

    db
    Fubarable likes this.
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  14. #14
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,165
    Rep Power
    20

    Default Re: Java Assignment Problem.

    When you get errors you need to post the full text of them, highlighting (if necessary) the lines on which they occur.

    In this case, you don't want a return statement, and the System.exit() is pointless as the app is about to exit anyway.

    Fix those and then you need to decide what you are going to do with your Fibonacci object...
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  15. #15
    Southpaw is offline Member
    Join Date
    Dec 2012
    Posts
    24
    Rep Power
    0

    Default Re: Java Assignment Problem.

    Allright here is the current code ive changed it around quite a bit so far both compile fine. The problem seems to lie in the Fibonacci.Java file.
    Java Code:
    public class Fibonacci
    	{
    	int Fib (int n)
    		{
    			int in1=1,in2=1;
    			int sum=in1+in2;//initial value
    			int index=40;
    			while (index<n)
    			{
    			// sum=the sum of 2 values;
    			// in1 gets in2
    			// in2 gets sum
    			// increment index
    			}
    			return sum;
    		}
    	}
    This is the Dialog Code For right now this is fine as far as i know.
    Java Code:
    import javax.swing.JOptionPane;
    
    public class FibonacciJDialog
    {
    
        public static void main(String[] args)
        {
    	 	String num;
    		int n;
    
    
    		num =
              JOptionPane.showInputDialog("Enter a number: ");
              n = Integer.parseInt(num);
    
    
    
    
            Fibonacci box = new Fibonacci();
    
    
            JOptionPane.showMessageDialog(null,"That Fibonacci Number is " + box.Fib(n));
           }
    
    
    }
    So far the problem that im getting is when i run my jdialog program the output when i enter a number is wrong. It's supposed to be in fibonacci sequence so if I was to input the number like 5 the program would create an output of 8.

  16. #16
    diamonddragon is offline Senior Member
    Join Date
    Jan 2012
    Posts
    210
    Rep Power
    3

    Default Re: Java Assignment Problem.

    What's the problem with Fibonacci class?

  17. #17
    Southpaw is offline Member
    Join Date
    Dec 2012
    Posts
    24
    Rep Power
    0

    Default Re: Java Assignment Problem.

    Everything compiles correctly the problem is that when i run the program and i input the Number for variable n the output of the program is wrong. As i said above if i input 5 i should 8 when i run the program. Instead im getting 2 as an output when i put 5 in the input. This class is supposed to run the fibonacci sequence. This means that when the user inputs a number the program is supposed to output the next number in the sequence.

  18. #18
    diamonddragon is offline Senior Member
    Join Date
    Jan 2012
    Posts
    210
    Rep Power
    3

    Default Re: Java Assignment Problem.

    Well, that's what You programmed, to return 2, isn't so?
    So, if You need 8 when input 5, You need to know algorithm for getting output 8, when input is 5.

  19. #19
    Southpaw is offline Member
    Join Date
    Dec 2012
    Posts
    24
    Rep Power
    0

    Default Re: Java Assignment Problem.

    Ok i've gone back and looked over the code and the comments and ive re-written some of the coding. In the comments of the code it says something about increment index can someone explain what this is? Im still getting the wrong input but maybe if i can figure out what increment index is so i can make sure everything is on here then it will fix it.

    Java Code:
    public class Fibonacci
    	{
    	int Fib (int n)
    		{
    			int in1=1,in2=1;
    			int sum=1;//initial value
    			int index=40;
    			while (index<n)
    			{
    			sum = in1 + in2;
    			in1 = in2;
    			in2 = sum;
    			// sum=the sum of 2 values;
    			// in1 gets in2
    			// in2 gets sum
    			// increment index
    			}
    			return sum;
    		}
    	}

  20. #20
    Southpaw is offline Member
    Join Date
    Dec 2012
    Posts
    24
    Rep Power
    0

    Default Re: Java Assignment Problem.

    I'm sorry i meant to say im getting the wrong output in my previous post.

Page 1 of 2 12 LastLast

Similar Threads

  1. problem with assignment
    By frickenj in forum New To Java
    Replies: 9
    Last Post: 05-08-2011, 06:02 PM
  2. problem with java assignment
    By jaire in forum New To Java
    Replies: 9
    Last Post: 03-10-2011, 12:00 PM
  3. Problem with my assignment - in need of help!
    By Chopaan in forum New To Java
    Replies: 1
    Last Post: 01-17-2011, 03:19 AM
  4. While Loop Problem with Java Assignment Program
    By welsh_rocker in forum New To Java
    Replies: 9
    Last Post: 01-12-2011, 02:55 PM
  5. The Assignment Problem
    By bumblyb33 in forum New To Java
    Replies: 5
    Last Post: 03-04-2009, 05:21 AM

Posting Permissions

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