LAB Manual
LAB Manual
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 atleast one if else condition and a for loop.
Procedure:-
STEP 1 : - Open any browser and search for Eclipse or Netbean IDE.
STEP 2 : - Open website of Eclipse or Netbean IDE and download the executable file(.exe) or rar file with
your operating system matched bit processor(32 or 64 bit). Download only the suitable file.
NOTE :- In some systems is necessary to set java Class Path and Java home path.
STEP 3 : - Install the eclipse or Netbean IDE, after completion of installation your ready to use the
IDE(Integrated Development Environment).
STEP 2 : - At the top right corner you will have a File option, click on new option and then New Project
option.
STEP 3 : - A new window will be pop up and you need to name the Project eg:- CSE. Make sure that the
module is also given, if module name is not given it arises error at compile time. For naming the module
a specific box is given below the New Project Naming box.
STEP 4 : - At the top right corner below you will be having an option called Project Explorer. All the
projects can be viewed there including with your newly created project.
STEP 5 : - left click or right click on that project a small window will be pop’s, click on new and click on
class to create new class.
STEP 6 : - Name the class name and select the main method and click on ok, a new class file will be seen
at the screen.
STEP 7 : - Make the code in the main method and click on run button to execute the project.
package heloo;
Refactor is used to rename the classes, interfaces, members and also Extraction.
Press F11 for debug or click on run option and select the debug option.
EXPERIMENT -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.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
JFrame actualWindow;
JTextField resultTxt;
BuildCalculator() {
actualWindow.setResizable(false);
expression.setFont(expressionFont);
siteTitle.setFont(expressionFont);
siteTitle.setHorizontalAlignment(SwingConstants.CENTER);
siteTitle.setForeground(Color.BLUE);
resultTxt.setBorder(null);
resultTxt.setFont(txtFont);
resultTxt.setHorizontalAlignment(SwingConstants.RIGHT);
btn_digits[i].addActionListener(this);
btn_plus.addActionListener(this);
btn_minus.addActionListener(this);
btn_mul.addActionListener(this);
btn_dot.addActionListener(this);
btn_equal.addActionListener(this);
btn_clear.addActionListener(this);
resultPanel.add(appTitle);
resultPanel.add(resultTxt);
resultPanel.add(expression);
buttonPanel.add(btn_digits[i]);
buttonPanel.add(btn_plus);
buttonPanel.add(btn_minus);
buttonPanel.add(btn_mul);
buttonPanel.add(btn_div);
buttonPanel.add(btn_dot);
buttonPanel.add(btn_equal);
infoPanel.add(btn_clear);
infoPanel.add(siteTitle);
actualWindow.add(resultPanel);
actualWindow.add(buttonPanel);
actualWindow.add(infoPanel);
actualWindow.setSize(300, 500);
actualWindow.setVisible(true);
@Override
eventFrom = e.getActionCommand().charAt(0);
String buildNumber;
if(Character.isDigit(eventFrom)) {
resultTxt.setText(buildNumber);
resultTxt.setText(buildNumber);
oparand_1 = Double.parseDouble(resultTxt.getText());
operator = e.getActionCommand();
resultTxt.setText("");
else {
operand_2 = Double.parseDouble(resultTxt.getText());
switch(operator) {
if(operand_2 == 0)
throw new
ArithmeticException();
resultTxt.setText(""+(oparand_1 /
operand_2)); break;
} catch(ArithmeticException ae) {
EXPERIMENT -3
b) 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 the button named “Compute” is clicked.
EXPERIMENT -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.
import javax.swing.event.*;
A() {
tf3.setText(String.valueOf(c));
A a = new A();
EXPERIMENT -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.
package multithread;
import java.util.*;
// class for Even Number
public int a;
public EvenNum(int a) {
this.a = a;
System.out.println("The Thread "+ a +" is EVEN and Square of " + a + " is : " + a * a);
public int a;
public OddNum(int a) {
this.a = a;
System.out.println("The Thread "+ a +" is ODD and Cube of " + a + " is: " + a * a * a);
int n = 0;
try {
for (int i = 0; i < 10; i++) {
n = rand.nextInt(20);
if (n % 2 == 0) {
thread1.start();
else {
thread2.start();
Thread.sleep(1000);
System.out.println("------------------------------------");
System.out.println(ex.getMessage());
// Driver class
rand_num.start();
EXPERIMENT-6
Write a Java program for the following: Create a doubly linked list of elements. Delete a given element
from the above list. Display the contents of the list after deletion.
package dbllist;
import java.util.*;
int i,ch,element,position;
System.out.println("6.Exit");
do {
ch=sc.nextInt();
switch(ch) {
case 1: // To read element form the user
element=sc.nextInt();
dblList.addFirst(element);
System.out.println("Successfully Inserted");
break;
element=sc.nextInt();
dblList.addLast(element);
System.out.println("Successfully Inserted");
break;
position=sc.nextInt();
if(position<=dblList.size()) {
// To read element
element=sc.nextInt();
dblList.add(position,element);
System.out.println("Successfully Inserted");
}
else {
break;
Integer ele_rm;
ele_rm=sc.nextInt();
if (dblList.contains(ele_rm)){
dblList.remove(ele_rm);
System.out.println("Successfully Deleted");
Iterator itr=dblList.iterator();
while(itr.hasNext()) {
System.out.print(itr.next()+"<->");
System.out.println("NULL");
else {
break;
while(itr.hasNext()) {
System.out.print(itr.next()+"<->");
System.out.println("NULL");
break;
break;
default:System.out.println("Invalid choice");
while(ch!=6);
EXPERIMENT - 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.
import java.awt.event.*;
public JRadioButton r1, r2, r3; public ButtonGroup bg; public JPanel p, p1;
public A()
setSize(800, 400);
add(l1);
p.add(l1);
add(p);
p1.add(r2); r2.addItemListener(this);
bg.add(r2);
bg.add(r3); setVisible(true);
switch (jb.getText())
{
case "Red Light":
l1.setText("STOP"); l1.setForeground(Color.RED);
break;
l1.setText("Ready"); l1.setForeground(Color.YELLOW);
break;
l1.setText("GO"); l1.setForeground(Color.GREEN);
break;
A a = new A();
}
Experiment-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.
package areaofrectangle;
import java.util.*;
float area;
area= x * y;
float area;
area= (x * y) / 2.0f;
float area;
area=(22 * x * x) / 7.0f;
int choice;
choice=sc.nextInt();
switch(choice) {
r.x=sc.nextInt();
r.y=sc.nextInt();
r.printArea();
break;
t.x=sc.nextInt();
t.y=sc.nextInt();
t.printArea();
break;
c.x = sc.nextInt();
c.printArea();
break;
EXPERIMENT – 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.
public A()
setLayout(g);
try
String a;
while (sc.hasNextLine())
a = sc.nextLine();
arrayList = a.split(",");
add(new JLabel(i));
setDefaultLookAndFeelDecorated(true); pack();
setVisible(true);
Experiment -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).
l1.setForeground(Color.BLUE); l1.setAlignmentX(Component.CENTER_ALIGNMENT);
l1.setAlignmentY(Component.CENTER_ALIGNMENT); add(l1);
addMouseListener(this); setVisible(true);
l1.setText("Mouse Exited");
l1.setText("Mouse Entered");
l1.setText("Mouse Released");
l1.setText("Mouse Pressed");
}
l1.setText("Mouse Clicked");
public class Mevents { public static void main(String[] args) { A a = new A();
EXPERIMENT-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).
package namesphone;
import java.io.*;
import java.util.*;
try
String a,str;
while(sc.hasNext())
a=sc.nextLine();
strarray=a.split("\t");
ht.put(strarray[0],strarray[1]);
str=s.next();
if(ht.containsKey(str))
System.out.println("phone no is"+ht.get(str));
else
catch(Exception e)
System.out.println(e);
}
}
EXPERIMENT-12
12. Write a Java program that correctly implements the producer – consumer problem using the
concept of inter thread communication.
package prdcrconscr;
class Thread1
int n;
boolean valueset=false;
if (!valueset)
try
wait();
catch (Exception e)
System.out.println("get" +n);
try
{
Thread.sleep(1000);
catch (Exception e)
valueset=false;
notify();
return n;
if (valueset)
try
wait();
catch (Exception e)
this.n=n;
valueset=true;
System.out.println("put" +n);
try
{
Thread.sleep(1000);
catch (Exception e)
notify();
return n;
Thread1 t;
Producer(Thread1 t)
this.t=t;
new Thread(this,"Producer").start();
int i=0;
while (true)
t.put(i++);
}
Thread1 t;
Consumer(Thread1 t)
this.t=t;
new Thread(this,"Consumer").start();
int i=0;
while (true)
t.get();
class Pdrcnsr
new Consumer(t);
EXPERIMENT-13
Write a Java program to list all the files in a directory including the files present in all its sub directories.
package listdict;
import java.io.File;
if(i == a.length)
return;
{
System.out.print("\t");
if(a[i].isFile())
System.out.println(a[i].getName());
// for sub-directories
else if(a[i].isDirectory())
printFileNames(a, i + 1, lvl);
// Main Method
// display statements
System.out.println("= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =");
System.out.println("= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =");
obj.printFileNames(a, 0, 0);
EXPERIMENT-14
Write a Java program that implements Quick sort algorithm for sorting a list of names in ascending order
package quicksort;
class Qsort
of pivot */
// equal to pivot
i++;
arr[i] = arr[j];
arr[j] = temp;
arr[i+1] = arr[high];
arr[high] = temp;
return i+1;
int n = arr.length;
System.out.print(arr[i]+" ");
System.out.println();
// Driver program
int n = arr.length;
ob.sort(arr, 0, n-1);
System.out.println("sorted array");
printArray(arr);
EXPERIMENT-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.
import java.util.Scanner;
public class Bsort {
num = input.nextInt();
array[i] = input.nextInt();
temp = array[j];
array[j] = array[j+1];
array[j+1] = temp;
}
}
System.out.println(array[i]);