Thursday, November 17, 2016

Jam : Menampilkan jam pada Java program

Dibawah ini adalah program untuk menampilkan jam pada bahasa Java.

dibawah ada 3 class yang perlu dalam program jam java !!


Yang pertama adalah kelas numberofdisplay.

DATA HOSTED WITH ♥ BY PASTEBIN.COM - DOWNLOAD RAW - SEE ORIGINAL
  1. public class NumberDisplay
  2. {
  3.     private int limit;
  4.     private int value;
  5.    
  6.     public NumberDisplay(int rollOverLimit)
  7.     {
  8.         limit = rollOverLimit;
  9.         value = 0;
  10.     }
  11.     public int getValue()
  12.     {
  13.         return value;
  14.     }
  15.    
  16.     public void setValue(int replacementValue)
  17.     {
  18.         if((replacementValue >= 0) && (replacementValue < limit))
  19.         {
  20.             value = replacementValue;
  21.         }
  22.     }
  23.    
  24.     public String getDisplayValue()
  25.     {
  26.         if(value < 10)
  27.         {
  28.             return "0" + value;
  29.         }
  30.         else
  31.         {
  32.             return "" + value;
  33.         }
  34.     }
  35.    
  36.     public void increment()
  37.     {
  38.         value = (value + 1) % limit;
  39.     }
  40. }

Yang kedua adalah class Clockdisplay
DATA HOSTED WITH ♥ BY PASTEBIN.COM - DOWNLOAD RAW - SEE ORIGINAL
  1. public class ClockDisplay
  2. {
  3.    private NumberDisplay hours;
  4.    private NumberDisplay minutes;
  5.    private String displayString;
  6.    
  7.    public ClockDisplay()
  8.    {
  9.        hours = new NumberDisplay(24);
  10.        minutes = new NumberDisplay(60);
  11.        updateDisplay();
  12.    
  13.    }
  14.    
  15.    public ClockDisplay(int hour, int minute)
  16.    {
  17.        hours = new NumberDisplay(24);
  18.        minutes = new NumberDisplay(60);
  19.        setTime(hour, minute);
  20.        
  21.    }
  22.    
  23.    public void timeTick()
  24.    {
  25.        minutes.increment();
  26.        if(minutes.getValue() == 0)
  27.        {
  28.            hours.increment();
  29.            
  30.        }
  31.        updateDisplay();
  32.    }
  33.    
  34.    public void setTime(int hour, int minute)
  35.    {
  36.        hours.setValue(hour);
  37.        minutes.setValue(minute);
  38.        updateDisplay();
  39.    }
  40.    
  41.    public String getTime()
  42.    {
  43.        return displayString;
  44.    }
  45.    
  46.    private void updateDisplay()
  47.    {
  48.        displayString = hours.getDisplayValue() + ":" + minutes.getDisplayValue();
  49.    }
  50.    
  51. }

Yang ketiga adalah class test Clockdisplay sebagai main programnya.
DATA HOSTED WITH ♥ BY PASTEBIN.COM - DOWNLOAD RAW - SEE ORIGINAL
  1. public class TestClockDisplay
  2. {
  3.     public void test()
  4.     {
  5.         ClockDisplay clock = new ClockDisplay();
  6.        
  7.         clock.setTime(22,30);
  8.         System.out.println(clock.getTime());
  9.        
  10.         clock.setTime(10,30);
  11.         System.out.println(clock.getTime());
  12.     }
  13. }

Berikut adalah tampilannya !!


Dan ini ketika program jalan setelah dicompile !!

No comments:

Post a Comment