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:
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
threadsthreads what are
threads? what is the use in progarmming
threadsthreads how to print names in different lines in different spaces generating random numbers and
using thread...like
ping
pong
ping
pong
digital diarydigital diary please post the code for a simple
digital diary... with contacts storing facility, passwords , notes, alarm clock and calender
digital diarydigital diary please post the code for a simple
digital diary... with contacts storing facility, passwords , notes, alarm clock and calender
digital diarydigital diary please post the code for a simple
digital diary... with contacts storing facility, passwords , notes, alarm clock and calender
analog to digitalanalog 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 javathreads 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
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 ThreadsSync 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 digitalconvert 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 '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 '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 '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
threads and eventsthreads and events Can you explain
threads and events in java for me. Thank you.
Java Event Handling
Java Thread Examples
convert voice to digitalconvert 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 systemdigital 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 threadsJava 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 sciencedigital 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 javathreads 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-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