Untitled
plain_text
a month ago
3.7 kB
10
Indexable
Never
LAB 1B: Program to catch Negative Array Size Exception. This exception is caused when the array size is initialized to negative values. public class Pgm1b { public static void main(String[] args) { try { int[] array = new int[-2]; } catch(NegativeArraySizeException e) { System.out.println("ArraySize Error"); e.printStackTrace(); } System.out.println("Continuing execution"); } } OUTPUT: ArraySize Error java.lang.NegativeArraySizeException: -2 at Pgm1b.main(Pgm1b.java:8) Continuing execution LAB 2B: Program to demonstrate exception handling with try, catch and finally. import java.util.Scanner; public class Pgm2b { public static void main(String[] args) { int a,b; double c; Scanner in = new Scanner(System.in); System.out.println("Enter 1st number"); a = in.nextInt(); System.out.println("Enter 2nd number"); b = in.nextInt(); try { c = a / b; System.out.println("Division = " + c); } catch(ArithmeticException e) { System.out.println("Divide by zero error"); System.out.println(e.getLocalizedMessage()); } finally { System.out.println("Inside finally block"); } System.out.println("Outside try-catch-finally block"); } } OUTPUT: Enter 1st number 6 Enter 2nd number 3 Division = 2.0 Inside finally block Outside try-catch-finally block Enter 1st number 5 Enter 2nd number 0 Divide by zero error / by zero Inside finally block Outside try-catch-finally block LAB 3B: Program which create and displays a message on the window package LabPgm3b; import javax.swing.*; import java.awt.event.*; import java.awt.*; public class Pgm3b implements ActionListener { public static void main(String[] args) { JFrame frame = new JFrame("Original Frame"); frame.setSize(300,300); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Pgm3b obj = new Pgm3b(); JButton button = new JButton("View Message"); frame.add(button); button.addActionListener(obj); frame.setVisible(true); } public void actionPerformed(ActionEvent e) { JFrame sub_frame = new JFrame("Sub Frame"); sub_frame.setSize(200,200); JLabel label = new JLabel("!!! HELLO !!!"); sub_frame.add(label); sub_frame.setVisible(true); } } OUTPUT LAB 5B: Program to create a 4×4 grid and fills it in with 15 buttons, each 1 labelled with its index. package LabPgm5b; import java.awt.*; import javax.swing.*; public class Pgm5b { JFrame frameObj; Pgm5b() { frameObj = new JFrame(); JButton btn1 = new JButton("1"); JButton btn2 = new JButton("2"); JButton btn3 = new JButton("3"); JButton btn4 = new JButton("4"); JButton btn5 = new JButton("5"); JButton btn6 = new JButton("6"); JButton btn7 = new JButton("7"); JButton btn8 = new JButton("8"); JButton btn9 = new JButton("9"); JButton btn10 = new JButton("10"); JButton btn11 = new JButton("11"); JButton btn12 = new JButton("12"); JButton btn13 = new JButton("13"); JButton btn14 = new JButton("14"); JButton btn15 = new JButton("15"); frameObj.add(btn1); frameObj.add(btn2); frameObj.add(btn3); frameObj.add(btn4); frameObj.add(btn5); frameObj.add(btn6); frameObj.add(btn7); frameObj.add(btn8); frameObj.add(btn9); frameObj.add(btn10); frameObj.add(btn11); frameObj.add(btn12); frameObj.add(btn13); frameObj.add(btn14); frameObj.add(btn15); frameObj.setLayout(new GridLayout(4,4)); frameObj.setSize(300, 300); frameObj.setVisible(true); } public static void main(String args[]) { new Pgm5b(); } } OUTPUT