0% found this document useful (0 votes)
19 views

Lab Java

The document provides details about Java programming lab assignments divided into two parts - Part A and Part B. Part A includes 10 programming problems on various Java concepts like methods, constructors, strings, arrays etc. Part B also includes 10 problems on topics like checking Armstrong number, finding GCD/LCM, Fibonacci series etc.

Uploaded by

Manesh Manesh
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views

Lab Java

The document provides details about Java programming lab assignments divided into two parts - Part A and Part B. Part A includes 10 programming problems on various Java concepts like methods, constructors, strings, arrays etc. Part B also includes 10 problems on topics like checking Armstrong number, finding GCD/LCM, Fibonacci series etc.

Uploaded by

Manesh Manesh
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 20

BCA 504P: JAVA PROGRAMMING LAB

(V BCA ‘A ‘ and ‘B’ Section)


Faculty : Srivatsala V
PART - A
1.Write a program to find factorial of list of number reading input as command line argument.
2.Write a program to display all prime numbers between two limits.
3. Write a program to sort list of elements in ascending and descending order and show the exception
handling.
4.Write a program to implement all string operations.
5.Write a program to find area of geometrical figures using method.
6.Write a program to implement constructor overloading by passing different number of parameter of
different types.
7.Write a program to create student report using applet, read the input text boxes and display the
o/p using buttons.
8.Write a program to calculate bonus for different departments using method overriding.
9. Write a program to implement thread, applets and graphics by implementing animation of ball
moving.
10.Write a program to implement mouse events.

PART - B
1.Java Program to Check If a Given Number is ArmStrong Number
2.Write a program to check whether a given character is a vowel or consonant.
3.Java Program to Find the GCD and LCM of two Numbers.
4.Write a program for generating Fibonacci Sequence.
5.How to Copy a File to another File in Java.
6.Write a program in Java for Basic Vector Operations.
7.Write a program in Java to find area of geometrical figures using method overloading
8. Write a program in Java for displaying Rhombus pattern
9.Write a program in JAVA to create a thread.
10.Write a program to play sound using Applet in JAVA
Part – A

//Part A Program 1:WAP in Java for finding factorial of command line arguments.
class FactDemo
{
public static void main(String args[])
Output : java FactDemo 2 3 4
{
Factorial is 2 is 2
int n;
Factorial is 3 is 6
if(args.length==0)
Factorial is 4 is 24
System.out.println("no command line arguments");
for(int i=0;i<args.length;i++)
{
n=Integer.parseInt(args[i]);
int f=1;
for(int j=n;j>=1;j--)

{
f=f*j;
}
System.out.println("Factorial of " + n + "is "+f);
}
}
}

