Pangil_aku_Andi

Don't Give Up in Life …

Interface Random


//Superclass random
class bentuk {
public void gambar()
{
System.out.println(“Menggambar”);
}
public void hapus()
{
System.out.println(“Menghapus Gambar”);
}
}
class lingkaran extends bentuk {
public void gambar()
{
System.out.println(“Menggambar lingkaran”);
}
public void hapus()
{
System.out.println(“Menghapus lingkaran”);
}
}
class elips extends bentuk {
public void gambar()
{
System.out.println(“Menggambar Elips”);
}
public void hapus()
{
System.out.println(“Menghapus Elips”);
}
}
class segitiga extends bentuk {
public void gambar()
{
System.out.println(“Mengggambar segitiga”);
}

public void hapus()
{
System.out.println(“Menghapus segitiga”);
}
}
class Random
{
private java.util.Random rand=new java.util.Random();
public bentuk next()
{
switch(rand.nextInt(3))
{
default: case 0:return new lingkaran();
case 1:return new elips();
case 2:return new segitiga();
}
}
}

class latihan3 {
private static Random gen=new
Random();
public static void main(String[]args)
{
bentuk[]bangun=new bentuk[3];
for(int i=0;i<bangun.length;i++)
bangun[i]=gen.next();
for(int i=0;i<bangun.length;i++)
bangun[i].gambar();
for(int i=0;i<bangun.length;i++)
bangun[i].hapus();
}
}

//Save dengan Format latihan3.java

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: