IT Java Programming Lab Manual
IT Java Programming Lab Manual
LAB MANUAL
B. Tech – II Year - II Semester
R18 Regulation
NAME: __________________________________________
BRANCH: ________________________________________
INDEX
Sl.no Contents Page no
1 Preface 3
2 Objective and Relevance 3
3 Outcomes 3
4 Code of Conduct 4
5 List of Experiments 5
6 Syllabus analysis 7
7 Session Plan 10
8 Operating Environment 13
9 Experiment Write Up 13
1. PREFACE
This laboratory manual is prepared by the Department of Information Technology for Java
Programming laboratory. This lab manual can be used as instructional book for students, staff and
instructors to assist in performing and understanding the experiments. In this manual, experiments as
per the college syllabus are described. This manual will be available in electronic form from
College’s official website, for the betterment of students.
To understand Object Oriented Programming concepts and apply them in problem solving.
To learn the basics of java concepts and GUI based programming.
To write programs using abstract classes.
To write programs for solving real world problems using java collection frame work.
To write multithreaded programs.
To introduce java compiler and eclipse platform.
3. OUTCOMES
4. CODE OF CONDUCT
Students should report to the concerned lab as per the time table.
Students who turn up late to the labs will in no case be permitted to do the program schedule for the day.
After completion of the program, certification of the concerned staff in-charge in the observation book is
necessary.
Student should bring a notebook of 100 pages and should enter the readings/observations into the
notebook while performing the experiment.
The record of observations along with the detailed experimental procedure of the experiment in the
immediate last session should be submitted and certified staff member in-charge.
Students are required to prepare thoroughly to perform the experiment before coming to laboratory
5. LIST OF EXPERIMENTS
1. Use Eclipse or Net bean platform and acquaint with the various menus. Create a test project, add a
test class, and run it. See how you can use auto suggestions, auto fill. Try code formatter and code
refactoring like renaming variables, methods, and classes. Try debug step by step with a small
program of about 10 to 15 lines which contains at least one if else condition and a for loop.
2. Write a Java program that works as a simple calculator. Use a grid layout to arrange buttons for the
digits and for the +, -,*, % operations. Add a text field to display the result. Handle any possible
exceptions like divided by zero.
4. Write a Java program that creates a user interface to perform integer divisions. The user enters
two numbers in the text fields, Num1 and Num2. The division of Num1 and Num 2 is displayed in
the Result field when the Divide button is clicked. If Num1 or Num2 were not an integer, the
program would throw a Number Format Exception. If Num2 were Zero, the program would throw an
Arithmetic Exception. Display the exception in a message dialog box.
5. Write a Java program that implements a multi-thread application that has three threads. First
thread generates random integer every 1 second and if the value is even, second thread computes the
square of the number and prints. If the value is odd, the third thread will print the value of cube of the
number.
7. Write a Java program that simulates a traffic light. The program lets the user select one of three
lights: red, yellow, or green with radio buttons. On selecting a button, an appropriate message with
“Stop” or “Ready” or “Go” should appear above the buttons in selected color. Initially, there is no
message shown.
8. Write a Java program to create an abstract class named Shape that contains two integers and an
empty method named print Area (). Provide three classes named Rectangle, Triangle, and Circle such
that each one of the classes extends the class Shape. Each one of the classes contains only the method
print Area () that prints the area of the given shape.
9. Suppose that a table named Table.txt is stored in a text file. The first line in the file is the header,
and the remaining lines correspond to rows in the table. The elements are separated by commas.
Write a java program to display the table using Labels in Grid Layout.
10. Write a Java program that handles all mouse events and shows the event name at the center of the
window when a mouse event is fired (Use Adapter classes).
11. Write a Java program that loads names and phone numbers from a text file where the data is
organized as one line per record and each field in a record are separated by a tab (\t). It takes a name
or phone number as input and prints the corresponding other value from the hash table (hint: use hash
tables).
12. Write a Java program that correctly implements the producer – consumer problem using the
concept of interthread communication.
13. Write a Java program to list all the files in a directory including the files present in all its
subdirectories.
14. Write a Java program that implements Quick sort algorithm for sorting a list of names in
ascending order.
15. Write a Java program that implements Bubble sort algorithm for sorting in descending order and
also shows the number of interchanges occurred for the given set of integers.
6. SYLLABUS ANALYSIS
7. SESSION PLAN
S. No. Week No. Unit as per Activity Remarks
Syllabus
Introduction PREREQUISITE
1 1 UNIT-I Basic java programs
8. OPERATING ENVIRONMENT
Hardware
No. of System : 60(IBM)
Processor : PIV 1.67 GHZ
RAM : 512 MB
Hard Disk : 40 GB
Software
Operating System : Window XP
IDE : JDK 1.7 and Eclicpse or Net Beans
EXPERIMENT 1:
OBJECTIVE:
To debug step by step with small program of about 10 to 15 lines which contains at least one
if else condition and a for loop.
PROGRAM LOGIC:
PROCEDURE:
SOURCE CODE
Import java.util.*;
int c = 0;
for (inti = 1; i<= n; i++)
if (n % i == 0)
{
c++;
}
}
if (c == 2)
{
System.out.println(n + "is Prime Number");
}
else
{
System.out.println(n + "is not Prime Number");
}
}
}
EXPECTED OUTPUT:
run:
Welcome To Aurora’s Technological & Research Institute
--------Prime Number--------
Enter valid Number
23
23is Prime Number
BUILD SUCCESSFUL (total time: 11 seconds)
EXPERIMENT 2:
OBJECTIVE:
To develop a program that works as simple calculator .Uses grid layout to arrange buttons for
the digits and for the +,-,*,% operations. Add text field to display the results, Handle any
possible exceptions like divide by zero.
RESOURCES:
PROGRAM LOGIC:
PROCEDURE:
SOURCE CODE:
importjavax.swing.*;
importjavax.swing.event.*;
importjava.awt.*;
importjava.awt.event.*;
publicJPanel p;
public A() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(400, 400);
p = new JPanel(new FlowLayout());
tf1 = new JTextField(10); p.add(tf1);
add(p);
case "1":
{
v = v + "1"; tf1.setText(v);
}
break;
case "2":
{
v = v +"2";
tf1.setText(v);
}
break;
case "3":
{
v = v + "3";
tf1.setText(v); }
break;
case "4":
{
v = v + "4";
tf1.setText(v); }
break;
case "5":
{
v = v + "5";
tf1.setText(v);
}
break;
case "6":
{
v = v + "6";
tf1.setText(v);
}
break;
case "7":
{
v = v + "7";
tf1.setText(v);
}
break;
Aurora’s Technological & Research Institute 16
Department of IT Lab Manual
case "8":
{
v = v + "8";
tf1.setText(v);
}
break;
case "9":
{
v = v + "9"; tf1.setText(v);
}
break;
case "0":
{
v = v + "0";
tf1.setText(v);
}
break;
case "+":
{
op = "+";
v1 = tf1.getText(); v = "";
}
break;
case "-":
{
op = "-";
v1 = tf1.getText();
v = "";
}
break;
case "*":
{
op = "*";
v1 = tf1.getText();
v = "";
}
break;
case "/":
{
Aurora’s Technological & Research Institute 17
Department of IT Lab Manual
op = "/";
v1 = tf1.getText(); v = "";
}
break;
case "%":
{
op = "%";
v1 = tf1.getText();
v = "";
}
break; case "=":
{
switch (op)
{
case "+": {
v = tf1.getText();
if (v.equals("")) v = "0";
long i = Long.parseLong(v1)+Long.parseLong(v);
tf1.setText(String.valueOf(i));
v="";
}break;
case "-": {
v = tf1.getText(); if(v.equals(""))
v = "0";
long i = Long.parseLong(v1) - Long.parseLong(v);
tf1.setText(String.valueOf(i));
v="";
}break;
case "*": {
v = tf1.getText();
if (v.equals(""))
v = "0";
long i = Long.parseLong(v1) * Long.parseLong(v);
tf1.setText(String.valueOf(i));
v="";
}break;
case "/": {
Aurora’s Technological & Research Institute 18
Department of IT Lab Manual
try {
v = tf1.getText(); if(v.equals("")) v = "0";
long i = Long.parseLong(v1) / Long.parseLong(v);
tf1.setText(String.valueOf(i));
v="";
} catch (Exception ex)
{
JOptionPane.showMessageDialog(this, ex.getMessage());
}
}break;
case "%": {
try {
v = tf1.getText(); if(v.equals(""))
v = "0";
long i = Long.parseLong(v1) % Long.parseLong(v);
tf1.setText(String.valueOf(i)); v="";
} catch (Exception ex)
{
JOptionPane.showMessageDialog(this, ex.getMessage());
}}
break;}}break;}}}
EXPECTED OUTPUT:
EXPERIMENT-3(A)
OBJECTIVE:
To develop an Applet in java that displays a Simple Message
PROGRAM LOGIC:
1. Start the program
2. Create a class with the name „Sms‟ extends Applet.
3. Declare the Font class object.
4. Set the font.
5. Call the setFont method and drawString method g.setFont(font); g.drawString(“String",
50, 50);
6.Create the HTML file for applet tag.
PROCEDURE:
To execute a java program we require setting a class path:
1.C:\set path= C:\Program Files\Java\ JDK1.7\bin;.;
2.C:\javac HelloJava.java
3.C:\appletviewer Sms.html
SOURCE CODE:
importjava.applet.Applet;
import java.awt.*;
public class HelloJava extends Applet
{
public void init()
{}
public void paint(Graphics g)
{
g.setColor(Color.blue);
Font font = new Font("verdana", Font.BOLD, 15); g.setFont(font);
g.drawString("Hello Java ", 50, 50);
}
}
Expected Output:
EXPERIMENT-3(B)
OBJECTIVE:
To develop an Applet in java that receives an integer in one Text Field, and computes its
Factorial value and returns it in another text Field ,when button named “Compute” is clicked
PROGRAM LOGIC:
Start the program
Create a class with the name “Fact‟ extends Applet which implements ActionListener.
Declare the Label,TextField, Button variables.
Call init method
Call the setLayout method, setLayout(g);
Create the HTML file for applet tag.
PROCEDURE:
1.C:\javac Fact.java
2.C:\appletviewer Fact.html
SOURCE CODE:
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
public class Fact extends Applet implements ActionListener
{
Label l1, l2, l3;
TextField tf1, tf2;
Button b1;
public void init()
{
setSize(400, 200);
FlowLayout g = new FlowLayout();
setLayout(g);
l1 = new Label("Enter Value");
l1.setForeground(Color.BLUE);
add(l1);
tf1 = new TextField(5);
tf1.setText("0");
add(tf1);
b1 = new Button("Compute");
b1.addActionListener(this);
add(b1);
l3 = new Label();
add(l3);
l2 = new Label("factorial: ");
l2.setForeground(Color.BLUE);
add(l2);
tf2 = new TextField(5);
add(tf2);
}
public void actionPerformed(ActionEventae)
{
long n = Integer.parseInt(tf1.getText());
long f = 1;
while (n != 0)
{ f = f * n;
n--;
} tf2.setText(String.valueOf(f));
}
}
Expected Output:
EXPERIMENT-4
OBJECTIVE:
To creates User Interface to perform Integer Divisions. The user enters two numbers in text
fields, Num1 and Num2.The division of Num1 and Num2 is displayed in the result field when
the divide button clicked. If Num1 or Num2 were not integer, the program would throw a
NumberFormatException, If Num2 is Zero, and the program would throw an Arithmetic
Exception. Display the Exception in message box.
PROGRAM LOGIC:
PROCEDURE:
1.C:\javac JavaApplication9.java
2. C:\java JavaApplication9
SOURCE CODE:
importjava.awt.*;
import java.awt.event.*;
import javax.swing.*;
importjavax.swing.event.*;
class A1 extends JFrame implements ActionListener
{
JLabel l1, l2, l3;
JTextField tf1, tf2, tf3;
JButton b1;
A1()
{
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new FlowLayout());
l1 = new JLabel("Welcome");
setSize(800, 400);
l1 = new JLabel("Enter Number1");
add(l1);
tf1 = new JTextField(10);
add(tf1);
l2 = new JLabel("Enter Number2");
add(l2);
tf2 = new JTextField(10);
add(tf2);
l3 = new JLabel("Result");
add(l3);
}
}
}
public class JavaApplication9
{
public static void main(String[] args)
{
A1 a = new A1();
}
}
EXPECTED OUTPUT:
Experiment: 5
OBJECTIVE:
Write Java Program that implements a multithread application that has three threads. First
thread generates Random integer for every second and if the value is even, second thread
computes the square of number and prints. If the value is odd, the third thread will print the
value of cube of number.
PROCEDURE:
1.C:\javac JavaApplication5.java
2. C:\java JavaApplication5
SOURCE CODE
import java.util.*;
System.out.println("New Thread "+ x +" is ODD and Cube of " + x + " is: " + x * x * x);
}
}
class A extends Thread
{
public void run()
{
Intnum = 0;
Random r = new Random();
try
{
for (int i = 0; i < 5; i++)
{
num = r.nextInt(100);
System.out.println("Main Thread and Generated Number is " + num);
if (num % 2 == 0)
{
Thread t1 = new Thread(new even(num));
t1.start();
} else {
Thread t2 = new Thread(new odd(num));
t2.start();
}
Thread.sleep(1000);
System.out.println("--------------------------------------");
}
}
catch (Exception ex)
{
System.out.println(ex.getMessage());
} } }
public class JavaProgram5
{
public static void main(String[] args)
{
A a = new A();
a.start();
}
Aurora’s Technological & Research Institute 28
Department of IT Lab Manual
EXPECTED OUTPUT:
EXPERIMENT-6
OBJECTIVE
WriteaJavaprogramforthefollowing:
Createadoublylinkedlistofelements,
Deletea given elementfromtheabovelist,
Display thecontents of the list after deletion.
SOURCE CODE:
import java.io.*;
importjava.util.Scanner;
classlinkedlist {
int data;
linkedlistprev;
linkedlist next;
linkedlist(int value) {
this.data = value;
}
void display() {
System.out.println(data);
}
}
class linked {
publiclinkedlistfstnode, lastnode;
linked() {
fstnode = null;
lastnode = null;
}
}
else {
node.prev = null;
node.next = fstnode;
fstnode.prev = node;
fstnode = node;
System.out.println("Node Inserted at the front of the Linked list!");
}
}
node = lastnode;
System.out.println("List of node in Descending order!");
while(node != null) {
node.display();
node = node.prev;
}
}
}
classdoublylinkedlist {
public static void main(String args[ ]) {
linked list = new linked();
Scanner input = new Scanner(System.in);
int op = 0;
while(op != 5) {
System.out.println("1. Insert at front 2. Insert at back 3. Delete 4. Display 5. Exit");
System.out.println("Enter your choice:");
op = Integer.parseInt(input.nextLine());
switch(op) {
case 1:
System.out.println("Enter the positive value for Linked list:");
list.insert_front(Integer.parseInt(input.nextLine()));
break;
case 2:
System.out.println("Enter the positive value for Linked list:");
list.insert_end(Integer.parseInt(input.nextLine()));
break;
case 3:
list.delete();
break;
case 4:
list.display();
break;
case 5:
System.out.println("Bye Bye!");
System.exit(0);
break;
default:
System.out.println("Invalid choice!");
}
}
}
}
EXPECTED OUTPUT:
1
Enter the positive value for Linked list:
6
Node inserted successfully!
1. Insert at front 2. Insert at back 3. Delete 4. Display 5. Exit
6
5
4
7
8
6
5
4
7
8
8
7
4
5
6
5
nodes available here!
Enter the node number which you want to delete from ascending order list:
Bye Bye!
EXPERIMENT-7
OBJECTIVE
To simulate a Traffic Light. The program lets the use select one of three lights :red, yellow or
Green with radio buttons. On selecting radio button, an appropriate message with “stop” or
“Ready” or “GO” should appear above the button in selected color. Intially ,there is no
message Shown.
PROGRAM LOGIC:
1. Start the program
2. Create a class with the name A implements ItemListener.
3. Create the ButtonGroup ,JRadiobuttons,JPanel.
4. Add to the JFrame
5. Register the components to the Jframe.
6. Close the Jframe.
PROCEDURE:
To execute a java program we require setting a class path:
1.C:\set path= C:\Program Files\Java\ JDK1.7\bin;.;
2.C:\javac TLights.java
3.C:\java TLights
SOURCE CODE:
importjavax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
l1.setText("STOP");
l1.setForeground(Color.red);
} break;
case "Yellow Light": { l1.setText("Ready");
l1.setForeground(Color.YELLOW);} break;
case "Green Light": { l1.setText("GO");
l1.setForeground(Color.GREEN);
} break;
}
}
}
A a = new A();
}}
EXPECTED OUTPUT:
EXPERIMENT-8
OBJECTIVE:
To create an abstract class named shape that contains two integers and an empty method
named printArea .Provide three classes named Rectangle ,Triangle and Circle subclass that
each one of the classes extends the Class Shape .Each one of the classes contains only the
method printArea() that prints the area of Shape.
PROGRAM LOGIC:
1. Start the program
2. Create a class with the name Shape.
3. Create the Rectangle,Triangle,Circle extends Shape.
4. Create the objects to the individual classes.
5. Call the PrintArea() on individual object.
PROCEDURE:
To execute a java program we require setting a class path: 1.C:\set path= C:\Program
Files\Java\ JDK1.7\bin;.; 2.C:\javac Abstex.java
3.C:\java Abstex
SOURCE CODE:
import java.util.*;
abstract class shape
{
public int x,y;
public abstract void printArea();
}
class Rectangle1 extends shape
{
public void printArea()
{
float area;
area= x * y;
System.out.println("Area of Rectangle is " +area);
}
}
class Triangle extends shape
Aurora’s Technological & Research Institute 40
Department of IT Lab Manual
{
public void printArea()
{
float area;
area= (x * y) / 2;
System.out.println("Area of Triangle is " + area);
}
}
class Circle extends shape
{
public void printArea()
{
float area;
area=(22 * x * x) / 7;
System.out.println("Area of Circle is " + area);
}
}
public class Shapes
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
EXPECTED OUTPUT:
EXPERIMENT-9
OBJECTIVE:
To display a table using Label in Grid Layout.Suppose that a table named Table.txt is stored
in a text file. The First line in the file is the header, and the remaining lines corresponding
rows in table. The elements are separated by commas.
PROGRAM LOGIC:
1. Start the program
2. Create a class with the name A implements ItemListener.
3.Create the FileInputStream object.
4.Create the Scanner object delimited by comma.
5.Close the Jframe.
SOURCE CODE:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
import java.io.*;
public class Table1 extends JFrame
{
int i=0;
int j=0,k=0;
Object data[][]=new Object[5][4];
Object list[][]=new Object[5][4];
JButton save;
JTable table1;
FileInputStream fis;
DataInputStream dis;
public Table1()
{
String d= " ";
Container con=getContentPane();
con.setLayout(new BorderLayout());
final String[] colHeads={"Name","Roll Number","Department","Percentage"};
try
{
String s=JOptionPane.showInputDialog("Enter the File name present in the current directory");
FileInputStream fis=new FileInputStream(s);
DataInputStream dis = new DataInputStream(fis);
while ((d=dis.readLine())!=null)
{
StringTokenizer st1=new StringTokenizer(d,",");
while (st1.hasMoreTokens())
{
for (j=0;j<4;j++)
{
data[i][j]=st1.nextToken();
System.out.println(data[i][j]);
}
i++;
}
System.out.println ("______________");
}
} catch (Exception e)
{
System.out.println ("Exception raised" +e.toString());
}
table1=new JTable(data,colHeads);
int v=ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED;
int h=ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED;
JScrollPane scroll=new JScrollPane(table1,v,h);
con.add(scroll,BorderLayout.CENTER);
}
public static void main(String args[])
{
EXPERIMENT-10
OBJECTIVE:
Write a Java Program that handles all mouse events and show event name at the center of the
window when the mouse event is fired. (Use Adapter Classes)
importjavax.swing.*;
importjava.awt.*;
importjavax.swing.event.*;
importjava.awt.event.*;
class A extends JFrame implements MouseListener
{
JLabel l1;
public A()
{
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(400, 400);
setLayout(new
FlowLayout()); l1 = new
JLabel();
Font f = new Font("Verdana", Font.BOLD, 20);
l1.setFont(f);
l1.setForeground(Color.BLUE);
l1.setAlignmentX(Component.CENTER_ALIGNM
ENT);
l1.setAlignmentY(Component.CENTER_ALIGNM
ENT); add(l1);
addMouseListener(this);
setVisible(true);
}
public void mouseExited(MouseEvent m)
{
l1.setText("Mouse Exited");
}
public void mouseEntered(MouseEvent m)
{
l1.setText("Mouse Entered");
}
public void mouseReleased(MouseEvent m)
{
l1.setText("Mouse Released");
}
public void mousePressed(MouseEvent m)
{
l1.setText("Mouse Pressed");
}
public void mouseClicked(MouseEvent m)
{
l1.setText("Mouse Clicked");
}
}
public class Mevents
{
public static void main(String[] args)
{
A a = new A();
}
}
EXPECTED OUTPUT:
EXPERIMENT-11
OBJECTIVE:
To load the names and phone numbers from the text file where data is organized as one line
per record and each field in record are separated by a tab(\t).It takes a name or phone number
as input and prints corresponding other value from hash table.(hint: use Hash Table)
PROGRAM LOGIC:
1. Start the program
2. Create a class with the name Hashtbl
3. Create FileInputStream, Scanner, Hashtable objects
4. Read the data from the hash table.
5. Display the data.
PROCEDURE:
To execute a java program we require setting a class path:
1.C:\set path= C:\Program Files\Java\ JDK1.7\bin;.;
2.C:\javac Hashtbl.java
3.C:\java Hashtbl
SOURCE CODE:
importjava.util.*;
import java.io.*;
public class Hashtbl
{
public static void main(String[] args)
{
try
{
FileInputStreamfs = new FileInputStream("D:\\ph.txt");
Scanner sc = new Scanner(fs).useDelimiter("\\s+");
Hashtable<String, String>ht = new Hashtable<String, String>(); String[] arrayList;
String a;
System.out.println("Welcome to Aurora’s Technological and Research Institute ");
System.out.println("HASH TABLE IS");
System.out.println("--------------------------");
System.out.println("KEY : VALUE");
while (sc.hasNext())
Aurora’s Technological & Research Institute 49
Department of IT Lab Manual
{
a = sc.nextLine();
arrayList = a.split("\\s+");
ht.put(arrayList[0], arrayList[1]);
System.out.println(arrayList[0] + ":" + arrayList[1]);
}
System.out.println("----MENU------");
System.out.println("----1.Search by Name------");
System.out.println("----2.Search by Mobile------");
System.out.println("----3.Exit------");
String opt = "";
String name, mobile;
Scanner s = new Scanner(System.in);
while (opt != "3")
{
System.out.println("Enter Your Option 1,2,3");
opt = s.next();
switch (opt)
{
case "1":
{
System.out.println("Enter Name"); name = s.next();
if (ht.containsKey(name))
{
System.out.println("Mobile is " + ht.get(name));
}
else { System.out.println("Not Found"); }}
break;
case "2":
{
System.out.println("Enter mobile"); mobile = s.next();
if (ht.containsValue(mobile))
{
for (Map.Entry e : ht.entrySet())
{
if (mobile.equals(e.getValue()))
{
System.out.println("Name is " + e.getKey());
}
Aurora’s Technological & Research Institute 50
Department of IT Lab Manual
}
}
else
{
System.out.println("Not Found");
}
}
break;
case "3":
{
System.out.println("Menu Successfully Exited"); } break; default:
System.out.println("Choose Option betwen 1 and Three"); break;
}
}
}
catch (Exception ex)
{ System.out.println(ex.getMessage()); }
}
}
EXPECTED OUTPUT:
EXPERIMENT-12
OBJECTIVE:
Write a Java program that correctly implements the producer – consumer problem using the
concept of interthread communication.
PROGRAM LOGIC:
1. Start the program
2. Create a class with the name Threadexample
3. Display the data.
PROCEDURE:
To execute a java program we require setting a class path:
1. C:\set path= C:\Program Files\Java\ JDK1.7\bin
2. C:\javacThreadexample.java
3. C:\java Threadexample
SOURCE CODE:
import java.util.LinkedList;
public class Threadexample
{
public static void main(String[] args)throws InterruptedException
{
// Object of a class that has both produce() and consume() methods
final PC pc = new PC();
// Create producer thread
Thread t1 = new Thread(new Runnable()
{
public void run()
{
try
{
pc.produce();
}
catch(InterruptedException e)
{
e.printStackTrace();
Aurora’s Technological & Research Institute 52
Department of IT Lab Manual
}
}
});
// t1 finishes before t2
t1.join();
t2.join();
}
// and sleep
Thread.sleep(1000);
}
}
}
}
}
EXPECTED OUTPUT:
EXPERIMENT-13
OBJECTIVE:
Writea Java program to list all the files in a directory including the files present in all its sub
directories.
SOURCE CODE:
import java.io.File;
// for files
if(arr[index].isFile())
System.out.println(arr[index].getName());
// for sub-directories
else if(arr[index].isDirectory())
{
System.out.println("["+ arr[index].getName() + "]");
// Driver Method
public static void main(String[] args)
{
// Provide full path for directory(change accordingly)
String maindirpath = "G:\\CSI";
// File object
if(maindir.exists() &&maindir.isDirectory())
{
// array for files and sub-directories
// of directory pointed by maindir
File arr[] = maindir.listFiles();
System.out.println("**********************************************");
System.out.println("Files from main directory : "+ maindir);
System.out.println("**********************************************");
EXPECTED OUTPUT:
EXPERIMENT-14
OBJECTIVE:
Writea Java program that implements Quick sort algorithm for sorting alist of numbers in ascending
order.
SOURCE CODE:
importjava.util.Scanner;
public class QuickSort
{
i++;
j--;
}
EXPECTED OUPUT:
Enter number of integer elements
10
Enter 10 integer elements
877 567 3456 876 467 26 934 9876 1 4567
Elements after sorting
1 26 467 567 876 877 934 3456 4567 9876
EXPERIMENT-15
OBJECTIVE:
Write a Java program that implements Bubble sort algorithm for sorting in descending order
and also shows the number of interchanges occurred for the given set of integers.
PROGRAM LOGIC:
1. Start the program
2. Create a class with the name Threadexample
3. Display the data.
PROCEDURE:
SOURCE CODE:
import java.util.Scanner;
class BubbleSort
{
public static void main(String []args)
{
int num, i, j, temp,count=0;
Scanner input = new Scanner(System.in);