ihsan1

FINAL

May 18th, 2019
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.56 KB | None | 0 0
  1. package semester2;
  2. import java.io.*;
  3. import javax.swing.JOptionPane;
  4.  
  5. class Kelompok6{
  6.    
  7.    
  8.  
  9.     int front,rear;
  10.     int A[];
  11.        
  12.        
  13.            
  14.        
  15.  
  16.     public Kelompok6(){
  17.         this.A = new int[5];
  18.            
  19.          front=rear=-1;
  20.     }
  21.  
  22.    
  23.  
  24.    
  25.  
  26.     public boolean isEmpty(){
  27.  
  28.         return(front==-1);
  29.     }
  30.  
  31.     public boolean isFull(){
  32.        
  33.         return((rear+1)%A.length==front);
  34.     }
  35.  
  36.     public void enQ(int x){
  37.  
  38.         if(!isFull()){
  39.  
  40.             rear=(rear+1)%A.length;
  41.             A[rear]=x;
  42.             if(front==-1)
  43.                 front++;
  44.         }
  45.  
  46.         else{
  47.  
  48.             JOptionPane.showMessageDialog(null,"Queue Penuh","Error",JOptionPane.INFORMATION_MESSAGE);
  49.  
  50.         }
  51.     }
  52.  
  53.  
  54.     public int  deQ(){
  55.  
  56.         int temp=-1;
  57.  
  58.         if(!isEmpty()){
  59.  
  60.             temp=A[front];
  61.             if(rear==front)
  62.                 rear=front=-1;
  63.             else
  64.                 front=(front+1)%A.length;
  65.         }
  66.  
  67.         else
  68.  
  69.             JOptionPane.showMessageDialog(null,"Queue Kosong","Error",JOptionPane.INFORMATION_MESSAGE);
  70.         return temp;
  71.     }
  72.  
  73.  
  74.     public void display(){
  75.  
  76.         StringBuffer str=new StringBuffer(30);
  77.  
  78.         if(!isEmpty()){
  79.  
  80.             if(front<=rear)
  81.  
  82.                 for(int i=0;i<=rear;i++)
  83.                     str.append(A[i]).append(" ");
  84.             else{
  85.  
  86.                 for(int i=front;i<A.length;i++)
  87.                     str.append(A[i]).append(" ");
  88.    
  89.                 for(int i=0;i<=rear;i++)
  90.                     str.append(A[i]).append(" ");
  91.             }
  92.             JOptionPane.showMessageDialog(null,str,"Isi",JOptionPane.INFORMATION_MESSAGE);
  93.  
  94.         }
  95.  
  96.         else
  97.  
  98.             JOptionPane.showMessageDialog(null,"Queue Kosong","Error",JOptionPane.INFORMATION_MESSAGE);
  99.     }
  100. }
  101.  
  102.  
  103.  
  104. class CirMain{
  105.  
  106.         public static void main (String[] args) {
  107.            
  108.             String msg = null;
  109.             Kelompok6 C= new Kelompok6();
  110.             int pilihan=0,N;
  111.            
  112.             do{
  113.                             msg="Menu Circular Array\n1.Enqueue\n2.Dequeue\n3.Tampilkan\n4.Keluar\n\nMasukan Pilihan";
  114.                 pilihan=Integer.parseInt(JOptionPane.showInputDialog(msg));
  115.                                
  116.                 switch(pilihan){
  117.                    
  118.                     case 1:
  119.                        
  120.                         N=Integer.parseInt(JOptionPane.showInputDialog("Masukan angka"));
  121.                         C.enQ(N);
  122.                         break;
  123.                        
  124.                     case 2:
  125.                        
  126.                         N=C.deQ();
  127.                         JOptionPane.showMessageDialog(null,N+" Telah dihapus ","Hapus",JOptionPane.INFORMATION_MESSAGE);
  128.                         break;
  129.                        
  130.                     case 3:
  131.                        
  132.                         C.display();
  133.                         break;
  134.                        
  135.                     case 4:
  136.                        
  137.                         JOptionPane.showMessageDialog(null,"Program akan tertutup","Oke",JOptionPane.INFORMATION_MESSAGE);
  138.                         break;
  139.                        
  140.                     default:
  141.                        
  142.                         JOptionPane.showMessageDialog(null,"Mohon masukan pilihan yang valid","Gagal",JOptionPane.ERROR_MESSAGE);
  143.                 }
  144.             }while(pilihan!=4);
  145.  
  146.         }
  147. }
Advertisement
Add Comment
Please, Sign In to add comment