How to Write a Java code for a random Clock time and Alram?

2 Answers

Relevance
  • 1 decade ago
    Best Answer

    //pk

    Clock.java

    ========

    import java.util.Date;

    public class Clock {

    private int currentHour, currentMinute, currentSecond;

    private int alarmHour, alarmMinute, alarmSecond;

    public Clock() {

    Date today = new Date(); //system date

    currentHour = today.getHours();

    currentMinute = today.getMinutes();

    currentSecond = today.getSeconds();

    }

    public int getCurrentHour() {

    return currentHour;

    }

    public void setCurrentHour(int currentHour) {

    if (currentHour > 24 || currentHour < 0) {

    System.out.println ("............Hour should be between 0-24");

    } else {

    this.currentHour = currentHour;

    }

    }

    public int getCurrentMinute() {

    return currentMinute;

    }

    public void setCurrentMinute(int currentMinute) {

    if (currentMinute > 60 || currentMinute < 0) {

    System.out.println ("............Minute should be between 0-24");

    } else {

    this.currentMinute = currentMinute;

    }

    }

    public int getCurrentSecond() {

    return currentSecond;

    }

    public void setCurrentSecond(int currentSecond) {

    if (currentSecond > 60 || currentSecond < 0) {

    System.out.println ("............Second should be between 0-24");

    } else {

    this.currentSecond = currentSecond;

    }

    }

    public int getAlarmHour() {

    return alarmHour;

    }

    public void setAlarmHour(int alarmHour) {

    if (alarmHour > 24 || alarmHour < 0) {

    System.out.println ("............Hour should be between 0-24");

    } else {

    this.alarmHour = alarmHour;

    }

    }

    public int getAlarmMinute() {

    return alarmMinute;

    }

    public void setAlarmMinute(int alarmMinute) {

    if (alarmMinute > 60 || alarmMinute < 0) {

    System.out.println ("............Minute should be between 0-24");

    } else {

    this.alarmMinute = alarmMinute;

    }

    }

    public int getAlarmSecond() {

    return alarmSecond;

    }

    public void setAlarmSecond(int alarmSecond) {

    if (alarmSecond > 60 || alarmSecond < 0) {

    System.out.println ("............Second should be between 0-24");

    } else {

    this.alarmSecond = alarmSecond;

    }

    }

    public void randomizeTime() {

    }

    public void randomizeAlarmTime() {

    alarmHour = (int) (Math.random() * 24);

    alarmMinute = (int) (Math.random() * 60);

    alarmSecond = (int) (Math.random() * 60);

    System.out.println ("Randomizing the alarm time...");

    }

    public void printTime() {

    System.out.println ("The time is: " + currentHour + ":" + currentMinute + ":" + currentSecond);

    }

    public void printAlarmTime() {

    System.out.println ("The alarm time is: " + alarmHour + ":" + alarmMinute + ":" + alarmSecond);

    }

    public boolean checkAlarm() {

    return (currentHour == alarmHour && currentMinute == alarmMinute && currentSecond == alarmSecond);

    }

    }

    ClockTestDrive.java

    ===============

    public class ClockTestDrive {

    public static void main(String[] args) {

    Clock c = new Clock();

    c.printTime();

    c.randomizeAlarmTime();

    c.printAlarmTime();

    System.out.println(c.checkAlarm());

    }

    }

  • miers
    Lv 4
    3 years ago

    Java Alarm Clock

Still have questions? Get your answers by asking now.