0% found this document useful (0 votes)
0 views

Part-B Programs

The document contains multiple Java programs demonstrating exception handling, AWT GUI creation, applet graphics, and file I/O operations. Key examples include generating exceptions like NegativeArraySizeException and NullPointerException, creating an AWT window with buttons for greetings, and reading/writing binary files. Additionally, it showcases mouse event handling and creating menus in a GUI application.

Uploaded by

ameeanbee
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views

Part-B Programs

The document contains multiple Java programs demonstrating exception handling, AWT GUI creation, applet graphics, and file I/O operations. Key examples include generating exceptions like NegativeArraySizeException and NullPointerException, creating an AWT window with buttons for greetings, and reading/writing binary files. Additionally, it showcases mouse event handling and creating menus in a GUI application.

Uploaded by

ameeanbee
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

1.Program to generate Negative Array Size Exception.

package negativearraysizeexceptiondemo;
public class NegativeArraySizeExceptionDemo {
public static void main(String[] args) {
try {
int[] arr = new int[-5];
}
catch (NegativeArraySizeException e) {
System.out.println("Caught exception: " + e);
}
}
}
Output:
Caught exception: java.lang.NegativeArraySizeException
2.program to generate Null Pointer Exception.
package nullpointerexceptiondemo;
public class NullPointerExceptionDemo {
public static void main(String[] args) {
String str = null;
try {
int length = str.length(); }
catch (NullPointerException e) {
System.out.println("Caught exception: " + e);
}
}
}

Output:
Caught exception: java.lang.NullPointerException
3.program that reads two integer numbers for the variables and b. The program should
catch Number Format Exception and display the error message.
package numberformatexceptiondemo;
import java.util.Scanner;
public class NumberFormatExceptionDemo {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
try {
System.out.print("Enter the first integer (a): ");
String inputA = scanner.nextLine();
int a = Integer.parseInt(inputA);
System.out.print("Enter the second integer (b): ");
String inputB = scanner.nextLine();
int b = Integer.parseInt(inputB);
System.out.println("The values you entered are a = " + a + " and b = " + b);
} catch (NumberFormatException e) {
System.out.println("Caught Exception:"+e);
}
}
}
Output:
Enter the first integer (a): 10
Enter the second integer (b): hello
Caught Exception:java.lang.NumberFormatException: For input string: "hello"
4. Program to create AWT window with 4 buttons M/A/E/Close. Display M for Good
Morning, A for Afternoon, E for Evening and close button to exit the window.
package awtdemo;
import java.awt.*;
import java.awt.event.*;
public class AWTDemo {
public static void main(String[] args) {
Frame frame = new Frame("Simple AWT App");
Label label = new Label();
label.setAlignment(Label.CENTER);
label.setFont(new Font("Arial", Font.PLAIN, 20));
label.setPreferredSize(new Dimension(400, 40));
Button btnM = new Button("M");
Button btnA = new Button("A");
Button btnE = new Button("E");
Button btnClose = new Button("Close");
btnM.setPreferredSize(new Dimension(100, 40));
btnA.setPreferredSize(new Dimension(100, 40));
btnE.setPreferredSize(new Dimension(100, 40));
btnClose.setPreferredSize(new Dimension(100, 40));
frame.setLayout(new FlowLayout(FlowLayout.CENTER, 20, 20));
frame.add(btnM);
frame.add(btnA);
frame.add(btnE);
frame.add(btnClose);
frame.add(label);
frame.setSize(500, 300);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
btnM.addActionListener(e -> label.setText("Good Morning!"));
btnA.addActionListener(e -> label.setText("Good Afternoon!"));
btnE.addActionListener(e -> label.setText("Good Evening!"));
btnClose.addActionListener(e -> frame.dispose());
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent we) {
System.exit(0);
}
});
}
}
Output:
5.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(25,40,100,50);
g.drawRoundRect(25,125,100,50,15,15);
g.fillRoundRect(25,125,100,50 ,15,15);
g.drawOval(25,205,100,50);
g.fillOval(25,205,100,50);
g.drawArc(25,345,100,50,25,75);}
}
Output:
6.Program to create an applet and grid lines.
import java.applet.Applet;
import java.awt.Graphics;
public class GridApplet extends Applet {
@Override
public void paint(Graphics g) {
int width = getWidth();
int height = getHeight();
for (int x = 0; x < width; x += 20) {
g.drawLine(x, 0, x, height);
}
for (int y = 0; y < height; y += 20) {
g.drawLine(0, y, width, y);
}
}
}
Output:
7.Program to demonstrate the various mouse handling events.
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
public class mousehandlingdemo extends Applet implements MouseListener,
MouseMotionListener
{
public void init()
{
addMouseListener(this);
addMouseMotionListener(this);
}
@Override
public void mouseClicked(MouseEvent e)
{
showStatus("mouse clicked : "+e.getX()+","+e.getY());
}
@Override
public void mouseEntered(MouseEvent e)
{
showStatus("mouse entered : "+e.getX()+","+e.getY());
}
@Override
public void mouseExited(MouseEvent e)
{
showStatus("mouse exited : "+e.getX()+","+e.getY());
}
@Override
public void mousePressed(MouseEvent e)
{
showStatus("mouse pressed : "+e.getX()+","+e.getY());
}
@Override
public void mouseReleased(MouseEvent e)
{
showStatus("mouse released : "+e.getX()+","+e.getY());
}
@Override
public void mouseMoved(MouseEvent e)
{
showStatus("mouse moved : "+e.getX()+","+e.getY());
}
@Override
public void mouseDragged(MouseEvent e)
{
showStatus("mouse dragged : "+e.getX()+","+e.getY());
}
}
Output:
8.program to read and write Binary I/O Files.

