Digital watch using threads

Digital watch using threads

import java.lang.Thread;
import java.lang.*;
import java.awt.*;
class Time implements Runnable
{
int i,j,k,m;
Frame f;
Panel p;
Label l,l1,l2;
TextField tf1,tf2,tf3,tf4;
Thread t1;
Time()
{
f=new Frame();
p=new Panel();
l=new Label("seconds");
l1=new Label("minutes");
l2=new Label("hours");



tf1=new TextField(20);
tf2=new TextField(20);
tf3=new TextField(20);
tf4=new TextField(20);
p.add(l2);p.add(tf3);
p.add(l1);p.add(tf2);
p.add(l);p.add(tf1);
p.add(tf4);
f.add(p);
f.setSize(200,300);
f.setVisible(true);
t1=new Thread(this);

try
{
t1.start();
}
catch(Exception e)
{}
}
public static void main(String args[])
{
Time t=new Time();
}
public void run()
{
m=0;
i=0;
j=0;
k=0;
String s[]={"00","01","02","03","04","05","06","07","08","09","10","11","12","13","14","15","16","17","18","19","20","21","22","23","24","25","26","27","28","29","30","31","32","33","34","35","36","37","38","39","40","41","42","43","44","45","46","47","48","49","50","51","52","53","54","55","56","57","58","59"};
String s1[]={"00","01","02","03","04","05","06","07","08","09","10","11","12","13","14","15","16","17","18","19","20","21","22","23","24","25","26","27","28","29","30","31","32","33","34","35","36","37","38","39","40","41","42","43","44","45","46","47","48","49","50","51","52","53","54","55","56","57","58","59"};
String s2[]={"01","02","03","04","05","06","07","08","09","10","11","12"};

String s3[]={"am","pm"};
while(k<s3.length)
{
while(j<s2.length)
{
while(m<s1.length)
{
while(i<s.length)
{
tf1.setText(s[i]);
try
{
t1.sleep(1000);
}
catch(Exception e)
{
}
i++;
if(i==60)
{
i=0;
}m++;
tf2.setText(s1[m]);
if(m==60)
{
m=0;
}j++;
tf3.setText(s2[j]);
if(j==12)
{
j=0;
}k++;
tf4.setText(s3[k]);
if(k==2)
k=0;
}
}
}
}
}
}





I would like to design a digital watch using threads in JAVA.In the above program I would like to know the corrections to be made for making the program work.

Thanking you,
Avishek Bhattacharyya.
View Answers

January 24, 2010 at 7:00 AM

import java.lang.Thread;
import java.lang.*;
import java.awt.*;
class Time implements Runnable
{
int i,j,m;
Frame f;
Panel p;
Label l,l1,l2;
TextField tf1,tf2,tf3,tf4;
Thread t1;
Time()
{
f=new Frame();
p=new Panel();
l=new Label("seconds");
l1=new Label("minutes");
l2=new Label("hours");
tf1=new TextField(20);
tf2=new TextField(20);
tf2.setText("00");
tf3=new TextField(20);
tf3.setText("00");
tf4=new TextField(20);
tf4.setText("am");
p.add(l2);p.add(tf3);
p.add(l1);p.add(tf2);
p.add(l);p.add(tf1);
p.add(tf4);
f.add(p);
f.setSize(200,300);
f.setVisible(true);
t1=new Thread(this);
try
{
t1.start();
}
catch(Exception e)
{}
}
public static void main(String args[])
{
Time t=new Time();
}
public void run()
{
m=0;
i=0;
j=0;

String s[]={"00","01","02","03","04","05","06","07","08","09","10","11","12","13","14","15","16","17","18","19","20","21","22","23","24","25","26","27","28","29","30","31","32","33","34","35","36","37","38","39","40","41","42","43","44","45","46","47","48","49","50","51","52","53","54","55","56","57","58","59"};
String s1[]={"01","02","03","04","05","06","07","08","09","10","11","12","13","14","15","16","17","18","19","20","21","22","23","24","25","26","27","28","29","30","31","32","33","34","35","36","37","38","39","40","41","42","43","44","45","46","47","48","49","50","51","52","53","54","55","56","57","58","59"};
String s2[]={"01","02","03","04","05","06","07","08","09","10","11"};

while(j<s2.length)
{
while(m<s1.length)
{
while(i<s.length)
{
tf1.setText(s[i]);
try
{
t1.sleep(1000);
}
catch(Exception e)
{
} //while loop closed
i++;
if(i==60)
{
i=0;

tf2.setText(s1[m]);
}m++;
}

if(m==59)
{
m=0;
tf3.setText(s2[j]);
}j++;
}
if(j==11)
{
j=0;
String s3=tf4.getText();
if(s3=="am")
tf4.setText("pm");
if(s3=="pm")
tf4.setText("am");
}
}

}
}




