Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
13 kB
33
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








LAB 6B: 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.

package LabPgm6b;

import java.awt.*; 
import java.awt.event.*; 

public class Pgm6b 
{
	public static void main(String args[]) 
	{      
		Frame f=new Frame("Button Event");      
		Label l=new Label("DETAILS OF PARENTS");      
		l.setFont(new Font("Calibri",Font.BOLD, 16));   
   
		Label nl=new Label();      
		Label dl=new Label();      
		Label al=new Label();      

		l.setBounds(20,20,500,50);      
		nl.setBounds(20,110,500,30);      
		dl.setBounds(20,150,500,30); 
		
		al.setBounds(20,190,500,30);      
		Button mb=new Button("Mother");      
		mb.setBounds(20,70,50,30);      
		
		mb.addActionListener(new ActionListener() 
		{           
			public void actionPerformed(ActionEvent e) 
			{                  
				nl.setText("NAME:"+"    "+"Aishwarya");                  
				dl.setText("DESIGNATION:"+"  "+"Professor"); 
				al.setText("AGE:"+"    "+"42");           
			}    
		});    
		
		Button fb=new Button("Father");   
		fb.setBounds(80,70,50,30);    

		fb.addActionListener(new ActionListener() 
		{        
			public void actionPerformed(ActionEvent e) 
			{           
				nl.setText("NAME:"+"   "+"Ram");           
				dl.setText("DESIGNATION:"+"    "+"Manager");           
				al.setText("AGE:"+"    "+"44");            
				}    
		});    
				
		f.add(mb);    
		f.add(fb);    
		f.add(l);    
		f.add(nl);    
		f.add(dl);    
		f.add(al);    
		
		f.setSize(250,250);    
		f.setLayout(null);    
		f.setVisible(true);    
	}
}







OUTPUT

































LAB 7B: Create a frame which displays personal details with respect to a button click

package LabPgm7b;

import java.awt.*; 
import java.awt.event.*;

public class Pgm7b 
{
	public static void main(String args[])    
	{      
		Frame f = new Frame("Button Example");      
		Label l = new Label("Welcome TO MY PAGE");      
		l.setFont(new Font("Calibri",Font.BOLD,16)); 
		
		Label fnl= new Label();       
		Label mnl = new Label();       
		Label lnl = new Label();       
		Label rl = new Label();       
		Label al = new Label();    
		
		l.setBounds(150,30,600,50);    
		fnl.setBounds(20,120,600,30);    
		mnl.setBounds(20,160,600,30);    
		lnl.setBounds(20,200,600,30);    
		rl.setBounds(20,240,600,30);     
		al.setBounds(20,280,600,30); 
		
		Button mb = new Button ("Click Here FOR MY PERSONAL DETAILS"); 
		mb.setFont(new Font ("Calibri",Font.BOLD,14)); 
		mb.setBounds(110,70,320,30); 
		



		mb.addActionListener(new ActionListener()
		{      
			public void actionPerformed(ActionEvent e)
			{         
				fnl.setText("FULL NAME: Aishwarya Rao");
				mnl.setText("Roll No: BNU35628");         
				lnl.setText("College Name: Jain Degree College");
				rl.setText("Father Name: Ranjit");
				al.setText("Mother Name: Rekha");
				} 
			}); 
	
		f.add(mb);
		f.add(mnl);  f.add(fnl); f.add(lnl);
		f.add(l);  f.add(rl); f.add(al); 
		
		f.setSize(500, 400); 
		f.setLayout(null); 
		f.setVisible(true);  
	}
}















OUTPUT:





















LAB 8B: Program 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.

package LabPgm8b;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Pgm8b extends Frame implements ActionListener 
{
	Label L1,L2,L3;
    	TextField T1,T2,Result;
   	 Button B1,B2;
   
 Pgm8b()
	{
	        L1=new Label("Enter First Num:");
	        L1.setBounds(40,35,100,50);
	        add(L1);
	        
	        T1=new TextField(10);
	        T1.setBounds(140,50,50,20);
	        add(T1);
	        
	        L2=new Label("Enter Second Num:");
	        L2.setBounds(240,35,120,50);
	        add(L2);
	        
	        T2=new TextField(10);
	        T2.setBounds(360,50,60,20);
	        add(T2);
	        
	        L3=new Label("result");
	        L3.setBounds(170,150,50,50);
	        add(L3);
	        
	        Result=new TextField(10);
	        Result.setBounds(220,160,60,30);
	        add(Result);
	        
	        B1=new Button("ADD");
	        B1.setBounds(120,260,60,30);
	        add(B1);
	        
	        B2=new Button("CLEAR");
	        B2.setBounds(220,260,60,30);
	        add(B2);
	        
	        setSize(500,400);  
	        setLayout(null);  
	        setVisible(true);

	        B1.addActionListener(this);
	        B2.addActionListener(this);
   	 }

   	 public void actionPerformed(ActionEvent e) 
   	 {
      		  if(e.getSource()==B1) 
        		{
                int value1=Integer.parseInt(T1.getText());
                int value2=Integer.parseInt(T2.getText());
                int result=value1+value2;
                Result.setText(String.valueOf(result));
  		}
        



       		if(e.getSource()==B2) 
        		{
        	T1.setText("");
        	T2.setText("");
        	Result.setText("");
       		}
   	}

public static void main(String args[])
	{
		new Pgm8b();
	}
}


