5 Replies - 365 Views - Last Post: 06 December 2012 - 09:31 PM Rate Topic: -----

#1 xSouthpaw  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 32
  • Joined: 06-December 12

Java Assignment Problem

Posted 06 December 2012 - 08:06 PM

In this assignment im supposed 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. So far it's going good except i ran into a rough patch. For some reason when i enter a number for the fibonacci if gives me the wrong output. Like if i enter 2 in my code it responds with 2 when i should respond with 3 I believe.

Here is the code take a look at it and tell me what you think.

This is the Dialog coding for input and output.
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));
       }


}





This is the Fibonacci.java code

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



Is This A Good Question/Topic? 0
  • +

Replies To: Java Assignment Problem

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8332
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Java Assignment Problem

Posted 06 December 2012 - 08:19 PM

So basically, your question, after you remove all the nice GUI gadjet and the Object creation is: is this method returns the Fibonaci number for the N levels

	int Fib (int n)
	{
		int in1=1,in2=1;
		int sum=0;//initial value
		int index=1;
		while (index<n)
		{
	  	sum = in1 + in2;
			in1 = in2;
			in2 = sum;
			index++;
		}
		return sum;
	}



beside your horrible indentation it should be easy to test, justr remove the whipped cream around your stuff

	public static void main (String args[])
	{
		for(int i = 1; i < 10; ++i) {
			System.out.println(i + ") " + Fib(i));
		}
	}
	private static int Fib (int n)
	{
		int in1=1,in2=1;
		int sum=0;//initial value
		int index=1;
		while (index<n)
		{
			sum = in1 + in2;
			in1 = in2;
			in2 = sum;
			index++;
		}
		return sum;
	}


yields:
1) 0
2) 2
3) 3
4) 5
5) 8
6) 13
7) 21
8) 34
9) 55

so your calculations seems correct to me
now is 1) supposed to return 0 or 2 ? That is a very philosophical question
just add 1 to your parameter n as the first line of code in your method if you want to shift by 1 the answer :)

:^:
Was This Post Helpful? 0
  • +
  • -

#3 xSouthpaw  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 32
  • Joined: 06-December 12

Re: Java Assignment Problem

Posted 06 December 2012 - 08:30 PM

for this assignment Fibonacci.java has to be an object class. I cannot have a main method in this class whatsoever. According to the professor the Fibonacci Sequence goes like this....1,1,2,3,5,8,13...and so on. So basically if someone were to input the number 2 when prompted by JDialog they would get the response 3 because 2+1=3.
Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8332
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Java Assignment Problem

Posted 06 December 2012 - 08:39 PM

I don't argue with your assigment justification/requirement/...

I just wrote a quick/dirty way to test/validate your method and it proved that obvioulsy it works

so now you can put all the sherries and whiped cream you want to satisfy your teacher. If the offset is not the one you want simply n++; as the first isntruction of your method. But it works
Was This Post Helpful? 0
  • +
  • -

#5 xSouthpaw  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 32
  • Joined: 06-December 12

Re: Java Assignment Problem

Posted 06 December 2012 - 09:22 PM

I see that but the output you got is wrong when i replied i showed you the output. 1,1,2,3,5,8,13 is the way it should be. When the user enters 1 you get 2 because 1+1=2 then when you put in 2 you get 3 because 1+2=3 etc....
in another format you could display it like this.
(1)1
(2)3
(3)5
(4)7
(5)8
and so on.
Was This Post Helpful? 0
  • +
  • -

#6 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8332
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Java Assignment Problem

Posted 06 December 2012 - 09:31 PM

First, it is not my outpout but YOUR output. I used YOUR method
Yes the first one is not correct

0, 2, 3, 5, 8, 13, 21
should be
1, 2, 3, 5, 8, 13, 21

so YOUR first 0 is wrong
and your serie
(1)1
(2)3
(3)5
(4)7
(5)8
is completly wrong so your are complaining about stuff you don't know anything about :v:

I just told you that 1) your first element was wrong 2) may be dur to wrong offset
beside that your method works
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1