Java Assignment Problem.

Show 40 post(s) from this thread on one page
Page 1 of 2 12 Last
• 12-06-2012, 12:48 AM
Southpaw
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.
• 12-06-2012, 12:51 AM
Fubarable
Re: Java Assignment Problem.
Quote:

Originally Posted by Southpaw
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.
• 12-06-2012, 12:54 AM
Zyril
Re: Java Assignment Problem.
Fub, beat me to it! *edited*
• 12-06-2012, 12:59 AM
Southpaw
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.
• 12-06-2012, 01:04 AM
Fubarable
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.
• 12-06-2012, 01:09 AM
Southpaw
Re: Java Assignment Problem.
The first Java coding is Fibonacci.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.
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?
• 12-06-2012, 01:20 AM
Zyril
Re: Java Assignment Problem.

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         }```
• 12-06-2012, 01:22 AM
Southpaw
Re: Java Assignment Problem.
I appreciate the help but the comments are cut off early so i can't see everything your saying.
• 12-06-2012, 01:23 AM
Southpaw
Re: Java Assignment Problem.
Nvm when i reloaded the page again everything showed up.
• 12-06-2012, 01:26 AM
Zyril
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.
• 12-06-2012, 01:46 AM
Southpaw
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-06-2012, 02:00 AM
Southpaw
Re: Java Assignment Problem.
Ok i've re-written my Dialog code and im still getting a few errors:
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     } }```
• 12-06-2012, 04:50 AM
DarrylBurke
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
• 12-06-2012, 10:56 AM
Tolls
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...
• 12-07-2012, 01:32 AM
Southpaw
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.
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.
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.
• 12-07-2012, 01:55 AM
diamonddragon
Re: Java Assignment Problem.
What's the problem with Fibonacci class?
• 12-07-2012, 02:08 AM
Southpaw
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.
• 12-07-2012, 03:01 AM
diamonddragon
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.
• 12-07-2012, 03:14 AM
Southpaw
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.

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;                 }         }```
• 12-07-2012, 03:14 AM
Southpaw
Re: Java Assignment Problem.
I'm sorry i meant to say im getting the wrong output in my previous post.
Show 40 post(s) from this thread on one page
Page 1 of 2 12 Last