hii /*1 .Program to catch Negative Array Size Exception. This exception is caused when the array size is initialized to negative values.*/ import java.util.Scanner; public class Negative_Array { public static void main(String[] args) { Scanner in=new Scanner(System.in); try { System.out.println("Enter array size:"); int n=in.nextInt(); int arr[]=new int[n]; //if n<0, NegativeArraySizeException is raised System.out.println("Array created."); System.out.println("Array length: "+arr.length); } catch(NegativeArraySizeException e) { System.out.println("You are trying to declare an array with negativsize:"+e); } System.out.println("Continuing execution"); } } /*2Program to demonstrate exception handling with try, catch and finally. */ import java.util.*; class MultipleCatches { public static void main(String args[]) { try { Scanner sc=new Scanner(System.in); System.out.println("Enter the Number :"); int a=sc.nextInt(); System.out.println("a = " + a); int b = 42 / a; //if a=0, causes ArithmeticException int data[] = {2, 3, 4, 5}; System.out.println("Value at : " + data[4]); //causesArrayIndexOutOfBoundsException } catch(ArithmeticException e) { System.out.println("Exception Divide by 0: " + e); } catch(ArrayIndexOutOfBoundsException e) { System.out.println("Sorry you are trying to print beyond the size of data[]"); System.out.println("Exception: " + e); } catch(Exception e) //catches any other exception { System.out.println("Exception: " + e); } finally { System.out.println("Finally Block Executed."); } } } /*3 Program to displays a message on the window .*/ import java.awt.*; import java.awt.event.*; public class Display_msg extends WindowAdapter implements ActionListener { Frame jf; Button b1; Label lbl; Display_msg() { jf= new Frame("Displaying Message"); b1=new Button("Display"); lbl = new Label(); jf.add(b1); jf.add(lbl); jf.addWindowListener(this); b1.addActionListener(this); jf.setLayout(new FlowLayout()); jf.setSize(250,250); jf.setVisible(true); } public void actionPerformed(ActionEvent ae) { lbl.setText("Welcome to GUI Programming"); } public void windowClosing(WindowEvent e) { System.exit(0); } public static void main(String args[]) { new Display_msg(); } } /*4Program to draw several shapes in the created window.*/ import java.awt.*; import java.awt.event.*; class Draw extends WindowAdapter { Draw() { Frame f = new Frame(); f.addWindowListener(this); // Add a component with a custom paint method f.add(new Component() { public void paint(Graphics g) { g.setColor(Color.RED); g.drawRect(300,30,80,100); g.fillRect(100,30,100,80); g.drawOval(30,130,50,60); g.fillOval(130,130,50,60); } }); f.setSize(400, 400); f.setVisible(true); } public void windowClosing(WindowEvent e) { System.exit(0); } public static void main(String[] args) { new Draw(); } } /*5.Programto create a 4×4 grid and fills it in with 15 buttons, each labeled with its index. */ import java.awt.*; import java.awt.event.*; class GridLayoutDemo extends WindowAdapter { Frame f=new Frame("Grid Layout"); public GridLayoutDemo() { f.setLayout(new GridLayout(4,4)); for(int i=1;i<=16;i++) { Button b=new Button(String.valueOf(i)); f.add(b); } f.setSize(400,300); f.setVisible(true); f.addWindowListener(this); } public void windowClosing(WindowEvent e) { System.exit(0); } public static void main(String[] args) { new GridLayoutDemo(); } } /* 6.Write a 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 button similar details of mother also appear. */ import java.awt.*; import java.awt.event.*; public class Parents_Details extends WindowAdapter implements ActionListener { Frame jf; Button b1, b2; Label lbl; Parents_Details() { jf= new Frame("Parent Details"); b1=new Button("Father"); b2= new Button("Mother"); lbl = new Label(); //Add components on frame jf.add(b1); jf.add(b2); jf.add(lbl); //Add action listener and window listener b1.addActionListener(this); b2.addActionListener(this); jf.addWindowListener(this); jf.setLayout(new FlowLayout()); jf.setSize(300,150); jf.setVisible(true); } public void actionPerformed(ActionEvent ae) { if(ae.getActionCommand().equals("Father")) { lbl.setText("Name : Ashok Joshi Age:55 Designation:Business"); } if(ae.getActionCommand().equals("Mother")) { lbl.setText("Name : Anita Joshi Age:50 Designation:Teacher"); } } public void windowClosing(WindowEvent e) { jf.dispose(); System.exit(0); } public static void main(String args[]) { new Parents_Details(); } } /* 7Create a frame which displays your personal details with respect to a button click */ import java.awt.*; import java.awt.event.*; public class Button_PDetails extends WindowAdapter implements ActionListener { Frame jf; Button b1; Label lb1,lb2,lb3; Button_PDetails() { jf= new Frame("Personal Details"); jf.addWindowListener(this); b1=new Button("Personal Details"); lb1 = new Label(); lb2=new Label(); lb3=new Label(); jf.add(b1); jf.add(lb1); jf.add(lb2); jf.add(lb3); b1.addActionListener(this); jf.setLayout(new FlowLayout()); jf.setSize(200,250); jf.setVisible(true); } public void actionPerformed(ActionEvent ae) { lb1.setText("Name : Abc"); lb2.setText("Roll No : 1"); lb3.setText("Course : BCA"); } public void windowClosing(WindowEvent e) { System.exit(0); } public static void main(String args[]) { new Button_PDetails(); } } /* 8Program to create a window with TextFields and Buttons. The "ADD" button adds the two integers and display the result. The "CLEAR" button shall clear all the text fields. */ import java.awt.*; import java.awt.event.*; public class Add extends WindowAdapter implements ActionListener { Frame jf; Button b1, b2; Label lb1,lb2,lb3; TextField t1,t2,t3; Add() { jf= new Frame("Addition"); jf.addWindowListener(this); b1=new Button("Add"); b2=new Button("Clear"); lb1=new Label("First number :"); lb2=new Label("Second number :"); lb3=new Label("Addition :"); t1=new TextField(8); t2=new TextField(8); t3=new TextField(8); jf.add(lb1);jf.add(t1); jf.add(lb2);jf.add(t2); jf.add(lb3);jf.add(t3); jf.add(b1); jf.add(b2); b1.addActionListener(this); b2.addActionListener(this); jf.setLayout(new FlowLayout()); jf.setSize(250,500); jf.setVisible(true); } public void actionPerformed(ActionEvent ae) { if(ae.getActionCommand().equals("Add")) { int n1,n2; n1=Integer.parseInt(t1.getText()); n2=Integer.parseInt(t2.getText()); int n3=n1+n2; t3.setText(" "+n3); } if(ae.getActionCommand().equals("Clear")) { t1.setText(""); t2.setText(""); t3.setText(""); } } public void windowClosing(WindowEvent e) { System.exit(0); } public static void main(String args[]) { new Add(); } } /* 9.Program to create a window, when we press M or m, the window displays “good morning”,A or a, the window displays Good Afternoon”, E or e, the window displays “good morning”,N or n, the window displays “good morning”. */ import java.awt.*; import java.awt.event.*; class Keywish extends WindowAdapter implements KeyListener { Label lbl; Frame f; TextField t; Keywish() { //initializing components f = new Frame(); t = new TextField(100); lbl = new Label(); //Adding components f.add(t); f.add(lbl); //Registering with listener f.addWindowListener(this); t.addKeyListener(this); f.setSize(450, 400); f.setVisible(true); f.setLayout(new GridLayout(2,1)); } //Responding to events public void windowClosing(WindowEvent we) { System.exit(0); } public void keyTyped(KeyEvent ke) { char ch = ke.getKeyChar(); if (ch == 'm' || ch == 'M') lbl.setText("Good Morning"); if (ch == 'n' || ch == 'N') lbl.setText("Good Night"); } public void keyPressed(KeyEvent ke) { char ch = ke.getKeyChar(); if (ch == 'a' || ch == 'A') lbl.setText("Good Afternoon"); } public void keyReleased(KeyEvent ke) { char ch = ke.getKeyChar(); if (ch == 'e' || ch == 'E') lbl.setText("Good Evening"); } public static void main(String[] args) { new Keywish(); } } /*10.Demonstrate the various mouse handling events using suitable example.*/ import java.awt.*; import java.awt.event.*; public class MouseEventDemo extends WindowAdapter implements MouseListener { int x=0, y=0; Frame f; Label lbl=new Label("Mouse Events"); MouseEventDemo() { f=new Frame("New Fame"); f.add(lbl); //add window listener f.addWindowListener(this); //add mouse listener f.addMouseListener(this); f.setSize(300,300); f.setLayout(new FlowLayout()); f.setVisible(true); } public void mouseClicked(MouseEvent e) { x = e.getX(); y = e.getY(); lbl.setText("Mouse Clicked at location (x,y): "+x+","+y); } public void mousePressed(MouseEvent e) { x = e.getX(); y = e.getY(); lbl.setText("Mouse Pressed at location (x,y): "+x+","+y); } public void mouseReleased(MouseEvent e) { x = e.getX(); y = e.getY(); lbl.setText("Mouse Released at location (x,y): "+x+","+y); } public void mouseEntered(MouseEvent e) { lbl.setText("Mouse Entered"); } public void mouseExited(MouseEvent e) { lbl.setText("Mouse Exited"); } public void windowClosing(WindowEvent we) { f.dispose(); System.exit(0); } public static void main(String[] args) { new MouseEventDemo(); } } /*11Create a package „BCA‟ in your current working directory. a. Create a class student in the above package with the following attributes: Name, age, gender. Include appropriate constructor and a method for displaying the details. b. Import above package and access the member variables and function contained in a package.*/ package BCA; //Creates package BCA public class Student //Creation of class Student in BCA package { String sname,gender; int age; public Student(String sn,String g,int a) { sname=sn; gender=g; age=a; } public void display() { System.out.println("Name :"+sname); System.out.println("Gender :"+gender); System.out.println("Age :"+age); } } import BCA.*; //imports package BCA in the program class StudBCA { public static void main(String[] args) { Student s=new Student("Akshay","Male",25); //Accessing display() of class Student from BCA package s.display(); } }