Java Object-Oriented Programming Lab
Java Object-Oriented Programming Lab
VASUDEVAN COLLEGE OF
ENGINEERING AND TECHNOLOGY
PONMAR, CHENNAI- 600 127
NAME :
REGISTER NO. :
SEMESTER :
BRANCH : _
CONTENTS:
4 FindingAreaUsingAbstractClass
5 ADTStack UsingJavaInterface
7 Multithreading
8 Input/OutputFiles
10 Event-Driven Programming
11 Miniproject-Online TestSystem
[Link].:1(a)
SOLVE PROBLEMS BY USING SEQUENTIAL SEARCH
DATE:
AIM:
To develop a Java application to search an element in an array using sequential search
algorithm.
ALGORITHM:
1
REG. NO:411623149047
OUTPUT:
D:\Java\CS3381>javac [Link]
D:\Java\CS3381>java LinearSearch
Element found at index 1
RESULT:
Thus, the Java application to perform sequential search was implemented and executed
successfully.
2
REG. NO:411623149047
[Link].:1(b)
SOLVE PROBLEMS BY USING BINARY SEARCH
DATE:
AIM:
To develop a Java application to search an element in an array using binary search
algorithm.
ALGORITHM:
class BinarySearch
{
public static intbinarySearch(intarr[], int first, int last, int key)
{
if (last>=first)
{
int mid = (first +last)/2;
if (arr[mid] == key)
{
return mid;
}
else if (arr[mid] >key)
{
return binarySearch(arr, first, mid-1, key);
}
else
{
return binarySearch(arr, mid+1, last, key);
}
}
3
REG. NO:411623149047
return -1;
}
public static void main(String args[])
{
intarr[] = {10,20,30,40,50};
int key = 30;
int last=[Link]-1;
int result = binarySearch(arr,0,last,key);
if (result == -1)
[Link]("Element is not found!");
else
[Link]("Element is found at index: "+result);
}
}
OUTPUT:
D:\Java\CS3381>javac [Link]
D:\Java\CS3381>java BinarySearch
Element is found at index: 2
D:\Java\CS3381>
RESULT:
Thus, the Java application to perform binary search was implemented and executed
successfully.
4
REG. NO:411623149047
AIM:
To develop a Java application to sort an array of elements in ascending order using selection
sort.
ALGORITHM:
PROGRAM:
[Link]
int temp=arr[i];
arr[i]=arr[min];
arr[min]=temp;
}
}
public static void main(String[] args)
{
int[] arr= {15,21,6,3,19,20};
[Link]("Elements in the array before Sorting");
for(int i:arr)
[Link](i+" ");
selectionsort(arr);
[Link]("\nElements in the array after Sorting");
for(int i:arr)
[Link](i+" ");
}
}
OUTPUT:
D:\Java\CS3381>javac [Link]
D:\Java\CS3381>java SelectionSort
Elements in the array before Sorting
15 21 6 3 19 20
Elements in the array after Sorting
3 6 15 19 20 21
D:\Java\CS3381>
RESULT:
Thus, the Java application to sort an array of N elements using selection sort was
implemented and executed successfully.
6
REG. NO:411623149047
AIM:
To develop a Java application to sort an array of elements in ascending order using insertion
sort.
ALGORITHM:
num[j+1]=num[j];
}
else
{
break;
}
j=j-1;
}
num[j+1]=x;
}
[Link]("\n\nArray after Insertion Sort\n");
for(i=0; i<[Link]; i++)
{
[Link](num[i]+" ");
}
}
}
OUTPUT:
D:\Java\CS3381>javac [Link]
D:\Java\CS3381>java InsertionSort
Array before Insertion Sort
12 9 37 86 2 17 5
Array after Insertion Sort
2 5 9 12 17 37 86
D:\Java\CS3381>
RESULT:
Thus, the Java application to sort an array of N elements using insertion sort was
implemented and executed successfully.
8
REG. NO:411623149047
AIM:
To develop a Java program to implement stack data structure using classes and objects.
ALGORITHM:
Step 1: Start the program.
Step 2: Create a class named Stack and declare the instance variables st[], top,
maxsize as private.
Step3:Use constructor to allocate memory space for the array and initialize top as -1.
Step 4: Define methods such as isFull(), isEmpty(), push(), pop() and printStack();
Step 5: Push Operation
Step 5.1: Check whetherstack has some space or stack is full.
Step 5.2: If the stack has no space then display “overflow” and exit.
Step 5.3: If the stack has space then increase top by 1 to point next empty space.
Step 5.4: Add element to the new stack location, where top is pointing.
Step 5.5: Push operation performed successfully.
Step 6: Pop operation
Step 6.1: Check whether stack has some element or stack is empty.
Step 6.2: If the stack has no element means it is empty then display “underflow”
Step 6.3: If the stack has some element, accesses the data element at which top is
pointing.
Step 6.4: Decrease the value of top by 1.
Step 6.5: Pop operation performed successfully.
Step 7: PrintStack operation
Step 7.1:Check whether stack has some element or stack is empty.
Step 7.2: If the stack has no element means it is empty then display “underflow”.
Step 7.3: If the stack has some element, traverse the array from top to bottom and
display the elements.
Step 8: Define the main() method
Step 9: Create object of Stack class.
Step 10: Display the menu and get the user choice and invoke appropriate method.
Step 11: Stop the program.
9
REG. NO:411623149047
PROGRAM:
[Link]
import [Link];
public class Stack
{
private intmaxsize, top;
private int[] st;
public Stack(int size)
{
maxsize = size;
st = new int[maxsize];
top = -1;
}
booleanisEmpty()
{
return top==-1;
}
booleanisFull()
{
return top==maxsize-1;
}
public void push(int element)
{
if(isFull())
[Link]("Overflow");
else
st[++top] = element;
}
public intpop()
{
if(isEmpty())
{
[Link]("UnderFlow");
return (-1);
}
return (st[top--]);
}
public void printStack()
{
[Link]("Stack Elements:");
for (int i = top; i>=0; i--)
10
REG. NO:411623149047
[Link](st[i]);
}
public static void main(String[] args)
{
Scanner sc = new Scanner([Link]);
[Link]("Enter stack size");
int size=[Link]();
Stack obj = new Stack(size);
while (true)
{
[Link]("\nSTACK\n*****\[Link]\[Link]
\[Link]\[Link]\nEnter your choice");
intch = [Link]();
switch (ch)
{
case 1:
[Link]("Enter Element");
int n = [Link]();
[Link](n);
break;
case 2:
[Link]("Poped element is %d", [Link]());
break;
case 3:
[Link]();
break;
case 4:
[Link](0);
default:
[Link]("Wrong option");
}
}
}
}
OUTPUT:
D:\Java\CS3381>java Stack
Enter stack size
5
STACK
*****
[Link]
2. POP
11
REG. NO:411623149047
3. Display
[Link]
Enter your choice
1
Enter Element
12
STACK
*****
[Link]
2. POP
3. Display
[Link]
Enter your choice
1
Enter Element
34
STACK
*****
[Link]
2. POP
3. Display
[Link]
Enter your choice
1
Enter Element
56
STACK
*****
[Link]
2. POP
3. Display
[Link]
Enter your choice
1
Enter Element
78
STACK
*****
[Link]
2. POP
3. Display
[Link]
Enter your choice
3
Stack Elements:
78
12
REG. NO:411623149047
56
34
12
STACK
*****
[Link]
2. POP
3. Display
[Link]
Enter your choice
2
Poped element is 78
STACK
*****
[Link]
2. POP
3. Display
[Link]
Enter your choice
3
Stack Elements:
56
34
12
STACK
*****
[Link]
2. POP
3. Display
[Link]
Enter your choice
4
D:\Java\CS3381>
RESULT:
Thus, the Java program to implement stack data structure using classes and objects has
developed and executed successfully.
13
REG. NO:411623149047
AIM:
To develop a Java program to implement queue data structure using classes and objects.
ALGORITHM:
Step 1: Start the program.
Step 2: Create a class named Queue and declare the instance variables items[], front, rear,
maxsize as private.
Step3:Use constructor to allocate memory space for the array and initialize front as -1 and
rear as -1.
Step 4: Define methods such as isFull(), isEmpty(), enQueue(), deQueue() and display();
Step 5: Enqueue Operation
Step 5.1: Check whetherqueue has some space or queue is full.
Step 5.2: If the queue has no space then display “overflow” and exit.
Step 5.3: If the queue has space then increase rear by 1 to point next empty space.
Step 5.4: Add element to the new location, where rear is pointing.
Step 5.5: Enqueue operation performed successfully.
Step 6: Dequeue operation
Step 6.1: Check whether queue has some element or queue is empty.
Step 6.2: If the queue has no element means it is empty then display “underflow”
Step 6.3: If the queue has some element, accesses the data element at which front is
pointing.
Step 6.4: Incrementthe value of front by 1.
Step 6.5:Dequeue operation performed successfully.
Step 7: Displayoperation
Step 7.1:Check whether queue has some element or queue is empty.
Step 7.2: If the stack has no element means it is empty then display “underflow”.
Step 7.3: If the stack has some element, traverse the array from front to rear and
display the elements.
Step 8: Define the main() method
Step 9: Create object of Queue class.
Step 10: Display the menu and get the user choice and invoke appropriate method.
Step 11: Stop the program.
14
REG. NO:411623149047
PROGRAM:
[Link]
import [Link];
public class Queue
{
private intitems[];
private intmaxsize, front, rear;
Queue(int size)
{
maxsize=size;
items = new int[size];
front = -1;
rear = -1;
}
booleanisFull()
{
if (front == 0 && rear ==maxsize-1)
{
return true;
}
return false;
}
booleanisEmpty()
{
if (front == -1)
return true;
else
return false;
}
15
REG. NO:411623149047
}
else
{
if (front == -1)
front = 0;
rear++;
items[rear] = element;
[Link]("Inserted " + element);
}
}
intdeQueue()
{
int element;
if (isEmpty())
{
[Link]("Queue is empty");
return (-1);
}
else
{
element = items[front];
if (front >= rear)
{
front = -1;
rear = -1;
}
else
{
front++;
}
return (element);
}
}
void display()
16
REG. NO:411623149047
{
inti;
if (isEmpty())
{
[Link]("Empty Queue");
}
else
{
[Link]("\nFront index-> " + front);
[Link]("Items -> ");
for (i = front; i<= rear; i++)
[Link](items[i] + " ");
[Link]("\nRear index-> " + rear);
}
}
case 3:
[Link]();
break;
case 4:
[Link](0);
default:
[Link]("Wrong option");
}
}
}
}
OUTPUT:
D:\Java\CS3381>javac [Link]
D:\Java\CS3381>java Queue
Enter queue size
5
QUEUE
*****
[Link]
2. DEQUEUE
3. DISPLAY
4. EXIT
Enter your choice
1
Enter Element
12
Inserted 12
QUEUE
*****
[Link]
2. DEQUEUE
3. DISPLAY
4. EXIT
Enter your choice
1
Enter Element
34
Inserted 34
18
REG. NO:411623149047
QUEUE
*****
[Link]
2. DEQUEUE
3. DISPLAY
4. EXIT
Enter your choice
1
Enter Element
56
Inserted 56
QUEUE
*****
[Link]
2. DEQUEUE
3. DISPLAY
4. EXIT
Enter your choice
1
Enter Element
78
Inserted 78
QUEUE
*****
[Link]
2. DEQUEUE
3. DISPLAY
4. EXIT
Enter your choice
3
Front index-> 0
Items ->
12 34 56 78
Rear index-> 3
QUEUE
*****
[Link]
2. DEQUEUE
3. DISPLAY
4. EXIT
Enter your choice
2
Poped element is 12
QUEUE
*****
[Link]
19
REG. NO:411623149047
2. DEQUEUE
3. DISPLAY
4. EXIT
Enter your choice
3
Front index-> 1
Items ->
34 56 78
Rear index-> 3
QUEUE
*****
[Link]
2. DEQUEUE
3. DISPLAY
4. EXIT
Enter your choice
4
D:\Java\CS3381>
RESULT:
Thus, the Java program to implement queue data structure using classes and objects has
developed and executed successfully.
20
REG. NO:411623149047
AIM:
To develop a java application to generate pay slip for different category of employees using
the concept of inheritance.
ALGORITHM:
import [Link];
class Employee
{
String Emp_name,Mail_id,Address,Emp_id, Mobile_no;
double BP,GP,NP,DA,HRA,PF,CF;
Scanner get = new Scanner([Link]);
Employee()
{
21
REG. NO:411623149047
22
REG. NO:411623149047
{
[Link]("Enter Basic pay of the Assistant Professor:");
BP = [Link]();
}
void display()
{
[Link]("=============================="+"\n"+"Assistant ProfessorPay
Slip"+"\n"+"=============================="+"\n");
[Link]();
}
}
class AssociateProfessor extends Employee
{
AssociateProfessor()
{
[Link]("Enter Basic pay of the Professor:");
BP = [Link]();
}
void display()
{
[Link]("=============================="+"\n"+"Associate
Professor Pay Slip"+"\n"+"=============================="+"\n");
[Link]();
}
}
class Professor extends Employee
{
Professor()
{
[Link]("Enter Basic pay of the Professor:");
BP = [Link]();
}
void display()
{
[Link]("=============================="+"\n"+"Professor Pay
Slip"+"\n"+"=============================="+"\n");
[Link]();
}
}
class Payslip
{
public static void main(String[] args)
{
char ans;
Scanner sc = new Scanner([Link]);
do
{
[Link]("Main Menu");
[Link]("1. Programmer \n2. Assistant Professor \n3. Associate
Professor \n4. Professor");
23
REG. NO:411623149047
OUTPUT:
Main Menu
1. Programmer
2. Assistant Professor
3. Associate Professor
4. Professor
Enter your choice: 1
Enter Name of the Employee: Jeremiah
Enter Address of the Employee: Chennai
Enter ID of the Employee: 12345
Enter Mobile Number: 9360362173
Enter E-Mail ID of the Employee : jeremiahe@[Link]
Enter Basic pay of the Programmer: 56000
==============================
Programmar Pay Slip
==============================
Employee Name: Jeremiah
Employee Address: Chennai
24
REG. NO:411623149047
RESULT:
Thus, the Java application to generate pay slip for different category of employees was
implemented using inheritance and the program was executed successfully.
25
REG. NO:411623149047
AIM:
To write a Java program to calculate the area of rectangle, circle and triangle using the
concept of abstract class.
ALGORITHM:
Step 1: Start the program.
Step 2: Create an abstract class named shape that contains two integers and an empty method
named printArea().
Step 3: Create three classes named rectangle, triangle and circle such that each one of the
classes extends the class Shape.
Step 4: Each of the inherited class from shape class should provide the implementation for
the methodprintArea().
Step 5: In printAree() method get the input from user and calculate the area of rectangle,
circle and triangle.
Step 6: In the AbstractArea, create the objects for the three inherited classes and invoke the
methods and display the area values of the different shapes.
Step 7: Stop the program.
PROGRAM:
[Link]
import [Link].*;
abstract class Shape
{
inta,b;
abstract void printArea();
}
class Rectangle extends Shape
{
void printArea()
{
[Link]("\t\tCalculating Area of Rectangle");
Scanner input=new Scanner([Link]);
[Link]("Enter length: ");
a=[Link]();
[Link]("\nEnter breadth: ");
26
REG. NO:411623149047
b=[Link]();
int area=a*b;
[Link]("Area of Rectangle: "+area);
}
}
class Triangle extends Shape
{
void printArea()
{
[Link]("\t\tCalculating Area of Triangle");
Scanner input=new Scanner([Link]);
[Link]("Enter height: ");
a=[Link]();
[Link]("\nEnter breadth: ");
b=[Link]();
double area=0.5*a*b;
[Link]("Area of Triangle: "+area);
}
}
class Circle extends Shape
{
void printArea()
{
[Link]("\t\tCalculating Area of Circle");
Scanner input=new Scanner([Link]);
[Link]("Enter radius: ");
a=[Link]();
double area=3.14*a*a;
[Link]("Area of Circle: "+area);
}
}
class AbstractArea
{
public static void main(String[] args)
{
Shape obj;
obj=new Rectangle();
[Link]();
obj=new Triangle();
[Link]();
obj=new Circle();
[Link]();
}
}
27
REG. NO:411623149047
OUTPUT:
Calculating Area of Rectangle
Enter length: 10
Enter breadth: 20
Area of Rectangle: 200
Calculating Area of Triangle
Enter height: 34
Enter breadth: 56
Area of Triangle: 952.0
Calculating Area of Circle
Enter radius: 23
Area of Circle: 1661.06
RESULT:
Thus, the Java program to calculate the area of rectangle, circle and triangle using the
concept of abstract class wasdeveloped and executed successfully.
28
REG. NO:411623149047
AIM:
To write a Java program to calculate the area of rectangle, circle and triangle by
implementing the interface shape.
ALGORITHM:
PROGRAM:
[Link]
import [Link];
interface Shape
{
public void printArea();
}
class Rectangle implements Shape
{
public void printArea()
{
[Link]("\t\tCalculating Area of Rectangle");
Scanner input=new Scanner([Link]);
[Link]("Enter length: ");
int a=[Link]();
[Link]("\nEnter breadth: ");
int b=[Link]();
int area=a*b;
[Link]("Area of Rectangle: "+area);
29
REG. NO:411623149047
}
}
class Triangle implements Shape
{
public void printArea()
{
[Link]("\t\tCalculating Area of Triangle");
Scanner input=new Scanner([Link]);
[Link]("Enter height: ");
int a=[Link]();
[Link]("\nEnter breadth: ");
int b=[Link]();
double area=0.5*a*b;
[Link]("Area of Triangle: "+area);
}
}
class Circle implements Shape
{
public void printArea()
{
[Link]("\t\tCalculating Area of Circle");
Scanner input=new Scanner([Link]);
[Link]("Enter radius: ");
int a=[Link]();
double area=3.14*a*a;
[Link]("Area of Circle: "+area);
}
}
public class TestArea
{
public static void main(String[] args)
{
Shape obj;
obj=new Rectangle();
[Link]();
obj=new Triangle();
[Link]();
obj=new Circle();
[Link]();
}
}
30
REG. NO:411623149047
OUTPUT:
D:\Java\CS3381>javac [Link]
D:\Java\CS3381>java TestArea
Calculating Area of Rectangle
Enter length: 14
Enter breadth: 24
Area of Rectangle: 336
Calculating Area of Triangle
Enter height: 45
Enter breadth: 32
Area of Triangle: 720.0
Calculating Area of Circle
Enter radius: 5
Area of Circle: 78.5
D:\Java\CS3381>
RESULT:
Thus, the Java program to calculate the area of rectangle, circle and triangle using the
concept of interfacewasdeveloped and executed successfully.
31
REG. NO:411623149047
AIM:
To write a Java program to implement user defined exception handling.
ALGORITHM:
PROGRAM:
[Link]
import [Link].*;
class NegativeAmtException extends Exception
{
String msg;
NegativeAmtException(String msg)
{
[Link]=msg;
}
public String toString()
{
return msg;
}
}
32
REG. NO:411623149047
class BankAccount
{
private double balance;
private intaccountNumber;
public BankAccount(intaccountNumber,doubleinitialBalance)throws
NegativeAmtException
{
if(initialBalance<0)
throw new NegativeAmtException("Initial amount should not be
negative!");
balance = initialBalance;
[Link] = accountNumber;
}
public void deposit(double amount)throws NegativeAmtException
{
if (amount < 0)
{
throw new NegativeAmtException("Don't deposit negative amount!");
}
balance = balance + amount;
[Link]("Amount deposited");
[Link]("Balance amount : "+getBalance());
}
public void withdraw(double amount)throws NegativeAmtException
{
if (amount < 0)
{
throw new NegativeAmtException("Don't withdraw a negative
amount!");
}
else if(amount<=balance)
{
balance = balance – amount;
}
else
{
[Link]("Insufficient amount");
}
[Link]("Your account
details\n"+ac);
break;
case 5:
[Link]();
[Link](0);
default:
34
REG. NO:411623149047
[Link]("Invalid Choice");
}
}
}
catch(NegativeAmtException e)
{
[Link]("Exception Caught : "+e);
}
}
}
OUTPUT:
Enter Account Number:1234
Enter the initial Amount:500
Main Menu
[Link]
[Link]
[Link] Balance
[Link]
5. Exit
Enter your Choice: 1
Enter the amount to deposit:-456
Exception Caught : Don't deposit negative amount!
RESULT:
Thus the Java program to implement user defined exception handling was implementedand
executed successfully.
35
REG. NO:411623149047
AIM:
To write a java program to implement a multi-threaded application.
ALGORITHM:
PROGRAM:
[Link]
import [Link].*;
class even implements Runnable
{
public int x;
public even(int x)
{
this.x = x;
}
public void run()
{
[Link]("New Thread "+ x +" is EVEN and Square of " + x + " is: "
+ x * x);
}
}
36
REG. NO:411623149047
[Link](1000);
[Link](" ");
}
}
catch (Exception ex)
{
[Link]([Link]());
}
37
REG. NO:411623149047
}
}
public classMultiThread
{
public static void main(String[] args)
{
A a = new A();
[Link]();
}
}
OUTPUT:
Main Thread and Generated Number is 33
New Thread 33 is ODD and Cube of 33 is: 35937
RESULT:
Thus, the java program to implement multithreaded application was executed successfully.
38
REG. NO:411623149047
[Link].: 8
WRITE A PROGRAM TO PERFORM FILE OPERATIONS
DATE:
AIM:
To write a java program to copy the contents of one file to another file using file operations.
ALGORITHM:
PROGRAM:
[Link]
import [Link].*;
class CopyFile
{
public static void main(String args[]) throws IOException
{
inti;
FileInputStream fin = null;
FileOutputStreamfout = null;
if([Link] != 2)
{
[Link]("Usage: CopyFile from to");
return;
}
[Link]("Displaying contents of "+ args[0]+"\n");
try
{
fin = new FileInputStream(args[0]);
do
39
REG. NO:411623149047
{
i = [Link]();
if(i != -1)
[Link]((char) i);
} while(i != -1);
}
catch(IOException e)
{
[Link]("Error Reading File");
}
finally
{
try
{
[Link]();
}
catch(IOException e)
{
[Link]("Error Closing File");
}
}
[Link]("\nCopying contents of "+ args[0] + "to " + args[1]+"\n");
try
{
fin = new FileInputStream(args[0]);
fout = new FileOutputStream(args[1]);
do
{
i = [Link]();
if(i != -1) [Link](i);
} while(i != -1);
}
catch(IOException e)
{
[Link]("I/O Error: " + e);
}
finally
{
try
{
if(fin != null)
[Link]();
}
catch(IOException e2)
40
REG. NO:411623149047
{
[Link]("Error Closing Input File");
}
try
{
if(fout != null)
[Link]();
}
catch(IOException e2)
{
[Link]("Error Closing Output File");
}
}
[Link]("\nFile Copied\n");
[Link]("\nDisplaying contents of "+ args[1]+"\n");
try
{
fin = new FileInputStream(args[1]);
do
{
i = [Link]();
if(i != -1)
[Link]((char) i);
} while(i != -1);
}
catch(IOException e)
{
[Link]("Error Reading File");
}
finally
{
try
{
[Link]();
}
catch(IOException e)
{
[Link]("Error Closing File");
}
}
}
}
41
REG. NO:411623149047
OUTPUT:
R:\oop>javac [Link]
R:\oop>java CopyFile [Link] [Link]
File Copied
R:\oop>
RESULT:
Thus, the java program to copy the contents of one file to another file using file was written,
executed and verified.
42
REG. NO:411623149047
AIM:
To write a java program to find the maximum and minimum value from the given type of
elements using ageneric function.
ALGORITHM:
Step 1: Start the program.
Step 2: Create a class Myclassto implement generic class and generic methods.
Step 3: Get the set of the values belonging to specific data type.
Step 4:Create the objects of the class to hold integer, character and double values.
Step 5:Create the method to compare the values and find the maximum value stored in the
array.
Step 6: Invoke the method with integer, character, double and string values. The output will
be displayedbased on the data type passed to the method.
Step 7:Stop the program.
PROGRAM:
[Link]
class MyClass<T extends Comparable<T>>
{
T[] vals;
MyClass(T[] obj)
{
vals = obj;
}
public T min()
{
T v = vals[0];
for(inti=1; i<[Link]; i++)
if(vals[i].compareTo(v) < 0)
v = vals[i];
return v;
}
public T max()
{
T v = vals[0];
for(inti=1; i<[Link];i++)
if(vals[i].compareTo(v) > 0)
v = vals[i];
return v;
43
REG. NO:411623149047
}
}
class GenericsDemo
{
public static void main(String args[])
{
Integer num[]={10,2,5,4,6,1};
Character ch[]={'v','p','s','a','n','h'};
Double d[]={20.2,45.4,71.6,88.3,54.6,10.4};
String str[]= {"hai","how","are","you"};
MyClass<Integer>iob = new MyClass<Integer>(num);
MyClass<Character> cob = new MyClass<Character>(ch);
MyClass<Double>dob = new MyClass<Double>(d);
MyClass<String>sob=new MyClass<String>(str);
[Link]("Max value in num: " + [Link]());
[Link]("Min value in num: " + [Link]());
[Link]("Max value in ch: " + [Link]());
[Link]("Min value in ch: " + [Link]());
[Link]("Max value in d: " + [Link]());
[Link]("Min value in d: " + [Link]());
[Link]("Max value in str: " + [Link]());
[Link]("Min value in str: " + [Link]());
}
}
OUTPUT:
Max value in num: 10
Min value in num: 1
Max value in ch: v
Min value in ch: a
Max value in d: 88.3
Min value in d: 10.4
Max value in str: you
Min value in str: are
RESULT:
Thus, the Java program to find the maximum and minimum value from the given type of
elements was implemented using generics and executed successfully.
44
REG. NO:411623149047
[Link].: 10(a)
DEVELOP APPLICATIONS USING JAVAFX CONTROLS
DATE:
AIM:
To write a program to display multiple choice test question using JavaFX.
ALGORITHM:
PROGRAM:
import [Link];
import static [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
public class MCTest extends Application
{
@Override
public void start(Stage primaryStage)
{
[Link]("Test Question 1");
Label labelfirst= new Label("What is 10 + 20?");
Label labelresponse= new Label();
Button button= new Button("Submit");
RadioButton radio1, radio2, radio3, radio4;
radio1=new RadioButton("10");
45
REG. NO:411623149047
[Link](true);
[Link](e ->[Link](false) );
[Link](e ->[Link](false) );
[Link](e ->[Link](false) );
[Link](e ->[Link](false) );
[Link](e ->
{
if ([Link]())
{
[Link]("Correct answer");
[Link](true);
}
else
{
[Link]("Wrong answer");
[Link](true);
}
});
46
REG. NO:411623149047
OUTPUT:
RESULT:
Thus, the Java program for multiple choice test questions was implemented and executed
successfully.
47
REG. NO:411623149047
[Link].: 10(b)
DEVELOP APPLICATIONS USING LAYOUTS AND MENUS
DATE:
AIM:
To write a program to create simple editor with menu using JavaFX.
ALGORITHM:
PROGRAM:
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
public class MenuUI extends Application {
@Override
public void start(Stage primaryStage) throws Exception
{
48
REG. NO:411623149047
49
REG. NO:411623149047
[Link]("JavaFX Menu");
[Link]();
}
public static void main(String[] args) {
[Link](args);
}
}
OUTPUT:
RESULT:
Thus, the Java program for simple editor was implemented and executed successfully.
50
REG. NO:411623149047
Aim:
To create a library management system using java program.
Procedure:
The Library Management System has become an essential tool for public libraries, school libraries,
college libraries. The Library management system helps librarians to manage, maintain and monitor
the library.
There will be two module:
1. Librarian
2. User/Student
Functions of Librarian:
Librarians can add users.
Librarians can add books.
Librarians can issue books for users.
Librarians can make entries of the return books of the users.
Librarians can view users.
Librarians can view books.
Librarians can view issued books.
Librarians can view returned books.
Functions of Student/User:
Users can check or view available books of the library
Users can check or view his/her issued books.
Users can check or view his/her returned books status.
Project Prerequisites:
IDE Used: NetBeans 11.2 (you can use Eclipse)
Java and MySql should be installed on the machine.
Database Used: MySQL 5.5.
To build a library management system using java we require basic knowledge of java and
MySQL database operations ( and JDBC).
Java provides by default packages such as Abstract Window Toolkit (AWT) & Swing
packages to create user interface (UI) for desktop applications. Also, we need two more
libraries such as Mysql-JDBC-connector and java-date-picker.
Steps to Create Library Management System in Java
1. Creating Database
2. Importing packages
3. Functions used
4. Connection to database
5. Defining Login function
6. Defining Librarian functions
7. Defining Student function
1. Create Database
51
REG. NO:411623149047
Users table for storing information such as username, password, user_type and users table is
used for login purposes.
Books table for storing books details of library.
Issued books table for storing the entries of the user if he/she took a book from the library.
Return books table for storing the entries of the user if he/she returns the books to the library.
Sql Queries
a) Creating database:
52
REG. NO:411623149047
f) Inserting user:
2. Importing packages
53
REG. NO:411623149047
In this step, we will import required packages such as swing, awt, jxdatepicker library, mysql-
connector library. [Link] default, Swing & AWT packages are installed by JAVA.
But, jxdatepicker library and MySQL-connector library we have to download and import into our
library management system project.
How to import them?
Click on Tools >> Libraries >> At bottom left Click on “New Library”. Now, name your library
and click on “OK”:
Click on Add JAR/Folder and select the downloaded file to add in our project. Now, add that
library to java library management project.
Code:
import [Link];
import [Link];
import [Link];
import [Link];
54
REG. NO:411623149047
import [Link].*;
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link].*;
import [Link];
import [Link];
3. Functions used
setLayout(layout): This function will define the layout of the frame, window, pane, in which
all the components are arranged.
setText(“your text”): This function will set the title or the text on the label, button, etc.
setVisible(true): This function will make the frame or window visible to the user. By default,
it is false.
setSize(int width, int height): This function takes two parameters such as height and width. It
is used to define the size of frame/window, panel, etc.
setBackground(new Color(255,255,255)): This function will set the background color of the
UI component.
setForeground(new Color(255,255,255)): This function will set the foreground color of the
UI component.
add(obj): This function is used to add the components in a sequential manner to the frame or
panel.
executeQuery(string query): This function will execute the sql query.
next(): This function will move the cursor to a new row of the table.
showMessageDialog(): This function will enable the dialog box to show the message on the
top of the frame.
getText(): This function will get the text from the textfield input of the user.
parseInt(“text”): This function will convert the string into the integer data type.
setResizable(false): This function sets the frame/window resizable. By default, it is true.
setColumnIdentifiers(String names): This function will set the column names of the model of
the table.
setOpaque(true): This function will set up opaque so that the label component paints every
pixel within its bounds.
addRow(Object): This function will add the data to the new row of the model of the table.
isSelected(): This function will return true if the radio button is selected otherwise it will
return false.
dispose(): This function will close the frame / window of library management system.
getString(int column): This function will get the varchar/string data from the table of the
mysql database.
55
REG. NO:411623149047
getInt(int column): This function will get the integer data from the table of the mysql
database.
4. Connection to database
In this step, we will create a connection method as a reusable component, this method will connect
the library management to mysql database. Make sure that you enter the proper url string of database
connection such as port number, username, password, etc.
Function definitions:
forName(driverClassName): This function is used to register with the driver of the database.
getConnection(url): This function is used to make connections with databases. It takes parameters
such as hostname, port number, database, username, password, etc. Generalized form:
“jdbc:mysql://hostname:port/dbName”, “username”, “password”. If your mysql does not have a
password then enter “” as an empty string.
Code:
public static Connection connect() {
//Making Database Connection once & using multiple times whenever required.
try {
[Link]("[Link]");
Connection con =
[Link]("jdbc:mysql://localhost:3306/
return con;
} catch (Exception ex) {
[Link]();
return null;
Login Code:
56
REG. NO:411623149047
//Setting up opaque so that label component paints every pixel within its bounds.
[Link](true);
[Link]([Link]);
//Setting up opaque so that label component paints every pixel within its bounds.
[Link](true);
[Link]([Link]);
[Link]([Link]);
57
REG. NO:411623149047
[Link]([Link]);
[Link]([Link]);
[Link]([Link]);
[Link](new ActionListener() {
@Override
else if ([Link]()) {
} //If both the fields are present then to login the user, check whether the user exists already
else {
58
REG. NO:411623149047
try {
} else {
[Link]();
while ([Link]()) {
[Link](admin);
librarian_frame();
} else {
//Redirecting to User Frame for that user ID
user_frame(UID);
}
[Link]();
59
REG. NO:411623149047
});
[Link](new ActionListener() {
@Override
[Link]();
}
});
//Adding all login components in the login frame of java library management system.
[Link](l1);
[Link](usernameTF);
[Link](l2);
[Link](passwordTF);
[Link](loginBtn);
[Link](cancelBtn);
[Link](true);
//Setting frame non-resizable
[Link](false);
}
6. Defining Librarian functions
In this step, we will create the dashboard of the librarian, in which we will have 8 buttons such as add
user, add book, issue book, return book, view users, view books, view issued books, view returned
books. Each button will have its own action listeners to perform its own task.
60
REG. NO:411623149047
Dashboard Functions:
1. Add user: The librarian will enter the details such as username, password, user type of the user
and user will be added.
2. Add books: The librarian will enter the details of the new book such as isbn, book name, book
publisher, price, pages, edition, etc. The details will be entered into the database and the book
will be added.
3. Issue book: The librarian will enter the details of the issue book of the user such as book id, user
id, date at which book was issued, etc. The details will be entered into the database and the
book will be issued.
4. Return book: The librarian will enter the details of the return book of the user such as book id,
user id, date at which book was returned, fine(if any), etc. The details will be entered into the
java library management system.
5. View users: The librarian can view details of the users anytime.
6. View books: The librarian can view details of the books of the library anytime.
7. View issued books: The librarian can view details of the issued books anytime.
8. View returned books: The librarian can view details of the returned books anytime.
Code:
public static void librarian_frame() {
//Creating Button
view_books_btn.setForeground([Link]);
view_books_btn.addActionListener(new ActionListener() {
@Override
//Connection to Database
61
REG. NO:411623149047
try {
//Creating Statement
//Executing query
ResultSet rs = [Link](sql);
String[] bookColumnNames = {"Book ID", "Book ISBN", "Book Name", "Book Publisher", "Book
Edition", "Book Genre", "Book price", "Book Pages"};
[Link](bookColumnNames);
book_list.setModel(bookModel);
book_list.setForeground([Link]);
book_list.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
book_list.setFillsViewportHeight(true);
book_list.setFocusable(false);
//Creating scrollbars for table
[Link](JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEED
ED);
[Link](JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
62
REG. NO:411623149047
while ([Link]()) {
[Link](scrollBook);
[Link](800, 400);
[Link](true);
[Link](null, e1);
});
//Creating button
63
REG. NO:411623149047
view_users_btn.setForeground([Link]);
view_users_btn.addActionListener(new ActionListener() {
@Override
//Connection to database
try {
//Creating Statement
//Executing query
ResultSet rs = [Link](sql);
//Creating Table for to data will be in table format
JTable users_list = new JTable();
[Link](userColumnNames);
users_list.setModel(userModel);
users_list.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
64
REG. NO:411623149047
users_list.setFillsViewportHeight(true);
users_list.setForeground([Link]);
[Link](JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDE
D);
[Link](JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
while ([Link]()) {
if (user_type == 1) {
//Checking if it is 1 then it is admin
} else {
[Link](scrollUser);
[Link](800, 400);
[Link](true);
65
REG. NO:411623149047
[Link](null, e1);
}
}
});
//Creating button
view_issued_books_btn.setForeground([Link]);
view_issued_books_btn.addActionListener(new ActionListener() {
@Override
//Connection to database
try {
//Creating Statement
Statement stmt = [Link]();
//Executing query
ResultSet rs = [Link](sql);
66
REG. NO:411623149047
String[] issueBookColumnNames = {"Issue ID", "User ID", "Book ID", "Issue Date", "Period"};
[Link](issueBookColumnNames);
issue_book_list.setModel(issuedBookModel);
issue_book_list.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
issue_book_list.setFillsViewportHeight(true);
issue_book_list.setFocusable(false);
issue_book_list.setForeground([Link]);
[Link](JScrollPane.HORIZONTAL_SCROLLBAR_AS_
NEEDED);
[Link](JScrollPane.VERTICAL_SCROLLBAR_AS_NEED
ED);
while ([Link]()) {
67
REG. NO:411623149047
[Link](scrollIssuedBook);
[Link](800, 400);
[Link](true);
});
//Creating button
view_returned_books_btn.setForeground([Link]);
view_returned_books_btn.addActionListener(new ActionListener() {
@Override
68
REG. NO:411623149047
try {
//Creating Statement.
//Executing query.
ResultSet rs = [Link](sql);
String[] returnBookColumnNames = {"Return ID", "Book ID", "User ID", "Return Date", "Fine"};
[Link](returnBookColumnNames);
returned_book_list.setModel(returnBookModel);
returned_book_list.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
returned_book_list.setFillsViewportHeight(true);
returned_book_list.setFocusable(false);
returned_book_list.setForeground([Link]);
[Link](JScrollPane.HORIZONTAL_SCROLLBAR_AS
_NEEDED);
[Link](JScrollPane.VERTICAL_SCROLLBAR_AS_NEE
DED);
69
REG. NO:411623149047
while ([Link]()) {
[Link](800, 400);
[Link](true);
[Link](null, e1);
});
//Creating button
add_user_btn.setForeground([Link]);
70
REG. NO:411623149047
add_user_btn.addActionListener(new ActionListener() {
@Override
//Creating frame
JFrame add_user_frame = new JFrame("Enter User Details"); //Frame to enter user details
//Creating label
//Setting up opaque so that label component paints every pixel within its bounds.
[Link](true);
[Link]([Link]);
//Creating label
JLabel l2 = new JLabel("Password", [Link]);
//Setting up opaque so that label component paints every pixel within its bounds.
[Link](true);
[Link]([Link]);
//Creating textfield
JTextField add_username_tf = new JTextField();
//Creating textfield
71
REG. NO:411623149047
add_password_tf.setForeground([Link]);
//Aligning center
user_type_radio1.setHorizontalAlignment([Link]);
//Aligning center
user_type_radio2.setHorizontalAlignment([Link]);
user_type_radio2.setForeground([Link]);
user_type_btn_grp.add(user_type_radio1);
user_type_btn_grp.add(user_type_radio2);
//Creating button.
72
REG. NO:411623149047
create_btn.setForeground([Link]);
//Creating button.
user_entry_cancel_btn.setForeground([Link]);
create_btn.addActionListener(new ActionListener() {
@Override
//Connection to database.
try {
//Creating statement
Statement stmt = [Link]();
if (user_type_radio1.isSelected()) {
add_user_frame.dispose();
} else {
73
REG. NO:411623149047
add_user_frame.dispose();
}
} catch (Exception e1) {
[Link](null, e1);
});
user_entry_cancel_btn.addActionListener(new ActionListener() {
@Override
add_user_frame.dispose();
}
});
add_user_frame.add(l1);
add_user_frame.add(add_username_tf);
add_user_frame.add(l2);
add_user_frame.add(add_password_tf);
add_user_frame.add(user_type_radio1);
add_user_frame.add(user_type_radio2);
add_user_frame.add(create_btn);
add_user_frame.add(user_entry_cancel_btn);
74
REG. NO:411623149047
add_user_frame.setSize(350, 200);
add_user_frame.setVisible(true);
//Setting up the table auto-resizable.
add_user_frame.setResizable(false);
}
});
//Creating button.
JButton add_book_btn = new JButton("Add Book");
add_book_btn.setForeground([Link]);
add_book_btn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
//Creating Frame.
//Creating labels
//Setting up opaque so that label component paints every pixel within its bounds.
[Link](true);
75
REG. NO:411623149047
[Link]([Link]);
//Setting up opaque so that label component paints every pixel within its bounds.
[Link](true);
[Link]([Link]);
//Setting up opaque so that label component paints every pixel within its bounds.
[Link](true);
[Link]([Link]);
l4 = new JLabel("Edition", [Link]);
//Setting up opaque so that label component paints every pixel within its bounds.
[Link](true);
[Link]([Link]);
//Setting up opaque so that label component paints every pixel within its bounds.
[Link](true);
76
REG. NO:411623149047
[Link]([Link]);
//Setting up opaque so that label component paints every pixel within its bounds.
[Link](true);
[Link]([Link]);
book_isbn_tf.setForeground([Link]);
//Creating textfield
book_name_tf.setForeground([Link]);
//Creating textfield.
JTextField book_publisher_tf = new JTextField();
77
REG. NO:411623149047
book_publisher_tf.setForeground([Link]);
//Creating textfield.
JTextField book_edition_tf = new JTextField();
book_edition_tf.setForeground([Link]);
//Creating textfield.
JTextField book_genre_tf = new JTextField();
book_genre_tf.setForeground([Link]);
//Creating textfield.
JTextField book_price_tf = new JTextField();
book_price_tf.setForeground([Link]);
//Creating textfield.
JTextField book_pages_tf = new JTextField();
book_pages_tf.setForeground([Link]);
//Creating button.
78
REG. NO:411623149047
create_btn.setForeground([Link]);
//Creating button.
JButton add_book_cancel_btn = new JButton("Cancel");
add_book_cancel_btn.setForeground([Link]);
create_btn.addActionListener(new ActionListener() {
@Override
//Connection to database.
try {
//Creating statement
Statement stmt = [Link]();
79
REG. NO:411623149047
[Link]("INSERT INTO
BOOKS(book_isbn,book_name,book_publisher,book_edition,book_genre,book_price,book_pages)
"
+ " VALUES ('" + book_isbn + "','" + book_name + "','" + book_publisher + "','" + book_edition +
"','" + book_genre + "','" + book_price + "'," + book_pages + ")");
book_frame.dispose();
[Link](null, e1);
});
add_book_cancel_btn.addActionListener(new ActionListener() {
@Override
});
book_frame.add(l1);
book_frame.add(book_isbn_tf);
book_frame.add(l2);
book_frame.add(book_name_tf);
book_frame.add(l3);
book_frame.add(book_publisher_tf);
book_frame.add(l4);
80
REG. NO:411623149047
book_frame.add(book_edition_tf);
book_frame.add(l5);
book_frame.add(book_genre_tf);
book_frame.add(l6);
book_frame.add(book_price_tf);
book_frame.add(l7);
book_frame.add(book_pages_tf);
book_frame.add(create_btn);
book_frame.add(add_book_cancel_btn);
book_frame.setSize(800, 500);
book_frame.setVisible(true);
book_frame.setResizable(false);
}
});
//Creating button
add_issue_book_btn.setForeground([Link]);
add_issue_book_btn.addActionListener(new ActionListener() {
@Override
81
REG. NO:411623149047
//Creating frame.
//Creating panel.
//Creating a datepicker.
[Link]([Link]().getTime());
//Formatting datepicker.
[Link](new SimpleDateFormat("[Link]"));
[Link](picker);
[Link]([Link]);
//Creating labels
[Link](true);
[Link]([Link]);
[Link](true);
82
REG. NO:411623149047
[Link]([Link]);
[Link](true);
[Link]([Link]);
[Link](true);
[Link]([Link]);
//Creating textfield.
JTextField bid_tf = new JTextField();
bid_tf.setForeground([Link]);
//Creating textfield.
JTextField uid_tf = new JTextField();
uid_tf.setForeground([Link]);
83
REG. NO:411623149047
//Creating textfield.
period_tf.setForeground([Link]);
//Creating button.
//Creating button.
issue_book_cancel_btn.setForeground([Link]);
//Performing actions on the button.
create_btn.addActionListener(new ActionListener() {
@Override
84
REG. NO:411623149047
try {
//Creating Statement
Statement stmt = [Link]();
issue_book_frame.dispose();
[Link](null, e1);
});
issue_book_cancel_btn.addActionListener(new ActionListener() {
@Override
issue_book_frame.dispose();
}
});
issue_book_frame.add(l1);
issue_book_frame.add(bid_tf);
85
REG. NO:411623149047
issue_book_frame.add(l2);
issue_book_frame.add(uid_tf);
issue_book_frame.add(l3);
issue_book_frame.add(period_tf);
issue_book_frame.add(l4);
issue_book_frame.getContentPane().add(pickerPanel);
issue_book_frame.add(create_btn);
issue_book_frame.add(issue_book_cancel_btn);
issue_book_frame.setSize(600, 300);
issue_book_frame.setVisible(true);
issue_book_frame.setResizable(false);
}
});
//Creating button.
add_return_book_btn.addActionListener(new ActionListener() {
@Override
//Creating frame.
86
REG. NO:411623149047
//Setting up opaque so that label component paints every pixel within its bounds.
[Link](true);
[Link]([Link]);
[Link]([Link]);
//Creating labels.
JLabel l3 = new JLabel("Return Date(DD-MM-YYYY)", [Link]);
//Setting up opaque so that label component paints every pixel within its bounds.
[Link](true);
[Link]([Link]);
[Link](true);
87
REG. NO:411623149047
[Link]([Link]);
//Creating textfield.
bid_tf.setForeground([Link]);
//Creating textfield.
JTextField uid_tf = new JTextField();
uid_tf.setForeground([Link]);
//Creating a datepicker.
[Link]([Link]().getTime());
[Link](new SimpleDateFormat("[Link]"));
//Creating textfield.
JTextField fine_tf = new JTextField();
fine_tf.setForeground([Link]);
88
REG. NO:411623149047
[Link](picker);
[Link]([Link]);
//Creating button.
JButton return_book_btn = new JButton("Return");
return_book_btn.setForeground([Link]);
//Creating button.
JButton cancel_book_btn = new JButton("Cancel");
cancel_book_btn.setForeground([Link]);
return_book_btn.addActionListener(new ActionListener() {
@Override
//Formatting Date.
89
REG. NO:411623149047
try {
//Connection to database
//Creating Statement
Statement stmt = [Link]();
[Link]();
[Link](null, e1);
});
cancel_book_btn.addActionListener(new ActionListener() {
@Override
[Link]();
}
});
[Link](l1);
[Link](bid_tf);
[Link](l2);
[Link](uid_tf);
90
REG. NO:411623149047
[Link](l3);
[Link]().add(pickerPanel);
[Link](l4);
[Link](fine_tf);
[Link](return_book_btn);
[Link](cancel_book_btn);
[Link](true);
[Link](false);
}
});
[Link](add_user_btn);
[Link](add_book_btn);
[Link](add_issue_book_btn);
[Link](add_return_book_btn);
[Link](view_users_btn);
[Link](view_books_btn);
[Link](view_issued_books_btn);
[Link](view_returned_books_btn);
[Link](800, 200);
91
REG. NO:411623149047
[Link](true);
[Link](false);
Dashboard Functions:
1. View books: The student can view details of the books of the library anytime.
2. View issued books: The student can check and view which books he/she has issued in the java
library management system.
3. View returned books: The student can check and view which books he/she has returned to the
library.
Code:
public static void user_frame(String UID) {
//Creating button
view_books_btn.setForeground([Link]);
view_books_btn.addActionListener(new ActionListener() {
@Override
//Creating Frame.
//Connection to database.
92
REG. NO:411623149047
try {
//Creating Statement.
//Executing query.
ResultSet rs = [Link](sql);
String[] bookColumnNames = {"Book ID", "Book ISBN", "Book Name", "Book Publisher", "Book
Edition", "Book Genre", "Book price", "Book Pages"};
//Creating a model for the table.
[Link](bookColumnNames);
book_list.setModel(bookModel);
book_list.setForeground([Link]);
book_list.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
book_list.setFillsViewportHeight(true);
book_list.setFocusable(false);
//Creating scrollbars for table.
[Link](JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEED
ED);
93
REG. NO:411623149047
[Link](JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
while ([Link]()) {
[Link](scrollBook);
[Link](true);
[Link](null, e1);
);
//Creating Button.
94
REG. NO:411623149047
view_user_issued_books_btn.setForeground([Link]);
view_user_issued_books_btn.addActionListener(new ActionListener() {
@Override
//Storing userid
//Connection to database
//Database Query
try {
//Creating statement
//Executing query
ResultSet rs = [Link](sql);
//Creating Table for to data will be in table format
95
REG. NO:411623149047
String[] issuedBookColumnNames = {"Issue ID", "Book ID", "User ID", "Book ISBN", "Book
Name", "Book Publisher", "Book Edition", "Book Genre", "Book Price", "Book Pages", "Issued
Date", "Period"};
//Creating model for the table
[Link](issuedBookColumnNames);
issued_book_list.setModel(bookModel);
issued_book_list.setForeground([Link]);
issued_book_list.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
issued_book_list.setFillsViewportHeight(true);
issued_book_list.setFocusable(false);
[Link](JScrollPane.HORIZONTAL_SCROLLBAR_AS_
NEEDED);
[Link](JScrollPane.VERTICAL_SCROLLBAR_AS_NEED
ED);
while ([Link]()) {
96
REG. NO:411623149047
//Adding scrollbars.
[Link](scrollIssuedBook);
[Link](1200, 600);
[Link](true);
[Link](false);
[Link](null, e1);
});
//Creating Button
97
REG. NO:411623149047
view_user_returned_books_btn.setForeground([Link]);
view_user_returned_books_btn.addActionListener(new ActionListener() {
@Override
//Storing userid
try {
//Creating Statement
//Executing query
ResultSet rs = [Link](sql);
98
REG. NO:411623149047
String[] returnedBookColumnNames = {"Return ID", "Book ID", "User ID", "Book ISBN", "Book
Name", "Book Publisher", "Book Edition", "Book Genre", "Book Price", "Book Pages", "Returned
Date", "Fine"};
//Creating model for the table
[Link](returnedBookColumnNames);
returned_book_list.setModel(bookModel);
returned_book_list.setForeground([Link]);
returned_book_list.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
returned_book_list.setFillsViewportHeight(true);
returned_book_list.setFocusable(false);
[Link](JScrollPane.HORIZONTAL_SCROLLBAR_AS_
NEEDED);
[Link](JScrollPane.VERTICAL_SCROLLBAR_AS_NEED
ED);
while ([Link]()) {
99
REG. NO:411623149047
//Adding scrollbars.
[Link](scrollIssuedBook);
[Link](1200, 600);
[Link](true);
[Link](false);
[Link](null, e1);
});
100
REG. NO:411623149047
[Link](view_books_btn);
[Link](view_user_issued_books_btn);
[Link](view_user_returned_books_btn);
[Link](500, 500);
[Link](true);
[Link](false);
}
Screen Shorts:
Login Page
Librarian Functions:
101
REG. NO:411623149047
Conclusion:
We have finally built our Library Management System using Java and MySQL. Now librarians can
add users, books, issue books, return books, and also they can view users, books, issued books,
returned books. Students/Users can view available books of the library, also users can check which
book his/her issued books and returned books. From this java project, we have also learned how we
can connect a MySQL database with java, and also how to query a database via Java program.
102