Java Programming Part B
Java Programming Part B
: INDEX – PART B :
import java.io.*;
import java.lang.Thread;
class MyThreadRunnable1 implements Runnable
{
public void run()
{
System.out.println("I am a thread 1 not a threat 1");
System.out.println("I am a thread 1 not a threat 1");
System.out.println("I am a thread 1 not a threat 1");
System.out.println("I am a thread 1 not a threat 1");
System.out.println("I am a thread 1 not a threat 1");
}
}
class MyThreadRunnable2 implements Runnable
{
public void run()
{
System.out.println("I am a thread 2 not a threat 2");
System.out.println("I am a thread 2 not a threat 2");
System.out.println("I am a thread 2 not a threat 2");
System.out.println("I am a thread 2 not a threat 2");
System.out.println("I am a thread 2 not a threat 2");
}
}
public class Thread_runnable
{
public static void main(String[] args)
{
MyThreadRunnable1 bullet1 = new MyThreadRunnable1();
Thread gun1 = new Thread(bullet1);
gun1.start();
MyThreadRunnable2 bullet2 = new MyThreadRunnable2();
Thread gun2 = new Thread(bullet2);
gun2.start();
}
}
3. 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 Exception_Handling
{
public static void main(String args[])
{
int[] myArray = {897, 56, 78, 90, 12, 123, 75};
System.out.println("Elements in the array are: ");
for(int i=0;i<myArray.length;i++)
System.out.println(myArray[i]);
Scanner sc = new Scanner(System.in);
System.out.println("Enter the index of the required element :");
try
{
int element = sc.nextInt();
System.out.println("Element in the given index is:"+myArray[element]);
}
catch(ArrayIndexOutOfBoundsException e)
{
System.out.println("The index you have entered is invalid");
System.out.println("Please enter an index number between 0 and 6");
}
}
}
import java.util.Scanner;
public class Exception_Handling123
{
public static void main(String args[])
{
int a=0,b=0;
Scanner sc = new Scanner(System.in);
System.out.println("Enter the two integers :");
try
{
a = sc.nextInt();
b= sc.nextInt();
System.out.println("Division Result: "+a/b);
catch(ArithmeticException e)
{
}
finally
{
System.out.println("Divide By Zero Exception Occured");
if(b==0)
{
b=5;
System.out.println("New value is assigned to b:" + b);
System.out.println("Division Result: "+a/b);
}
}
}
}
import javax.swing.*;
import java.awt.*;
class Text_Label
{
//Driver function
public static void main(String args[])
{
//Create a frame
JFrame frame=new JFrame("NEW FRAME");
frame.setSize(500,500);
frame.setBackground(Color.white);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(null);
//Create a Label
JLabel label=new JLabel();
label.setBounds(0,100,500,50);
frame.add(label);
//Write text to the label
String str="DEPARTMENT OF BCA, GFGC HOSADURGA";
label.setText(str);
//Display the frame
frame.setVisible(true);
}
}
import javax.swing.*;
class MenuExample
{
JMenu menu, submenu;
JMenuItem i1, i2, i3, i4, i5;
MenuExample()
{
JFrame f= new JFrame("Menu and MenuItem Example");
JMenuBar mb=new JMenuBar();
menu=new JMenu("Menu");
submenu=new JMenu("Sub Menu");
i1=new JMenuItem("Item 1");
i2=new JMenuItem("Item 2");
i3=new JMenuItem("Item 3");
i4=new JMenuItem("Item 4");
i5=new JMenuItem("Item 5");
menu.add(i1); menu.add(i2); menu.add(i3);
submenu.add(i4); submenu.add(i5);
menu.add(submenu);
mb.add(menu);
f.setJMenuBar(mb);
f.setSize(400,400);
f.setLayout(null);
f.setVisible(true);
}
public static void main(String args[])
{
new MenuExample();
}
}
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
f.add(ta);
f.setJMenuBar(mb);
f.setLayout(null);
f.setSize(400,400);
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 MenuExample();
}
}