Part-B Java List 2023
Part-B Java List 2023
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;
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.*;
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
*/
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.
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;
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);
}
}
import java.awt.*;
import java.applet.*;
Image picture;
}
/*
<applet code="eventimg.class" width=250 height=350></applet>
*/