Minggu, 30 September 2018

Jam DIgital

Tugas PBO B - Jam Digital

Nama   : Yasinta Yusniawati
Kelas    : PBO-B
NRP     : 05111740000054


Untuk membuat jam digital di perlukan beberapa class yaitu :
  1. NumberDisplay
  2. ClockDisplay
  3. TestClockDisplay 
  4. Clock (GUI)

Source Code dari masing-masing class :


1. NumberDisplay

/**  
  * Write a description of class NumberDisplay here.  
  *  
  * @author Yasinta Yusniawati 
  * @version 1.0  
  */   
 public class NumberDisplay  
 {  
   private int limit;  
   private int value;  
   public NumberDisplay(int rollOverLimit)  
   {  
     limit = rollOverLimit;  
     value = 0;  
   }  
   public int getValue()  
   {  
     return value;  
   }  
   public void setValue(int replacementValue)  
   {  
     if((replacementValue >= 0) && (replacementValue < limit)) {  
           value = replacementValue;  
     }  
   }  
   public String getDisplayValue()  
   {  
     if(value < 10) {  
       return "0" +value ;  
     }  
     else {  
       return "" +value ;  
     }  
   }  
   public void increment()  
   {  
     value = (value + 1) % limit;  
   }  
 }  

2. ClockDisplay

/**  
  * Write a description of class ClockDisplay here.  
  *  
  * @author Yasinta Yusniawati  
  * @version 1.0  
  */   
 public class ClockDisplay  
 {  
   private NumberDisplay hours;  
   private NumberDisplay minutes;  
   private String displayString; //simulates the actual display  
   public ClockDisplay()  
   {  
     hours = new NumberDisplay(24);  
     minutes = new NumberDisplay(60);  
     updateDisplay();  
   }  
   public ClockDisplay(int hour, int minute)  
   {  
     hours = new NumberDisplay(12);  
     minutes = new NumberDisplay(60);  
     setTime(hour, minute);  
   }  
   public void timeTick()  
   {  
     minutes.increment();  
     if(minutes.getValue() == 0) { //it just rolled over  
       hours.increment();  
     }  
     updateDisplay();  
   }  
   public void setTime(int hour, int minute)  
   {  
     hours.setValue(hour);  
     minutes.setValue(minute);  
     updateDisplay();  
   }  
   public String getTime()  
   {  
     return displayString;  
   }  
   private void updateDisplay()  
   {  
     displayString = hours.getDisplayValue() + ":" +  
             minutes.getDisplayValue();  
   }  
 }  

3. TestClockDisplay

/**  
  * Write a description of class TestClockDisplay here.  
  *  
  * @author Yasinta Yusniawati  
  * @version 1.0  
  */   
 public class TestClockDisplay  
 {  
   public void test()  
   {  
     ClockDisplay clock = new ClockDisplay();  
     clock.setTime(0,0);  
     System.out.println(clock.getTime());  
     clock.setTime(12,00);  
     System.out.println(clock.getTime());  
     clock.setTime(18,45);  
     System.out.println(clock.getTime());  
     clock.setTime(21,16);  
     System.out.println(clock.getTime());  
   }  
 }  


4. ClockGUI

/**  
  * Write a description of class ClockGUI here  
  *  
  * @author Yasinta Yusniawati  
  * @version 1.0  
  */   
  import javax.swing.*;  
  import javax.swing.border.*;  
  import java.awt.*;  
  import java.awt.event.*;  
  public class ClockGUI   
  {   
   private JFrame frame;   
   private JLabel label;   
   private ClockDisplay clock;   
   private boolean clockOn = false;   
   private TimerThread timerThread;   
   public void Clock()   
   {   
    makeFrame();   
    clock = new ClockDisplay();   
   }   
   private void start()   
   {   
    clockOn = true;   
    timerThread = new TimerThread();   
    timerThread.start();   
   }   
   private void stop()   
   {   
    clockOn = false;   
   }   
   private void step()   
   {   
    clock.timeTick();   
    label.setText(clock.getTime());   
   }   
   private void showTentang()   
   {   
    JOptionPane.showMessageDialog (frame, "Jam Digital\n" +    
    "Jam digital dengan Bahasa Java.",   
    "Tentang",  
    JOptionPane.INFORMATION_MESSAGE);   
   }   
   private void quit()   
   {   
    System.exit(0);   
   }   
   private void makeFrame()   
   {   
    frame = new JFrame("Jam");   
    JPanel contentPane = (JPanel)frame.getContentPane();   
    contentPane.setBorder(new EmptyBorder(1,60,1,60));   
    makeMenuBar(frame);   
    contentPane.setLayout(new BorderLayout(12,12));   
    label = new JLabel("00:00", SwingConstants.CENTER);   
    Font displayFont = label.getFont().deriveFont(96.0f);   
    label.setFont(displayFont);   
    contentPane.add(label, BorderLayout.CENTER);   
    JPanel toolbar = new JPanel();   
    toolbar.setLayout(new GridLayout(1,0));   
    JButton startButton = new JButton("Start");   
    startButton.addActionListener(e->start());   
    toolbar.add(startButton);   
    JButton stopButton = new JButton("Stop");   
    stopButton.addActionListener(e->stop());   
    toolbar.add(stopButton);   
    JButton stepButton = new JButton("Step");   
    stepButton.addActionListener(e->step());   
    toolbar.add(stepButton);   
    JPanel flow = new JPanel();   
    flow.add(toolbar);   
    contentPane.add(flow, BorderLayout.SOUTH);   
    frame.pack();   
    Dimension d = Toolkit.getDefaultToolkit().getScreenSize();   
    frame.setLocation(d.width/2 - frame.getWidth()/2, d.height/2 - frame.getHeight()/2);   
    frame.setVisible(true);   
   }   
   private void makeMenuBar(JFrame frame)   
   {   
    final int SHORTCUT_MASK =    
    Toolkit.getDefaultToolkit().getMenuShortcutKeyMask();   
    JMenuBar menubar = new JMenuBar();   
    frame.setJMenuBar(menubar);   
    JMenu menu;   
    JMenuItem item;   
    menu = new JMenu("File");   
    menubar.add(menu);   
    item = new JMenuItem("Tentang Jam...");   
     item.addActionListener(e->showTentang());   
    menu.add(item);   
    menu.addSeparator();   
    item = new JMenuItem("Quit");   
     item.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Q,SHORTCUT_MASK));   
     item.addActionListener(e->quit());   
    menu.add(item);   
   }   
   class TimerThread extends Thread   
   {   
    public void run()   
     {   
      while(clockOn)   
      {   
       step();   
       tunda();   
      }   
     }   
     private void tunda()   
     {   
      try    
      {   
       Thread.sleep(900);   
      }   
      catch(InterruptedException exc)   
      {   
      }   
    }   
   }   
  }

Ketika TestClockDisplay dijalankan akan muncul seperti ini :


Saat class ClockGUI di run, ada bebeapa tombol. Tombol start untuk memulai jam dan stop untuk menghentikan jam tombol set digunakan untuk melakukan increment pada detik jam secara manual. Secara default jam menunjukkan pukul 00.00 ketika tombol start belum di klik. berikut gambarnya :


Ketika tombol start di klik, detik dalam jam akan berjalan dan ketika di klik tombol stop jam akan berhenti








Tidak ada komentar:

Posting Komentar

Tugas APSI - C

USE CASE Analisa Use Case adalah teknik yang digunakan untuk mengidentifikasi kebutuhan sistem perangkat lunak dengan menggambarkan akt...