0% found this document useful (0 votes)
43 views8 pages

Part B 1. Program To Catch Negative Array Size Exception. This Exception Is Caused When The Array Is Initialized To Negative Values

Uploaded by

preethamsu3
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
43 views8 pages

Part B 1. Program To Catch Negative Array Size Exception. This Exception Is Caused When The Array Is Initialized To Negative Values

Uploaded by

preethamsu3
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

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)
{ nase.printStackTrace();
//handle the exception
}
System.out.println("Continuing execution...");
}
}

Output:

2. Program to handle Null Pointer Exception and use the “finally” method to
display a message to the user. import java.io.*; class NullPointerEx
{

public static void main(String []args)


{

String ptr=null;
try
{

System.out.println("Inside try block");


if("gfg".equals(ptr))
System.out.println("Same");

else

System.out.println("Not same");

catch (NullPointerException e)
{

System.out.println("Exception handled");
System.out.println(e);
}

finally
{

System.out.println("Finally block is always executed");


}

System.out.println("rest of the code .... ");


}
}

Output:

3.Program which create and displays a message on the window

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class Demo extends JFrame implements ActionListener
{
JButton B1; Demo(
)
{ this.setLayout(null);
B1=new JButton("Button 1");
B1.setBounds(130,05,100,50);
this.add(B1);
B1.addActionListener(this);
}
public void actionPerformed(ActionEvent evt)
{ if(evt.getSource( ) == B1)
{
JOptionPane.showMessageDialog(this,"Enter a valid
Number","ERROR",JOptionPane.ERROR_MESSAGE);
}
}
}

class MessageDialogs1
{ public static void main(String args[ ])
{
Demo f=new Demo();
f.setBounds(200,200,400,300);
f.setResizable(false);
f.setVisible(true);
}
}

Output:
4. Program to draw several shapes in the created window
import java.awt.*; import java.applet.*; public class shap
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);
g.draw3DRect(25,280,100,50,true);
g.draw3DRect(145,280,100,50,false);
g.drawArc(25,345,100,50,25,75);
g.drawArc(145,345,100,50,125,75);
}
}
/*
<html>
<body>
<applet code="shap.class" height=500 width=500>
</applet>
</body>
</html>
*/

5. Program to create an applet and draw grid lines

import java.awt.*; import


java.applet.*; import
javax.swing.JOptionPane; public
class VertLine extends Applet
{ public void paint (Graphics g)
{
String input; int
Lines;
input=JOptionPane.showInputDialog("Enter number of Lines to draw
oneach side:");
Lines=Integer.parseInt(input); int
horizontal,vertical,x,y; int
appletWidth=getSize().width; int
appletHeight=getSize().height;
for(horizontal=0;horizontal<Lines;horizontal++)
{ y=horizontal*(appletWidth/10);
g.setColor(Color.red);
g.drawLine(0,y,appletWidth,y); }
for(vertical=0;vertical<Lines;vertical++)
{ x=vertical*(appletHeight/10);
g.setColor(Color.blue);
g.drawLine(x,0,x,appletHeight);
}
}
}
/*
<html>
<body>
<applet code="VertLine.class" height=500 width=500>
</applet>
</body>
</html>

*/

Output:

You might also like