15.
Write a Java program to demonstrate package
Step 1: Create the subdirectory “mypackage”.
Step 2: Create the classes that are to be placed inside mypackage folder.
This class should contain following statement in the beginning of the program.
package mypackage;
Create the class “ salary.java “ inside the subdirectory as follows:
package mypackage;
package mypackage;
public class subtraction
public class addition
{
{
int m,n;
int m,n;
public subtraction(int x,int y)
public addition(int x,int y)
{
{
m=x;
m=x;
n=y;
n=y;
}
}
public void subpkg()
public void addpkg()
{
{
System.out.println("subtraction="+(m-n));
System.out.println("addition="+(m+n));
}
}
}
}
Step 3: Now package folder is ready. We have to use this package in the main program. So the first
statement in the main program has to be as follows:
import mypackage.*;
Outside the subdirectory create the file “packagedemo.java” as follows:
import mypackage.*;
class packagedemo
{
public static void main(String args[])
{
addition a=new addition(8,9);
a.addpkg();
subtraction s=new subtraction(8,5);
s.subpkg();
}
}
16. Write a java program to demonstrate ArithmeticException
class ari
{
public static void main(String args[])
{
int a=15,b=5,c=5,x,y;
try
{
x=a/(b-c);
System.out.println("X="+x);
}
catch(ArithmeticException e)
{
System.out.println("\nHandling Exception divide by zero exception");
}
y=a/(b+c);
System.out.println("Y="+y);
}
}
/*output
C:\bcafy\new list>javac ari.java
C:\bcafy\new list>java ari
Handling Exception divide by zero exception
Y=1
*/
17. Write a java program to demonstrate Multiple Catch Exception
class multiplecatch
{
public static void main(String args[])
{
float div;
int arr[]={6,2};
try
{
div=arr[1]/arr[2];
System.out.println("Division ="+div);
}
catch(ArithmeticException e)
{
System.out.println(e.toString());
}
catch(ArrayIndexOutOfBoundsException e)
{
System.out.println(e.toString());
}
catch(NumberFormatException e)
{
System.out.println(e.toString());
}
}
}
/* output
C:\bcafy\new list>javac multiplecatch.java
C:\bcafy\new list>java multiplecatch
java.lang.ArrayIndexOutOfBoundsException: 2
*/
18. Write a java program to demonstrate your own exception.
import java.lang.Exception;
class MyException extends Exception
{
MyException(String msg)
{
super(msg); } }
class ownexception
{
public static void main(String args[])
{
int x=15,y=15;
try{
int z=x/y;
if(z==1)
{
throw new MyException("Division is equal to one");
}
}
catch(MyException e)
{
System.out.println("Caught my exception");
System.out.println(e.getMessage());
}
}
}
/*
C:\bcafy\new list>javac ownexception.java
C:\bcafy\new list>java ownexception
Caught my exception
Division is equal to one
*/
19. Write a java program to demonstrate multithreading.
class A extends Thread
{
int i;
public void run()
{
for(i=1;i<=5;i++)
{
System.out.println("From Thread A "+i);
}
System.out.println("End of A");
}
}
class B extends Thread
{
int j;
public void run()
{
for(j=1;j<=5;j++)
{
System.out.println("From Thread B "+j);
}
System.out.println("End of B");
}
}
class testthread
{
public static void main(String args[])
{
A t1=new A();
B t2=new B();
t1.setPriority(Thread.MAX_PRIORITY);
t1.setPriority(Thread.MIN_PRIORITY);
System.out.println("Thread A");
t1.start();
System.out.println("Thread B");
t2.start();
}
}
/*output
C:\bcafy\new list>javac testthread.java
C:\bcafy\new list>java testthread
Thread AThread BFrom Thread A 1
From Thread B 1
From Thread A 2
From Thread B 2
From Thread A 3
From Thread A 4
From Thread B 3
From Thread A 5
From Thread B 4
End of A
From Thread B 5
End of B */
20. Write a java program to display addition of two numbers using applet and event
handling
import java.awt.event.*;
import java.awt.*;
import java.applet.*;
/*<applet code="readapplet.class" width=300 height=200></applet>*/
public class readapplet extends Applet implements ActionListener
{ Label l1, l2, l3;
int x,y,z;
TextField t1, t2,t3;
Button b1;
public void init()
{
setLayout(new GridLayout(4, 2));
t1 =new TextField(8);
t2 = new TextField(8);
t3 = new TextField(8);
b1=new Button("Calculate");
l1 = new Label("Number 1");
l2 = new Label("Number 2");
l3 = new Label("Result");
add(l1);
add(t1);
add(l2);
add(t2);
add(l3);
add(t3);
add(b1);
t1.setText("0");
t2.setText("0");
t3.setText("0");
b1.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource() == b1)
{
x = Integer.parseInt(t1.getText());
y = Integer.parseInt(t2.getText());
z = x + y;
t3.setText(String.valueOf(z));
}
}
}
21. Write a java program to display parents information using applet and event
handling
import java.awt.event.*;
import java.awt.*;
import java.applet.*;
/*<applet code="printinfo.class" width=300 height=200></applet>*/
public class printinfo extends Applet implements ActionListener
{
TextField t1, t2;
Button b1,b2;
public void init()
{
t1=new TextField(30);
t2=new TextField(30);
b1=new Button("Mother");
b2=new Button("Father");
add(b1);
add(b2);
add(t1);
add(t2);
b1.addActionListener(this);
b2.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==b1)
{
t1.setText("Mary");
t2.setText("Home Minister");
}
if(e.getSource()==b2)
{
t1.setText("John");
t2.setText("Chemical Engineer");
}
}}
22. Write a java program to demonstrate ItemListener using applet and event
handling
import java.awt.event.*;
import java.awt.*;
import java.applet.*;
/*<applet code="addapp1f.class" width=300 height=300></applet>*/
public class addapp1f extends Applet implements ItemListener
{
String msg;
Checkbox r1,r2;
CheckboxGroup cbg;
public void init()
{
cbg=new CheckboxGroup();
r1=new Checkbox("Male",cbg,false);
r2=new Checkbox("Female",cbg,false);
add(r1);
add(r2);
r1.addItemListener(this);
r2.addItemListener(this);
}
public void itemStateChanged(ItemEvent e)
{
repaint();
}
public void paint(Graphics g)
{
msg="Gender is:";
msg=msg+cbg.getSelectedCheckbox().getLabel();
g.drawString(msg,50,100);
}
}
23.Write a java program to demonstrate passing parameters to the Applet
import java.awt.*;
import java.applet.*;
public class helloparam extends Applet
{
String str;
public void init()
{
setBackground(Color.yellow);
str=getParameter("string");
if(str==null)
str="5BCA";
str="hello"+" "+str;
}
public void paint(Graphics g)
{
g.drawString(str,50,50);
}
}
/*<applet code=helloparam.class height=300 width=250>
<param name="string" value="BSC"> </applet>*/
24.Write a java program to display image on the Applet
import java.awt.*;
import java.applet.*;
public class eventimg extends Applet {
Image picture;
public void init() {
picture = getImage(getDocumentBase(),"images.jfif");
}
public void paint(Graphics g) {
g.drawImage(picture, 30,30, this);
}
}
/*
<applet code="eventimg.class" width=250 height=350></applet>
*/