Tugas #PBOA-tugas 5 (Jam)

Interaksi Objek

Membuat Jam


Untuk membuat jam diperlukan tiga class, yaitu :

  1. NumberDisplay
  2. ClockDisplay
  3. TestClockDisplay

Berikut adalah souce code yang digunakan :
  1. NumberDisplay

  2.  /*  
     /*   
      * @author Nitama Nurlingga Yotifa  
      * @version 20180939  
     */  
     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;  
       }  
     }  
    

  3. ClockDisplay

  4.  /*  
     /*   
      * @author Nitama Nurlingga Yotifa  
      * @version 20180939  
     */  
     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(24);  
         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()  
       {  
         int hour = hours.getValue();  
         String suffix;  
         if(hour >= 12){  
           suffix = "pm";  
         }  
         else{  
           suffix = "am";  
         }  
         if(hour >= 12){  
           hour-=12;  
         }  
         if(hour == 0){  
           hour = 12;  
         }  
         displayString = hour + "." +  
                 minutes.getDisplayValue() + suffix;  
       }  
     }  
    

  5. TestClockDisplay

  6.  /**  
      * @author (Nitama Nurlingga Yotifa)  
      * @version (20180930)  
      */  
     public class TestClockDisplay  
     {  
       public TestClockDisplay()  
       {  
       }  
       public void test()  
       {  
         ClockDisplay clock = new ClockDisplay();  
         clock.setTime(22, 30);  
         System.out.println(clock.getTime());  
         clock.setTime(10, 30);  
         System.out.println(clock.getTime());  
         clock.setTime(00, 00);  
         System.out.println(clock.getTime());  
         clock.setTime(18, 00);  
         System.out.println(clock.getTime());  
       }  
     }  
    
Hasil dari program di atas:




Untuk membuat GUI clock display diperlukan dua class, yaitu :
  1. clock
  2. Executor
Berikut adalah souce code yang digunakan :

  1. clock

  2.  /*  
      *  
      * @author (Nitama Nurlingga Yotifa)  
      * @version (20180930)  
      */  
     import javax.swing.*;  
     import java.awt.*;  
     import java.awt.event.*;  
     import java.util.Calendar;  
     public class clock extends JFrame  
     {  
       private static final long serialVersionUID = 1L;  
       JTextField timeF;  
       JPanel panel;  
       public clock()  
       {  
         super("Java Clock by Nitama Nurlingga Yotifa");  
         setSize(225, 200);  
         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
         setVisible(true);  
         setResizable(true);  
         setLocationRelativeTo(null);  
         panel = new JPanel();  
         panel.setLayout(new FlowLayout());  
         timeF = new JTextField(10);  
         timeF.setEditable(false);  
         timeF.setFont(new Font("Arial",Font.PLAIN,48));  
         panel.add(timeF);  
         add(panel);  
         Timer t = new Timer(1000,new Listener());  
         t.start();  
       }  
       class Listener implements ActionListener  
       {  
         public void actionPerformed(ActionEvent e)  
         {  
           Calendar rightNow = Calendar.getInstance();  
           int hour = rightNow.get(Calendar.HOUR_OF_DAY);  
           int min = rightNow.get(Calendar.MINUTE);  
           int sec = rightNow.get(Calendar.SECOND);  
           timeF.setText(hour+":"+min+":"+sec);  
         }  
       }  
     }  
    

  3. Executor

  4.  /**  
      * Write a description of class Executor here.  
      *  
      * @author (Nitama Nurlingga Yotifa)  
      * @version (20180930)  
      */  
     public class Executor  
     {  
       public static void main (String[]args)  
       {  
         new Executor();  
       }  
       public Executor()  
       {  
         new clock();  
       }  
     }  
    

Berikut adalah hasil dari program diatas :


Komentar

Postingan populer dari blog ini

[Pemprograman Berbasis KK] Tugas 3

Tugas #PBOA-tugas 6 (Auction)

Tugas #PBOA-tugas 9 (The foxes and Rabbits Simulator)