Home Answers Viewqa Design-concepts-design-patterns Digital watch using threads

 
 


Avishek Bhattacharyya
Digital watch using threads
1 Answer(s)      3 years and 4 months ago
Posted in : Design concepts & design patterns

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 Pages:
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
threads
threads  how to print names in different lines in different spaces generating random numbers and using thread...like ping pong ping pong
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
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
disadvantage of threads
, the other threads using the same memory location will be killed automatically... is the disadvantage of threads?   hello, The Main disadvantage of in threads... disadvantage of Threads. Let?s discuss the disadvantages of threads. The global
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
Finding shapes in digital photos - java
Finding shapes in digital photos - java  Hello, I need some hints... in digital photos (JPEG file). Here's what I have to do: A digital photo... interested in is using short random walks, moving from pixel to pixel randomly
Synchronized Threads
a synchronized method in a class, and both threads are using the same instance... Synchronized Threads       In Java, the threads are executed independently to each other. These types
Explain about threads:how to start program in threads?
. Start, run, stop. Threads can be started using start method, the start...Explain about threads:how to start program in threads?  import...; Learn Threads   Thread is a path of execution of a program
Synchronized Threads
a synchronized method in a class, and both threads are using the same instance... Synchronized Threads       In Java, the threads are executed independently to each other. These types
Synchronized Threads
to execute a synchronized method in a class, and both threads are using... Synchronized Threads       In Java, the threads are executed independently to each other. These types
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
java code digital clock - Java Beginners
java code digital clock  hello can you share or give me a code that will display digital time.hh:mm:ss it shows the seconds is running. thats all...;ohhh i forget its a java program and it is using looping can you give me
threads
threads  what are threads? what is the use in progarmming
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
multi threads - Java Beginners
using three threads. I want to declare variables which will be available to all the threads to access. Is there a way to declare the variables as global variables which will be available to all the threads. If so please send me the code
interfaces,exceptions,threads
THE COMPLETE CONEPTS OF INTERFACES,EXCEPTIONS,THREADS   Interface... class. In java, multiple inheritance is achieved by using the interface...: Exception Handling in Java   Threads A thread is a lightweight process which
Threads - Java Interview Questions
to Runnable Interface. 3)Using Runnable Interface, you can run the class several.... If you want to create threads, please visit the following link: http
Threads - Java Beginners
Threads  hi, how to execute threads prgm in java? is it using appletviewer as in applet?   Hi manju import java.awt.*; import...("/home/vinod/amarexamples:9090/" + "amarexamples/Threads/applet
running time watch
running time watch   how to set running watch in applycation with ajax
Threads - Java Beginners
Threads  Hi all, Can anyone tell me in detail about the following question. when we start the thread by using t.start(),how it knows that to execute run()method ? Thanks in advance. Vinod
returning a value from Threads
returning a value from Threads  Hello I have worker pattern that uses ExecutorService to schedule Thread execution but getting stuck at some point when returning a value using Future.I have code snippet below: ExecutorService
Threads on runnable interface - Java Beginners
Threads on runnable interface  need a program.....please reply asap Create 2 threads using runnable interface.First threads shd print "hello" & second shd print "welcome". using synchronisation tech print d words alternatively
Threads on runnable interface - Java Beginners
Threads on runnable interface  need a program.....please reply asap Create 2 threads using runnable interface.First threads shd print "hello" & second shd print "welcome". using synchronisation tech print d words alternatively
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
Java - Threads in Java
be increased by using threads because the thread can stop or suspend a specific... Java - Threads in Java       Thread is the feature of mostly languages including Java. Threads
java threads - Java Interview Questions
java threads  How can you change the proirity of number of a thread.  Hi Friend, By using setPriority() method, you can change... for Java threads in the range of 1 to 10. Following is the constaints defined
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? Thanks
creating multiple threads - Java Beginners
creating multiple threads  demonstrate a java program using multiple thread to create stack and perform both push and pop operation synchronously.  Hi friend, Use the following code: import java.util.*; class
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
Shutting down threads cleanly,java tutorial,java tutorials
Shutting Down Threads Cleanly 2002-09-16 The Java Specialists' Newsletter [Issue 056] - Shutting down threads cleanly Author: Dr. Heinz M. Kabutz... My thoughts on shutting down threads is based on ideas I gleaned from those two
Threads
Threads Basic Idea Execute more than one piece of code at the "same... time slicing. Rotates CPU among threads / processes. Gives.... Threads vs Processes Multiple processes / tasks Separate programs
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
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
threads and events
threads and events  Can you explain threads and events in java for me. Thank you.   Java Event Handling Java Thread Examples
Java threads
Java threads  What are the two basic ways in which classes that can be run as threads may be defined
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
VoIP Watch
VoIP Watch           Introduction of VoIP Watch VoIPWatch has learned that tomorrow GizmoProject will introduce their ?All Calls Free? program. According
Creation of Multiple Threads
Creation of Multiple Threads   ... than one thread (multithreads) in a program using class Thread or implementing... In this program, two threads are created along with the "main" thread
Threads in realtime projects
Threads in realtime projects  Explain where we use threads in realtime projects with example

Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.