import java.util.Scanner;
public class himpunan{
Scanner input = new Scanner(System.in);
int[] himpunanA= new int[100];
int[] himpunanB= new int[100];
int jumlahAnggotaA;
int jumlahAnggotaB;
public void dataAnggota() {
System.out.print("\nMasukkan Jumlah Anggota Himpunan A : ");
jumlahAnggotaA = input.nextInt();
System.out.println("Anggota Himpunan A");
for(int i=1;i<=jumlahAnggotaA;i++){
System.out.print("Anggota "+i+" : ");
himpunanA[i]=input.nextInt();
}
System.out.print("\nMasukkan Jumlah Anggota Himpunan B : ");
jumlahAnggotaB = input.nextInt();
System.out.println("Anggota Himpunan B");
for(int i=1;i<=jumlahAnggotaB;i++){
System.out.print("Anggota "+i+" : ");
himpunanB[i]=input.nextInt();
}
}
public void tampilAnggota(){
System.out.print("\nAnggota Himpunan A = { ");
for(int i=1;i<=jumlahAnggotaA;i++){
System.out.print(himpunanA[i]+" ");
}
System.out.println("}");
System.out.print("Anggota Himpunan B = { ");
for(int i=1;i<=jumlahAnggotaB;i++){
System.out.print(himpunanB[i]+ " ");
}
System.out.println("}");
}
public void himpunanGabungan(){
System.out.print("Himpunan Gabungan A U B : { ");
for(int i=1;i<=jumlahAnggotaA;i++){
System.out.print(himpunanA[i]+" ");
}
for( int i=1;i<=jumlahAnggotaB;i++){
boolean adaSama = false;
for(int j=1;j<=jumlahAnggotaA;j++){
if(himpunanB[i]==himpunanA[j])
adaSama=true;
}
if(adaSama==false){
System.out.print(himpunanB[i]+" ");
}
}
System.out.println("}");
}
public void himpunanIrisan(){
System.out.print("Himpunan Irisan A n B : { ");
for( int i=1;i<=jumlahAnggotaB;i++){
boolean adaSama = false;
for(int j=1;j<=jumlahAnggotaA;j++){
if(himpunanB[i]==himpunanA[j])
adaSama=true;
}
if(adaSama==true){
System.out.print(himpunanB[i]+" ");
}
}
System.out.println(" }");
}
public void himpunanSelisih(){
System.out.print("Himpunan Selisih A - B : { ");
for( int i=1;i<=jumlahAnggotaA;i++){
boolean adaSama = false;
for(int j=1;j<=jumlahAnggotaB;j++){
if(himpunanA[i]==himpunanB[j])
adaSama=true;
}
if(adaSama==false){
System.out.print(himpunanA[i]+" ");
}
}
System.out.println("}");
System.out.print("Himpunan Selisih B - A : { ");
for( int i=1;i<=jumlahAnggotaB;i++){
boolean adaSama = false;
for(int j=1;j<=jumlahAnggotaA;j++){
if(himpunanB[i]==himpunanA[j])
adaSama=true;
}
if(adaSama==false){
System.out.print(himpunanB[i]+" ");
}
}
System.out.println("}");
}
public static void main(String args[]){
himpunan bilangan = new himpunan();
bilangan.dataAnggota();
bilangan.tampilAnggota();
bilangan.himpunanGabungan();
bilangan.himpunanIrisan();
bilangan.himpunanSelisih();
}
}
Program Himpunan
Contoh program Kalkulator
import java.awt.*;
import java.awt.event.*;
public class Kalkulator extends Frame implements WindowListener, ActionListener {
TextField txtBil1, txtOpr, txtBil2, txtHasil;
Button btnHitung;
public Kalkulator(String judul) {
super(judul);
this.addWindowListener(this);
this.setLayout(new FlowLayout(FlowLayout.LEFT,10,10));
txtBil1 = new TextField();
txtBil1.setColumns(5);
txtOpr = new TextField();
txtOpr.setColumns(5);
txtBil2 = new TextField();
txtBil2.setColumns(5);
txtHasil = new TextField();
txtHasil.setColumns(5);
btnHitung = new Button(" = ");
btnHitung.addActionListener(this);
add(txtBil1);
add(txtOpr);
add(txtBil2);
add(btnHitung);
add(txtHasil);
}
public void windowActivated(WindowEvent e) {}
public void windowClosed(WindowEvent e) {}
public void windowClosing(WindowEvent e) {
setVisible(false);
System.exit(0);
}
public void windowDeactivated(WindowEvent e) {}
public void windowDeiconified(WindowEvent e) {}
public void windowIconified(WindowEvent e) {}
public void windowOpened(WindowEvent e) {}
public void actionPerformed(ActionEvent e) {
double a, b, hasil;
String c;
if (e.getSource() == btnHitung){
a = Double.parseDouble(txtBil1.getText());
b = Double.parseDouble(txtBil2.getText());
c = txtOpr.getText();
if (c.equals("*")) {
hasil = a * b;
} else if (c.equals("/")) {
hasil = a / b;
} else if (c.equals("+")) {
hasil = a + b;
} else if (c.equals("-")) {
hasil = a - b;
} else {
hasil = 0;
}
txtHasil.setText(""+hasil);
}
}
public static void main(String[] args) {
Kalkulator frm = new Kalkulator("Kalkulator Culun");
frm.setSize(500, 200);
frm.setVisible(true);
}
}
Contoh program array pada Java
public class Array2
{
public static void main(String[] args)
{
int matriks [][] = new int [4][5];
matriks [0][0]=1;
matriks [0][1]=2;
matriks [0][2]=3;
matriks [1][0]=4;
matriks [1][1]=5;
matriks [1][2]=6;
matriks [2][0]=7;
matriks [2][1]=8;
matriks [2][2]=9;
matriks [3][0]=10;
matriks [3][1]=11;
matriks [3][2]=12;
for (int i=0;i<=3;i++)
{
for (int j=0;j<=2;j++)
{
System.out.print(matriks [i][j]+" ");
}
System.out.println(" ");
}
System.out.println(" ");
int matriks2 [][] = new int [5][5];
matriks2 [0][0]=12;
matriks2 [0][1]=11;
matriks2 [0][2]=10;
matriks2 [1][0]=9;
matriks2 [1][1]=8;
matriks2 [1][2]=7;
matriks2 [2][0]=6;
matriks2 [2][1]=5;
matriks2 [2][2]=4;
matriks2 [3][0]=3;
matriks2 [3][1]=2;
matriks2 [3][2]=1;
for (int a=0;a<=3;a++)
{
for (int b=0;b<=2;b++)
{
System.out.print(matriks2 [a][b]+" ");
}
System.out.println(" ");
}
}
}
Untuk Kw Yang Jauh DI Sana
Malam bergerak semakin larut..
Saat sepi di sini temani aku lewati malam..
Merasakan segala aroma kelam dan dingin yang kian malam kian menusuk..
Saat memoriku berlari,dia tertabrak bayanganmu..
Saat berlari kembali, bayanganmu mengejar..
Aku bingung..
Tapi segera hatiku menjawab..
"Chelly, Kw merindukan Dia"........!!!!!!!!
Label: For U
Pemuda dan Sosialisasi
Label: Tugas