Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



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

Have Programming Question? Ask it here!
 
 
 


Avishek Bhattacharyya
Digital watch using threads
1 Answer(s)      4 years and 6 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.

Advertisement
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
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
Advertisements
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
threads
threads  how to print names in different lines in different spaces generating random numbers and using thread...like ping pong ping pong
running time watch
running time watch   how to set running watch in applycation with ajax
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
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
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
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
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
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
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
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
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
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
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
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
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
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 in realtime projects
Threads in realtime projects  Explain where we use threads in realtime projects with example
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
how to set running watch with descending order in applycation with ajax
how to set running watch with descending order in applycation with ajax   like hh : mm : ss 23 : 59 : 59 23 : 58 : 10 22 : 20 : 01 21 : 45 : 59 . . . . . . 02 : 23 : 03 01 : 10 : 23 00 : 00 : 00
how to set running watch with descending order in applycation with ajax
how to set running watch with descending order in applycation with ajax   like hh : mm : ss 23 : 59 : 59 23 : 58 : 10 22 : 20 : 01 21 : 45 : 59 . . . . . . 02 : 23 : 03 01 : 10 : 23 00 : 00 : 00
Coding for life cycle in threads
Coding for life cycle in threads  program for life cycle in threads
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
Examples on threads and mulithreading.....
Examples on threads and mulithreading.....  Is any good examples on threads and Mulithreading...   Hi Friend, Please visit the following link: Thread Tutorial Thanks
java threads - Java Beginners
java threads  What are the two basic ways in which classes that can be run as threads may be defined
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
What is Digital Relationship Marketing?
Digital relationship marketing is increasingly becoming the focus of marketing... in the technological interface or digitized way of handling things in everyday life digital... and campaigns in the new era and thus digital relationship marketing became
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
Java Threads - Java Beginners
allows the threads to wait for resources to become available and also notify the thread that makes resource available to notify other threads
threads in java - Java Beginners
threads in java  what is the difference between preemptive scheduling and time slicing?   hi friend, In Preemptive scheduling, a thread... or the priority of one of the waiting threads is increased. While in Time Slicing
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
Daemon Threads
Daemon Threads       In Java, any thread can be a Daemon thread. Daemon threads are like a service providers for other threads or objects running in the same process as the daemon
threads & autorelease pool
threads & autorelease pool  How to set autorelease pool for NSThread method in Objective C?   [NSThread detachNewThreadSelector:@selector(yourMethod) toTarget:self withObject:nil]; - (void)yourMethod
 

 

 

DMCA.com