Tugas PBO A - Membuat Jam
Tugas PBO A kali ini yaitu membuat jam dan digital clock menggunakan aplikasi BlueJ.
Nama : Ifta Jihan NNRP : 05111740000034
Kelas : PBO A
Terdapat 3 Class yang digunakan, yaitu:
1. NumberDisplay : untuk mengatur menit dan jam.
2. ClockDisplay : sebagai tampilan dari jam yang telah dibuat.
3. TestClockDisplay : test case dari jam yang dibuat (menampilkan jam yang telah ditentukan).
Source code:
1. NumberDisplay
/**
* Write a description of class NumberDisplay here.
*
* @author Ifta Jihan N (05111740000034)
* PBO A
* @version 27/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 String getDisplayValue()
{
if(value<10){
return "0" + value;
}
else{
return "" + value;
}
}
public void setValue(int replacementValue)
{
if((replacementValue>=0)&&(replacementValue<limit))
{
value=replacementValue;
}
}
public void increment()
{
value=(value+1)%limit;
}
}
2. ClockDisplay
/**
* Write a description of class ClockDisplay here.
*
* @author Ifta Jihan N (05111740000034)
* PBO A
* @version 27/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";
}
displayString=hours.getDisplayValue() + "." + minutes.getDisplayValue() + suffix;
}
}
3. TestClockDisplay
/**
* Write a description of class TestClockDisplay here.
*
* @author Ifta Jihan N (05111740000034)
* PBO A
* @version 27/09/2018
*/
public class TestClockDisplay
{
public TestClockDisplay()
{
}
public void test(){
ClockDisplay clock=new ClockDisplay();
clock.setTime(22,15);
System.out.println(clock.getTime());
clock.setTime(12,45);
System.out.println(clock.getTime());
clock.setTime(17,05);
System.out.println(clock.getTime());
clock.setTime(11,25);
System.out.println(clock.getTime());
}
}
Berikut ini hasilnya:
Berikut ini source code saya untuk membuat GUI Digital Clock
1. Class DigitalClock
/**
* Write a description of class DigitalClock here.
*
* @author Ifta Jihan N (05111740000034)
* PBO A
* @version (27/09/2018)
*/
import java.awt.Font;
import java.awt.FlowLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class DigitalClock extends JFrame
{
JLabel jlabClock;
ClockThread ct;
public DigitalClock()
{
super("Digital Clock by Jihan");
jlabClock = new JLabel("Time");
setLayout(new FlowLayout());
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jlabClock.setFont(new Font("Arial", Font.BOLD, 25));
add(jlabClock);
setSize(430,100);
setLocationRelativeTo(null);
ct = new ClockThread(this);
setVisible(true);
}
public static void main(String[] args)
{
new DigitalClock();
}
}
2. Class ClockThread
/**
* Write a description of class ClockThread here.
*
* @author Ifta Jihan N (05111740000034)
* PBO A
* @version 27/09/2018
*/
import java.util.Date;
public class ClockThread extends Thread
{
DigitalClock dc;
String time;
public ClockThread(DigitalClock dc)
{
this.dc = dc;
start();
}
public void run()
{
while(true)
{
time= ""+new Date();
dc.jlabClock.setText(time);
}
}
}
Berikut ini hasilnya
Menampilkan tanggal dan tahun serta jam yang sesuai dengan time zone ICT (Indochina Time).
Comments
Post a Comment