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



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

View Questions and Answers by Category

Advertisements


 
Have Programming Question? Ask it here!
 
 
 


Avishek Bhattacharyya
Digital watch using threads
1 Answer(s)      5 years 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
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
Advertisements
 

 

 

DMCA.com