1. Member
Join Date
Dec 2012
Posts
24
Rep Power
0

## 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. ## Re: Java Assignment Problem.

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.

3. Senior Member
Join Date
Oct 2011
Location
Sweden
Posts
124
Rep Power
0

## Re: Java Assignment Problem.

Fub, beat me to it! *edited*

4. Member
Join Date
Dec 2012
Posts
24
Rep Power
0

## 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. ## 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. Member
Join Date
Dec 2012
Posts
24
Rep Power
0

## 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. Senior Member
Join Date
Oct 2011
Location
Sweden
Posts
124
Rep Power
0

## Re: Java Assignment Problem.

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. Member
Join Date
Dec 2012
Posts
24
Rep Power
0

## Re: Java Assignment Problem.

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

9. Member
Join Date
Dec 2012
Posts
24
Rep Power
0

## Re: Java Assignment Problem.

Nvm when i reloaded the page again everything showed up.

10. Senior Member
Join Date
Oct 2011
Location
Sweden
Posts
124
Rep Power
0

## 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. Member
Join Date
Dec 2012
Posts
24
Rep Power
0

## 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. Member
Join Date
Dec 2012
Posts
24
Rep Power
0

## 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. ## 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

14. Moderator
Join Date
Apr 2009
Posts
13,130
Rep Power
23

## 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...

15. Member
Join Date
Dec 2012
Posts
24
Rep Power
0

## 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. Senior Member
Join Date
Jan 2012
Posts
210
Rep Power
6

## Re: Java Assignment Problem.

What's the problem with Fibonacci class?

17. Member
Join Date
Dec 2012
Posts
24
Rep Power
0

## 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. Senior Member
Join Date
Jan 2012
Posts
210
Rep Power
6

## 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. Member
Join Date
Dec 2012
Posts
24
Rep Power
0

## 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. Member
Join Date
Dec 2012
Posts
24
Rep Power
0

## 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 Last

#### Posting Permissions

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