Difference between revisions of "Fibonacci sequence"
m |
(Added quotations to student quotes) |
||
(22 intermediate revisions by 3 users not shown) | |||
Line 6: | Line 6: | ||
*prompts the user to enter an input number and casts it to integer | *prompts the user to enter an input number and casts it to integer | ||
*gets the n<sup>th</sup> fibonacci number using a <B>for</B> loop, based on the user's input | *gets the n<sup>th</sup> fibonacci number using a <B>for</B> loop, based on the user's input | ||
− | *prints out the n<sup>th</sup> fibonacci number, using [[JOptionPane_Methods|JOptionPane | + | *prints out the n<sup>th</sup> fibonacci number, using [[JOptionPane_Methods|JOptionPane]] method. |
<br> | <br> | ||
Note that the fibonacci sequence is <math>F(0) = 1, F(1) = 1, F(n) = F(n-1) + F(n-2) for n > 1</math>.<br> | Note that the fibonacci sequence is <math>F(0) = 1, F(1) = 1, F(n) = F(n-1) + F(n-2) for n > 1</math>.<br> | ||
Line 15: | Line 15: | ||
You may assume that the user always enters correct integer format. | You may assume that the user always enters correct integer format. | ||
− | |SideSectionTitle= | + | |SideSectionTitle=...By students |
|SideSection= | |SideSection= | ||
− | + | "Before taking Comp 1010 course, I didn't have any programming experiences. I didn't know much and I had many problems with my assignments. Now that I have gained some experience, I'd like to share some tips that helped me when doing programming assignments. <br> | |
+ | <br> | ||
+ | First, always write comments. This may depend on each student's skill, but most students won't finish their code or assignments in few hours. The reason is because when you work on it later, you are very likely to forget what you were doing. Writing comments will help you remember what you were doing and it will be easier for you to fix errors when going through your code.<br> | ||
+ | Second, take a break if you are stuck. If you are mentally tired and cannot figure a way out then take a break, otherwise you may mess up with the parts that were actually correct. This will cause you to extremely stressed out and you will most likely <i>NOT</i> finish your assignment.<br> | ||
+ | Third, start assignments ahead of time. I used to start assignments a day before and I had a lot of trouble finishing them properly. Even if the first year assignments are easy, they still take up some time. Besides, if you start taking second year computer science courses, you will NEVER finish a programming assignment in one day.<br> <br> | ||
+ | These tips always helped me doing assignments. As you experience more and more in programming, you will find more tips that are more specific to your case." | ||
|SolutionCode= | |SolutionCode= | ||
Line 45: | Line 50: | ||
|Solution= | |Solution= | ||
− | In order to get user input from JOptionPane you will need to import the swing package. | + | In order to get user input from <b>''JOptionPane''</b> you will need to import the swing package.<br> |
− | You can import swing package by | + | You can import swing package by<br> |
− | + | <br> | |
− | < | + | {{CodeBlock |
+ | |Code= | ||
import javax.swing.*; | import javax.swing.*; | ||
− | + | }} | |
or | or | ||
− | + | {{CodeBlock | |
+ | |Code= | ||
import javax.swing.JOptionPane; | import javax.swing.JOptionPane; | ||
− | < | + | }} |
− | + | <br> | |
− | The next step is declaring variables. In this program we will need three | + | The next step is declaring variables. <br> |
− | Once you declare the necessary variables, the last variable will be the casted integer from the user input. | + | In this program we will need three integer variables. <br> |
− | + | One variable will be used as <math>F(n)</math> and another variable will be used as <math>F(n-1)</math>. <br> | |
− | < | + | Our initial value for <math>F(n)</math> will be 1 and <math>F(n-1)</math> will be 0, referring to the fibonacci formula. <br> |
+ | <b><math>F(0) = 1, F(1) = 1</math></b><br><br> | ||
+ | Once you declare the necessary variables, the last variable will be the casted integer from the user input.<br> | ||
+ | <br> | ||
+ | {{CodeBlock | ||
+ | |Code= | ||
int n = 1 | int n = 1 | ||
int n-1 = 0 | int n-1 = 0 | ||
String str = get the user input using JOptionPane | String str = get the user input using JOptionPane | ||
int input = cast str to integer | int input = cast str to integer | ||
− | < | + | }} |
− | + | <br> | |
− | Once you have all the variables ready, make a for loop that starts from 0 and ends when the counter is less than n (user input). In this for loop, the increment will be 1. | + | Once you have all the variables ready, make a for loop that starts from 0 and ends when the counter is less than n (user input). <br> |
− | + | In this for loop, the increment will be 1.<br> | |
− | < | + | <br> |
+ | {{CodeBlock | ||
+ | |Code= | ||
for(count = 0; count < user input; count increment by 1) | for(count = 0; count < user input; count increment by 1) | ||
{ | { | ||
} | } | ||
− | < | + | }} |
− | + | <br> | |
− | We will need another variable to keep track of <math>F(n-2)</math>. You simply set it <math>F(n-2) = F(n-1)</math> as it starts a new iteration, and set <math>F(n-1) = F(n)</math>. Then sum up the <math>F(n-2) + F(n-1)</math> which will now be <math>F(n)</math>. | + | We will need another variable to keep track of <math>F(n-2)</math>. <br> |
− | + | You simply set it <math>F(n-2) = F(n-1)</math> as it starts a new iteration, and set <math>F(n-1) = F(n)</math>. <br> | |
− | <math>(n-2) = (n-1)</math> | + | Then sum up the <math>F(n-2) + F(n-1)</math> which will now be <math>F(n)</math>. <br> |
− | + | <br> | |
− | <math>(n-1) = n</math> | + | <math>(n-2) = (n-1)</math><br> |
− | + | <br> | |
− | <math>n = (n-2) + (n-1)</math> | + | <math>(n-1) = n</math><br> |
− | + | <br> | |
− | When the for loop ends, you will have the n<sup>th</sup> fibonacci number. At last, print it out using JOptionPane. When you are printing the result out with JOptionPane, note that you have to put 'null' before the actual message you want to print out. See below for the solution code.<br> | + | <math>n = (n-2) + (n-1)</math><br> |
+ | <br> | ||
+ | When the for loop ends, you will have the n<sup>th</sup> fibonacci number. <br> | ||
+ | At last, print it out using <b>''JOptionPane''</b>.<br> | ||
+ | <br> | ||
+ | {{CodeBlock | ||
+ | |Code= | ||
+ | JOptionPane.showMessageDialog(null, "The " + n + "th Fibonacci number is " + current); | ||
+ | }} | ||
+ | <br> | ||
+ | When you are printing the result out with JOptionPane, note that you have to put <b>'null'</b> before the actual message you want to print out. <br> | ||
+ | <br> | ||
+ | This problem was the review of 'While and For Loops'.<br> | ||
+ | See below for the solution code.<br> | ||
}} | }} |
Latest revision as of 15:28, 8 December 2011
Back to the Program-A-Day homepage
ProblemWrite a complete Java program titled Fibonacci that:
|
...By students"Before taking Comp 1010 course, I didn't have any programming experiences. I didn't know much and I had many problems with my assignments. Now that I have gained some experience, I'd like to share some tips that helped me when doing programming assignments. | |
---|---|---|
SolutionIn order to get user input from JOptionPane you will need to import the swing package. import javax.swing.*; or import javax.swing.JOptionPane;
int n = 1 int n-1 = 0 String str = get the user input using JOptionPane int input = cast str to integer
for(count = 0; count < user input; count increment by 1) { }
JOptionPane.showMessageDialog(null, "The " + n + "th Fibonacci number is " + current);
| ||
Code |