Part B
1. Program to catch Negative Array Size Exception. This exception is caused
when the array is initialized to negative values.
public class Example
{ public static void main(String args[])
{ try
{
int []array = new int[-5];
}
catch(NegativeArraySizeException nase)
{ [Link]();
//handle the exception
}
[Link]("Continuing execution...");
}
}
Output:
2. Program to handle Null Pointer Exception and use the “finally” method to
display a message to the user. import [Link].*; class NullPointerEx
{
public static void main(String []args)
{
String ptr=null;
try
{
[Link]("Inside try block");
if("gfg".equals(ptr))
[Link]("Same");
else
[Link]("Not same");
catch (NullPointerException e)
{
[Link]("Exception handled");
[Link](e);
}
finally
{
[Link]("Finally block is always executed");
}
[Link]("rest of the code .... ");
}
}
Output:
[Link] which create and displays a message on the window
import [Link].*;
import [Link].*;
import [Link].*;
class Demo extends JFrame implements ActionListener
{
JButton B1; Demo(
)
{ [Link](null);
B1=new JButton("Button 1");
[Link](130,05,100,50);
[Link](B1);
[Link](this);
}
public void actionPerformed(ActionEvent evt)
{ if([Link]( ) == B1)
{
[Link](this,"Enter a valid
Number","ERROR",JOptionPane.ERROR_MESSAGE);
}
}
}
class MessageDialogs1
{ public static void main(String args[ ])
{
Demo f=new Demo();
[Link](200,200,400,300);
[Link](false);
[Link](true);
}
}
Output:
4. Program to draw several shapes in the created window
import [Link].*; import [Link].*; public class shap
extends Applet
{
public void paint(Graphics g)
{
[Link](25,25,100,25);
[Link](25,40,100,50);
[Link](145,40,100,50);
[Link](265,40,50,50);
[Link](25,125,100,50,15,15);
[Link](145,125,100,50 ,15,15);
[Link](25,205,100,50);
[Link](145,205,100,50);
[Link](265,205,50,50);
g.draw3DRect(25,280,100,50,true);
g.draw3DRect(145,280,100,50,false);
[Link](25,345,100,50,25,75);
[Link](145,345,100,50,125,75);
}
}
/*
<html>
<body>
<applet code="[Link]" height=500 width=500>
</applet>
</body>
</html>
*/
5. Program to create an applet and draw grid lines
import [Link].*; import
[Link].*; import
[Link]; public
class VertLine extends Applet
{ public void paint (Graphics g)
{
String input; int
Lines;
input=[Link]("Enter number of Lines to draw
oneach side:");
Lines=[Link](input); int
horizontal,vertical,x,y; int
appletWidth=getSize().width; int
appletHeight=getSize().height;
for(horizontal=0;horizontal<Lines;horizontal++)
{ y=horizontal*(appletWidth/10);
[Link]([Link]);
[Link](0,y,appletWidth,y); }
for(vertical=0;vertical<Lines;vertical++)
{ x=vertical*(appletHeight/10);
[Link]([Link]);
[Link](x,0,x,appletHeight);
}
}
}
/*
<html>
<body>
<applet code="[Link]" height=500 width=500>
</applet>
</body>
</html>
*/
Output: