Aarti
Aarti
The aim of the course is to help the student to affiant following industry identify compendance through
various teaching, learning experience.
Proposed methodology
Java uses statements of control the flow of execution of program based on certain conditions. This are used
to cause the flow of execution to book conditions. Person will able to use switch case to check multiple
conditions.
Introduction:-
Platform freelance – several languages area unit compatible with only 1 platform. Java was
specifically designed in order that it might run on any laptop, regardless if it had been running
Windows, Linux, Mac, UNIX operating system or any of the opposite software package.
Simple and simple to use – Java’s creators tried to style it therefore code can be written with
efficiency and simply. Java doesn't have some drawbacks. Since it's automatic trash collection, it
will tend to use additional memory than alternative similar languages. There area unit typically
implementation variations on completely different platforms, that have LED to Java being
delineated as a “write once, check everywhere” system. Lastly, since it uses associate degree
abstract “virtual machine”, a generic java program doesn’t have access to native API’s on a
system directly.
What is Applet?
An applications programme is dynamic and interactive java program that run within the net
page or applets are tiny java programs that are primarily utilized in net computing. The java
application programs run on prompt exploitation java interpreter whereas the java applets will be
transported over the web from one laptop to a different and run exploitation the Applet viewer or
any application program that support java. AN applications programme is like program which
may perform arithmetic operations, show graphic, play sounds settle for user input, produce
animation and play interactive games. To run AN applications programme, it should be enclosed
in hypertext makeup language tags for online page. application program may be a program to
look at online page. Java has enabled sites to make and use absolutely hypermedia system net
documents. An internet page will currently contain not solely a straightforward text or a static
image however additionally a Java applications programme that, when run, will manufacture
graphics, sounds and moving pictures. this java applications programme has created vital impact
on World Wide net. AN applications programme category extends from AWT Panel category, that
extend AWT instrumentation category, that extend AWT part category. AN applications
programme is largely a specialised panel that's embedded in an exceedingly.
Specialized element instrumentality referred to as applications programme Context which might
be an online browser to show associate applications programme. From element category
associate applications programme inherits the flexibility to draw and handle event. From
instrumentality, associate applications programme inherits the flexibility to incorporate alternative
elements and to own layout manager to manage the dimensions and position of these elements.
associate applications programme implements no. of strategies that the applications programme
context calls once it need to initialize, start, stop or destroy the applications programme.
Following program shows the chain of categories genetic by applications programme
category in java:
java.lang.Object
java.awt.Component
java.awt.Container
java.awt.Panel
java.applet.Applet
Program Information:-
1. once the beginning button is click, we tend to begin the timer. whereas the reset or stop button
isn’t pressed the we tend to keep the timer running i.e we alter the worth of the timer
(HH:MM:SS:MS) each one time unit so update the screen.
2. once button is clicked, then we have a tendency to stop the timer and set it to its default worth
(00:00:00:000). 3. once the stop button is ironed, then we have a tendency to stop the timer with
the worth because it is. 4. once the beginning button is ironed and if no alternative button is
placed the timer can continue and can stop only if the applications programme program is
closed.
Procedure:-
b) Declare a category that extends application and implements Runnable .Action Listener.
d) Initialize all the specified variables of kind Button, int, String and mathematician
e) Declare a user-defined technique to initialize all the variables that are to be used
f) Then declare a performs to reset () the timer that contain the try-catch block to handle
exception.
g) Then declare the perform update () to line all the values consequently.
h) Develop a change Label () methodology to properly format the show of the timer in output.
j) Then develop an Action Listener () to handle the button click event properly.
Program Code:-
******************Stop Watch******************
import java.applet.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
Panel p;
Label display;
//Button
//Time
String disp;
boolean on;
//Initialization
public void init()
//Initially off
on=false;
p=new Panel();
p.setLayout(new GridLayout(4,1,6,10));
hour=minute=second=millisecond=0;
//Label
disp="00:00:00:000";
display.setText(disp);
p.add(display);
//Start Button
start=new Button("Start");
start.addActionListener((ActionListener)this);
p.add(start);
//Reset Button
reset.addActionListener((ActionListener)this);
p.add(reset);
//Stop Button
stop=new Button("Stop");
stop.addActionListener((ActionListener)this);
p.add(stop);
add(p);
//Starting Thread
new Thread(this,"StopWatch").start();
//Reset Function
try{
Thread.sleep(1);
catch(Exception e){
System.out.println(e);
hour=minute=second=millisecond=0;
//Update function
millisecond++;
if(millisecond==1000) {
millisecond=0;
second++;
if(second==60)
second=0;
minute++;
if(minute==60)
minute=0;
hour++;
//Changing Label
if(hour<10)
disp="0"+hour+" : ";
else
disp=hour+" : ";
if(minute<10)
disp+="0"+minute+" : ";
else
disp+=minute+" : ";
if(second<10)
disp+="0"+second+" : ";
else
disp+=second+" : ";
if(millisecond<10)
disp+="00"+millisecond;
else if (millisecond<100)
disp+="0"+millisecond;
else
disp+=millisecond;
display.setText(disp);
//thread.run function
{
//while the strength is on
while(on)
try{
//pause 1 millisecond
Thread.sleep(1);
update();
//changeLabel
changeLabel();
catch(InterruptedException e){
System.out.println(e);
//actionPerformed
if(e.getSource()==start)
{
//stopwatch is on
on=true;
new Thread(this,"StopWatch").start();
//reset
if(e.getSource()==reset)
//stopwatch off
on=false;
reset();
changeLabel();
if(e.getSource()==stop)
//stopwatch off
on=false;}
}
**********************Applet Code*********************
<html>
<body>
<applet>
</applet>
</body>
</html>
Output:-
Conclusion:-
Hence from this project we successfully learn to develop a stop watch using applet by
using Action Listener to handle events. This program contains three buttons start, Stop and
Reset. When the Start button is placed the timer gets start, when we press the Stop button
timer gets stop and when the Reset button is pressed then the timer again starts from its initial
value.