|
//class fibonachi(ali asadiyan) public class FIB { private double a; private double b; private double x; public double getA() {return a; } public void setA(double a) {this.a = a; } public double getB() {return b; } public void setB(double b) {this.b = b; } public double getX() {return x; } public void setX(double x) {this.x = x; } public FIB() {a=0; b=0; x=0;} public FIB(double a, double b, double x) { super(); this.a = a; this.b = b; this.x = x;} public double toFib(double a,double b,double x){ if (x<1){System. out.println("ERROR! INCORECT INPUT DATA");System.exit(0); } if (x == 1)return a; if (x == 2)return b; return toFib(b,a+b,x-1);} } |
|
//main class fibonachi(ali asadiyan) import java.util.Scanner;public class FibTest { public static void main(String[] args) {Scanner Input = new Scanner(System.in);FIB MYFIB = new FIB();System. out.print("please Enter 1st Number:"); double a = Input.nextDouble();System. out.print("please Enter 2nd Number:"); double b = Input.nextDouble();System. out.print("please Enter Arguman Number:"); double x = Input.nextDouble();System. out.println("Arguman: "+(int)x+ " of string("+(int)a+","+(int)b+","+(int)x+") = "+( int)MYFIB.toFib(a, b, x));} } |
و این هم GUIاین تابع
|
import javax.swing.*;public class FibGUI { public static void main(String[] args) {FIB MYFIB = new FIB();String Input1,Input2, Input3 ; Input1 = JOptionPane.showInputDialog( "please Enter 1st Number:"); double a = Double.parseDouble(Input1);Input2 = JOptionPane.showInputDialog( "please Enter 2nd Number:"); double b = Double.parseDouble(Input2);Input3 = JOptionPane.showInputDialog( "please Enter Arguman Number:"); double x = Double.parseDouble(Input3);JOptionPane.showMessageDialog( null,"Arguman: "+(int)x+ " of string of FIBONACHI ("+(int)a+","+(int)b+","+(int)x+") = "+( int)MYFIB.toFib(a, b, x));} } |