Java Lab Manual
Java Lab Manual
Enter a:2
Enter b:3
Enter c:1
Roots are real and unequal First root is:0.5
Second root is:-1.0
Exercise - 2
a) Implementation of Binary search mechanism
AIM: To write a JAVA program to search for an element in a given list of elements
using binary search mechanism
SOURCE-CODE:
import java.util.Scanner; class binarysearchdemo
{
public static void main(String args[])
{
int n, i, num,first, last, middle; int a[ ]=new int[20];
Scanner s = new Scanner(System.in); System.out.println("Enter total number of
elements:"); n = s.nextInt();
System.out.println("Enter elements in sorted order:"); for (i = 0; i < n; i++)
a[i] = s.nextInt();
System.out.println("Enter the search value:"); num = s.nextInt();
first = 0; last = n - 1;
middle = (first + last)/2; while( first <= last )
{
if ( a[middle] < num ) first = middle + 1;
else if ( a[middle] == num )
{
System.out.println("number found"); break;
}
else
{
last = middle - 1;
}
middle = (first + last)/2;
}
if ( first > last )
System.out.println( " Number is not found");
}
}
OUT-PUT:
Enter total number of elements: 5
Enter elements:
24689
Enter the search value: 8
number found
b) Bubble sort
AIM: To write a JAVA program to sort for an element in a given list of elements
using bubble sort
SOURCE-CODE:
import java.util.Scanner; class bubbledemo
{
public static void main(String args[])
{
int n, i,j, temp;
int a[ ]=new int[20];
Scanner s = new Scanner(System.in); System.out.println("Enter total number of
elements:"); n = s.nextInt();
System.out.println("Enter elements:"); for (i = 0; i < n; i++)
a[i] = s.nextInt(); for(i=0;i<n;i++)
{
for(j=0;j<n-1;j++)
{
if(a[j]>a[j+1])
{
temp=a[j]; a[j]=a[j+1]; a[j+1]=temp;
}
}
}
System.out.println("The sorted elements are:"); for(i=0;i<n;i++)
System.out.print("\t"+a[i]);
}
}
OUT-PUT:
Enter total number of elements:
10
Enter elements:
3257689140
The sorted elements are:
0123456789
c) Implementing StringBuffer
AIM: To write a JAVA program using StringBuffer to delete, remove
character
SOURCE-CODE:
class stringbufferdem o
{
public static void main(String[] args)
{
StringBuffer sb1 = new StringBuffer("Hello World"); sb1.delete(0,6);
System.out.println(sb1);
StringBuffer sb2 = new StringBuffer("Some Content");
System.out.println(sb2);
sb2.delete(0, sb2.length()); System.out.println(sb2);
StringBuffer sb3 = new StringBuffer("Hello World"); sb3.deleteCharAt(0);
System.out.println(sb3);
}
}
OUT-PUT:
World
Some Content
hello World
Exercise - 3
a) Implementing Class & Objects
AIM: To write a JAVA program to implement class mechanism. – Create a
class, methods and invoke them inside main method
SOURCE-CODE:
1. no return type and without parameter-list:
class A
{
int l=10,b=20; void display()
{
System.out.println(l); System.out.println(b);
}
}
class methoddemo
{
public static void main(String args[])
{
A a1=new A(); a1.display();
}
}
OUT-PUT:
10
20
2.no return type and with parameter-list:
class A
{
void display(int l,int b)
{
System.out.println(l); System.out.println(b);
}
}
class methoddemo
{
public static void main(String args[])
{
A a1=new A(); a1.display(10,20);
}
}
OUT-PUT:
10
20
2. return type and without parameter-list
class A
{
int l=10,b=20;
int area()
{
return l*b;
}
}
class methoddemo
{
public static void main(String args[])
{
A a1=new A(); int r=a1.area();
System.out.println("The area is: "+r);
}
}
OUT-PUT:
The area is:200
3. return type and with parameter-list:
class A
{
int area(int l,int b)
{
return l*b;
}
}
class methoddemo
{
public static void main(String args[])
{
A a1=new A();
int r=a1.area(10,20); System.out.println(“The area is:”+r);
}
}
OUT-PUT:
The area is:200
b) Implementing Constructor
AIM: To write a JAVAto implement constructor
SOURCE-CODEs:
(i) A constructor with no parameters:
class A
{
int l,b;
A()
{ l=10; b=20;
}
int area()
{
return l*b;
}
}
class constructordemo
{
public static void main(String args[])
{
A a1=new A(); int r=a1.area();
System.out.println("The area is: "+r);
}
}
OUT-PUT:
The area is:200
(ii) A constructor with parameters
class A
{
int l,b;
A(int u,int v)
{
l=u;
b=v;
}
int area()
{
return l*b;
}
}
class constructordemo
{
public static void main(String args[])
{
A a1=new A(10,20);
int r=a1.area(); System.out.println("The area is: "+r);
}
}
OUT-PUT:
The area is:200
c)Constructor Overloading
AIM: To write a JAVA program to implement constructor overloading
SOURCE-CODE:
class A
{
int l,b;
A()
{ l=10; b=20;
}
A(int u,int v)
{
l=u; b=v;
}
int area()
{
return l*b;
}
}
class overconstructdemo
{
public static void main(String args[])
{
A a1=new A(); int r1=a1.area();
System.out.println("The area is: "+r1);
A a2=new A(30,40);
int r2=a2.area();
System.out.println("The area is: "+r2);
}
}
OUT-PUT:
The area is: 200
The area is: 1200
d)Method Overloading
AIM: To write a JAVA program implement method overloading
SOURCE-CODE:
class A
{
int l=10,b=20;
int area()
{
return l*b;
}
int area(int l,int b)
{
return l*b;
}
}
class overmethoddemo
{
public static void main(String args[])
{
A a1=new A(); int r1=a1.area();
System.out.println("The area is: "+r1); int r2=a1.area(5,20);
System.out.println("The area is: "+r2);
}
}
OUT-PUT:
The area is: 200
The area is: 100
Exercise - 4
a) Implementing Single Inheritance
AIM: To write a JAVA program to implement Single Inheritance
SOURCE-CODE:
class A
{
A()
{
System.out.println("Inside A's Constructor");
}
}
class B extends A
{
B()
{
System.out.println("Inside B's Constructor");
}
}
class singledemo
{
public static void main(String args[])
{
B b1=new B();
}
}
OUT-PUT:
Inside A's Constructor
Inside B's Constructor
b) Multi level Inheritance
AIM: To write a JAVA program to implement multi level Inheritance
SOURCE-CODE:
class A
{
A()
{
System.out.println("Inside A's Constructor");
}
}
class B extends A
{
B()
{
System.out.println("Inside B's Constructor");
}
}
class C extends B
{ C()
{
System.out.println("Inside C's Constructor");
}
}
class multidemo
{
public static void main(String args[])
{
C c1=new C();
}
}
OUT-PUT:
Inside A's Constructor
Inside B's Constructor
Inside C's Constructor
c) Abstract Class
AIM: To write a java program for abstract class to find areas of different
shapes
SOURCE-CODE:
abstract class shape
{
abstract double area();
}
class rectangle extends shape
{
double l=12.5,b=2.5; double area()
{
return l*b;
}
}
class triangle extends shape
{
double b=4.2,h=6.5; double area()
{
return 0.5*b*h;
}
}
class square extends shape
{
double s=6.5; double area()
{
return 4*s;
}
}
class shapedemo
Exercise6
a) Exception handling mechanism
AIM: To write a JAVA program that describes exception handling
mechanism
SOURCE-CODE:
Usage of Exception Handling:
class trydemo
{
public static void main(String args[])
{
try
{
int a=10,b=0; int c=a/b;
System.out.println(c);
}
catch(ArithmeticException e)
{
System.out.println(e);
}
System.out.println("After the catch statement");
}
}
OUT-PUT:
java.lang.ArithmeticException: / by zero After the catch statement
(ii)NullPointer Exception
class nullpointerdemo
{
public static void main(String args[])
{
try
{
String a = null; System.out.println(a.charAt(0));
}
catch(NullPointerException e)
{
System.out.println(e);
}
}
}
OUT-PUT:
java.lang.NullPointerException
(iii)StringIndexOutOfBound Exception
class stringbounddemo
{
public static void main(String args[])
{
try
{
String a = "This is like chipping "; char c = a.charAt(24); System.out.println(c);
}
catch(StringIndexOutOfBoundsException e)
{
System.out.println(e);
}
}
}
OUT-PUT:
java.lang.StringIndexOutOfBoundsException:
String index out of range: 24
(iv)FileNotFound Exception
import java.io.*;
class filenotfounddemo
{
public static void main(String args[])
{
try
{
File file = new File("E://file.txt"); FileReader fr = new FileReader(file);
}
catch (FileNotFoundException e)
{
System.out.println(e);
}
}
}
OUT-PUT:
java.io.FileNotFoundException: E:\file.txt (The system cannot find the file
specified)
(v)NumberFormat Exception
class numberformatdemo
{
public static void main(String args[])
{
try
{
int num = Integer.parseInt ("akki") ; System.out.println(num);
}
catch(NumberFormatException e)
{
System.out.println(e);
}
}
}
OUT-PUT:
java.lang.NumberFormatException: For input string: "akki"
(vi)ArrayIndexOutOfBounds Exception
class arraybounddemo
{
public static void main(String args[])
{
try
{
int a[] = new int[5]; a[6] = 9;
}
catch(ArrayIndexOutOfBoundsException e)
{
System.out.println (e);
}
}
}
OUT-PUT:
java.lang.ArrayIndexOutOfBoundsException: 6
d)creation of User Defined Exception
AIM: To write a JAVA program for creation of User Defined Exception
SOURCE-CODE:
class A extends Exception
{
A(String s1)
{
super(s1);
}
}
class owndemo
{
public static void main(String args[])
{
try
{
throw new A("demo ");
}
catch(Exception e)
{
System.out.println(e);
}
}
}
OUT-PUT:
A: demo
Exercise – 7
a) Extending Thread class
AIM: To write a JAVA program that creates threads by extending Thread
class .First thread display “Good Morning “every 1 sec, the second thread displays
“Hello “every 2
seconds and the third display “Welcome” every 3 seconds ,(Repeat the
same by implementing Runnable)
SOURCE-CODEs:
(i) Creating multiple threads using Thread class
class A extends Thread
{
public void run()
{
try
{
for(int i=1;i<=10;i++)
{
sleep(1000); System.out.println("good morning");
}
}
catch(Exception e)
{
System.out.println(e);
}
}
}
class B extends Thread
{
public void run()
{
try
{
for(int j=1;j<=10;j++)
{
sleep(2000); System.out.println("hello");
}
}
catch(Exception e)
{
System.out.println(e);
}
}
}
class C extends Thread
{
public void run()
{
try
for(int k=1;k<=10;k++)
{
sleep(3000); System.out.println("welcome");
}
}
catch(Exception e)
{
System.out.println(e);
}
}
}
class threaddemo
{
public static void main(String args[])
{
A a1=new A();
B b1=new B();
C c1=new C(); a1.start();
b1.start();
c1.start();
}
}
OUT-PUT:
good morning
hello
good morning
good morning
welcome
hello
good morning
good morning
hello
good morning
welcome
good morning
hello
good morning
good morning
welcome
hello
good morning
hello
welcome
hello
welcome
hello
welcome
hello
welcome
welcome
welcome
welcome
(ii) Creating multiple threads using Runnable interface
class A implements Runnable
{
public void run()
{
try
{
for(int i=1;i<=10;i++)
{
Thread.sleep(1000); System.out.println("good morning");
}
}
catch(Exception e)
{
System.out.println(e);
}
}
}
class B implements Runnable
{
public void run()
{
try
{
for(int j=1;j<=10;j++)
{
Thread.sleep(2000); System.out.println("hello");
}
}
catch(Exception e)
{
System.out.println(e);
}
}
}
class C implements Runnable
{
public void run()
try
{
for(int k=1;k<=10;k++)
{
Thread.sleep(3000); System.out.println("welcome");
}
}
catch(Exception e)
{
System.out.println(e);
}
}
}
class runnabledemo
{
public static void main(String args[])
{
A a1=new A();
B b1=new B();
C c1=new C();
Thread t1=new Thread(a1); Thread t2=new Thread(b1); Thread t3=new
Thread(c1); t1.start();
t2.start();
t3.start();
}
}
OUT-PUT:
good morning
good morning
hello
good morning
Welcome
good morning
hello
good morning
good morning
welcome
hello
good morning
good morning
hello
good morning
welcome
good morning
welcome
hello
hello
welcome
hello
welcome
hello
hello
welcome
welcome
welcome
welcome
(b)Implementing isAlive() and join()
AIM: To write a program illustrating isAlive and join ()
SOURCE-CODE:
class A extends Thread
{
public void run()
{
try
{
for(int i=1;i<=10;i++)
{
sleep(1000); System.out.println("good morning");
}
}
catch(Exception e)
{
System.out.println(e);
}
}
}
class B extends Thread
{
public void run()
{
try
{
for(int j=1;j<=10;j++)
{
sleep(2000); System.out.println("hello");
}
}
catch(Exception e)
{
System.out.println(e);
}
}
}
class C extends Thread
{
public void run()
{
try
{
for(int k=1;k<=10;k++)
{
sleep(3000); System.out.println("welcome");
}
}
catch(Exception e)
{
System.out.println(e);
}
}
}
class isalivedemo
{
public static void main(String args[])
{
A a1=new A();
B b1=new B();
C c1=new C(); a1.start();
b1.start();
c1.start(); System.out.println(a1.isAlive()); System.out.println(b1.isAlive());
System.out.println(c1.isAlive()); try
{
a1.join();
b1.join();
c1.join();
}
catch(InterruptedException e)
{
System.out.println(e);
}
System.out.println(a1.isAlive());
System.out.println(b1.isAlive());
System.out.println(c1.isAlive());
}
}
OUT-PUT:
true good morning
true hello
true welcome
good morning hello
good morning hello
hello welcome
good morning hello
welcome welcome
good morning hello
hello hello
good morning welcome
good morning welcome
welcome welcome
hello welcome
good morning false
good morning false
hello false
good morning welcome
Output:
10
20
}
}
c) Build a Tip Calculator app using several JavaFX
components
Aim: Build a Tip Calculator app using several JavaFX
components and learn how to respond to user interactions
with the GUI
Program:
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.effect.ImageInput;
import javafx.scene.image.Image;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;
public class ImageExample extends Application {
@Override
public void start(Stage primaryStage) throws Exception {
// TODO Auto-generated method stub
Image img = new
Image("https://picsum.photos/536/354");
ImageInput imginput = new ImageInput();
Rectangle rect = new Rectangle();
imginput.setSource(img);
imginput.setX(20);
imginput.setY(100);
Group root = new Group();
rect.setEffect(imginput);
root.getChildren().add(rect);
Scene scene = new Scene(root,530,500,Color.BLACK);
primaryStage.setScene(scene);
primaryStage.setTitle("ImageInput Example");
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
import java.sql.*;
Exercise-9
a) connects to a database using JDBC
Program:
public class odbcdriver
{
public static void main(String s[])
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection
c=DriverManager.getConnection("jdbc:odbc:connect_ora","s
cott","tiger");
DatabaseMetaData dm=c.getMetaData();
System.out.println("Data Base
Name :"+dm.getDatabaseProductName());
System.out.println("Versio
:"+dm.getDatabaseProductVersion());
System.out.println("User Name :"+dm.getUserName())
;
System.out.println("Driver name :"+dm.getDriverName(
));
System.out.println("Driver Version :"+dm.getDriverVers
ion());
}
catch(Exception e)
{
System.out.println(e);
}
}
}
TextField eno;
TextField ename;
Button next;
Button addnew;
Panel p;
static ResultSet rs;
static Connection c;
static Statement st;
public odbcframe()
{
super("The query Application:");
setLayout(new GridLayout(5,1));
eno=new TextField(20);
ename =new TextField(50);
next =new Button("Next");
addnew =new Button("AddNewRec");
p=new Panel();
add(new Label("Emp no:"));
add(eno);
add(new Label("Emp name:"));
add(ename);
add(p);
p.add(next);
p.add(addnew);
next.addActionListener(this);
addnew.addActionListener(this);
pack();
setVisible(true);
}
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
c=DriverManager.getConnection("jdbc:odbc:connect_ora","s
cott","tiger");
st=c.createStatement();
rs=st.executeQuery("select empno,ename from empdet");
rs.next();
}
catch(Exception e)
{
System.out.println("Error Inside.."+ e );
}
obj.showRecord(rs);
}
public void actionPerformed(ActionEvent event)
{
if(event.getSource()==next)
{
try
{
rs.next();
}
catch(Exception e)
{
}
showRecord(rs);
}
if(event.getSource()==addnew)
{
try
{
addRecord();
}
catch(Exception e)
{
}