package javaapplication3;
import java.io.*;
public class JavaApplication3 {
public static void main(String[] args)
{
String fileName = "example1.bin";
try (FileOutputStream fos = new FileOutputStream(fileName);
DataOutputStream dos = new DataOutputStream(fos)) {
System.out.println("Writing data to the binary file...");
dos.writeUTF("Hello, Binary World!");
System.out.println("Data written successfully!");
} catch (IOException e) {
System.err.println("Error writing to the file: " + e.getMessage());
}
try (FileInputStream fis = new FileInputStream(fileName);
DataInputStream dis = new DataInputStream(fis)) {
System.out.println("\nReading data from the binary file...");
String stringValue = dis.readUTF();
System.out.println("String: " + stringValue);

} catch (IOException e) {
System.err.println("Error reading from the file: " + e.getMessage());
}

}
}

Output:
Writing data to the binary file...
Data written successfully!
Reading data from the binary file...
String: Hello, Binary World!
9.Program to create AWT Windows with 3 buttons father, mother and close. Display
the respective details of father and mother as name,age and designation using AWT
controls.

package javaapplication3;
import java.awt.*;
import java.awt.event.*;
public class JavaApplication3 {
public static void main(String[] args)
{
Frame frame = new Frame("Family Details");
// Create labels for displaying the details
Label label = new Label("Click a button to see details.");
label.setBounds(50, 50, 300, 30);
Button fatherButton = new Button("Father");
Button motherButton = new Button("Mother");
Button closeButton = new Button("Close");
fatherButton.setBounds(50, 100, 80, 30);
motherButton.setBounds(150, 100, 80, 30);
closeButton.setBounds(250, 100, 80, 30);
TextArea textArea = new TextArea();
textArea.setBounds(50, 150, 300, 100);
textArea.setEditable(false);
frame.add(label);
frame.add(fatherButton);
frame.add(motherButton);
frame.add(closeButton);
frame.add(textArea);
frame.setSize(400, 300);
frame.setLayout(null);
frame.setVisible(true);
fatherButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// Display father details in the text area
textArea.setText("Name: John Doe\nAge: 45\nDesignation: Engineer");
}
});

motherButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// Display mother details in the text area
textArea.setText("Name: Jane Doe\nAge: 42\nDesignation: Teacher");
}
});

closeButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// Close the window
frame.dispose();
}
});

frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent we) {
System.exit(0);
}
});
}
}

Output:
10. Program to create menu bar and pull-down menus.
Package menuexample;
import java.awt.*;
class MenuExample
{
MenuExample()
{
Frame f=new Frame("Menu and MenuItem Example");
MenuBar mb=new MenuBar();
Menu menu=new Menu("Menu");
Menu submenu=new Menu("sub Menu");
MenuItem i1=new MenuItem("Item 1");
MenuItem i2=new MenuItem("Item 2");
MenuItem i3=new MenuItem("Item 3");
MenuItem i4=new MenuItem("Item 4");
MenuItem i5=new MenuItem("Item 5");
menu.add(i1);
menu.add(i2);
menu.add(i3);
submenu.add(i4);
submenu.add(i5);
menu.add(submenu);
mb.add(menu);
f.setMenuBar(mb);
f.setSize(400,400);
f.setLayout(null);
f.setVisible(true);
f.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent we) {
System.exit(0);
}
});

}
public static void main(String args[])
{
new MenuExample();
}
}
Output:

You might also like