Kamis, 27 Oktober 2016

Program Display Jam

          Hi semua, bertemu lagi nih dengan saya Sidqi, mahasiswa Teknik Informatika ITS yang saat ini berada di semester 3 hehe. Pada kesempatan kali ini saya ingin mengupload sekaligus berbagi ilmu mengenai Program Display Jam. Program ini dibuat untuk memenuhi tugas Pemrograman Berorientasi Objek (PBO) Kelas D.

          Langsung saja, inti dari program ini yaitu untuk menampilkan (mendisplay) suatu jam yang terdiri dari satuan jam dan menit. Untuk satuan jam memiliki nilai maksimal 23 dan satuan menit memiliki nilai maksimal 59. Problem untuk mendisplay Jam ini dibantu oleh beberapa subproblem yang merupakan suatu kelas-kelas. Kelas-kelas tersebut yaitu NumberDisplay, ClockDisplay, dan TestClockDisplay.

          Hubungannya seperti berikut :



Source codenya (created by Format My SourceCode for Blogging) :
public class NumberDisplay
{
private int limit;
private int value;
/**
* Constructor for objects of class NumberDisplay
*/
public NumberDisplay(int rollOverLimit)
{
limit = rollOverLimit;
value = 0;
}
/**
* Return the current value.
*/
public int getValue()
{
return value;
}
/**
* Set the value of the display to the new specified
* value. If the new value is less than zero or over the
* limit, do nothing.
*/
public void setValue(int replacementValue)
{
if((replacementValue >= 0) &&
(replacementValue < limit)) {
value = replacementValue;
}
}
/**
* Return the display value (that is, the current value
* as a two-digit String. If the value is less than ten,
* it will be padded with a leading zero).
*/
public String getDisplayValue()
{
if(value < 10) {
return "0" + value;
}
else {
return "" + value;
}
}
/**
* Increment the display value by one, rolling over to zero if
* the limit is reached.
* 
*/
public void increment()
{
value = (value + 1) % limit;
}
}
public class ClockDisplay
{
private NumberDisplay hours;
private NumberDisplay minutes;
private String displayString; // simulates the actual display
/**
* Constructor for ClockDisplay objects. This constructor
* creates a new clock set at 00:00.
*/
public ClockDisplay()
{
hours = new NumberDisplay(24);
minutes = new NumberDisplay(60);
updateDisplay();
}
/**
* Constructor for ClockDisplay objects. This constructor
* creates a new clock set at the time specified by the
* parameters.
*/
public ClockDisplay(int hour, int minute)
{
hours = new NumberDisplay(24);
minutes = new NumberDisplay(60);
setTime(hour, minute);
}
/**
* This method should get called once every minute - it
* makes the clock display go one minute forward.
*/
public void timeTick()
{
minutes.increment();
if(minutes.getValue() == 0) { // it just rolled over!
hours.increment();
}
updateDisplay();
}
/**
* Set the time of the display to the specified hour and
* minute.
*/
public void setTime(int hour, int minute)
{
hours.setValue(hour);
minutes.setValue(minute);
updateDisplay();
}
/**
* Return the current time of this display in the format
* HH:MM.
*/
public String getTime()
{
return displayString;
}
/**
* Update the internal string that represents the
* display.
*/
private void updateDisplay()
{
displayString = hours.getDisplayValue() + ":" +
minutes.getDisplayValue();
}
}
public class TestClockDisplay
{
 
    public TestClockDisplay()
    {
    }
 
    public void test()
    {
        ClockDisplay clock = new ClockDisplay();
       
        clock.setTime(19,04);
        System.out.println(clock.getTime());
       
        clock.setTime(04,52);
        System.out.println(clock.getTime());
    }
}
Nah, kelas yang pertama yaiut NumberDisplay, berikut screen capture source codenya :


Lalu, kelas yang kedua yaitu ClockDisplay, berikut source codenya :


Dan yang terakhir yaitu kelas TestClockDisplay. Berikut source codenya :

Lalu ketiga itu akan saling berinteraksi dan setelah dicompile akan tampak jam yang terdiri dari jam dan menit. (Sebagai contoh saya menginput jam 19:04 dan 04:52. Berikut hasil setelah dicompile :)

Nama : R. Sidqi Tri Priwi
NRP  : 5115100153
Kelas : PBO D

Tidak ada komentar:

Posting Komentar

Related Posts Plugin for WordPress, Blogger...