Tugas #PBOA-tugas 5 (Jam)
Interaksi Objek
Membuat Jam
Untuk membuat jam diperlukan tiga class, yaitu :
- NumberDisplay
- ClockDisplay
- TestClockDisplay
Berikut adalah souce code yang digunakan :
- NumberDisplay
- ClockDisplay
- TestClockDisplay
/*
/*
* @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;
}
}
/*
/*
* @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;
}
}
/**
* @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());
}
}
Untuk membuat GUI clock display diperlukan dua class, yaitu :
- clock
- Executor
- clock
- Executor
/*
*
* @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);
}
}
}
/**
* 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();
}
}
Komentar
Posting Komentar