PIC20A
Homework 4: Clock
Assignment
You are going to write a Clock class. The class has the following
public constructor:
- public Clock(int hr, int min, int sec)
The class also has the following public members
void setHr(int hr), void setMin(int min), void setSec(int sec)
int getHr(), int getMin(), int getSec()
increaseOneSec(): increse sec by one, it the sec becomes 60, increase min by one.
if the min becomes 60, increase the hr by one. If the hr exceed 13, set it to 1.
paint(Graphics g)
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
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
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
- All source code submitted must contain the following information at the beginning of each file:
- Your full name
- Your student ID number
- Your PIC login ID
- Your e-mail address
- Your discussion section number and TA's name
- The assignment number and description
- Honesty pledge
Properly formatted, this information should appear as follows:(Please change the Name, Student ID, PIC ID, E-mail, Discussion
/** Name: Charles Li
* Student ID : 000 000 000
* PIC ID: ccli
* E-mail: ccli@math.ucla.edu
* Discussion: 1A, Baber
* Assignment: hw1
*
* I, Charles Li, pledge that this is my own independent work, which
* conforms to the guidelines of academic honesty as described in the course
* syllabus.
*
*/
- If you develop your program on your home computer, make sure it compiles and runs on the lab machines before you turn it in.
- Please use consistent and neat indenting in all your programs.
- Please comment your program.
Solution
Clock.java