Results 1 to 20 of 21
Thread: Java Assignment Problem.
 12062012, 12:48 AM #1Member
 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.

Re: Java Assignment Problem.
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.
 12062012, 12:54 AM #3Senior Member
 Join Date
 Oct 2011
 Location
 Sweden
 Posts
 124
 Rep Power
 0
Re: Java Assignment Problem.
Fub, beat me to it! *edited*
 12062012, 12:59 AM #4Member
 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.

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.
 12062012, 01:09 AM #6Member
 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; } }
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 } }
 12062012, 01:20 AM #7Senior Member
 Join Date
 Oct 2011
 Location
 Sweden
 Posts
 124
 Rep Power
 0
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 }
 12062012, 01:22 AM #8Member
 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.
 12062012, 01:23 AM #9Member
 Join Date
 Dec 2012
 Posts
 24
 Rep Power
 0
Re: Java Assignment Problem.
Nvm when i reloaded the page again everything showed up.
 12062012, 01:26 AM #10Senior 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.
 12062012, 01:46 AM #11Member
 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.
 12062012, 02:00 AM #12Member
 Join Date
 Dec 2012
 Posts
 24
 Rep Power
 0
Re: Java Assignment Problem.
Ok i've rewritten 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 } }
 12062012, 04:50 AM #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 followon question. Any more multiposting or starting threads with inane subject lines and you are liable to be banned for a period.
dbIf you're forever cleaning cobwebs, it's time to get rid of the spiders.
 12062012, 10:56 AM #14Moderator
 Join Date
 Apr 2009
 Posts
 13,321
 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...Please do not ask for code as refusal often offends.
** This space for rent **
 12072012, 01:32 AM #15Member
 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; } }
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)); } }
 12072012, 01:55 AM #16Senior Member
 Join Date
 Jan 2012
 Posts
 210
 Rep Power
 6
Re: Java Assignment Problem.
What's the problem with Fibonacci class?
 12072012, 02:08 AM #17Member
 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.
 12072012, 03:01 AM #18Senior 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.
 12072012, 03:14 AM #19Member
 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 rewritten 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; } }
 12072012, 03:14 AM #20Member
 Join Date
 Dec 2012
 Posts
 24
 Rep Power
 0
Similar Threads

problem with assignment
By frickenj in forum New To JavaReplies: 9Last Post: 05082011, 05:02 PM 
problem with java assignment
By jaire in forum New To JavaReplies: 9Last Post: 03102011, 12:00 PM 
Problem with my assignment  in need of help!
By Chopaan in forum New To JavaReplies: 1Last Post: 01172011, 03:19 AM 
While Loop Problem with Java Assignment Program
By welsh_rocker in forum New To JavaReplies: 9Last Post: 01122011, 02:55 PM 
The Assignment Problem
By bumblyb33 in forum New To JavaReplies: 5Last Post: 03042009, 05:21 AM
Bookmarks