/*Part A - Program 2 -Write a program to display all prime numbers


between 2 limits */
import java.util.Scanner;
class Prime
{
public static void main(String args[])
{
int i,n,m,f;
System.out.println("Enter the lower limit ");
Scanner sc=new Scanner(System.in);
m=sc.nextInt();
System.out.println("Enter the upper limit "); Output :
n=sc.nextInt(); Enter the lower limit
System.out.println("the prime nos are: "); 3
Enter the upper limit
for (int j=m;j<=n;j++)
19
{ the prime nos are:
f=0; 3
for (i=2;i<j-1;i++) 5
{ 7
11
if (j%i==0) 13
f=1; 17
} 19
if (f==0)
System.out.println(j);
}} }
/*PART -A PROGRAM 3: Write a program to sort list of elements in ascending and descending
order and show the exception handling.*/
import java.util.*;
class bubblesort
{
public static void main(String args[])
{
int a[],i,j,n;

try
{
System.out.println("enter the no of elements");
Scanner sc=new Scanner(System.in);
n=sc.nextInt();
a=new int[6];
System.out.println("enter the elements");
for(i=0;i<n;i++)
a[i]=sc.nextInt();
for(i=0;i<n;i++)
for(j=0;j<n-i-1;j++)
if(a[j]>a[j+1])
{
int temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
System.out.println("the sorted array in ascending order");
for(i=0;i<n;i++)
System.out.print(a[i] + " ");
System.out.println("\nthe sorted array in descending order");
for(i=n-1;i>=0;i--)
System.out.print(a[i] + " ");
}
//try block end
catch(ArrayIndexOutOfBoundsException e)
{ Output : Enter the no of elements
System.out.println("Check no of elements "); 5
} enter the elements
catch(InputMismatchException e) 4
{ 5
System.out.println("Check the input "); 9
} 1
} 2
} the sorted array in ascending order
12459
the sorted array in descending order
95421
//Part A : Program 4 WAP for all string operations.
import java.util.Scanner;
class StringDemo
{
public static void main(String args[])
{
String str,str1;
Scanner sc=new Scanner(System.in);
System.out.println("Enter a string");
str=sc.nextLine();
System.out.println("upper="+str.toUpperCase());
System.out.println("original string"+ str);// strings are immutable
System.out.println("Comparison with string dscasc ="+str.compareTo("dscasc"));
System.out.println("lastindexOf('c')="+str.lastIndexOf('c'));
String str2="Apple";
System.out.println("Apple-indexOf(A)="+str2.indexOf('A'));
S0ystem.out.println("replace="+str.replace('c','d'));
System.out.println(str);
str1=str.concat(" hello");
System.out.println("after Concat"+str1);
str1="hello bca ";
System.out.println("After trim()"+str1.trim());
char ch[];
ch=str1.toCharArray();
for (int i=0;i<ch.length;i++)
System.out.print(ch[i]);
}
}

Output :
Enter a string
HelloDsi
upper=HELLODSI
original stringHelloDsi
Comparison with string dscsac =-28
lastindexOf()=-1
Apple- indexOf(A)=0
replace=HelloDsi
HelloDsi
HelloDsi hello
trim()hello bca
h
e
l
l
o

b
c
a
/*Part A: Prog 5:WAP in Java to find area of geometrical figures using method */
import java.util.Scanner; Output :
class Area enter the raidus of circle
{ 4
static double circle(double r) area of circle is48.0
{ enter the side of square
return(22/7*r*r); 4
} area of square16
static int rectangle (int l,int b) enter the l,b of rectangle
{ 4
return(l*b); 5
} area of a rectangle20
static int square (int s) enter the base and height of a triangel
{ 46
return(s*s); area of triangle is 12.0
}
static double triangle(double base,double height)
{
return(0.5*base*height);
}

public static void main(String args[])


{
Scanner sc=new Scanner(System.in);
System.out.println("enter the raidus of circle");
double r=sc.nextDouble();
System.out.println("area of circle is"+circle(r));
System.out.println("enter the side of square");
int s=sc.nextInt();
System.out.println("area of square"+square(s));
System.out.println("enter the l,b of rectangle");
int l=sc.nextInt();

int b=sc.nextInt();
System.out.println("area of a rectangle"+rectangle(l,b));
System.out.println("enter the base and height of a triangel");
double base=sc.nextDouble();
double height=sc.nextDouble();
System.out.println("area of triangle is "+triangle(base,height));
}
}
/* Part A program 6 Write a program to implement constructor overloading by passing
different number of parameter of different types.*/
class Box {
double width;
double height;
double depth;
Box(double w, double h, double d) {
width = w;
height = h;
depth = d;
}

Box() {
width = -1; // use -1 to indicate
height = -1; // an uninitialized
depth = -1; // box
}
Box(double len) {
width = height = depth = len;
}
double volume() {
return width * height * depth;
}
}
class OverloadCons {
public static void main(String args[]) {

Box mybox1 = new Box(10, 20, 15);


Box mybox2 = new Box();
Box mycube = new Box(7);
double vol;
vol = mybox1.volume();
System.out.println("Volume of mybox1 is " + vol);
vol = mybox2.volume();
System.out.println("Volume of mybox2 is " + vol);
vol = mycube.volume();
System.out.println("Volume of mycube is " + vol);
}
}

Output :
Volume of mybox1 is 3000.0
Volume of mybox2 is -1.0
Volume of mycube is 343.0
//Part A 7:Write a program to create student report
//using applet, read the input using text boxes and display the o/p using buttons.

import java.awt.*;
import java.applet.*;
import java.awt.event.*;
/*<applet code="StudReport" width=300 height=300>
</applet>*/
public class StudReport extends Applet implements ActionListener
{
Label lblTitle,lblRegNo,lblName,lblJava,lblSE,lblCA,lblBI,lblSSPD;
TextField txtRegNo,txtName,txtJava,txtSE,txtCA,txtBI,txtSSPD;
Button cmdReport;
int total;
float avg;
public void init()
{
setLayout(null);
lblTitle=new Label("Enter Student’s Details");
lblRegNo=new Label("Reg. No:");
lblName=new Label("Name:");
lblJava=new Label("Java:");
lblSE=new Label("SE:");
lblCA=new Label("CA:");
lblBI=new Label("BI:");
lblSSPD=new Label("SSPD:");
txtRegNo=new TextField(10);
txtName=new TextField(25);
txtJava=new TextField(3);
txtSE=new TextField(3);
txtCA=new TextField(3);
txtBI=new TextField(3);
txtSSPD=new TextField(3);
cmdReport=new Button("View Student Result");
lblTitle.setBounds(100,0,200,20);
lblRegNo.setBounds(0,50,100,20);
txtRegNo.setBounds(120,50,100,20);
lblName.setBounds(0,75,100,20);
txtName.setBounds(120,75,250,20);
lblJava.setBounds(0,100,100,20);
txtJava.setBounds(120,100,40,20);
lblSE.setBounds(0,125,100,20);
txtSE.setBounds(120,125,40,20);
lblCA.setBounds(0,150,100,20);
txtCA.setBounds(120,150,40,20);
lblBI.setBounds(0,175,100,20);
txtBI.setBounds(120,175,40,20);
lblSSPD.setBounds(0,200,100,20);
txtSSPD.setBounds(120,200,40,20);
cmdReport.setBounds(100,225,150,30);
add(lblTitle);
add(lblRegNo);add(txtRegNo);
add(lblName);add(txtName);
add(lblJava);add(txtJava);
add(lblSE);add(txtSE);
add(lblCA);add(txtCA);
add(lblBI);add(txtBI);
add(lblSSPD);add(txtSSPD);
add(cmdReport);
cmdReport.addActionListener(this);//registering the event with te event handler
(ActionListener)
}
public void actionPerformed(ActionEvent ae)
{
try
{
int java=Integer.parseInt(txtJava.getText());
int se=Integer.parseInt(txtSE.getText());
int ca=Integer.parseInt(txtCA.getText());
int bi=Integer.parseInt(txtBI.getText());
int sspd=Integer.parseInt(txtSSPD.getText());
total=(java+se+ca+bi+sspd);
avg=total/5;
}
catch(NumberFormatException e)
{
}
repaint();
}
public void paint(Graphics g)
{
g.drawString("STUDENT REPORT",100,275);
g.drawString("Reg. No.: "+txtRegNo.getText(),0,300);
g.drawString("Name : "+txtName.getText(),0,325);
g.drawString("Java: "+txtJava.getText(),0,350);
g.drawString("Software Engineering : "+txtSE.getText(),0,375);
g.drawString("Computer Architecture : "+txtCA.getText(),0,400);
g.drawString("Banking & Insurance : "+txtBI.getText(),0,425);
g.drawString("SSPD : "+txtSSPD.getText(),0,450);
g.drawString("Total: "+total,0,475);
g.drawString("Average: "+avg,0,500);
}
}
/*Part A Prog 8: WAP in Java to calculate and
display bonus of sales and accounts department using method overriding*/
abstract class Department
{
double salary,bonus,totalsalary;
public abstract void calBonus(double salary);
public void displaytotalsalary(String dept)
{
System.out.println(dept+"\t"+salary+"\t\t"+bonus+"\t"+totalsalary);
}
}
class Accounts extends Department
{
public void calBonus(double sal)
{
salary=sal;
bonus=sal*0.2;
totalsalary=salary+bonus;
}
}
class sales extends Department
{
public void calBonus(double sal)
{
salary=sal;
bonus=sal*0.3;
totalsalary=salary+bonus;
}
}

public class BonusCalculate


{
public static void main(String args[])
{
Department acc=new Accounts();
Department sales=new sales();
acc.calBonus(10000);
sales.calBonus(20000);
System.out.println("Department \t BasicSalary \t Bonus \t TotalSalary");
System.out.println("-----------------------------------------------------");
acc.displaytotalsalary("Accounts");
sales.displaytotalsalary("sales Dept");
System.out.println("-----------------------------------------------------");
}}

Output :
Department BasicSalary Bonus TotalSalary
-----------------------------------------------------
Accounts 10000.0 2000.0 12000.0
sales Dept 20000.0 6000.0 26000.0
-----------------------------------------------------
/*Part A-9 WAP to implement thread,applet and graphics by
implementing animation of moving ball */
import java.applet.*;
import java.awt.*;

/* <applet code="MovingBall" width=200 height=200>


</applet> */

public class MovingBall extends Applet implements Runnable


{
int x=150,y=50,r=50;
int dx=11,dy=7;
Thread animator;
volatile boolean pleaseStop;

public void paint(Graphics g)


{
g.setColor(Color.red);
g.fillOval(x-r,y-r,r*2,r*2);
}

public void animate()


{
Rectangle bounds=getBounds();
if((x-r+dx<0) || (x+r+dx>bounds.width))
dx=-dx;
if((y-r+dy<0) || (y+r+dy>bounds.height))
dy=-dy;
x+=dx;
y+=dy;
repaint();
}
public void run()
{
while(!pleaseStop)
{
animate();
try
{
Thread.sleep(100);
}
catch(InterruptedException e)
{
} }
}
public void start()
{ animator=new Thread(this);
pleaseStop=false;
animator.start(); }
public void stop()
{
pleaseStop=true;
} }
//Part A:Program 10: Demonstrate the mouse events.
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
/*
<applet code="MouseEvents" width=300 height=100>
</applet> */
public class MouseEvents extends Applet
implements MouseListener, MouseMotionListener
{
String msg = "";
int mouseX = 0, mouseY = 0; // coordinates of mouse
public void init()
{
addMouseListener(this);
addMouseMotionListener(this);
}
// Handle mouse clicked.
public void mouseClicked(MouseEvent me)
{
// save coordinates
mouseX = 0;
mouseY = 10;
msg = "Mouse clicked.";
repaint();
}
// Handle mouse entered.
public void mouseEntered(MouseEvent me)
{
// save coordinates
mouseX = 0;
mouseY = 10;
msg = "Mouse entered.";
repaint();
}
// Handle mouse exited.
public void mouseExited(MouseEvent me)
{ // save coordinates
mouseX = 0;
mouseY = 10;
msg = "Mouse exited.";
repaint();
}
public void mousePressed(MouseEvent me)
{ // save coordinates
mouseX = me.getX();
mouseY = me.getY();
msg = "Down";
repaint();
}

public void mouseReleased(MouseEvent me)


{ // save coordinates
mouseX = me.getX();
mouseY = me.getY();
msg = "Up";
repaint();
}
public void mouseDragged(MouseEvent me)
{ // save coordinates
mouseX = me.getX();
mouseY = me.getY();
msg = "*";
showStatus("Dragging mouse at " + mouseX + ", " + mouseY);
repaint();
}
public void mouseMoved(MouseEvent me)
{
// show status
showStatus("Moving mouse at " + me.getX() + ", " +me.getY());
}
public void paint(Graphics g)
{
g.drawString(msg, mouseX, mouseY);
}
}
Output :
PART B JAVA LAB PROGRAMS

/*1.Java Program to Check If a Given Number is ArmStrong Number


This is a Java Program to Check If a Given Number is ArmStrong Number.
Armstrong Number is an integer such that the sum of the cubes of its digits is equal to the number
itself
Enter the integer you want to check as an input.
Now we use modulos and division operations along with loops and if else statements to get the
output.
*/

import java.util.Scanner;
public class ArmStrong
{
public static void main(String[] args)
{
int n, count = 0, a, b, c, sum = 0;
Scanner s = new Scanner(System.in);
System.out.print("Enter any integer you want to check:");
n = s.nextInt();
a = n;
c = n;
while(a > 0)
{
a = a / 10;
count++;
}
while(n > 0)
{
b = n % 10;
sum = (int) (sum+Math.pow(b, count));
n = n / 10;
}
if(sum == c)
{
System.out.println("Given number is Armstrong");
}
else
{
System.out.println("Given number is not Armstrong");
}
}
}
Output:
$ javac ArmStrong.java
$ java ArmStrong

Enter any integer you want to check:153


Given number is Armstrong
2.JAVA PROGRAM TO CHECK IS A CHARACTER IS VOWEL OR CONSONANT.
*/
import java.util.Scanner;
class Vowel_Consonant
{
public static void main(String[] args)
{
char n;
Scanner sc=new Scanner(System.in);
System.out.print("Enter the character you want to check:");
n = sc.next().charAt(0); //String class in Java has a method charAt(index)"A" -> 'A'
switch(n)
{
case 'a':
case 'A':
System.out.println("The given character "+n+" is vowel");
break;
case 'e':
case 'E':
System.out.println("The given character "+n+" is vowel");
break; .Output:javac Vowel_Consonant .java
case 'i': java Vowel_Cononant
case 'I': Enter the character you want to check:a
System.out.println("The given character "+n+" is vowel"); The given character a is vowel
break;
case 'o': Enter the character you want to check:E
case 'O': The given character E is vowel
System.out.println("The given character "+n+" is vowel");
break;
case 'u':
case 'U':
System.out.println("The given character "+n+" is vowel");
break;
default:
System.out.println("The given character "+n+" is consonant");
break;
}
}
}
/*3.Java Program to Find the GCD and LCM of two Numbers*/
import java.util.Scanner;

public class GCD_LCM


{
static int gcd(int x, int y)
{
int r=0, a, b;
a = (x > y) ? x : y; // a is greater number
b = (x < y) ? x : y; // b is smaller number

r = b;
while(a % b != 0)
{
r = a % b;
a = b;
b = r;
}
return r;
}
static int lcm(int x, int y)
{
int a;
a = (x > y) ? x : y; // a is greater number
while(true)
{
if(a % x == 0 && a % y == 0)
return a;
++a;
}
}

public static void main(String args[])


{
Scanner input = new Scanner(System.in);
System.out.println("Enter the two numbers: ");
int x = input.nextInt();
int y = input.nextInt();

System.out.println("The GCD of two numbers is: " + gcd(x, y));


System.out.println("The LCM of two numbers is: " + lcm(x, y));
input.close();
}
}

/*
Output:
$ javac GCD_LCM.java
$ java GCD_LCM

Enter the two numbers:


15
25
The GCD of two numbers is: 5
The LCM of two numbers is: 75
/*4.Write a program for printing Fibonacci Sequence */
import java.util.Scanner;
public class Fibonacci
{
public static void main(String args[])
{
int fib1=0,fib2=1,temp=0,num;
Scanner sc=new Scanner(System.in);
System.out.println("Enter Limit:");
num=sc.nextInt();
System.out.print(fib1);
System.out.print(fib2);
for(int i=2;i<num;i++)
{
temp=fib1+fib2;
System.out.print(temp);
fib1=fib2;
fib2=temp;
}
}
}

Output:
Javac Fibonacci.java
Java Fibonacci
Enter Limit:
5
01123

5.WAP to Copy a File to another File in Java


*/

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class CopyExample
{
public static void main(String[] args)
{
FileInputStream instream = null;
FileOutputStream outstream = null;

try{
File infile =new File("C:\\MyInputFile.txt");
File outfile =new File("C:\\MyOutputFile.txt");

instream = new FileInputStream(infile);


outstream = new FileOutputStream(outfile);

byte[] buffer = new byte[1024];

int length;

while ((length = instream.read(buffer)) > 0){


outstream.write(buffer, 0, length);
}

instream.close();
outstream.close();

System.out.println("File copied successfully!!");

}catch(IOException ioe){
ioe.printStackTrace();
}
}
}

/*
Output:
File copied successfully!!
*/

// Part B program 6 : Write a program for demonstrating Vector class


import java.util.Vector;
public class BasicVectorOperations {

public static void main(String a[]){


Vector vct = new Vector();//create an object of the Vector class
vct.add("First");
vct.add("Second");
vct.add("Third");
System.out.println(vct);
System.out.println(vct.size());
System.out.println(vct.capacity());
vct.add(2,"Random");
System.out.println(vct);
System.out.println("Element at index 3 is: "+vct.get(3));
System.out.println("The first element of this vector is: "+vct.firstElement());
System.out.println("The last element of this vector is: "+vct.lastElement());
vct.insertElementAt("Sixth",4);
System.out.println("The last element of this vector is: "+vct.lastElement());
System.out.println("Is this vector empty? "+vct.isEmpty());//false
vct.removeAllElements();
System.out.println("Is this vector empty? "+vct.isEmpty()); // true
vct.add("First");
vct.add("Second");
vct.add("Third");
System.out.println("Vector is now"+ vct);
System.out.println(vct.contains("Fourth"));
vct.remove("First");
System.out.println("Vector is now"+ vct);

}
}

Output : 1. javac -Xlint BasicVectorOperations.java (only for this program )


2.java BasicVectorOperations

[First, Second, Third]


3
10
[First, Second, Random, Third]
Element at index 3 is: Third
The first element of this vector is: First
The last element of this vector is: Third
The last element of this vector is: Sixth
Is this vector empty? false
Is this vector empty? true
Vector is now[First, Second, Third]
false
Vector is now[Second, Third]

/* 7:WAP in Java to find area of geometrical figures using method overloading */


import java.util.Scanner;
class Area
{
static double area(double r)
{
return(22/7*r*r);
}
static int area(int l,int b)
{
return(l*b);
}
static int area(int s)
{
return(s*s);
}
static double area(double base,double height)
{
return(0.5*base*height);
}

public static void main(String args[])


{ Output:
Scanner sc=new Scanner(System.in); enter the raidus of circle
4
System.out.println("enter the raidus of circle");
area of circle is48.0
double r=sc.nextDouble(); enter the side of square
System.out.println("area of circle is"+area(r)); 3
System.out.println("enter the side of square"); area of square9
int s=sc.nextInt(); enter the l,b of rectangle
System.out.println("area of square"+area(s)); 67
System.out.println("enter the l,b of rectangle"); area of a rectangle42
int l=sc.nextInt(); enter the base and height of a triangel
int b=sc.nextInt(); 7
System.out.println("area of a rectangle"+area(l,b)); 5
area of triangle is 17.5
System.out.println("enter the base and height of a triangel");
double base=sc.nextDouble();
double height=sc.nextDouble();
System.out.println("area of triangle is "+area(base,height)); Output :
} Enter the limit
} 5
*
// Program 8:WAP in Java for displaying Rhombus pattern ***
import java.util.Scanner; *****
class Rhombuspattern *******
{ *********
public static void main(String args[]) *******
{ *****
***
*
int i,j,limit;
Scanner sc=new Scanner(System.in);
System.out.println("Enter the limit");
limit=sc.nextInt();
for(i=1;i<=limit;i++)
{
for(j=limit-i;j>0;j--)
System.out.print(" ");
for(j=1;j<=2 * i - 1; j++)
System.out.print("*");
System.out.println();
}
for(i=limit -1; i>=1;i--)
{
for(j=1;j<=limit -i;j++)
System.out.print(" ");
for(j=1;j<=2 * i -1; j++)
System.out.print("*");
System.out.println();
}}}

/*9.WAP in JAVA to create a thread .*/

class NewThread implements Runnable {


String name;
Thread t;
NewThread(String threadname) {
name=threadname;
t = new Thread(this, name);
System.out.println("New thread: " + t); Output :
t.start(); // Start the thread New thread: Thread[one,5,main]
} New thread: Thread[two,5,main]
// This is the entry point for the second thread. one:5
public void run() { two:5
try { New thread:
for(int i = 5; i > 0; i--) { Thread[three,5,main]
System.out.println(name+ ":" + i); Main Thread: 5
Thread.sleep(500); three:5
} one:4
} catch (InterruptedException e) { two:4
System.out.println("Child interrupted."); three:4
} one:3
System.out.println("Exiting " + name +" thread."); two:3
} Main Thread: 4
} three:3
class ThreadDemo { one:2
public static void main(String args[]) { two:2
new NewThread("one");//first three:2
one:1
new NewThread("two");//second two:1
new NewThread("three"); // create a third thread Main Thread: 3
try { three:1
for(int i = 5; i > 0; i--) { Exiting one thread.
System.out.println("Main Thread: " + i); Exiting two thread.
Thread.sleep(1000); Exiting three thread.
} Main Thread: 2
} catch (InterruptedException e) { Main Thread: 1
System.out.println("Main thread interrupted."); Main thread exiting.
}
System.out.println("Main thread exiting.");
}

/*
10.WAP to play sound using Applet in JAVA
*/
import java.applet.*;
import java.awt.*;
import java.awt.event.*;

public class PlaySoundApplet extends Applet implements ActionListener


{
Button play,stop;
AudioClip audioClip;

public void init() {


play = new Button(" Play in Loop ");
add(play);
play.addActionListener(this);
stop = new Button(" Stop ");
add(stop);
stop.addActionListener(this);
audioClip = getAudioClip(getCodeBase(), "Sound.wav");
}
public void actionPerformed(ActionEvent ae) {
Button source = (Button)ae.getSource();
if (source.getLabel() == " Play in Loop ") {
audioClip.play();
} else if(source.getLabel() == " Stop "){
audioClip.stop();
}
}
}

Output :

You might also like