Tuesday, May 6, 2014

Stack dan Queue menggunakan Joption Pane dan BufferedReader pada java netbeans

Selamat malam menuju pagi sahabat kreatif, kali ini saya menampilkan tentang Stack menggunakan Joption Pane dan BufferedReader dan Queue menggunakan Joption Pane dan BufferedReader pada java netbeans. bukan sombong atau merasa sok pinter sih, saya hanya memodifikasi dari modul pelajaran saya ke bentuk yang lebih bagus dilihat yaitu JOption Pane dan Buffered Reader. tanpa banyak bercerita panjang lebar keluar kedalam, yuk langsung aja cermati codingangannya atau sourcodnya...

Seperti yang kita ketahui bahwa stack menggunakan sistem LIFO dimana data yang terakhir masuk, maka data tersebut yang terakhir keluar, sedangkan Queue menggunakan konsep FIFO diamana pada saat data yang pertama kali masuk, maka data tersebut yang pertama kali keluar.

Stack

class tumpukan

/*
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package Stack;

/**
*
* @author MOHAMMAD RAMLI
* nim 1310651201
* kelas C
*/
public class tumpukan {

private int ukuran;
private long[] tumpukan;
private int top;

public tumpukan(int s) {
ukuran = s;
tumpukan = new long[ukuran];
top = -1;
}

public void push(long j) {
tumpukan[++top] = j;
}

public long pop() {
return tumpukan[top--];
}

public long peek() {
return tumpukan[top];
}

public boolean isEmpty() {
return (top == -1);
}

public boolean isFull() {
return (top == ukuran - 1);
}

public void baca() {
int i = top;
while (i >= 0) {
System.out.print(tumpukan[i]);
System.out.print(" ");
i--;
}
System.out.println(" ");
}
}

class tumpukan main dgn JOptionPane

/*
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package Stack;

import javax.swing.JOptionPane;

/**
*
* @author MOHAMMAD RAMLI
* nim 1310651201
* kelas C
*/
public class tumpukananMain_joptionpane {

public static void main(String[] args) {
tumpukan Stack = new tumpukan(10);
int satu = Integer.parseInt(JOptionPane.showInputDialog("masukkan data satu"));
Stack.push(satu);
Stack.baca();
int dua = Integer.parseInt(JOptionPane.showInputDialog("masukkan data dua"));
Stack.push(dua);
Stack.baca();
int tiga = Integer.parseInt(JOptionPane.showInputDialog("masukkan data tiga"));
Stack.push(tiga);
Stack.baca();
int empat = Integer.parseInt(JOptionPane.showInputDialog("masukkan data empat"));
Stack.push(empat);
Stack.baca();

System.out.println("data keluar");
while (!Stack.isEmpty()) {

long nilai = Stack.pop();
JOptionPane.showMessageDialog(null, "Data keluar : " + nilai);
System.out.print(nilai + " ");

}
System.out.println();
}
}
hasil output stack joptionpane


class tumpukan main dgn menggunakan Buffered Reader

/*
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package Stack;

import java.io.BufferedReader;
import java.io.InputStreamReader;

/**
*
* @author MOHAMMAD RAMLI
* nim 1310651201
* kelas C
*/
public class tumpukanMain_BufferedReader {

public static void main(String[] args) {
tumpukan stack = new tumpukan(10);
BufferedReader dataIn = new BufferedReader(new InputStreamReader(System.in));
try {
System.out.println("Tumpukan List");
System.out.println("Masukkan data satu");
int satu = Integer.parseInt(dataIn.readLine());
stack.push(satu);
stack.baca();
System.out.println("Masukkan data dua");
int dua = Integer.parseInt(dataIn.readLine());
stack.push(dua);
stack.baca();
System.out.println("Masukkan data tiga");
int tiga = Integer.parseInt(dataIn.readLine());
stack.push(tiga);
stack.baca();
System.out.println("Masukkan data empat");
int empat = Integer.parseInt(dataIn.readLine());
stack.push(empat);
stack.baca();

System.out.println("Data keluar");
while (!stack.isEmpty()) {
long nilai = stack.pop();
System.out.print(nilai);
System.out.print(" ");

}
System.out.println();

} catch (Exception e) {
}
}
}



