Berikut ini adalah sebuah program java utntuk menghitung " RELASI HIMPUNA " .
untuk mudahnya kamu gunkan aja EditPlus , mungkin juga NetBeans 6.1
SILAHKAN DICOBA @,@
import java.io.*;
class relasihimpunan2
{
private static BufferedReader input=new BufferedReader (new InputStreamReader(System.in));
public static void main (String[] args) throws Exception
{
System.out.print("masukkkan banyak himpunan A:");
int x=Integer.parseInt(input.readLine());
int himpA []=new int [x];
for(int i=0;i<x;i++)
{
System.out.print("masukkan elemen A ke-"+(i+1)+" :");
String a=input.readLine();
himpA[i]=Integer.parseInt(a);
}
System.out.print("A={");
for(int i=0;i<x;i++)
{
System.out.print(himpA[i]);
if(i!=x-1)
System.out.print(",");
}
System.out.println("}");
System.out.println();
System.out.println("Jenis Relasi");
System.out.println("=========================");
System.out.println("1. refleksi sederhana");
System.out.println("2. simestris sederhana");
System.out.println("3. transitif sederhana");
System.out.println("=========================");
System.out.print("Masukkan Jenis Relasi Yang Diinginkan :");
String b=input.readLine();
int pil=Integer.parseInt(b);
System.out.println();
switch (pil)
{
case 1:
System.out.println("Relasi R {");
for (int i=0;i<x;i++)
{
for (int j=0;j<x;j++)
{
if (himpA[i]==himpA [j])
{
System.out.print("{"+himpA[i]+","+himpA[j]+"}");
if (j!=x-1)
System.out.print(",");
}
}
}
System.out.println("}");
System.out.println();
break;
case 2:
System.out.println("Relasi R {");
for (int i=0;i<x;i++)
{
for (int j=0;j<x;j++)
{
if (himpA[i]==himpA [i])
{
System.out.print("{"+himpA[i]+","+himpA[j]+"}");
if (i!=x-1)
System.out.print(",");
}
}
}
System.out.println("}");
System.out.println();
break;
case 3:
System.out.print("Relasi R {");
for (int i=0; i<x;i++)
{
for (int j=0; j<x;j++)
{
if (himpA[i] !=himpA[j])
{
if (i%2!=0)
{
if (j%2==0)
{
System.out.print("("+himpA[i]+","+himpA[j]+")");
if (j!=x-1)
System.out.print(",");
}
else
{
if (j%2!=0)
{
System.out.print("("+himpA[i]+","+himpA[j]+")");
if (j!=x-1)
System.out.print(",");
}
}
}
}
}
Tidak ada komentar:
Posting Komentar