0% found this document useful (0 votes)
31 views48 pages

Oops Lab Manual 2vazur

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views48 pages

Oops Lab Manual 2vazur

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 48

ST.

JOSEPH COLLEGE OF ENGINEERING


SRIPERUMBUDU
R

CS3381- OBJECT ORIENTED


PROGRAMMING LABORATOR
Y

NAME OF THE STUDENT :


DEPARTMENT :Computer Science and Engineering
REGISTER NO :
YEAR / SEM : II/ III
INDEX

EX. PG.
DATE EXPERIMENT SIGNATURE
NO NO

1. 1

2. 6

3. 11

4. 20

5. 23

6. 25

7. 27

8. 30

9. 33

10. 35

11. 39
Ex.No:1
DATE:

AIM:

ALGORITHM:

PROGRAM:
package
exlcode;
public class
SequentialSearchExample
{
public static void main(String[]
{ args)
int[] exampleVariableOne = {2, 9, 6, 7, 4, 5, 3, 0,
1};
int target =
4;
sequentialSearch(exampleVariableOne,
} target);

public static void sequentialSearch(int[] parameterOne, int


parameterTwo)
{int index = -
1;
// searches each index of the array until it reaches the last
index
for (int i = 0; i < parameterOne.length; i++)
{if (parameterOne[i] == parameterTwo)
{ // if the target is found, int index is set as the value of i
and
// the for loop is
terminated
index =
i;
break
};
}
if (index == -1)
{System.out.println("Your target integer does not exist in the
}array");
else
{System.out.println("Your target integer is in index " + index + " of the
}array");
}
}

1
z
Output
:Enter value to
search:
68
Searched item 68 found at index
6
Enter value to
search:
8

Searched item 8 not found in the


array
Enter value to
search:
10
Searched item 10 found at index
2

2
BINARY
SEARCH
import
java.util.*;
class
Main{
public static void main(String
args[]){
int numArray[] =
{5,10,15,20,25,30,35};
System.out.println("The array " +
input
//key to be : Arrays.toString(numArray));
searched
int key =
20;
System.out.println("\nKey to be searched=" +
key);
//set first to first
index
int first =
0;
//set last to last elements in
array
int last=numArray.length-
1;
//calculate mid of the
array
int mid = (first +
last)/2;
//while first and last do not
overlap
while( first <=
last//if
){the mid < key, then key to be searched is in the first half of
array
if ( numArray[mid] <
keyfirst
){ = mid +
1; if ( numArray[mid] ==
}else
key//if
){key = element at mid, then print the
location
System.out.println("Element is found at index: " +
mid);
break
;
}else{
//the key is to be searched in the second half of the
array
last = mid -
} 1;
mid = (first +
} last)/2;
//if first and last overlap, then key is not present in the
array
if ( first >
last ){
System.out.println("Element is not
} found!");
}
}

Output
:Enter value to
search:
68
Searched item 68 found at index
6
Enter value to
search:
8
Searched item 8 not found in the
array
Enter value to
search:
10
Searched item 10 found at index
3
2
SELECTION
SORT
public class SelectionSortExample
{ public static void selectionSort(int[]
arr){
for (int i = 0; i < arr.length - 1;
i++)
{
int index =
i;
for (int j = i + 1; j < arr.length;
j++){
if (arr[j] <
arr[index]){
index = j;//searching for lowest
} index
}
int smallerNumber =
arr[index];
arr[index] =
arr[i];
arr[i] =
} smallerNumber;
}

public static void main(String


a[]){
int[] arr1 =
{9,14,3,2,43,11,58,22};
System.out.println("Before Selection
Sort");
for(in
t i:arr1){ System.out.pr
int(i+"
} ");
System.out.println()
;
selectionSort(arr1);//sorting array using selection
sort
System.out.println("After Selection
Sort");
for(in
t i:arr1){ System.out.pr
int(i+"
} ");
}
}

Output
:Before Selection
Sort
9 14 3 2 43 11 58
22
After Selection
Sort
2 3 9 11 14 22 43
58

4
INSERTION
SORT
public class InsertionSortExample
{ public static void insertionSort(int
array[])
{int n =
array.length;
for (int j = 1; j < n;
j++)
{int key =
array[j];
int i = j-
1;
while ( (i > -1) && ( array [i] >
key{array
) ) [i+1] = array
[i];
i--;
}
array[i+1] =
} key;
}

public static void


main(String
a[]){ int[] arr1
=
{9,14,3,2,43,11,58,22}
;System.out.println("Before Insertion
Sort");
for(int
i:arr1){
System.out.print(i+"
} ");
System.out.println()
;
insertionSort(arr1);//sorting array using insertion
sort
System.out.println("After Insertion
Sort");
for(int
i:arr1){ System.out.pr
int(i+"
} ");
}
}

Output
:Before Insertion
Sort
9 14 3 2 43 11 58
22
After Insertion
Sort
2 3 9 11 14 22 43
58

RESULT:

.
5
EX.NO:2
DATE:

AIM:

ALGORITHM:

PROGRAM:
STACK IMPLEMENTATION

class
Stack
{ private
int
arr[];private
int
top;
private int
capacity;
// Creating a
stack
Stack(int size)
{ arr = new
int[size];
capacity =
size;
top = -
} 1;

// Add elements into


stack
public void push(int
x)
{ if (isFull())
{ System.out.println("OverFlow\nProgram
Terminated\n");
System.exit(1)
};

System.out.println("Inserting " +
x);
arr[++top] =
} x;

// Remove element from


stack
public int pop()
{ if
(isEmpty())
{ System.out.println("STACK
EMPTY");
System.exit(1)
};
return arr[top-
} -];
6
// Utility function to return the size of the
stack
public int size()
{ return top +
1;
}

// Check if the stack is


empty
public Boolean
isEmpty()
{return top == -
1;
}

// Check if the stack is


full
public Boolean
isFull()
{ return top == capacity -
1;
}

public void printStack()


{ for (int i = 0; i <= top;
i++)
{System.out.println(arr[i])
};
}

public static void main(String[]


args)
{Stack stack = new
Stack(5);
stack.push(1)
;stack.push(2)
;stack.push(3)
;stack.push(4)
;
stack.pop()
;System.out.println("\nAfter popping
out");
stack.printStack()
;
}
}

OUTPUT
:
Inserting
1
Inserting
2
Inserting
3
Stack: 1, 2,
3,
After popping
out
1, 2,

7
QUEUE IMPLEMENTATION

public class
Queue
{int SIZE =
5;
int items[] = new
int[SIZE];
int front,
rear;
Queue(
) { front = -
1;
rear = -
} 1;

boolean isFull()
{ if (front == 0 && rear == SIZE -
1){return
}true;
return
} false;

boolean
isEmpty()
{if (front == -
1)
return
true;
else
return
} false;

void enQueue(int
element)
{if (isFull())
{System.out.println("Queue is
}full");
else
{if (front == -
1)
front =
0;
rear++
;items[rear] =
element;
System.out.println("Inserted " +
}element);
}

int
deQueue()
{int
element;
if
(isEmpty())
{ System.out.println("Queue
is
empty");return (-
}1);
else
{element =
items[front];
if (front >=
rear)
{ front = -
1;rear = -
} 1;
/* Q has only one element, so we reset the queue after deleting it.
else {
*/
front++
};
System.out.println("Deleted -> " +
element);
return
}(element);
} 8
void display()
{
/* Function to display elements of Queue
*/
int i;
if
(isEmpty())
{ System.out.println("Empty
Queue");
} else
{System.out.println("\nFront index-> " +
front);
System.out.println("Items ->
");
for (i = front; i <= rear;
i++)
System.out.print(items[i] + "
");
System.out.println("\nRear index-> " +
}rear);
}

public static void main(String[]


args)
{Queue q = new
Queue();
// deQueue is not possible on empty
queue
q.deQueue()
;
// enQueue 5
elements
q.enQueue(1)
;q.enQueue(2)
;q.enQueue(3)
;q.enQueue(4)
;q.enQueue(5)
;
// 6th element can't be added to because the queue is
full
q.enQueue(6)
;
q.display()
;
// deQueue removes element entered first i.e.
1
q.deQueue()
;
// Now we have just 4
elements
q.display()
;
}
}

9
OUTPUT
:
Queue is empty
Insert 1
Insert 2
Insert 3
Insert 4
Insert 5
Queue is full
Front index-> 0
Items ->
12345
Rear index-> 4
1 Deleted
Front index-> 1
Items ->
2345
Rear index-> 4

RESULT:

10
EX.NO:3
DATE:

AIM:

ALGORITHM:

PROGRAM:

//For Packages, Folder Name should be employee


//File Name should be Employee.java

package employee;
public class Employee
{
private String name;
private String id;
private String address;
private String mailId;
private String mobileNo;
public Employee(String name, String id, String address, String mailId, String mobileNo)
{
this.name= name;
this.id= id;
this.address= address;
this.mailId= mailId;
this.mobileNo= mobileNo;
}
11
public void display()
{
System.out.println("Emp_Name : "+ name + "\t" + "Emp_id : "+ id);
System.out.println("Address : " + address);
System.out.println("Mail_id : "+ mailId + "\t" + "Mobile_no : " + mobileNo);
}
public void paySlip()
{
}
}

//For Packages, Folder Name should be employee


//File Name should be Programmer.java

package employee;
public class Programmer extends Employee
{
private float bPay;
private String des;
public Programmer(String name, String id, String address, String mailId, String mobileNo,
float bPay, String des)
{
super(name, id, address, mailId, mobileNo);
this.bPay= bPay;
this.des= des;
}
public void paySlip()
{
float da=bPay*97/100;
float hra=bPay*10/100;
double grossSalary=bPay + da + hra;
float pf=bPay*12/100;
double scf=bPay*0.1/100;
double netSalary=grossSalary - pf - scf;
System.out.println("------------ Employees Pay Slips------------- ");
super.display();
System.out.println("Designation: "+des);
System.out.println("Basic_Pay: "+bPay);
System.out.println("Gross Salary : "+ grossSalary + "\t" + "Net Salary : " + netSalary);
System.out.println("------------ End of the Statements------------ ");
}
}

12
//For Packages, Folder Name should be employee
// File Name should be AssistantProfessor.java

package employee;
public class AssistantProfessor extends Employee
{
private float bPay;
private String des;
public AssistantProfessor(String name, String id, String address, String mailId, String
mobileNo, float bPay, String des)
{
super(name, id, address, mailId, mobileNo);
this.bPay= bPay;
this.des= des;
}
public void paySlip()
{
float da=bPay*97/100;
float hra=bPay*10/100;
double grossSalary=bPay + da + hra;
float pf=bPay*12/100;
double scf=bPay*0.1/100;
double netSalary=grossSalary - pf - scf;
System.out.println("------------ Employees Pay Slips------------- ");
super.display();
System.out.println("Designation: "+des);
System.out.println("Basic_Pay: "+bPay);
System.out.println("Gross Salary : "+ grossSalary + "\t" + "Net Salary : " + netSalary);
System.out.println("------------ End of the Statements------------ ");
}
}

//For Packages, Folder Name should be employee


//File Name should be AssociateProfessor.java

package employee;
public class AssociateProfessor extends Employee
{
private float bPay;
private String des;
public AssociateProfessor(String name, String id, String address, String mailId, String
mobileNo, float bPay, String des)
{
super(name, id, address, mailId, mobileNo);

13
this.bPay= bPay;
this.des= des;
}
public void paySlip()
{
float da=bPay*97/100;
float hra=bPay*10/100;
double grossSalary=bPay + da + hra;
float pf=bPay*12/100;
double scf=bPay*0.1/100;
double netSalary=grossSalary - pf - scf;
System.out.println("------------ Employees Pay Slips------------- ");
super.display();
System.out.println("Designation: "+des);
System.out.println("Basic_Pay: "+bPay);
System.out.println("Gross Salary : "+ grossSalary + "\t" + "Net Salary : " + netSalary);
System.out.println("------------ End of the Statements------------ ");
}
}

//For Packages, Folder Name should be employee


//File Name should be Professor.java

package employee;
public class Professor extends Employee
{
private float bPay;
private String des;
public Professor(String name, String id, String address, String mailId, String mobileNo, float
bPay, String des)
{
super(name, id, address, mailId, mobileNo);
this.bPay= bPay;
this.des= des;
}
public void paySlip()
{
float da=bPay*97/100;
float hra=bPay*10/100;
double grossSalary=bPay + da + hra;
float pf=bPay*12/100;
double scf=bPay*0.1/100;
double netSalary=grossSalary - pf - scf;
System.out.println("------------ Employees Pay Slips------------- ");
14
super.display();
System.out.println("Designation: "+des);
System.out.println("Basic_Pay: "+bPay);
System.out.println("Gross Salary : "+ grossSalary + "\t" + "Net Salary : " + netSalary);
System.out.println("------------ End of the Statements------------ ");
}
}

//File Name should be Emp.java separate this file from above folder

import employee.*;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;
public class Emp
{
Employee e;
ArrayList<Employee> obj= new ArrayList<>();
Scanner get= new Scanner(System.in);
public void addEmployee()
{
System.out.println("Enter the Emp_Name:");
String name = get.next();
System.out.println("Enter the Emp_id:");
String id = get.next();
System.out.println("Enter the Address:");
String address = get.next();
System.out.println("Enter the Mail_id:");
String mailId = get.next();
System.out.println("Enter the Mobile_no:");
String mobileNo = get.next();
System.out.println("Enter the Designation:");
String des = get.next();
System.out.println("Enter the Basic_Pay:");
float bPay = get.nextFloat();
if(des.equalsIgnoreCase("Programmer"))
{
e= new Programmer(name, id, address, mailId, mobileNo, bPay, des);
obj.add(e);
}
else if(des.equalsIgnoreCase("AssistantProfessor"))
{
e= new AssistantProfessor(name, id, address, mailId, mobileNo, bPay, des);
obj.add(e);
15
}
else if(des.equalsIgnoreCase("AssociateProfessor"))
{
e= new AssociateProfessor(name, id, address, mailId, mobileNo, bPay, des);
obj.add(e);
}
else if(des.equalsIgnoreCase("Professor"))
{
e= new Professor(name, id, address, mailId, mobileNo, bPay, des);
obj.add(e);
}
}
public void displayEmployee()
{
for(Employee e:obj)
{
e.paySlip();
}
}
public static void main(String args[]) throws IOException
{
Emp x= new Emp();
String check;
do
{
x.addEmployee();
System.out.println("Do you wnat continue press 'y'");
check=x.get.next();
}
while(check.equalsIgnoreCase("y"));
x.displayEmployee();
}
}

16
OUTPUT
:
D:\>javac Emp.java

D:\>java Emp
Enter the Emp_Name:
Suresh
Enter the Emp_id:
E708
Enter the Address:
cuddalore
Enter the Mail_id:
suresh708@tgarments.org
Enter the Mobile_no:
7894561230
Enter the Designation:
Programmer
Enter the Basic_Pay:
7500
Do you wnat continue press 'y'
y
Enter the Emp_Name:
Rakesh
Enter the Emp_id:
E705
Enter the Address:
pondy
Enter the Mail_id:
rakesh@gmail.com
Enter the Mobile_no:
4567891230
Enter the Designation:
Professor
Enter the Basic_Pay:
15000
Do you wnat continue press 'y'
y
Enter the Emp_Name:
kumar
Enter the Emp_id:
E405
Enter the Address:
madurai
Enter the Mail_id:

17
kumarat@ymail.com
Enter the Mobile_no:
1237894560
Enter the Designation:
AssistantProfessor
Enter the Basic_Pay:
18000
Do you wnat continue press 'y'
y
Enter the Emp_Name:
Naresh
Enter the Emp_id:
E102
Enter the Address:
villupuram
Enter the Mail_id:
nar12@rediffmail.com
Enter the Mobile_no:
9873214560
Enter the Designation:
AssociateProfessor
Enter the Basic_Pay:
20000
Do you wnat continue press 'y'
n

18
------------ Employees Pay Slips ------------
Emp_Name : Suresh Emp_id : E708
Address : cuddalore
Mail_id : suresh708@tgarments.org Mobile_no : 7894561230
Designation: Programmer
Basic_Pay: 7500.0
Gross Salary : 15525.0 Net Salary : 14617.5
------------ End of the Statements -----------
------------ Employees Pay Slips ------------
Emp_Name : Rakesh Emp_id : E705
Address : pondy
Mail_id : rakesh@gmail.com Mobile_no : 4567891230
Designation: Professor
Basic_Pay: 15000.0
Gross Salary : 31050.0 Net Salary : 29235.0
------------ End of the Statements -----------
------------ Employees Pay Slips ------------
Emp_Name : kumar Emp_id : E405
Address : madurai
Mail_id : kumarat@ymail.com Mobile_no : 1237894560
Designation: AssistantProfessor
Basic_Pay: 18000.0
Gross Salary : 37260.0 Net Salary : 35082.0
------------ End of the Statements -----------
------------ Employees Pay Slips ------------
Emp_Name : Naresh Emp_id : E102
Address : villupuram
Mail_id : nar12@rediffmail.com Mobile_no : 9873214560
Designation: AssociateProfessor
Basic_Pay: 20000.0
Gross Salary : 41400.0 Net Salary : 38980.0
------------ End of the Statements -----------

D:\>

RESULT:

19
EX.NO:4
DATE:

AIM:

ALGORITHM:

PROGRAM:

//File Name should be Area.java

import java.io.*;
import java.util.*;

abstract class Shape


{
double a = 0.0, b = 0.0;
abstract public void printArea();
}

class Rectangle extends Shape


{
double area = 0.0;
public void printArea()
{
System.out.println("Area of Rectangle");
System.out.println("---------- ");
Scanner in = new Scanner(System.in);
System.out.println("Enter the Width:");
this.a = in.nextDouble();
System.out.println("Enter the Length:");

20
this.b = in.nextDouble();
this.area = a*b; /* (width*length) */
System.out.println("The area of rectangle is:"+this.area);
}
}

class Triangle extends Shape


{
double area = 0.0;
public void printArea()
{
System.out.println("-----Area of Triangle----- ");
System.out.println("---------- ");
Scanner in = new Scanner(System.in);
System.out.println("Enter the Base:");
this.a = in.nextDouble();
System.out.println("Enter the Height:");
this.b = in.nextDouble();
this.area = 0.5*a*b; /* 1/2 (base*height) */
System.out.println("The area of triangle is:"+this.area);
}
}

class Circle extends Shape


{
double area = 0.0;
public void printArea()
{
System.out.println("-----Area of Circle----- ");
System.out.println("---------- ");
Scanner in = new Scanner(System.in);
System.out.println("Enter the Radius:");
this.a = in.nextDouble();
this.area = 3.14*a*a; /* 3.14*r*r */
System.out.println("The area of circle is:"+this.area);
}
}

public class Area


{
public static void main(String[] args)
{
System.out.println("-----Finding the Area of Shapes------ ");
Shape s;
s=new Rectangle();
21
s.printArea();
s=new Triangle();
s.printArea();
s=new Circle();
s.printArea();
}
}

NOTE:

To Compile:
javac Area.java
To Run:
java Area

OUTPUT:

RESULT:

22
EX.NO:05
DATE:

AIM:

ALGORITHM:

is invoked.

PROGRAM:

import java.io.*;
import java.util.*;
interface
area {
double pi =
3.14;
double calc(double x,double
} y);

class rect implements


area
{
public double calc(double x,double
y){
return(x*y)
};
}

class cir implements


area
{
public double calc(double x,double
y){
return(pi*x*x)
};
}
23
class tri implements
Area
{
public double Compute(double b, double
h)
{
return
(b*h/2);

}
}

class
test7 {
public static void main(String
arg[])
{
rect r = new
rect();
cir c = new
cir();
tri t =new
tri():
area
a;
a = r;
System.out.println("\nArea of Rectangle is : "
+a.calc(10,20));
a = c;
System.out.println("\nArea of Circle is : "
+a.calc(15,15));
a = t;
System.out.println("\nArea of triangle is : "
+a.calc(10,15));
}

OUTPUT:

Rectangle Area : 40.0


Square Area : 49.0
Circle Area : 38.5

RESULT:

24
EX.NO:06
DATE:

AIM:

ALGORITHM:

PROGRAM:

//File Name should be UserException.java

import java.io.*;
import java.util.*;

class MyException extends Exception


{
private int d;
MyException(int a)
{
d = a;
}

public String toString()


{
return "MyException [" + d + "]";
}
}

class UserException
{
static void compute(int a) throws MyException
{
System.out.println ("Called Compute(" + a + ")");
if(a>10)
throw new MyException(a);
25
System.out.println ("Normal Exit");
}

public static void main(String args[])


{
try
{
compute(1);
compute(20);
}
catch(MyException e)
{
System.out.println("Caught " + e);
}
}
}

OUTPUT:

RESULT:

26
EX.NO:07
DATE:

AIM:

ALGORITHM:

PROGRAM:

//File Name should be Multithread.java

import java.util.*;
class Even implements Runnable
{
public int x;
public Even(int x)
{
this.x = x;
}
public void run()
{
System.out.println("New Thread "+ x +" is EVEN and Square of " + x + " is: " + x * x);
}
}
class Odd implements Runnable
{
public int x;
public Odd(int x) 27
{
this.x = x;
}
public void run()
{
System.out.println("New Thread "+ x +" is ODD and Cube of " + x + " is: " + x * x * x);
}
}

class Generate extends Thread


{
public void run()
{
int num = 0;
Random r = new Random();
try
{
for (int i = 0; i < 5; i++)
{
num = r.nextInt(100);
System.out.println("Main Thread Generates Random Integer: " + 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());
}
}
}

28
public class Multithread
{
public static void main(String[] args)
{
Generate g = new Generate();
g.start();
}
}

NOTE:

To Compile:
javac Multithread.java
To Run:
java Multithread

OUTPUT:

RESULT:

29
EX.NO:08
DATE:

AIM:

ALGORITHM:

PROGRAM:

//File Name should be FileInfo.java

import java.io.*;
import java. util.*;
public class FileInfo
{
public static void main(String[] args) throws IOException
{
Scanner in=new Scanner(System.in);
System.out.print("\nEnter the FileName: ");
String fName = in.next(); 30
File f = new File(fName);
String result = f.exists() ? " exists." : " does not exist.";
System.out.println("\nThe given file " +fName + result);

System.out.println("\nFile Location: "+f.getAbsolutePath());

if(f.exists())
{
result = f.canRead() ? "readable." : "not readable.";
System.out.println("\nThe file is " + result);

result = f.canWrite() ? "writable." : "not writable.";


System.out.println("\nThe file is " + result);

System.out.println("\nFile length is " + f.length() + " in bytes.");

if (fName.endsWith(".jpg") || fName.endsWith(".gif") || fName.endsWith(".png"))


{
System.out.println("\nThe given file is an image file.");
}
else if (fName.endsWith(".pdf"))
{
System.out.println("\nThe given file is an portable document format.");
}
else if (fName.endsWith(".txt"))
{
System.out.println("\nThe given file is a text file.");
}
else
{
System.out.println("The file type is unknown.");
}
}
}}
OUTPUT:

31
RESULT:

32
EX.NO:09
DATE:

AIM:

ALGORITHM:

PROGRAM:

//File Name should be MyGeneric.java

import java.util.*;
class MyGeneric {
public static <T extends Comparable<T>> T max(T... elements)
{
T max = elements[0];
for (T element : elements) {
if (element.compareTo(max) > 0)
{
max = element;
}}
return max;
}

public static void main(String[] args)


{
System.out.println("Integer Max: " + max(Integer.valueOf(32), Integer.valueOf(89)));
System.out.println("String Max: " + max("GaneshBabu", "Ganesh"));
System.out.println("Double Max: " + max(Double.valueOf(5.6),
33 Double.valueOf(2.9)));
System.out.println("Boolean Max: " + max(Boolean.TRUE, Boolean.FALSE));
System.out.println("Byte Max: " + max(Byte.MIN_VALUE, Byte.MAX_VALUE));
}
}

NOTE:

To Compile:
javac MyGeneric.java
To Run:
java MyGeneric

OUTPUT:

RESULT:

34
EX.NO:10
DATE:

AIM:

PROGRAM:

importjavafx.application.Application;
importjavafx.collections.FXCollections;
importjavafx.collections.ObservableList;

import javafx.geometry.Insets;
import javafx.geometry.Pos;

importjavafx.scene.Scene;
importjavafx.scene.control.Button;
importjavafx.scene.control.CheckBox;
importjavafx.scene.control.ChoiceBox;
importjavafx.scene.control.DatePicker;
importjavafx.scene.control.ListView;
importjavafx.scene.control.RadioButton;
importjavafx.scene.layout.GridPane;
importjavafx.scene.text.Text;
importjavafx.scene.control.TextField;
importjavafx.scene.control.ToggleGroup;
importjavafx.scene.control.ToggleButton;
importjavafx.stage.Stage;

public class Registration extends


Application
{@Overrid
e
public void start(Stage stage)
{ //Label for
name
Text nameLabel = newText("Name");

//Text field for


name
TextField nameText = newTextField();

//Label for date of


birth
Text dobLabel = new Text("Date ofbirth");

//date picker to choose


date
DatePicker datePicker = newDatePicker();

//Label for
gender
Text genderLabel = newText("gender");

//Toggle group of radio 35


buttons
ToggleGroup groupGender = newToggleGroup();

RadioButton maleRadio = ne RadioButton("male");


maleRadio.setToggleGroup(groupGender)
;RadioButton femaleRadio = new
RadioButton("female");
femaleRadio.setToggleGroup(groupGender)
;
//Label for
reservation
Text reservationLabel = newText("Reservation");

//Toggle button for


reservation
ToggleButton Reservation = newToggleButton();
ToggleButton yes = ne ToggleButton("Yes");
ToggleButton no = newToggleButton("No");
ToggleGroup groupReservation = newToggleGroup();
yes.setToggleGroup(groupReservation)
;no.setToggleGroup(groupReservation)
;
//Label for technologies
known
TexttechnologiesLabel = new Text("Technologies
Known");
//check box for
education
CheckBox javaCheckBox = newCheckBox("Java");
javaCheckBox.setIndeterminate(false)
;
//check box for
education
CheckBox dotnetCheckBox = newCheckBox("DotNet"); javaCheckBox.setIndeterminate(false)
;
//Label for
education
Text educationLabel = new Text("Educationalqualification");

//list View for educational


qualification
ObservableList<String> names
= FXCollections.observableArrayList "Engineering" "MCA" "MBA" "Graduation"
(
"MTECH" "Mphil" "Phd") , , , ,
, ,
ListView<String> ;
educationListView = new
ListView<String>(names);
//Label for
location
Text locationLabel = newText("location");

//Choice box for


location
ChoiceBox locationchoiceBox = newChoiceBox();
locationchoiceBox.getItems().addAl
l ("Hyderabad" "Chennai" "Delhi" "Mumbai" "Vishakhapatnam")
, , , , ;
//Label for
register
Button buttonRegister = newButton("Register");

//Creating a Grid
Pane
GridPane gridPane = newGridPane();

36
\

//Setting size for the


pane
gridPane.setMinSize(500 500);
,
//Setting the
padding
gridPane.setPadding(newInsets(10, 10, 10, 10));

//Setting the vertical and horizontal gaps between the


columns
gridPane.setVgap(5)
;gridPane.setHgap(5)
;
//Setting the Grid
alignment
gridPane.setAlignment(Pos.CENTER)
;
//Arranging all the nodes in the
grid
gridPane.add(nameLabel, 0,0);
gridPane.add(nameText 1, 0);
,
gridPane.add(dobLabel 0, 1);
,
gridPane.add(datePicker, 1, 1);

gridPane.add(genderLabel 0, 2);
,
gridPane.add(maleRadio, 1 2);
gridPane.add(femaleRadio 2, 2);
,
gridPane.add(reservationLabel 0, 3);
,
gridPane.add(yes, 1,3);
gridPane.add(no, 2,3);

gridPane.add(technologiesLabel 0, 4);
,
gridPane.add(javaCheckBox 1, 4);
,
gridPane.add(dotnetCheckBox 2, 4);
,
gridPane.add(educationLabel 0, 5);
,
gridPane.add(educationListView, 1, 5);

gridPane.add(locationLabel 0, 6);
,
gridPane.add(locationchoiceBox 1, 6);
,
gridPane.add(buttonRegister, 2, 8);

//Styling
nodes
buttonRegister.setStyle(
"-fx-background-color: darkslateblue -fx-textfill: white;")
; ;
nameLabel.setStyle("-fx-font: normal bold 15px 'serif'");
dobLabel.setStyle("-fx-font: normal bold 15px 'serif ");
genderLabel.setStyle("-fx-font: normal bold 15px 'serif ");
reservationLabel.setStyle("-fx-font: normal bold 15px 'serif'");
technologiesLabel.setStyle("-fxfont: norma bold 15px'serif' ");
l
educationLabel.setStyle("-fx-font: normal bold 15px 'serif'");
locationLabel.setStyle("-fx-font: normal bold 15px 'serif'");

37
//Setting the back ground
color
gridPane.setStyle("-fx-backgroundcolor: BEIGE;")
;
//Creating a scene
object
Scene scene = newScene(gridPane);

//Setting title to the


Stage
stage.setTitle("RegistrationForm");

//Adding scene to the


stage
stage.setScene(scene)
;
//Displaying the contents of the
stage
stage.show()
};
public static void
main(String
args[]){ launch(args)
};
}

OUTPUT:

RESULT:

38
EX.NO:11
DATE:

AIM:

ALGORITHM:

PROGRAM:

//File Name should be Data.java

import java.sql.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Data extends JFrame implements ActionListener
{
JTextField id;
JTextField name;
JButton next;
JButton addnew;
JPanel p;
static ResultSet res;
static Connection conn;
static Statement stat;
public Data()
{
super("My Application");
Container c = getContentPane();
c.setLayout(new GridLayout(5,1));

id = new JTextField(20);
name = new JTextField(20);
next = new JButton("Next BOOK");
39
p = new JPanel();

c.add(new JLabel("ISBN Number",JLabel.CENTER));


c.add(id);
c.add(new JLabel("Book Name",JLabel.CENTER));
c.add(name);
c.add(p);

p.add(next);
next.addActionListener(this);
pack();
setVisible(true);
addWindowListener(new WIN());
}

public static void main(String args[])


{
Data d = new Data();
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
conn = DriverManager.getConnection("jdbc:odbc:stu");
// cust is the DSN Name
stat = conn.createStatement();
res = stat.executeQuery("Select * from stu"); // stu is the table name
res.next();
}
catch(Exception e)
{
System.out.println("Error" +e);
}
d.showRecord(res);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource() == next)
{
try
{
res.next();
}
catch(Exception e)
{
}
showRecord(res);
}
}
public void showRecord(ResultSet res)
{
try

40
{
id.setText(res.getString(2));
name.setText(res.getString(3));
}
catch(Exception e)
{
}
}//end of the main

//Inner class WIN implemented


class WIN extends WindowAdapter
{
public void windowClosing(WindowEvent w)
{
JOptionPane jop = new JOptionPane();
jop.showMessageDialog(null,"Thank you","My
Application",JOptionPane.QUESTION_MESSAGE);
}
}
}

NOTE:
Create a new Database
1. Create a new Database file in MS ACCESS (our backend) named “books.mdb”.
2. Then create a table named “stu” in it.
3. The table stu contains the following fields and data types
i. ISBN - Text
ii. BookName - Text
4. Enter various records as you wish.
5. Save the database file.

Next step is to add our “books.mdb” to the System DSN. To do that follows the procedure
given below,
i. Go to Start-> Control Panel -> Administrative tools.
ii. In that double click “Data Sources (ODBC)”.
iii. ODBC Data Source Administrator dialog appears.
iv. In that select “System DSN” tab and click the Add Button.
v. Select “Microsoft Access Driver(*.mdb)” and click Finish.
vi. ODBC Microsoft Access Setup appears. In the “Data Source name” type “stu”.
vii. Click on the “Select” button and choose your database file. Then click ok.

Now your database file gets added to the System DSN.

41
Table: Design View

Table Name: stu

42
Administrative Tools.

ODBC Data Source Administrator

43
Creating Microsoft Access Driver(*.mdb)

ODBC Microsoft Access Setup

44
OUTPUT:

RESULT:

45

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy