Program
Program
Program
class LinearSearch
{
public static void main(String args[])
{
int c, n, search, array[];
Scanner in = new Scanner(System.in);
System.out.println("Enter number of elements");
n = in.nextInt();
array = new int[n];
{
if (array[c] == search) /* Searching element is present */
{
System.out.println(search + " is present at location " + (c + 1) + ".");
break;
}
}
if (c == n) /* Element to search isn't present */
System.out.println(search + " isn't present in array.");
}
}
Binary Search
class BinarySearchExample{
public static void binarySearch(int arr[], int first, int last, int key){
int mid = (first + last)/2;
while( first <= last ){
if ( arr[mid] < key ){
first = mid + 1;
}else if ( arr[mid] == key ){
System.out.println("Element is found at index: " + mid);
break;
}else{
last = mid - 1;
}
mid = (first + last)/2;
}
if ( first > last ){
System.out.println("Element is not found!");
}
}
public static void main(String args[]){
int arr[] = {10,20,30,40,50};
int key = 30;
int last=arr.length-1;
binarySearch(arr,0,last,key);
}
}
Selection Sort
import java.util.Scanner;
import java.util.Scanner;
class StackExample {
int number[];
int top;
int limit;
// constructor
top =-1;
limit =size;
number=new int[size];
// insert an element
if(isFull())
System.out.println("Stack is full");
else
top=top+1;
number[top]=num;
void pop()
{
if(isEmpty())
System.out.println("Stack is Empty");}
else{
top=top-1;
void peek()
boolean isFull()
boolean isEmpty(){
return top==-1;
void print(){
System.out.println("Elements in Stack:-");
for(int i=top;i>=0;i--)
System.out.println(number[i]);
}
int size,option,element;
char chr;
size=input.nextInt();
do{
option=input.nextInt();
switch(option){
case 1:
element=input.nextInt();
obj.push(element);
break;
case 2:
obj.pop();
break;
case 3:
obj.peek();
break;
case 4:
obj.print();
break;
default:
System.out.println("Choose wrongoption");
chr=input.next().charAt(0);
}while(chr=='y'||chr=='Y');
}
Queue
import java.io.*;
class QueueMain {
InputStreamReader(System.in));
int items[];
void getdata() {
try {
itemnum = Integer.parseInt(buffread.readLine());
System.out.println(ex.getMessage());
void enqueue() {
try {
item = Integer.parseInt(buffread.readLine());
items[rear] = item;
rear++;
count++;
}
else
System.out.println("Queue Is Full");
System.out.println(ex.getMessage());
void dequeue() {
if (count != 0) {
count--;
} else {
System.out.println("Queue Is Empty");
if (rear == itemnum) {
rear = 0;
void display() {
int m = 0;
if (count == 0) {
System.out.println("Queue Is Empty");
} else {
class QueueProgram {
int choice;
que.getdata();
System.out.println("Queue\n\n");
try {
do {
System.out.println("1.Enqueue\n2.Dequeue\n3.Display\n4.Exit\n");
choice = Integer.parseInt(instr.readLine());
switch (choice) {
case 1:
que.enqueue();
break;
case 2:
que.dequeue();
break;
case 3:
que.display();
break;
System.out.println(ex.getMessage());
}
Payslip
package pay;
import java.util.*;
int choice,cont;
do
System.out.println("PAYROLL");
choice=c.nextInt();
switch(choice)
case 1:
p.getdata();
p.getprogrammer();
p.display();
p.calculateprog();
break;
}
case 2:
asst.getdata();
asst.getasst();
asst.display();
asst.calculateasst();
break;
case 3:
asso.getdata();
asso.getassociate();
asso.display();
asso.calculateassociate();
break;
case 4:
prof.getdata();
prof.getprofessor();
prof.display();
prof.calculateprofessor();
break;
cont=c.nextInt();
}while(cont==1);
class employee
int empid;
long mobile;
void getdata()
name = get.nextLine();
mailid = get.nextLine();
address = get.nextLine();
mobile = get.nextLong();
void display()
System.out.println("Employee id : "+empid);
System.out.println("Mail id : "+mailid);
System.out.println("Address: "+address);
double salary,bp,da,hra,pf,club,net,gross;
void getprogrammer()
bp = get.nextDouble();
void calculateprog()
da=(0.97*bp);
hra=(0.10*bp);
pf=(0.12*bp);
club=(0.1*bp);
gross=(bp+da+hra);
net=(gross-pf-club);
System.out.println("************************************************");
System.out.println("************************************************");
System.out.println("Basic Pay:Rs"+bp);
System.out.println("DA:Rs"+da);
System.out.println("PF:Rs"+pf);
System.out.println("HRA:Rs"+hra);
System.out.println("CLUB:Rs"+club);
System.out.println("GROSS PAY:Rs"+gross);
System.out.println("NET PAY:Rs"+net);
double salary,bp,da,hra,pf,club,net,gross;
void getasst()
bp = get.nextDouble();
void calculateasst()
{
da=(0.97*bp);
hra=(0.10*bp);
pf=(0.12*bp);
club=(0.1*bp);
gross=(bp+da+hra);
net=(gross-pf-club);
System.out.println("************************************************");
System.out.println("************************************************");
System.out.println("Basic Pay:Rs"+bp);
System.out.println("DA:Rs"+da);
System.out.println("HRA:Rs"+hra);
System.out.println("PF:Rs"+pf);
System.out.println("CLUB:Rs"+club);
System.out.println("GROSS PAY:Rs"+gross);
System.out.println("NET PAY:Rs"+net);
double salary,bp,da,hra,pf,club,net,gross;
void getassociate()
void calculateassociate()
da=(0.97*bp);
hra=(0.10*bp);
pf=(0.12*bp);
club=(0.1*bp);
gross=(bp+da+hra);
net=(gross-pf-club);
System.out.println("************************************************");
System.out.println("************************************************");
System.out.println("Basic Pay:Rs"+bp);
System.out.println("DA:Rs"+da);
System.out.println("HRA:Rs"+hra);
System.out.println("PF:Rs"+pf);
System.out.println("CLUB:Rs"+club);
System.out.println("GROSS PAY:Rs"+gross);
System.out.println("NET PAY:Rs"+net);
double salary,bp,da,hra,pf,club,net,gross;
void getprofessor()
bp = get.nextDouble();
void calculateprofessor()
da=(0.97*bp);
hra=(0.10*bp);
pf=(0.12*bp);
club=(0.1*bp);
gross=(bp+da+hra);
net=(gross-pf-club);
System.out.println("************************************************");
System.out.println("************************************************");
System.out.println("Basic Pay:Rs"+bp);
System.out.println("DA:Rs"+da);
System.out.println("HRA:Rs"+hra);
System.out.println("PF:Rs"+pf);
System.out.println("CLUB:Rs"+club);
System.out.println("GROSS PAY:Rs"+gross);
System.out.println("NET PAY:Rs"+net);
}
Abstract
import java.util.*;
abstract class shape
{
int a,b;
abstract public void printarea();
}
class rectangle extends shape
{
public int area_rect;
public void printarea()
{
Scanner s=new Scanner(System.in);
System.out.println("enter the length and breadth of rectangle");
a=s.nextInt();
b=s.nextInt();
area_rect=a*b;
System.out.println("Length of rectangle "+a +"breadth of rectangle "+b);
System.out.println("The area ofrectangle is:"+area_rect);
}
}
class triangle extends shape
{
double area_tri;
public void printarea()
{
Scanner s=new Scanner(System.in);
System.out.println("enter the base and height of triangle");
a=s.nextInt();
b=s.nextInt();
System.out.println("Base of triangle "+a +"height of triangle "+b);
area_tri=(0.5*a*b);
System.out.println("The area of triangle is:"+area_tri);
}
}
class circle extends shape
{
double area_circle;
public void printarea()
{
Scanner s=new Scanner(System.in);
System.out.println("enter the radius of circle");
a=s.nextInt();
area_circle=(3.14*a*a);
System.out.println("Radius of circle"+a);
System.out.println("The area of circle is:"+area_circle);
}
}
public class shapeclass
{
public static void main(String[] args)
{
rectangle r=new rectangle();
r.printarea();
triangle t=new triangle();
t.printarea();
circle r1=new circle();
r1.printarea();
} }
INTERFACE
import java.util.*;
interface shape
int a,b;
a=s.nextInt();
b=s.nextInt();
area_rect=a*b;
{
double area_tri;
int a,b;
a=s.nextInt();
b=s.nextInt();
area_tri=(0.5*a*b);
double area_circle;
int a,b;
a=s.nextInt();
area_circle=(3.14*a*a);
System.out.println("Radius of circle"+a);
r.printarea();
t.printarea();
r1.printarea();
}
EXCEPTION HANDLING
package multiplecatch;
public class Multiplecatch {
public static void main(String[] args) {
try{
int a[]=new int[5];
System.out.println(a[10]);
}
catch(ArithmeticException e)
{
System.out.println("Arithmetic Exception occurs");
}
catch(ArrayIndexOutOfBoundsException e)
{
System.out.println("ArrayIndexOutOfBounds Exception occurs");
}
catch(Exception e)
{
Exception
{
String msg;
NegativeAmtException(String msg)
{
this.msg=msg;
}
public String toString()
{
return msg;
}
}
int a=s.nextInt();
try
{
if(a<0)
{
throw new NegativeAmtException("Invalid Amount");
}
System.out.println("Amount Deposited");
}
catch(NegativeAmtException e)
{
System.out.println(e);
}
}
}
MULTITHREAD
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)
{
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;
else
{
Thread t2 = new Thread(new Odd(num));
t2.start();
}
Thread.sleep(1000);
System.out.println("--------------------------------------");
}
}
catch (Exception ex)
{
System.out.println(ex.getMessage());
}
}
}
}
}
FILE
package file;
import java.io.*;
if(f.exists())
}
else if (fName.endsWith(".pdf"))
else if (fName.endsWith(".txt"))
else
}
GENERICS CLASS
package generics;
import java.util.*;
T max = elements[0];
if (element.compareTo(max) > 0)
max = element;
return max;
}
JAVAFX
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.HPos;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.*;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.stage.Stage;
import javafx.stage.Window;
@Override
// Create a scene with registration form grid pane as the root node
primaryStage.setScene(scene);
primaryStage.show();
// Position the pane at the center of the screen, both vertically and horizontally
gridPane.setAlignment(Pos.CENTER);
gridPane.setHgap(10);
columnOneConstraints.setHalignment(HPos.RIGHT);
columnTwoConstrains.setHgrow(Priority.ALWAYS);
gridPane.getColumnConstraints().addAll(columnOneConstraints, columnTwoConstrains);
return gridPane;
// Add Header
gridPane.add(headerLabel, 0,0,2,1);
GridPane.setHalignment(headerLabel, HPos.CENTER);
GridPane.setMargin(headerLabel, new Insets(20, 0,20,0));
gridPane.add(nameLabel, 0,1);
nameField.setPrefHeight(40);
gridPane.add(nameField, 1,1);
gridPane.add(emailLabel, 0, 2);
emailField.setPrefHeight(40);
gridPane.add(emailField, 1, 2);
gridPane.add(passwordLabel, 0, 3);
// Add Password Field
passwordField.setPrefHeight(40);
gridPane.add(passwordField, 1, 3);
submitButton.setPrefHeight(40);
submitButton.setDefaultButton(true);
submitButton.setPrefWidth(100);
gridPane.add(submitButton, 0, 4, 2, 1);
GridPane.setHalignment(submitButton, HPos.CENTER);
submitButton.setOnAction(new EventHandler<ActionEvent>() {
@Override
if(nameField.getText().isEmpty()) {
return;
if(emailField.getText().isEmpty()) {
showAlert(Alert.AlertType.ERROR, gridPane.getScene().getWindow(), "Form
Error!", "Please enter your email id");
return;
if(passwordField.getText().isEmpty()) {
return;
showAlert(Alert.AlertType.CONFIRMATION, gridPane.getScene().getWindow(),
"Registration Successful!", "Welcome " + nameField.getText());
});
alert.setTitle(title);
alert.setHeaderText(null);
alert.setContentText(message);
alert.initOwner(owner);
alert.show();
}
public static void main(String[] args) {
launch(args);
}
LIBRARY MANAGEMENT
package Library_Management;
import java.awt.Dimension;
import java.awt.Toolkit;
public Home() {
initComponents();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("Main Menu");
jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
jLabel2.setText("Welcome");
jLabel3.setText("to");
jLabel4.setText(" Library");
jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createLineB
order(new java.awt.Color(102, 255, 204)), "Action",
javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION,
javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 1, 24), new
java.awt.Color(255, 102, 102))); // NOI18N
btnrbook.setText("Return Book");
btnrbook.addActionListener(new java.awt.event.ActionListener() {
btnrbookActionPerformed(evt);
}
});
btnibook.setText("Issue Book");
btnibook.addActionListener(new java.awt.event.ActionListener() {
btnibookActionPerformed(evt);
jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createLineB
order(new java.awt.Color(102, 255, 102)), "Operation",
javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION,
javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 1, 24), new
java.awt.Color(0, 102, 204))); // NOI18N
btnstatistics.setText("Statistics");
btnstatistics.addActionListener(new java.awt.event.ActionListener() {
btnstatisticsActionPerformed(evt);
});
btnnewstudent.setText("New Student");
btnnewstudent.addActionListener(new java.awt.event.ActionListener() {
btnnewstudentActionPerformed(evt);
}
});
btnnewbook.setText("New Book");
btnnewbook.addActionListener(new java.awt.event.ActionListener() {
btnnewbookActionPerformed(evt);
});
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addContainerGap(62, Short.MAX_VALUE))
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addContainerGap(29, Short.MAX_VALUE))
);
jPanel4Layout.setVerticalGroup(
jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING,
jPanel4Layout.createSequentialGroup()
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(106, Short.MAX_VALUE))
);
jMenu1.setText("File");
miexit.setText("Exit");
miexit.addActionListener(new java.awt.event.ActionListener() {
miexitActionPerformed(evt);
});
jMenu1.add(miexit);
milogout.setText("Logout");
milogout.addActionListener(new java.awt.event.ActionListener() {
milogoutActionPerformed(evt);
});
jMenu1.add(milogout);
jMenuBar1.add(jMenu1);
jMenu2.setText("Edit");
miabout.setText("About");
jMenu2.add(miabout);
jMenuBar1.add(jMenu2);
setJMenuBar(jMenuBar1);
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jPanel4, javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGroup(layout.createSequentialGroup()
.addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
.addComponent(jLabel2)
.addComponent(jLabel3)
.addComponent(jLabel4))
.addContainerGap())))
);
DATABASE
package DAO;
import java.sql.*;
try {
Class.forName("com.mysql.jdbc.Driver");
Connection con =
DriverManager.getConnection("jdbc:mysql://localhost:3306/library_management","root","");
return con;
} catch (Exception e) {
System.err.println("Connection error");
return null;
}}