OUTPUT:





LAB 9B: 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

package JavaLab9b;

import java.awt.*;
import java.awt.event.*; 

public class Lab9b extends Frame implements KeyListener
{
	Label lbl;

	Lab9b() 
	{
		addKeyListener(this);
		requestFocus();
		lbl=new Label();
		lbl.setBounds(100,100,200,40);
		lbl.setFont(new Font("Calibri",Font.BOLD,16));
		add(lbl);
		
		setSize(400,300);
		setLayout(null);
		setVisible(true);
	} 

	public void keyPressed(KeyEvent e)
	{
		if(e.getKeyChar() == 'M' || e.getKeyChar() == 'm')
			lbl.setText("Good Morning");
		else if(e.getKeyChar() == 'A'||e.getKeyChar() == 'a')
			lbl.setText("Good Afternoon");
		else if (e.getKeyChar() == 'N' || e.getKeyChar() =='n')
			lbl.setText("Good Night");
		else if(e.getKeyChar() == 'E' || e.getKeyChar() == 'e')
			lbl.setText("Good Evening");
	} 

	public void keyReleased(KeyEvent e){} 
	public void keyTyped (KeyEvent e){} 
	
	public static void main(String[] args)
	{
		new Lab9b();
	}
}


OUTPUT
















LAB 10B: Demonstrate the various mouse handling events using suitable example.

package LabPgm10b;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Pgm10b extends Frame implements MouseListener
{
	Label l1, l2;
	int X, Y;
	 Pgm10b()
	 {
		 addMouseListener(this);
		
		 l1=new Label();
		 l1.setBounds(20,50,100,20);
		 add(l1);
		
		 l2=new Label();
		 l2.setBounds(20,50,215,150);
		 add(l2);
		
		 setSize(300,300);
		 setLayout(null);
		 setVisible(true);
	 }
	 public void mouseClicked(MouseEvent e)
	 {
		 l2.setText("Moving mouse at " + e.getX() + ", " + e.getY());
		 l1.setText("Mouse Clicked");
	 }


	 public void mouseEntered(MouseEvent e)
	 {
		 l2.setText("Moving mouse at " + e.getX() + ", " + e.getY());
		 l1.setText("Mouse Entered");
	 }
	 public void mouseExited(MouseEvent e)
	 {
		 l2.setText("Moving mouse at " + e.getX() + ", " + e.getY());
		 l1.setText("Mouse Exited");
	 }
	 public void mousePressed(MouseEvent e)
	 {
		 l2.setText("Moving mouse at " + e.getX() + ", " + e.getY());
		 l1.setText("Mouse Pressed");
	 }
	 public void mouseReleased(MouseEvent e)
	 {
		 l2.setText("Moving mouse at " + e.getX() + ", " + e.getY());
		 l1.setText("Mouse Released");
	 }
	 public static void main(String[] args)
	 {
		 new Pgm10b();
	 }
} 











OUTPUT:




















LAB 11B: Program to create menu bar and pull-down menus.

package LabPgm11b;

import javax.swing.*;  
import java.awt.event.*; 

public class Pgm11b implements ActionListener
{
	JFrame f;  
	JMenuBar mb;  
	JMenu file,edit,help;  
	JMenuItem cut,copy,paste,selectAll;  
	JTextArea ta;  
	      
	Pgm11b()
	{  
	f=new JFrame("MENU DEMO");  
	  
	cut=new JMenuItem("cut");  
	copy=new JMenuItem("copy");  
	paste=new JMenuItem("paste");  
	selectAll=new JMenuItem("selectAll");  
	  
	cut.addActionListener(this);  
	copy.addActionListener(this);  
	paste.addActionListener(this);  
	selectAll.addActionListener(this);  
	  
	mb=new JMenuBar();  
	mb.setBounds(5,5,400,40);  
	  
	file=new JMenu("File");  
	edit=new JMenu("Edit");  
	help=new JMenu("Help");  
	  
	edit.add(cut);
edit.add(copy);
edit.add(paste);
edit.add(selectAll);  
	  
	mb.add(file);mb.add(edit);mb.add(help);  
	  
	ta=new JTextArea();  
	ta.setBounds(15,50,400,400);  
	  
	f.add(mb);
f.add(ta);  
	  
	f.setLayout(null);  
	f.setSize(500,500);  
	f.setVisible(true);  
	}  
	  
	public void actionPerformed(ActionEvent e) 
{  
	if(e.getSource()==cut)  
		ta.cut();  
	if(e.getSource()==paste)  
		ta.paste();  
	if(e.getSource()==copy)  
		ta.copy();  
	if(e.getSource()==selectAll)  
		ta.selectAll();  
	}  
	  


	public static void main(String[] args) 
{  
	    new Pgm11b();  
	}  
}



OUTPUT: