Latihan Membuat Jam
Latihan Membuat Jam
Tugas PBO A
Kali ini saya akan membahas tentang bagaimana membuat jam menggunakan aplikasi BlueJ. Pertama anda tentunya harus memiliki source codenya, berikut :
1. Source Code NumberDisplay
/**
* Write a description of class NumberDisplay here.
*
* @author (Yuki Yanuar Ratna)
* @version (26-09-2018)
*/
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. Source Code ClockDisplay
/**
* Write a description of class ClockDisplay here.
*
* @author (Yuki Yanuar Ratna)
* @version (26-09-2018)
*/
public class ClockDisplay
{
private NumberDisplay hours;
private NumberDisplay minutes;
private String displayString;
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)
{
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;
}
}
3. Source Code TestClockDisplay
/**
* Write a description of class TestClockDisplay here.
*
* @author (Yuki Yanuar Ratna)
* @version (26-09-2018)
*/
public class TestClockDisplay
{
/**
* Constructor for objects of class TestClockDisplay
*/
public TestClockDisplay()
{
}
public void test()
{
ClockDisplay Clock = new ClockDisplay();
Clock.setTime(1,0);
System.out.println(Clock.getTime());
Clock.setTime(07,30);
System.out.println(Clock.getTime());
Clock.setTime(12,30);
System.out.println(Clock.getTime());
Clock.setTime(18,0);
System.out.println(Clock.getTime());
}
}
3. Dan berikut adalah langkah-langkah untuk mengetahui hasil akhir
Sekian, semoga bermanfaat
Komentar
Posting Komentar