0% found this document useful (0 votes)
34 views48 pages

Advance Java Assignment No1

The document contains 12 questions about advanced Java concepts like threads, multithreading, and thread methods. It provides code examples to demonstrate thread usage for tasks like displaying a range of numbers or letters with a delay between each output.

Uploaded by

Isha Thakre
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
0% found this document useful (0 votes)
34 views48 pages

Advance Java Assignment No1

The document contains 12 questions about advanced Java concepts like threads, multithreading, and thread methods. It provides code examples to demonstrate thread usage for tasks like displaying a range of numbers or letters with a delay between each output.

Uploaded by

Isha Thakre
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 48

Advance Java Assignment No: 1

Name: - Angad Aware


Sec: - B
Roll No: - 09

1. Explain the concept of Thread in Advance Java.


2. Explain five steps for Implementing Thread with class.
3. Explain Multithreading in Advanced Java.
4. Explain the methods of Thread wait (), notify (), and notify All ()
5. Explain the methods start () and sleep ()
/* 6.Accept Start no and End no from user and display the numbers in that range and each number
should be displayed after two seconds (Using Thread with class).*/
import java.io.*;
import java.lang.*;
class abc extends Thread
{
int i=0;
int sno=0;
int eno=0;
public abc()
{
super();
try
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));

System.out.print("Enter the Start Number :: ");


sno=Integer.parseInt(br.readLine());

System.out.print("Enter the End Number :: ");


eno=Integer.parseInt(br.readLine());

1
start();
}
catch(Exception e)
{
System.out.println(e);
}
}
public void run()
{
try
{
System.out.println("Following are the all Numbers from "+sno+" to "+eno+"\n");
for(i=sno;i<=eno;i++)
{
System.out.println("\n\n\t\t"+i);

sleep(2000);
}
}
catch(Exception e)
{
System.out.println(e);
}
}
}
class Q06
{
public static void main(String args[])
{
abc x=new abc();
2
}
}

/* 7.Write a Java program that displays letters from A to Z on the command prompt, with each letter
appearing after a three seconds of time interval.*/
import java.io.*;
import java.lang.*;
class abc extends Thread
{
int i=0;
public abc()
{
super();
try
{

start();
}
catch(Exception e)
{
System.out.println(e);
}
}
public void run()
{
try
{
System.out.println("Following are the all Letters from A to Z.");
for (char letter = 'A'; letter <= 'Z'; letter++)
{
System.out.print(letter + " ");
3
sleep(3000);
}
}
catch(Exception e)
{
System.out.println(e);
}
}
}
class Q07
{
public static void main(String args[])
{
abc x=new abc();
}
}

/* 08. Write a Java program that accept start and end number from user and display all the Even
numbers from that range on command prompt after every two seconds of time interval.*/

import java.io.*;
import java.lang.*;
class abc extends Thread
{
int i=0;
int sno=0;
int eno=0;
public abc()
{
super();
try
4
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));

System.out.print("Enter the Start Number :: ");


sno=Integer.parseInt(br.readLine());

System.out.print("Enter the End Number :: ");


eno=Integer.parseInt(br.readLine());

start();
}
catch(Exception e)
{
System.out.println(e);
}
}
public void run()
{
try
{
System.out.println("Following are the all even Numbers from "+sno+" to "+eno+"\
n");
for(i=sno;i<=eno;i++)
{
if(i%2==0)
{
System.out.println("\n\n\t\t"+i);

sleep(2000);
}
}
5
}
catch(Exception e)
{
System.out.println(e);
}
}
}
class Q08
{
public static void main(String args[])
{
abc x=new abc();
}
}

/* 09. Write a Java program to display reverse of numbers from 90 to 50 on command prompt with time
interval of two seconds by using Thread with class.*/

import java.io.*;
import java.lang.*;
class abc extends Thread
{
int i=0;
int sno=50;
int eno=90;
public abc()
{
super();
try
{
start();
6
}
catch(Exception e)
{
System.out.println(e);
}
}
public void run()
{
try
{
System.out.println("\n\nFollowing are the all Numbers in Reverse order from
"+eno+" to "+sno+"\n");
for(i=eno;i>=sno;i--)
{
System.out.print("\t"+i);

sleep(2000);
}
System.out.println("\n\n");
}
catch(Exception e)
{
System.out.println(e);
}
}
}
class Q09
{
public static void main(String args[])
{
abc x=new abc();
7
}
}

/* 10. Write a Java program that demonstrate wait(), notify() and notifyAll() by using Thread with class.
*/
import java.io.*;
import java.lang.*;
class xyz extends Thread
{
int i=0;
int sum=0;

public void run()


{
try
{
synchronized(this)
{
for(i=1;i<=1000;i++)
{
sum=sum+i;
}
//this.notify();
this.notifyAll();
}
}
catch(Exception e)
{
System.out.println(e);
}

8
}
}
class Q10
{
public static void main(String args[])
{
try
{
xyz x=new xyz();
x.start();
synchronized(x)
{
x.wait();
System.out.println("\n\nThe Addition is = "+x.sum+"\n\n");
}
}
catch(Exception e)
{
System.out.println();
}
}
}

/* 11. Write Java program to demonstrate Multithreading where first Thread will print all numbers from
1 to 20 range and second Thread will print all the numbers from 70 to 90 after two seconds of time
interval.
*/
import java.io.*;
import java.lang.*;
class abc extends Thread
{

9
int i=0;
public abc()
{
super();
i=1;
start();
}
public void run()
{
try
{
for(i=1;i<=20;i++)
{
System.out.println("\n\t"+i);
sleep(2000);
}
}
catch(Exception e)
{
System.out.println(e);
}
}
}
class xyz extends Thread
{
int j=0;
public xyz()
{
super();
j=101;
start();
10
}
public void run()
{
try
{
for(j=70;j<=90;j++)
{
System.out.println("\n\t"+j);
sleep(2000);
}
System.out.println("\n\n");
}
catch(Exception e)
{
System.out.println(e);
}
}
}
class Q11
{
public static void main(String args[])
{
abc x=new abc();
xyz y=new xyz();
}
}

/* 12. Write Java program to demonstrate Multithreading where first Thread will print all letters from A
to Z and second Thread will print all the numbers from 50 to 76 after two seconds of time interval. */
import java.io.*;
import java.lang.*;
11
class abc extends Thread
{
String ch;

public abc()
{
super();
start();
}
public void run()
{
try
{
for(char ch ='A'; ch<='Z'; ch++)
{
System.out.println("\n\t\t"+ch);
sleep(2000);
}
}
catch(Exception e)
{
System.out.println(e);
}
}
}
class xyz extends Thread
{
int i=0;

public xyz()
{
12
super();
i=1;
start();
}
public void run()
{
try
{
for(i=50;i<=76;i++)
{
System.out.println("\n\t\t"+i);
sleep(2000);
}
}
catch(Exception e)
{
System.out.println(e);
}
}
}
class Q12
{
public static void main(String args[])
{
abc x=new abc();
xyz y=new xyz();
}
}

13
/* 13. Write Java program to demonstrate Multithreading where first Thread will print all the Even
numbers from 1 to 20 range and second Thread will print all the Odd numbers from 40 to 60 after two
seconds of time interval.*/
import java.io.*;
import java.lang.*;
class abc extends Thread
{
int i;
public abc()
{
super();
start();
}
public void run()
{
try
{
for(i=1;i<=20;i++)
{
if(i%2==0)
{
System.out.println("\n\t\t"+i);
sleep(2000);
}
}
}
catch(Exception e)
{
System.out.println(e);
}
}

14
}
class xyz extends Thread
{
int i=0;

public xyz()
{
super();
start();
}
public void run()
{
try
{
for(i=40;i<=60;i++)
{
if(i%2==1)
{
System.out.println("\n\t\t"+i);
sleep(2000);
}
}
}
catch(Exception e)
{
System.out.println(e);
}
}
}
class Q13
{
15
public static void main(String args[])
{
abc x=new abc();
xyz y=new xyz();
}
}

/* 14. Write a Java program for Multithreading where first Thread will print all the uppercase letters
from A to Z and second Thread will print all the lowercase letters from a to z on command prompt and
each letter should be displayed after the two second. */
import java.io.*;
import java.lang.*;
class abc extends Thread
{
String ch;

public abc()
{
super();
start();
}
public void run()
{
try
{
for(char ch ='A'; ch<='Z'; ch++)
{
System.out.println("\n\t\t"+ch);
sleep(2000);
}
}

16
catch(Exception e)
{
System.out.println(e);
}
}
}
class xyz extends Thread
{
String ch;
public xyz()
{
super();
start();
}
public void run()
{
try
{
for(char ch ='a'; ch<='z'; ch++)
{
System.out.println("\n\t\t"+ch);
sleep(2000);
}
}
catch(Exception e)
{
System.out.println(e);
}
}
}
class Q14
17
{
public static void main(String args[])
{
abc x=new abc();
xyz y=new xyz();
}
}

/* 15. Write a Java program where first Thread will print ICCS for 20 times and second Thread will print
Indira for 20 times on command prompt and provide 2000 value to sleep() method by using
multithreading. */
import java.io.*;
import java.lang.*;
class abc extends Thread
{
String ch="ICCS";
int i=0;
public abc()
{
super();
start();
}
public void run()
{
try
{
for(i=1;i<=20;i++)
{
System.out.println("\n\t\t"+i+" "+ch);
sleep(2000);
}

18
}
catch(Exception e)
{
System.out.println(e);
}
}
}
class xyz extends Thread
{
String ch="Indira";
int i=0;
public xyz()
{
super();
start();
}
public void run()
{
try
{
for(i=1; i<=20; i++)
{
System.out.println("\n\t\t"+i+" "+ch);
sleep(2000);
}
}
catch(Exception e)
{
System.out.println(e);
}
}
19
}
class Q15
{
public static void main(String args[])
{
abc x=new abc();
xyz y=new xyz();
}
}

/* 16. Develop a Java applet application that includes a button. When Onclick the button, the applet
should display numbers from 1 to 15, with each number appearing after a two-second interval (Use
Thread with Applet).*/
import java.io.*;
import java.lang.*;
import java.awt.*;
import java.awt.event.*;
import java.applet.*;

public class Q16 extends Applet implements ActionListener,Runnable


{
Thread t;
int i=0;
Button b1;
public void init()
{
t=new Thread(this);
b1=new Button("START");
i=1;
}
public void start()

20
{
add(b1);

setSize(400, 400);
setVisible(true);

b1.addActionListener(this);
}
public void actionPerformed(ActionEvent ae)
{
Object ob=ae.getSource();
if(ob==b1)
{
t.start();
}
}
public void run()
{
try
{
for(i=1;i<=15;i++)
{
System.out.println("\n\t\t"+i);
t.sleep(2000);
}
}
catch(Exception e)
{
System.out.println(e);
}
}
21
}

/* 17. Implement Thread with Applet of Advance Java to develop applet application that consist of two
Buttons Start and Stop and Onclick of Start Button display all the Even numbers from 1 to 40. Onclick of
Stop Button, Stop displaying numbers.*/
import java.io.*;
import java.lang.*;
import java.awt.*;
import java.awt.event.*;
import java.applet.*;

public class Q17 extends Applet implements ActionListener,Runnable


{
Thread t;
int i=0;
Button b1,b2;

public void init()


{
t=new Thread(this);
i=1;
b1=new Button("START");
b2=new Button("STOP");
}
public void start()
{
add(b1);
add(b2);

setSize(400,400);
setVisible(true);

22
b1.addActionListener(this);
b2.addActionListener(this);
}
public void actionPerformed(ActionEvent ae)
{
Object ob=ae.getSource();
if(ob==b1)
{
t.start();
}
else
{
if(ob==b2)
{
t.stop();
}
}
}
public void run()
{
try
{
System.out.println("Following are the all Even Numbers from 1 to 40.\n\n");
for(i=1;i<=40;i++)
{
if(i%2==0)
{
System.out.println("\n\t\t"+i);
}
t.sleep(1000);
23
}
}
catch(Exception e)
{
System.out.println(e);
}
}
}

/* 18. Write a Java program to create Applet Frame that consist of two Button Start and Stop. Onclick of
Start Button display ICCS for 15 times and Onclick of Stop Button stop displaying message. */
import java.io.*;
import java.lang.*;
import java.awt.*;
import java.awt.event.*;
import java.applet.*;

public class Q18 extends Applet implements ActionListener,Runnable


{
Thread t;
int i=0;
Button b1,b2;

public void init()


{
t=new Thread(this);
i=1;
b1=new Button("START");
b2=new Button("STOP");
}
24
public void start()
{
add(b1);
add(b2);

setSize(400,400);
setVisible(true);

b1.addActionListener(this);
b2.addActionListener(this);
}
public void actionPerformed(ActionEvent ae)
{
Object ob=ae.getSource();
if(ob==b1)
{
t.start();
}
else
{
if(ob==b2)
{
t.stop();
}
}
}
public void run()
{
try
{
for(i=1;i<=15;i++)
25
{
System.out.println("\n\t\t"+i+" ICCS");
t.sleep(1000);
}
}
catch(Exception e)
{
System.out.println(e);
}
}
}

/* 19. Write a Java program to create Applet application that consist of Three Labels, Three Text field and
Two Buttons. Accept Start no in Text field 1 and End no in Text field 2 from user and Onclick of Start
Button display all numbers in text field 3. Onclick of Stop Button stop displaying. . */

import java.io.*;
import java.lang.*;
import java.awt.*;
import java.awt.event.*;
import java.applet.*;

public class Q19 extends Applet implements ActionListener,Runnable


{
Thread t;
Label l1,l2,l3;
TextField t1,t2,t3;
Button b1,b2;
int sno=0;
int eno=0;
int i=0;

26
public void init()
{
t=new Thread(this);
l1=new Label("Enter the Start Number :: ");
l2=new Label("Enter the End Number :: ");
l3=new Label("Result :: ");
}
public void start()
{
t1=new TextField(30);
t2=new TextField(30);
t3=new TextField(30);

b1=new Button("START");
b2=new Button("STOP");

add(l1);
add(t1);

add(l2);
add(t2);

add(l3);
add(t3);

add(b1);
add(b2);

setSize(400,400);
setVisible(true);
27
b1.addActionListener(this);
b2.addActionListener(this);
}
public void actionPerformed(ActionEvent ae)
{
Object ob=ae.getSource();

if(ob==b1)
{
t.start();
}
else
{
if(ob==b2)
{
t.stop();
}
}
}
public void run()
{
try
{
sno=Integer.parseInt(t1.getText());
eno=Integer.parseInt(t2.getText());

for(i=sno;i<=eno;i++)
{
t3.setText(""+i);
t.sleep(2000);
28
}
}
catch(Exception e)
{
System.out.println(e);
}
}
}

/* 20. Write a Java program to create an Applet application that consist of two Buttons Start and Stop.
Onclick of Start button demonstrate Bouncing Ball. The ball should change its colour on each movement.
Onclick of stop Button stop activity. */
import java.io.*;
import java.lang.*;
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import java.applet.*;

public class Q20 extends Applet implements ActionListener,Runnable


{
Thread t;

Button b1,b2;
int y=0;
int a=0;

public void init()


{
t=new Thread(this);
}

29
public void start()
{
b1=new Button("START");
b2=new Button("STOP");

add(b1);
add(b2);

setSize(500,500);
setVisible(true);

b1.addActionListener(this);
b2.addActionListener(this);

}
public void actionPerformed(ActionEvent ae)
{
Object ob=ae.getSource();

if(ob==b1)
{
t.start();
}
else
{
if(ob==b2)
{
t.stop();
}
}
}
30
public void run()
{
try
{
while(true)
{
if(y<=850 && a==0)
{
setBackground(Color.ORANGE);
y=y+10;
repaint();
t.sleep(50);

if(y==850)
{
a=1;
}
}
else
{
if(y>=0 && a==1)
{
setBackground(Color.BLACK);
y=y-10;
repaint();
t.sleep(50);
}
if(y==0)
{
a=0;
}
31
}
}
}
catch(Exception e)
{
System.out.println(e);
}
}
public void paint(Graphics g)
{
Random r=new Random();

int a1=r.nextInt(150);
int a2=r.nextInt(200);
int a3=r.nextInt(100);

Color c=new Color(a1,a2,a3);

g.setColor(c);
g.fillOval(100,y,100,100);
}
}

/* 21. Write a Java program to Rotate ICCS String in Diamond shape of the Applet Frame and Onclick of
Start Button Rotate String label in given directions and Stop displaying onclick of stop Button.

32
*/

import java.io.*;
import java.lang.*;
import java.awt.*;
import java.awt.event.*;
import java.applet.*;

public class Q21 extends Applet implements ActionListener,Runnable

{
Thread t;
Button b1,b2;
Label l1;

int d=0;
int x=0;
int y;

public void init()


{
t=new Thread(this);
}
public void start()
{

x=0;
y=200;
l1=new Label("INDIRA");
l1.setBounds(x,y,100,100);

33
b1=new Button("START");
b2=new Button("STOP");

add(b1);
add(b2);

add(l1);

setSize(500,500);
setVisible(true);

b1.addActionListener(this);
b2.addActionListener(this);
}
public void actionPerformed(ActionEvent ae)
{
Object ob=ae.getSource();

if(ob==b1)
{
t.start();
}
else
{
if(ob==b2)
{
t.stop();
}
}
}
34
public void run()
{
try
{
while(true)
{
if(x<=200 && y>=0 && d==0)
{
x=x+10;
y=y-10;
l1.setBounds(x,y,100,100);
t.sleep(100);
}
else
{
if(x<=400 && y<=200)
{
x=x+10;
y=y+10;
l1.setBounds(x,y,100,100);
t.sleep(100);
d=1;
}
else
{
if(x>=200 && y<=400)
{
x=x-10;
y=y+10;
l1.setBounds(x,y,100,100);
t.sleep(100);
35
}
else
{
if(x>=0 && y>=200 && d==1)
{
x=x-10;
y=y-10;
l1.setBounds(x,y,100,100);
t.sleep(100);
}
else
{
d=0;
}
}
}
}
}
}
catch(Exception e)
{
System.out.println(e);
}
}
}

/* 22. Write a Java program to create an Applet application that consist of Buttons and Onclick of Start
Button Demonstrate Traffic signal with help of Red, Yellow and Green colours. Onclick of stop Button stop
the activity..*/

36
import java.io.*;
import java.lang.*;
import java.awt.*;
import java.awt.event.*;
import java.applet.*;

public class Q22 extends Applet implements ActionListener,Runnable


{
Thread t;
Button b1,b2;

Boolean red,yellow,green;

public void init()


{
t=new Thread(this);
}
public void start()
{
red=true;

b1=new Button("START");
b2=new Button("STOP");

add(b1);
add(b2);

setSize(400,400);
setVisible(true);

b1.addActionListener(this);
37
b2.addActionListener(this);
}
public void actionPerformed(ActionEvent ae)
{
Object ob=ae.getSource();
if(ob==b1)
{
t.start();
}
else
{
if(ob==b2)
{
t.stop();
}

}
}
public void run()
{
try
{
while(true)
{
if(red==true)
{
red=false;
yellow=true;
green=false;
}
else
38
{
if(yellow==true)
{
yellow=false;
red=false;
green=true;
}
else
{
if(green==true)
{
green=false;
red=true;
yellow=false;
}
}
}
repaint();
t.sleep(1000);
}
}
catch(Exception e)
{
System.out.println(e);
}
}
public void paint(Graphics g)
{
if(red==true)
{
g.setColor(Color.RED);
39
g.fillOval(300,100,100,100);
}
else
{
if(yellow==true)
{
g.setColor(Color.YELLOW);
g.fillOval(300,200,100,100);
}
else
{
if(green==true)
{
g.setColor(Color.GREEN);
g.fillOval(300,300,100,100);
}
}
}
}
}

/* 23. Write a Java program to create Applet application that Swap the names of Buttons. Take two
Button give them name as left and right and onclick on left button start swapping the names of button.
*/
import java.io.*;
import java.lang.*;
import java.awt.*;
import java.awt.event.*;
import java.applet.*;

40
public class Q23 extends Applet implements ActionListener,Runnable
{
Thread t;
Button b1,b2;
String s1,s2;

public void init()


{
t=new Thread(this);

}
public void start()
{
b1=new Button("left");
b2=new Button("right");

add(b1);
add(b2);

setSize(400,400);
setVisible(true);

b1.addActionListener(this);
b2.addActionListener(this);
}
public void actionPerformed(ActionEvent ae)
{
Object ob=ae.getSource();
if(ob==b1)
{
41
t.start();
}
else
{
if(ob==b2)
{
t.stop();
}
}
}
public void run()
{
try
{
while(true)
{
s1=b1.getLabel();
s2=b2.getLabel();

b1.setLabel(s2 );
b2.setLabel(s1);

t.sleep(1000);
}

}
catch(Exception e)
{
System.out.println(e);
}
42
}
}

/* 24. Write a Java program to develop Applet application that display counter onclick of start button
and stop displaying counter onclick of stop button. Onclick of restart button start counter from 1.*/

import java.io.*;
import java.lang.*;
import java.awt.*;
import java.awt.event.*;
import java.applet.*;

public class Q24 extends Applet implements ActionListener,Runnable


{
Thread t;
Button b1,b2,b3;
Label l1;
int cnt=0;

public void init()


{
t=new Thread(this);

}
public void start()
{
Font f=new Font("CAMBRIA",Font.BOLD,30);

l1=new Label("COUNTER START FROM HERE.");


l1.setFont(f);
43
b1=new Button("START");
b2=new Button("STOP");
b3=new Button("RESTART");

add(b1);
add(b2);
add(b3);
add(l1);

setSize(400,400);
setVisible(true);

b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
}
public void actionPerformed(ActionEvent ae)
{
Object ob=ae.getSource();
if(ob==b1)
{
t.start();
}
else
{
if(ob==b2)
{
t.stop();
}
44
else
{
if(ob==b3)
{
cnt=0;

}
}
}
}
public void run()
{
try
{
while(true)
{
cnt++;
l1.setText(""+cnt);
t.sleep(1000);
}
}
catch(Exception e)
{
System.out.println(e);
}
}
}

/* 25. Develop a Applet application where onclick of start button the background colour start changing
from red, black and pink. And onclick of stop button the activity get stopped. */
import java.io.*;
45
import java.lang.*;
import java.awt.*;
import java.awt.event.*;
import java.applet.*;

public class Q25 extends Applet implements ActionListener,Runnable


{
Thread t;
Button b1;
Button b2;
public void init()
{
t=new Thread(this);

b1=new Button("START");
b2=new Button("STOP");
}
public void start()
{
add(b1);
add(b2);

setSize(400,400);
setVisible(true);

b1.addActionListener(this);
b2.addActionListener(this);
}
public void actionPerformed(ActionEvent ae)
{
Object ob=ae.getSource();
46
if(ob==b1)
{
t.start();
}
else
{
if(ob==b2)
{
t.stop();
}
}
}
public void run()
{
try
{
while(true)
{
setBackground(Color.RED);
t.sleep(200);

setBackground(Color.BLACK);
t.sleep(200);

setBackground(Color.PINK);
t.sleep(200);
}
}
catch(Exception e)
{
System.out.println(e);
47
}
}
}

48

You might also like