The above program is working for only the seconds textfield.









Related Tutorials/Questions & Answers:
Digital watch using threads - Design concepts & design patterns
Digital watch using threads  import java.lang.Thread; import java.lang.*; import java.awt.*; class Time implements Runnable { int i,j,k,m; Frame f; Panel p; Label l,l1,l2; TextField tf1,tf2,tf3,tf4; Thread t1
how to create a reminder app using threads in Servlets?
how to create a reminder app using threads in Servlets?  I want... (threads will be required!), a "pop-up window or a web-page should automatically get re-directed!". I have used threads for core java, but never used for Servlets
Advertisements
hybrid digital image embedding process using invisible watermarking - Swing AWT
hybrid digital image embedding process using invisible watermarking   sir/madam, I am subramanian from chennai.i did not create this project using rsa algorithm and i did not understand.please help me with source
threads
threads  what are threads? what is the use in progarmming
implementing an algorithm using multi threads - Java Beginners
implementing an algorithm using multi threads  Hi i need to implement an algorith in multi threads.Algorithm has data dependency so i need to pass... to breakdown into two or three threads and need to implemented and need
threads
threads  how to print names in different lines in different spaces generating random numbers and using thread...like ping pong ping pong
Diff between Runnable Interface and Thread class while using threads
Diff between Runnable Interface and Thread class while using threads  Diff between Runnable Interface and Thread class while using threads  ... to Runnable Interface. 3)Using Runnable Interface, you can run class several
Diff between Runnable Interface and Thread class while using threads
Diff between Runnable Interface and Thread class while using threads  Diff between Runnable Interface and Thread class while using threads  ... to Runnable Interface. 3)Using Runnable Interface, you can run class several
digital diary
digital diary  please post the code for a simple digital diary... with contacts storing facility, passwords , notes, alarm clock and calender
digital diary
digital diary  please post the code for a simple digital diary... with contacts storing facility, passwords , notes, alarm clock and calender
digital diary
digital diary  please post the code for a simple digital diary... with contacts storing facility, passwords , notes, alarm clock and calender
analog to digital
analog to digital  how to convert analog to digital in java ?   Hi, What is to be converted? Is it sound or video?ADS_TO_REPLACE_1 Thanks
threads in java
threads in java  how to read a file in java , split it and write into two different files using threads such that thread is running twice
running time watch
running time watch   how to set running watch in applycation with ajax
ModuleNotFoundError: No module named 'watch'
ModuleNotFoundError: No module named 'watch'  Hi, My Python program is throwing following error: ModuleNotFoundError: No module named 'watch' How to remove the ModuleNotFoundError: No module named 'watch'
disadvantage of threads
. If due to memory leak if one thread is killed, the other threads using the same... is the disadvantage of threads?   hello,ADS_TO_REPLACE_1 The Main disadvantage of in threads these are operating system dependent.   Hi, Here
ModuleNotFoundError: No module named 'digital'
ModuleNotFoundError: No module named 'digital'  Hi, My Python... 'digital' How to remove the ModuleNotFoundError: No module named 'digital... to install padas library. You can install digital python with following command
Sync Threads
Sync Threads  "If two threads wants to execute a synchronized method in a class, and both threads are using the same instance of the class to invoke the method then only one thread can execute the method at a time." This is what
convert voice to digital
convert voice to digital  convert voice to digital How to convert human voice to the digital signal(binary) that can stored in notepad. Then, this notepad can be used
ModuleNotFoundError: No module named 'mongo-watch'
ModuleNotFoundError: No module named 'mongo-watch'  Hi, My Python... 'mongo-watch' How to remove the ModuleNotFoundError: No module named 'mongo-watch' error? Thanks   Hi, In your python environment
ModuleNotFoundError: No module named 'motor-watch'
ModuleNotFoundError: No module named 'motor-watch'  Hi, My Python... 'motor-watch' How to remove the ModuleNotFoundError: No module named 'motor-watch' error? Thanks   Hi, In your python environment
ModuleNotFoundError: No module named 'motor-watch'
ModuleNotFoundError: No module named 'motor-watch'  Hi, My Python... 'motor-watch' How to remove the ModuleNotFoundError: No module named 'motor-watch' error? Thanks   Hi, In your python environment
ModuleNotFoundError: No module named 'nbconvert_watch'
ModuleNotFoundError: No module named 'nbconvert_watch'  Hi, My... named 'nbconvert_watch' How to remove the ModuleNotFoundError: No module named 'nbconvert_watch' error? Thanks   Hi, In your python
ModuleNotFoundError: No module named 'nbconvert_watch'
ModuleNotFoundError: No module named 'nbconvert_watch'  Hi, My... named 'nbconvert_watch' How to remove the ModuleNotFoundError: No module named 'nbconvert_watch' error? Thanks   Hi, In your python
ModuleNotFoundError: No module named 'nights-watch'
ModuleNotFoundError: No module named 'nights-watch'  Hi, My Python... 'nights-watch' How to remove the ModuleNotFoundError: No module named 'nights-watch' error? Thanks   Hi, In your python
ModuleNotFoundError: No module named 'nose-watch'
ModuleNotFoundError: No module named 'nose-watch'  Hi, My Python... 'nose-watch' How to remove the ModuleNotFoundError: No module named 'nose-watch' error? Thanks   Hi, In your python environment you
ModuleNotFoundError: No module named 'pytest-watch'
ModuleNotFoundError: No module named 'pytest-watch'  Hi, My Python... 'pytest-watch' How to remove the ModuleNotFoundError: No module named 'pytest-watch' error? Thanks   Hi, In your python
ModuleNotFoundError: No module named 'python-watch'
ModuleNotFoundError: No module named 'python-watch'  Hi, My Python... 'python-watch' How to remove the ModuleNotFoundError: No module named 'python-watch' error? Thanks   Hi, In your python
ModuleNotFoundError: No module named 'rsync-watch'
ModuleNotFoundError: No module named 'rsync-watch'  Hi, My Python... 'rsync-watch' How to remove the ModuleNotFoundError: No module named 'rsync-watch' error? Thanks   Hi, In your python environment
ModuleNotFoundError: No module named 'trustlines-watch'
ModuleNotFoundError: No module named 'trustlines-watch'  Hi, My... named 'trustlines-watch' How to remove the ModuleNotFoundError: No module named 'trustlines-watch' error? Thanks   Hi, In your
ModuleNotFoundError: No module named 'watch-diff'
ModuleNotFoundError: No module named 'watch-diff'  Hi, My Python... 'watch-diff' How to remove the ModuleNotFoundError: No module named 'watch-diff' error? Thanks   Hi, In your python environment you
ModuleNotFoundError: No module named 'watch-dog'
ModuleNotFoundError: No module named 'watch-dog'  Hi, My Python... 'watch-dog' How to remove the ModuleNotFoundError: No module named 'watch... have to install padas library. You can install watch-dog python with following
ModuleNotFoundError: No module named 'watch-fs'
ModuleNotFoundError: No module named 'watch-fs'  Hi, My Python... 'watch-fs' How to remove the ModuleNotFoundError: No module named 'watch-fs... to install padas library. You can install watch-fs python with following
ModuleNotFoundError: No module named 'watch-rsync'
ModuleNotFoundError: No module named 'watch-rsync'  Hi, My Python... 'watch-rsync' How to remove the ModuleNotFoundError: No module named 'watch-rsync' error? Thanks   Hi, In your python environment
ModuleNotFoundError: No module named 'awslogs-watch'
ModuleNotFoundError: No module named 'awslogs-watch'  Hi, My... 'awslogs-watch' How to remove the ModuleNotFoundError: No module named 'awslogs-watch' error? Thanks   Hi, In your python
ModuleNotFoundError: No module named 'django-watch'
ModuleNotFoundError: No module named 'django-watch'  Hi, My Python... 'django-watch' How to remove the ModuleNotFoundError: No module named 'django-watch' error? Thanks   Hi, In your python
ModuleNotFoundError: No module named 'import-watch'
ModuleNotFoundError: No module named 'import-watch'  Hi, My Python... 'import-watch' How to remove the ModuleNotFoundError: No module named 'import-watch' error? Thanks   Hi, In your python
ModuleNotFoundError: No module named 'just-watch'
ModuleNotFoundError: No module named 'just-watch'  Hi, My Python... 'just-watch' How to remove the ModuleNotFoundError: No module named 'just-watch' error? Thanks   Hi, In your python environment you
Version of com.opentangerine>ot-watch dependency
List of Version of com.opentangerine>ot-watch dependency
threads and events
threads and events  Can you explain threads and events in java for me. Thank you.   Java Event Handling Java Thread Examples
convert voice to digital
convert voice to digital  How to convert human voice to the digital signal(binary) that can stored in notepad. Then, this notepad can be used... .... .... My final year project title is Voice Verification System using Back
digital communication system
digital communication system  A digital communication system capable of interconnecting, a large number of computers, terminals and other peripheral devices within a limited geographical area is called- a) LAN, B) WAN, C
Java threads
Java threads  What are the two basic ways in which classes that can be run as threads may be defined
What is Digital Relationship?
and expanse of the term digital relationship. But as we are immersed already in the digital age and everything right from the entertainment to information sharing to health care tends to become increasingly digital we cannot think otherwise
digital marketing data science
digital marketing data science  Hi, I am beginner in Data Science and machine learning field. I am searching for the tutorials to learn: digital... that I can learn the topic "digital marketing data science". Also tell
threads in java
threads in java  iam getting that the local variable is never read in eclipse in main classas:: class Synex4{ public static void main(String args[]){ Test1 ob1=new Test1(); //local variable never read
ModuleNotFoundError: No module named 'digital-comms'
ModuleNotFoundError: No module named 'digital-comms'  Hi, My... 'digital-comms' How to remove the ModuleNotFoundError: No module named 'digital-comms' error? Thanks   Hi, In your python
ModuleNotFoundError: No module named 'digital-rf'
ModuleNotFoundError: No module named 'digital-rf'  Hi, My Python... 'digital-rf' How to remove the ModuleNotFoundError: No module named 'digital-rf' error? Thanks   Hi, In your python environment you
ModuleNotFoundError: No module named 'digital-root'
ModuleNotFoundError: No module named 'digital-root'  Hi, My Python... 'digital-root' How to remove the ModuleNotFoundError: No module named 'digital-root' error? Thanks   Hi, In your python
ModuleNotFoundError: No module named 'digital-stopwatch'
ModuleNotFoundError: No module named 'digital-stopwatch'  Hi, My... named 'digital-stopwatch' How to remove the ModuleNotFoundError: No module named 'digital-stopwatch' error? Thanks   Hi, In your

Ads