hasil output Stack dgn buffered reader




Queue
class queue
/*
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package Queue;

/**
*
* @author MOHAMMAD RAMLI
* nim 1310651201
* kelas C
*/
public class Queue {

private int maxSize;
private long[] queArray;
private int front;
private int rear;
private int nItems;

public Queue(int s) {
maxSize = s;
queArray = new long[maxSize];
front = 0;
rear = -1;
nItems = 0;
}

public void insert(long j) {
if (rear == maxSize - 1) {
rear = -1;
}
queArray[++rear] = j;
nItems++;
}

public long remove() {
long temp = queArray[front++];
if (front == maxSize) {
front = 0;
}
nItems--;
return temp;
}

public long peekFront() {
return queArray[front];
}

public boolean isEmpty() {
return (nItems == 0);
}

public boolean isFull() {
return (nItems == 0);
}

public int size() {
return nItems;
}

public void lihat() {
for (int i = 0; i < nItems; i++) {
System.out.print(queArray[i] + " ");

}
System.out.println(" ");
}
}



Class Queue Main dgn JOptionPane
/*
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package Queue;

import javax.swing.JOptionPane;

/**
*
* @author MOHAMMAD RAMLI
* nim 1310651201
* kelas C
*/
public class QueueMain_JOptionPane {
public static void main(String[] args) {
Queue theQueue = new Queue(4);

int satu = Integer.parseInt(JOptionPane.showInputDialog("masukkan data satu : "));
theQueue.insert(satu);
theQueue.lihat();
int dua = Integer.parseInt(JOptionPane.showInputDialog("masukkan data dua : "));
theQueue.insert(dua);
theQueue.lihat();
int tiga = Integer.parseInt(JOptionPane.showInputDialog("masukkan data tiga : "));
theQueue.insert(tiga);
theQueue.lihat();
int empat = Integer.parseInt(JOptionPane.showInputDialog("masukkan data empat : "));
theQueue.insert(empat);
theQueue.lihat();

System.out.println("Data Keluar : ");
while (!theQueue.isEmpty()) {
long nilai = theQueue.remove();
JOptionPane.showMessageDialog(null, "Data keluar : " + nilai);
System.out.print(nilai + " ");
}
System.out.println();
}
}

Output Queue dgn JOptionPane



Class Queue Main dgn BufferedReader
/*
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package Queue;

import java.io.BufferedReader;
import java.io.InputStreamReader;

/**
*
* @author MOHAMMAD RAMLI
* nim 1310651201
* kelas C
*/
public class QueueMain_BufferedReader {

public static void main(String[] args) {
Queue theQueue = new Queue(4);
BufferedReader dataIn = new BufferedReader(new InputStreamReader(System.in));
try {
System.out.println("Antrian");
System.out.println("Masukkan data satu :");
int satu = Integer.parseInt(dataIn.readLine());
theQueue.insert(satu);
theQueue.lihat();
System.out.println("Masukkan data dua :");
int dua = Integer.parseInt(dataIn.readLine());
theQueue.insert(dua);
theQueue.lihat();
System.out.println("Masukkan data tiga :");
int tiga = Integer.parseInt(dataIn.readLine());
theQueue.insert(tiga);
theQueue.lihat();
System.out.println("Masukkan data empat :");
int empat = Integer.parseInt(dataIn.readLine());
theQueue.insert(empat);
theQueue.lihat();

System.out.println("Data Keluar : ");
while (!theQueue.isEmpty()) {
long nilai = theQueue.remove();
System.out.print(nilai);
System.out.print(" ");
}
System.out.println(" ");
} catch (Exception e) {
}

}
}
Output Queue dgn BufferedReader

No comments:
Write komentar

Terimakasih Atas Kunjungan Anda..
Kritik dan Saran Anda membantu blog ini lebih baik..