Java B
Java B
PART B PROGRAMS
NRUPATHUNGA UNIVERSITY
BCA 2nd Sem
1. Program to catch Negative Array Size Exception. This exception is caused when the array is
class Arrayexception
{
public static void main(String args[])
{
try
{
int[] array = new int[-5];
}
catch(NegativeArraySizeException e)
{
e.printStackTrace();
System.out.println("cought negative size for an array to positive value.....");
}
System.out.println("continue with execution ");
}
}
1
2. Program to handle Null Pointer Exception and use the “finally” method to display a message to
the user.
import java.io.*;
class Nullpoint
{
public static void main(String args[])
{
String ptr=null;
try
{
if(ptr.equals("gfg"))
System.out.println("same");
else
System.out.println("not same ");
}
catch(NullPointerException e)
{
System.out.println("Null Pointer Exception caught ");
}
finally
{
System.out.println("Exception handle used ");
}
}
}
2
3. Program which create and displays a message on the window
import java.applet.Applet;
import java.awt.Graphics;
public class First extends Applet
{
public void paint(Graphics g)
{
g.drawString("welcome to applet ",150,150);
}
}
/*
<applet code="First.class" width="300" height="300">
</applet>
*/
Output :
3
4. Program to draw several shapes in the created window
import java.awt.*;
import java.applet.*;
public class Shape extends Applet
{
public void paint(Graphics g)
{
g.drawLine(25,25,100,25);
g.drawRect(25,40,100,50);
g.fillRect(145,40,100,50);
g.drawRect(265,40,50,50);
g.drawRoundRect(25,125,100,50,15,15);
g.fillRoundRect(145,125,100,50,15,15);
g.drawOval(25,205,100,50);
g.fillOval(145,205,100,50);
g.drawOval(265,205,50,50);
}
}
/*
<applet code="Shape.class" height=500 width=500>
</applet>
*/
Output :
4
5. Program to create an applet and draw grid lines
import java.awt.*;
import java.applet.Applet;
import java.awt.event.*;
public class Grid1 extends Applet
{
public void paint(Graphics g)
{
for(int x=0;x<=240;x+=30)
{
g.drawLine(x,0,x,240);
}
for(int y=0;y<=240;y+=30)
{
g.drawLine(0,y,240,y);
}
}
}
/*
<applet code="Grid1.class" width="300" height="300">
</applet>
*/
Output :
5
6. Program which creates a frame with two buttons father and mother. When we click the father
button the name of the father, his age and designation must appear. When we click mother similar
details of mother also appear.
import javax.swing.*;
import java.awt.event.*;
public class checkButton extends JFrame
{
private JButton button1,button2;
private JLabel label;
public static void main(String args[])
{
new checkButton();
}
public checkButton()
{
this.setSize(300,300);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLayout(null);
ClickListener click=new ClickListener();
button1=new JButton("Father");
button1.setBounds(40,100,100,40);
button1.addActionListener(click);
button2=new JButton("Mother");
button2.setBounds(150,100,100,40);
button2.addActionListener(click);
label=new JLabel();
label.setBounds(50,150,250,20);
this.add(button1);
this.add(button2);
this.add(label);
this.setVisible(true);
}
6
Output :
7
7. Create a frame which displays your personal details with respect to a button click
import javax.swing.*;
import java.awt.event.*;
public class button extends JFrame
{
private JButton button1;
private JButton button2;
private JLabel label;
public static void main(String args[])
{
new button();
}
public button()
{
this.setSize(300,300);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLayout(null);
ClickListener click=new ClickListener();
button1=new JButton("Submit");
button1.setBounds(40,100,100,40);
button1.addActionListener(click);
label=new JLabel();
label.setBounds(50,150,250,20);
this.add(button1);
this.add(label);
this.setVisible(true);
}
private class ClickListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==button1)
{
label.setText("Name:Mithun Age:19 palce:Banglore ");
}
}
}
}
8
Output :
9
8. Create a simple applet which reveals the personal information of yours.
import java.applet.*;
import java.awt.*;
public class Display extends Applet
{
public void paint(Graphics g)
{
g.drawString("Name = Mithun ",100,100);
g.drawString("Age = 19 ",100,150);
g.drawString("Designation = Lawyer ",100,200);
g.drawString("Place = Banglore ",100,250);
}
}
/*
<applet code = "Display.class" height = "400" width = "400">
</applet>
*/
Output :
10
9. Program to move different shapes according to the arrow key pressed.
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
public class Key extends Applet implements KeyListener
{
String msg="";
public void init()
{
addKeyListener(this);
}
public void keyPressed(KeyEvent ke)
{
showStatus("key Pressed");
int key1=ke.getKeyCode();
if(ke.getKeyCode()==KeyEvent.VK_UP)
{
showStatus("Rectangle");
msg="Rectangle";
}
else if(ke.getKeyCode()==KeyEvent.VK_DOWN)
{
showStatus("Circle");
msg="Circle";
}
else if(ke.getKeyCode()==KeyEvent.VK_LEFT)
{
showStatus("Square");
msg="Square";
}
else if(ke.getKeyCode()==KeyEvent.VK_RIGHT)
{
showStatus("Triangle");
msg="Triangle";
}
else
{
showStatus("invalid ");
}
repaint();
}
11
public void keyTyped(KeyEvent ke)
{
showStatus("key Typed");
}
public void paint(Graphics g)
{
g.drawString(msg,100,150);
}
}
/*
<applet code="Key.class" width="300" height="300">
</applet>
*/
Output :
12
13
10. Program to create a window when we press M or m the window displays Good Morning, A or a
the window displays Good After Noon E or e the window displays Good Evening, N or n the window
displays Good Night
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
public class KeyEventHandler implements KeyListener
{
public void keyPressed(KeyEvent e)
{
if(e.getKeyCode()==KeyEvent.VK_M)
{
JDialog d=new JDialog();
JLabel l=new JLabel("Good Morning");
d.add(l);
d.setSize(100,100);
d.setVisible(true);
}
else if(e.getKeyCode()==KeyEvent.VK_A)
{
JDialog d=new JDialog();
JLabel l=new JLabel("Good Afternoon");
d.add(l);
d.setSize(100,100);
d.setVisible(true);
}
else if(e.getKeyCode()==KeyEvent.VK_E)
{
JDialog d=new JDialog();
JLabel l=new JLabel("Good Evening");
d.add(l);
d.setSize(100,100);
d.setVisible(true);
}
else if(e.getKeyCode()==KeyEvent.VK_N)
{
JDialog d=new JDialog();
JLabel l=new JLabel("Good Night");
d.add(l);
d.setSize(100,100);
d.setVisible(true);
}
14
else
System.out.println("invalid character input");
}
public void keyReleased(KeyEvent e)
{
System.out.println("Key released ");
}
public void keyTyped(KeyEvent e)
{
System.out.println("Key Pressed ");
}
public static void main(String args[])
{
JFrame window=new JFrame();
KeyEventHandler eventHandler=new KeyEventHandler();
Panel panel=new Panel();
panel.setFocusable(true);
panel.addKeyListener(eventHandler);
window.setContentPane(panel);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setSize(500,500);
window.setVisible(true);
}
}
Output :
15
16
11. Demonstrate the various mouse handling events using suitable example.
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
public class Mouse extends Applet implements MouseListener,MouseMotionListener
{
public void init()
{
addMouseListener(this);
addMouseMotionListener(this);
}
public void mouseClicked(MouseEvent e)
{
showStatus("Mouse Clicked");
}
public void mouseEntered(MouseEvent e)
{
showStatus("Mouse Entered");
}
public void mouseExited(MouseEvent e)
{
showStatus("Mouse Exited");
}
public void mousePressed(MouseEvent e)
{
showStatus("Mouse Pressed");
}
public void mouseReleased(MouseEvent e)
{
showStatus("Mouse Released");
}
public void mouseMoved(MouseEvent e)
{
showStatus("Mouse Moved");
}
public void mouseDragged(MouseEvent e)
{
showStatus("Mouse Dragged");
}
}
/*
<applet code="Mouse.class" height=200 width=200>
</applet>
*/
17
Output :
18
12. Program to create menu bar and pull-down menus.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class menudemo
{
Menu menu1,menu2;
MenuItem Item1,Item2,Item3,Item4,Item5,Item6,Item7;
menudemo()
{
Frame frame=new Frame("Menubar,Menu and MenuItem ");
MenuBar menubar=new MenuBar();
menu1=new Menu("File");
Item1=new MenuItem("new");
Item2=new MenuItem("open");
Item3=new MenuItem("save");
menu1.add(Item1);
menu1.add(Item2);
menu1.add(Item3);
menu2=new Menu("save_as");
Item5=new MenuItem(".jgeg");
Item6=new MenuItem(".png");
Item7=new MenuItem(".pdf");
menu2.add(Item5);
menu2.add(Item6);
menu2.add(Item7);
menu1.add(menu2);
menubar.add(menu1);
frame.setMenuBar(menubar);
frame.setSize(330,250);
frame.setVisible(true);
}
public static void main(String args[])
{
new menudemo();
}
}
19
Output :
20