Advertisement
PedroPauloFO

Fibonacci

Oct 30th, 2014
23
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.19 KB | None | 0 0
  1. /* Aluno: Pedro Paulo Freire Oliveira */
  2.  
  3. package lab04;
  4.  
  5. public class Fibonacci {
  6.         int termoatual = 0;
  7.         int termoanterior = 1;
  8.         double termo, termoant;
  9.        
  10.         public Fibonacci(){
  11.                 this.termoatual = 0;
  12.                 this.termoanterior = 1;
  13.         }
  14.        
  15.         public int proximo(){
  16.                 int temp = 0;
  17.                 temp = this.termoanterior;
  18.                 this.termoanterior = this.termoatual;
  19.                 this.termoatual += temp;
  20.                
  21.                 return this.termoatual;
  22.         }
  23.        
  24.         public int termo(int n){
  25.                 this.termoatual = 0;
  26.                 this.termoanterior = 1;
  27.                 if (n <= 0) return 1;
  28.                 for (int i =0; i < n; i++)
  29.                     this.termoatual = proximo();
  30.                
  31.                 return this.termoatual;
  32.         }
  33.        
  34.         public java.lang.String geraTermos(int n){
  35.                 String termos = "[";
  36.                 this.termoatual = 0;
  37.                 this.termoanterior = 1;
  38.                 for (int i =0; i < n; i++){
  39.                     termos += proximo();
  40.                     if (i < n - 1) termos += ", ";
  41.                 }
  42.                 termos += "]";
  43.    
  44.                 return termos;
  45.         }
  46.  
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement