Results 1 to 20 of 21
Thread: Java Assignment Problem.
- 12-05-2012, 11:48 PM #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.
-
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.
- 12-05-2012, 11:54 PM #3
Senior Member
- Join Date
- Oct 2011
- Location
- Sweden
- Posts
- 123
- Rep Power
- 0
Re: Java Assignment Problem.
Fub, beat me to it! *edited*
- 12-05-2012, 11:59 PM #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.
-
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, 12:09 AM #6
Member
- Join Date
- Dec 2012
- Posts
- 24
- Rep Power
- 0
Re: Java Assignment Problem.
The first Java coding is Fibonacci.java
This is the FibonacciJdialog.java code for input and output.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; } }
Is this what you needed?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 } }
- 12-06-2012, 12:20 AM #7
Senior Member
- Join Date
- Oct 2011
- Location
- Sweden
- Posts
- 123
- 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 }
- 12-06-2012, 12:22 AM #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.
- 12-06-2012, 12:23 AM #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.
- 12-06-2012, 12:26 AM #10
Senior Member
- Join Date
- Oct 2011
- Location
- Sweden
- Posts
- 123
- 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.
- 12-06-2012, 12:46 AM #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-06-2012, 01:00 AM #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 } }
- 12-06-2012, 03: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 follow-on question. Any more multiposting or starting threads with inane subject lines and you are liable to be banned for a period.
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 12-06-2012, 09:56 AM #14
Moderator
- Join Date
- Apr 2009
- Posts
- 10,476
- Rep Power
- 16
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.
- 12-07-2012, 12:32 AM #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.
This is the Dialog Code For right now this is fine as far as i know.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; } }
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.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)); } }
- 12-07-2012, 12:55 AM #16
Senior Member
- Join Date
- Jan 2012
- Posts
- 210
- Rep Power
- 2
Re: Java Assignment Problem.
What's the problem with Fibonacci class?
- 12-07-2012, 01:08 AM #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.
- 12-07-2012, 02:01 AM #18
Senior Member
- Join Date
- Jan 2012
- Posts
- 210
- Rep Power
- 2
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, 02:14 AM #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; } }
- 12-07-2012, 02:14 AM #20
Member
- Join Date
- Dec 2012
- Posts
- 24
- Rep Power
- 0
Similar Threads
-
problem with assignment
By frickenj in forum New To JavaReplies: 9Last Post: 05-08-2011, 05:02 PM -
problem with java assignment
By jaire in forum New To JavaReplies: 9Last Post: 03-10-2011, 11:00 AM -
Problem with my assignment - in need of help!
By Chopaan in forum New To JavaReplies: 1Last Post: 01-17-2011, 02:19 AM -
While Loop Problem with Java Assignment Program
By welsh_rocker in forum New To JavaReplies: 9Last Post: 01-12-2011, 01:55 PM -
The Assignment Problem
By bumblyb33 in forum New To JavaReplies: 5Last Post: 03-04-2009, 04:21 AM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks