102 Avani Practical Ass3
102 Avani Practical Ass3
ASSIGNMENT-3
1. Write a program which show example of Exception handling in which all keywords try,
catch, finally, throw and throws are used.
class ThrowsExecp
{
static void fun() throws IllegalAccessException
{
System.out.println("Inside fun(). ");
throw new IllegalAccessException("demo");
}
public static void main(String args[])
{
try
{
fun();
}
catch (IllegalAccessException e)
{
System.out.println("caught in main.");
}
finally
{
System.out.println("this is finally block");
}
}
}
2. Write a program which show finally block is called after try/catch block.
class ThrowsExecp
{
static void fun() throws IllegalAccessException
{
System.out.println("Inside fun(). ");
throw new IllegalAccessException("demo");
}
public static void main(String args[])
{
finally
1
SYBCA-DIV-2-JAVA AVANI JOSHI ROLL NO:-102
{
System.out.println("this is finally block");
}
try
{
fun();
}
catch (IllegalAccessException e)
{
System.out.println("caught in main.");
}
}
}
3. Write a program which show finally block is called before function return.
class FinallyDemo
{
public static int myMethod()
{
try
{
return 0;
}
finally
{
System.out.println("Inside Finally block");
}
}
public static void main(String args[])
{
System.out.println(FinallyDemo.myMethod());
}
}
4. Create your own User defined exception GreaterTenException if value is given greater
than 10.
import java.io.*;
2
SYBCA-DIV-2-JAVA AVANI JOSHI ROLL NO:-102
{
private int detail;
GreaterTenException(int a)
{
detail = a;
}
public String toString()
{
return "GreaterTenException["+detail+"]";
}
}
class Demo
{
static void takeSmallerTen(int a)
{
if(a<10)
System.out.println(a);
else
throw new GreaterTenException(a);
}
public static void main(String args[])
{
int a=0;
BufferedReader br = new BufferedReader(new
InputStreamReader(System.in));
try
{
System.out.print("Enter value (value<10):");
a = Integer.parseInt(br.readLine());
takeSmallerTen(a);
}
catch(Exception e)
{
System.out.println(e);
}
}
}
5. Create your own User defined exception InvalidCharException if any one of the char
@,*,? is present in given string.
3
SYBCA-DIV-2-JAVA AVANI JOSHI ROLL NO:-102
class GFG
{
public static void main (String[] args)
{
// array of size 4.
int[] arr = new int[4];
try
{
int i = arr[4];
// this statement will never execute
// as exception is raised by above statement
System.out.println("Inside try block");
}
catch(ArrayIndexOutOfBoundsException ex)
{
System.out.println("Exception caught in catch block");
}
finally
{
System.out.println("finally block executed");
}
6. Create java application which display clock. Display time using thread concept.
import java.applet.*;
import java.awt.*;
import java.util.*;
import java.text.*;
4
SYBCA-DIV-2-JAVA AVANI JOSHI ROLL NO:-102
7. Write a program to accept 10 names from the user and print all the names which start
from B with interval of 2 sec.
5
SYBCA-DIV-2-JAVA AVANI JOSHI ROLL NO:-102
import java.util.Scanner;
}
}
6
SYBCA-DIV-2-JAVA AVANI JOSHI ROLL NO:-102
}
}
class MyResource
{
synchronized void useRes()
{
System.out.print("[");
try
{
Thread.sleep(1000);
}
catch(Exception e)
{
}
System.out.print("]");
}
}
class MyThread implements Runnable
{
Thread t;
MyResource res;
MyThread(MyResource r)
{
res = r;
t = new Thread(this);
t.start();
}
public void run()
{
res.useRes();
}
}
class Demo
{
public static void main(String args[])
{
7
SYBCA-DIV-2-JAVA AVANI JOSHI ROLL NO:-102
10. Write a program which take one resource object and use its display method to display
0 to 5 for One Thread and same method display 10 to 15 for other thread. Use
synchronization concept by using synchronized block on resource object and called its
disp(int n) method in it. (output : 0 1 2 3 4 5 10 11 12 13 14 15 )
class Table
{
synchronized void disp(int n, int m)
{
for(;n<=m;n++)
{
System.out.println(n);
try
{
Thread.sleep(200);
}
catch(Exception e)
{
System.out.println(e);
}
}
}
}
class MyThread1 extends Thread
{
Table t;
MyThread1(Table t)
{
this.t=t;
}
public void run()
{
t.disp(0, 5);
}
}
8
SYBCA-DIV-2-JAVA AVANI JOSHI ROLL NO:-102
11. Write a program which show deadlock in the concept of synchronization in java.
class Res1
{
synchronized void task1(Res2 r2)
{
System.out.print("[");
try
{
Thread.sleep(1000);
}
catch(Exception e)
{
}
r2.fun2();
System.out.print("]");
}
9
SYBCA-DIV-2-JAVA AVANI JOSHI ROLL NO:-102
10
SYBCA-DIV-2-JAVA AVANI JOSHI ROLL NO:-102
re2.fun1(re1);
}
}
class Demo
{
public static void main(String args[])
{
Res1 ob1 = new Res1();
Res2 ob2 = new Res2();
MyThread t1 = new MyThread(ob1,ob2);
MyThread t2 = new MyThread(ob1,ob2);
}
}
class MyResource
{
int n;
boolean valueSet = false;
synchronized void put(int n)
{
if(valueSet)
try
{
wait();
}
catch(Exception e)
{
}
this.n = n;GUAGE
valueSet = true;
System.out.println("Put: " + n);
notify();
}
synchronized void get()
{
if(!valueSet)
try
{
wait();
11
SYBCA-DIV-2-JAVA AVANI JOSHI ROLL NO:-102
}
catch(Exception e)
{
}
System.out.println("Got: " + n);
valueSet = false;
notify();
}
}
class Producer implements Runnable
{
MyResource res;
Thread t;
Producer(MyResource r)
{
res = r;
t = new Thread(this, "Producer");
t.start();
}
public void run()
{
for(int i=0;i<3;i++)
{
res.put(i*2);
}
}
}
class Consumer implements Runnable
{
MyResource res;
Thread t;
Consumer(MyResource r)
{
res = r;
t = new Thread(this, "Consumer");
t.start();
}
public void run()
{
for(int i=0;i<3;i++)
{
12
SYBCA-DIV-2-JAVA AVANI JOSHI ROLL NO:-102
res.get();
}
}
}
class PCFixed
{
public static void main(String args[])
{
MyResource ob = new MyResource();
new Producer(ob);
new Consumer(ob);
}
}
13. Write a program which show use of join and isAlive methods in multithreading system.
13
SYBCA-DIV-2-JAVA AVANI JOSHI ROLL NO:-102
try
{
t1.t.join();
t2.t.join();
}
catch(InterruptedException e)
{
System.out.println(“Error caught”);
}
System.out.println("c1 isAlive :" + t1.t.isAlive());
System.out.println("c2 isALive :" + t2.t.isAlive());
System.out.println("main exit");
}
}
14. create an applet program which display rotated text of your name at one place.
import java.applet.*;
import java.awt.*;
14
SYBCA-DIV-2-JAVA AVANI JOSHI ROLL NO:-102
{
}
repaint();
}
}
15. create an applet program to display your name which move in window left to right.
import java.awt.*;
import java.applet.*;
15
SYBCA-DIV-2-JAVA AVANI JOSHI ROLL NO:-102
Thread.sleep(1000);
}
catch (InterruptedException ie)
{
System.out.println(ie);
}
}
}
public void paint(Graphics g)
{
g.drawString(display, x, y);
}
}
16. create an applet program which take input from parameters through param tag for
EMP class and Display name, age, salary, city of employee.
import java.awt.*;
import java.applet.*;
16
SYBCA-DIV-2-JAVA AVANI JOSHI ROLL NO:-102
17. create an applet program which display triangle having circle. Circle have to touch with
all edges of triangle. Also write your name between circle.
import java.applet.Applet;
import java.awt.Color;
import java.awt.Graphics;
import java.applet.*;
import java.awt.*;
import java.util.*;
17
SYBCA-DIV-2-JAVA AVANI JOSHI ROLL NO:-102
{
Thread.sleep(1000);
}
catch(execption e)
{
}
repaint();
}
}
import java.awt.*;
import java.applet.*;
import java.net.*;
20. Write an applet program which change background color by click on different buttons.
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
18
SYBCA-DIV-2-JAVA AVANI JOSHI ROLL NO:-102
Button b1,b2;
string s;
public void init()
{
b1=new Button("red");
b2=new Button("yellow");
add(b1);
add(b2);
b1.addActionListener(this);
b2.addActionListener(this);
}
public void actionperfromed(ActionEvent e)
{
s=e.getActionCommand();
if(s=="red")
{
setForeground(color.red);
}
if(s=="yellow")
{
setForeground(color.yellow);
}
}
public void paint(Graphics g)
{
g.fillOval(50,50,70,70);
}
}
19