Cetak Fibonacci pewarisan dan polymorfisme


import java.io.*;

class itung
{    public void itung(int n)
{    if (n==1)
{    System.out.println(“1, “);
}
else if(n==2)
{    System.out.println(“1, 1, “);
}

else
{
int n0=1,n1=1,n2;
System.out.print(n0+”, “+n1+”, “);
for(int i=0;i<=n-3;i++)
{
n2=n1+n0;
System.out.print(n2+”, “);

n0=n1;
n1=n2;
}    System.out.println();
}
}
}

class itung2 extends itung
{    public void itung(int n)
{    for (int a=1; a<=n; a++)
{
if (a==1)
{    System.out.println(“1, “);
}
else if(a==2)
{    System.out.println(“1, 1, “);
}
else
{    int n0=1,n1=1,n2;
System.out.print(n0+”, “+n1+”, “);
for(int i=0;i<=a-3;i++)
{
n2=n1+n0;
System.out.print(n2+”, “);

n0=n1;
n1=n2;
}    System.out.println();
}
}
}
}

class fibo
{    public static void main (String []args)throws Exception
{
System.out.println(“================================================”);
System.out.println(” NAMA    : M I K A   U S T A D H I “);
System.out.println(” NPM    : 06.2008.1.04435 “);
System.out.println(“================================================”);
System.out.println();
DataInputStream nama=new DataInputStream(System.in);
System.out.print(” Masukan angka    = “);
int n=Integer.parseInt(nama.readLine());

itung nilai;

itung horisontal=new itung();
nilai=horisontal;
nilai.itung(n);

itung2 vertikal=new itung2();
nilai=vertikal;
nilai.itung(n);
}
}

Tinggalkan Komentar

Isikan data di bawah atau klik salah satu ikon untuk log in:

Logo WordPress.com

You are commenting using your WordPress.com account. Logout / Ubah )

Gambar Twitter

You are commenting using your Twitter account. Logout / Ubah )

Foto Facebook

You are commenting using your Facebook account. Logout / Ubah )

Foto Google+

You are commenting using your Google+ account. Logout / Ubah )

Connecting to %s