PIC20A

Homework 4: Clock

Assignment

You are going to write a Clock class. The class has the following public constructor:

The class also has the following public members

There should be no other public members.
The clock is not a real clock, i.e., it doesn't show you the real time. It doesn't move at all. Your clock is NOT necessary similar to mine. You can have a more complicated design.

How to calculate Angles

Here is part of the code to calculate the angle
    private double getSecAngle() {
        return (6*sec)*2*Math.PI/360;
    }
    
     private double getMinAngle() {
        return (6*min)*2*Math.PI/360;
    }
    
    private double getHrAngle() {
        return ((30*hr) + (30* (6*min)/360)) * 2 * Math.PI/360;
    }

Test program

Notice that you don't have to set the title.

Test1.java
import javax.swing.*;

public class Test1 {
   public static void main(String[] args) {
       final Clock clock = new Clock(9, 25, 43);
       clock.setTitle("Clock 09:25:43");
       clock.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       clock.setSize(400,400);
       clock.setVisible(true);
   }
}
Output
[clock1]

Test2.java
import javax.swing.*;

public class Test2 {
   public static void main(String[] args) {
       final Clock clock = new Clock(2, 47, 22);
       clock.setTitle("Clock 02:47:22");
       clock.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       clock.setSize(400,400);
       clock.setVisible(true);
   }
}
Output
[clock2]


A real clock

If your class is implmented correctly, the following program will shows you a real clock.
import javax.swing.*;
import java.util.*;

public class RealClock {    
    public static void main(String[] args) {
       GregorianCalendar current = new GregorianCalendar();
       int hr = current.get(Calendar.HOUR);
       int min = current.get(Calendar.MINUTE);
       int sec = current.get(Calendar.SECOND);
       final Clock clock = new Clock(hr, min, sec);
       clock.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       clock.setSize(400,400);
       clock.setVisible(true);
       new Thread(new Runnable() {
           public  void run() {              
               try {
                   while(true) {
                     clock.increaseOneSec();
                     Thread.sleep(1000);
                     clock.repaint();
                   }
               } catch(Exception e) {
               }
           }
       }).start();
       
    }
}

What to submit

Call your source code file Clock.java. Put this file in your submit folder. Do not place this file inside another folder within your submit folder.

Remark

Solution

Clock.java