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

Java Programming Part B

The document contains 7 Java programs related to threads, exceptions, GUI components like labels and menus. It includes programs to create threads using Thread and Runnable classes, handle exceptions like ArrayIndexOutOfBounds and Arithmetic exceptions, create labels and display messages, create menu bars and pull-down menus.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views

Java Programming Part B

The document contains 7 Java programs related to threads, exceptions, GUI components like labels and menus. It includes programs to create threads using Thread and Runnable classes, handle exceptions like ArrayIndexOutOfBounds and Arithmetic exceptions, create labels and display messages, create menu bars and pull-down menus.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

JAVA-PROGRAMMING (PART-B)

: INDEX – PART B :

Sl. No Name of Program Date of Sign of


submission Faculty
01 Java program to create thread by Extending
Thread class.

02 Java program to create thread by Runnable Thread


Class.

03 Java Program to catch Negative Array Size


Exception. This exception is caused when the array
size is initialized to negative values.
04 Java Program to demonstrate exception handling
with try, catch and finally.
05 Java Program which create and displays a message
on the window
06 Java Program to create menu bar with label name

07 Java Program to create menu and pull-down menus.

CHETAN M, Lecturer in Dept of BCA, GFGC HOSADURGA Page 1


JAVA-PROGRAMMING (PART-B)

1. java program to create thread by Extending Thread class.


import java.io.*;
import java.lang.Thread;
class SampleThread extends Thread
{
public void run()
{
System.out.println("Thread is under Running...");
for(int i= 1; i<=10; i++)
{
System.out.println("i = " + i);
}
}
}
public class My_Thread_Test
{
public static void main(String[] args)
{
SampleThread t1 = new SampleThread();
System.out.println("Thread about to start...");
t1.start();
}
}

CHETAN M, Lecturer in Dept of BCA, GFGC HOSADURGA Page 2


JAVA-PROGRAMMING (PART-B)

2. java program to create thread by Runnable Thread Class.

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();
}
}

CHETAN M, Lecturer in Dept of BCA, GFGC HOSADURGA Page 3


JAVA-PROGRAMMING (PART-B)

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");
}
}
}

CHETAN M, Lecturer in Dept of BCA, GFGC HOSADURGA Page 4


JAVA-PROGRAMMING (PART-B)

4. Program to demonstrate exception handling with try, catch and finally.

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);
}
}
}
}

CHETAN M, Lecturer in Dept of BCA, GFGC HOSADURGA Page 5


JAVA-PROGRAMMING (PART-B)

5. Program which create and displays a message on the window

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);
}
}

CHETAN M, Lecturer in Dept of BCA, GFGC HOSADURGA Page 6


JAVA-PROGRAMMING (PART-B)

6. Program to create menu bar with label name.

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();
}
}

CHETAN M, Lecturer in Dept of BCA, GFGC HOSADURGA Page 7


JAVA-PROGRAMMING (PART-B)

7. Program to create menu and pull-down menus.

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

public class MenuExample implements ActionListener


{
JFrame f;
JMenuBar mb;
JMenu file,edit,help;
JMenuItem cut,copy,paste,selectAll;
JMenuItem newfile,save,saveas,exit;
JTextArea ta;
MenuExample()
{
f=new JFrame();
newfile =new JMenuItem("new");
save=new JMenuItem("save");
saveas=new JMenuItem("saveas");
exit=new JMenuItem("exit");
cut=new JMenuItem("cut");
copy=new JMenuItem("copy");
paste=new JMenuItem("paste");
selectAll=new JMenuItem("selectAll");
newfile.addActionListener(this);
save.addActionListener(this);
saveas.addActionListener(this);
exit.addActionListener(this);
cut.addActionListener(this);
copy.addActionListener(this);
paste.addActionListener(this);
selectAll.addActionListener(this);
mb=new JMenuBar();
file=new JMenu("File");
edit=new JMenu("Edit");
help=new JMenu("Help");
file.add(newfile);file.add(save);file.add(saveas);file.add(exit);
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(5,5,360,320);
f.add(mb);

CHETAN M, Lecturer in Dept of BCA, GFGC HOSADURGA Page 8


JAVA-PROGRAMMING (PART-B)

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();
}
}

CHETAN M, Lecturer in Dept of BCA, GFGC HOSADURGA Page 9

You might also like