0% found this document useful (0 votes)
33 views37 pages

KUTTY

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)
33 views37 pages

KUTTY

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/ 37

PROGRAM:

public class sequentialSearch


{
public static void main(String args[])
{
int[] One={2,9,6,7,4,5,3,0,1};
int target = 4 ;
sequentialSearch(One , target);
}
public static void sequentialSearch(int [] a,int b){ int index = -1;
for(int i=0;i<a.length;i++)
{
if(a[i]==b)
{
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 \t"+index+" \t of thearray");}
}
}
OUTPUT:
Your target integer is in index 4 of the array
PROGRAM:

public class BinarySearch {


public static int binarySearch(int arr[],int first,int last,int key) {
if (last>=first){
int mid = first +(last - first)/2;
if(arr[mid]==key){
return mid;
}
if(arr[mid]> key){
return binarySearch(arr,first,mid-1,key);//search in left subarray}
else{
return binarySearch(arr,mid+1,last,key);//search in right subarray}
}
return -1;
}
public static void main(String args[]){
int arr[]={10,20,30,40,50};
int key=30;
int last= arr.length-1;
int result = binarySearch(arr,0, last ,key);
if(result == -1)
System.out.println("Element is not fount !");
else
System.out.println("Element is found at index :"+result);

}
}
OUTPUT :

Element is found at index :2


PROGRAM:
public class SelectionSort {
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;
}
}
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(int
i:arr1){
System.out.println(i+" ");
}
System.out.println();
selectionSort(arr1);
System.out.println("After selection Sort"); for(int
i:arr1){
System.out.println(i+" ");
}
}
}
OUTPUT:

Before Selection sort 9


14
3
2
43
11
58
22

After selection Sort 2


3
9
11
14
22
43
58
PROGRAM:
public class InsertionSort {
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 args[]){
int[]arr1={9,14,3,2,43,11,58,22};
System.out.println("Before Insertion sort"); for (int i
:arr1){
System.out.println(i+" ");
}
System.out.println();
insertionSort(arr1);

// sorting array using insertion sort

System.out.println("After Insertionsort"); for(int


i:arr1){
System.out.println(i+" ");
}
}
}
OUTPUT :

Before Insertion sort 9


14
3
2
43
11
58
22

After Insertion sort 2


3
9
11
14
22
43
58
PROGRAM:

class Stack{
private int arr[];
private int top;
private int capacity;

// Construct to initialize the stack


Stack(int size){
arr=new int[size];
capacity=size;
top=-1;
}

// Utility function to add an element 'x ' and to the stack public void
push(int x){
if(isFull()){
System.out.println("overflow \n program Terminated \n"); System.exit(-
1);
}
System.out.println("Inserting"+x);
arr[++top]=x;
}

//Utility function to pop element from the stack public int pop(){
if(isEmpty()){
System.out.println("Underflow \n ProgramTerminated");
System.exit(-1);
}
System.out.println("Removing "+peek());
return arr[top--];
}
public int peek(){
if(!isEmpty()){
return arr[top];
}
else{
System.exit(-1);
}
return -1;
}
public int size() {
return top + 1;
}

public boolean isEmpty(){


return top ==-1;
}
public boolean isFull() {
return top == capacity -1;
}
}
class Main{
public static void main(String args[]){ Stack
stack=new Stack(3);
stack.push(1);
stack.push(2);
stack.pop();
stack.pop();

stack.push(3);
System.out.println("The top element is" +stack.peek());
System.out.println("The stack size is" + stack.size());

stack.pop();
if (stack.isEmpty()){
System.out.println("The stack size is empty"); }
else{
System.out.println("The stack is not empty"); }
}
}
OUTPUT:

Inserting1

Inserting2

Removing 2

Removing 1

Inserting3

The top element is3

The stack size is1

Removing 3

The stack size is empty


PROGRAM:
public class DemoQueue{
private int maxSize;
private int[] queueArray;
private int front;
private int rear;
private int currentSize;
public DemoQueue(int size){
this.maxSize=size;
this.queueArray = new int [size]; front=0;
rear=-1;
currentSize=0;
}

public void insert (int item){

if(isQueueFull()){
System.out.println("Queue is full !");
return ;
}
if (rear==maxSize-1){
rear = -1;
}
queueArray[++rear]=item;
currentSize++;
System.out.println("Item added to queue:"+item); }
public int delete(){

if(isQueueEmpty()){
throw new RuntimeException("Queue is empty"); }
int temp= queueArray[front++];
if(front==maxSize){
front=0;
}
currentSize--;
return temp;
}

public int peek(){


return queueArray[front];
}
public boolean isQueueFull(){
return(maxSize==currentSize);
}
public boolean isQueueEmpty(){
return (currentSize==0);
}
public static void main(String args[]){
DemoQueue queue=new DemoQueue(10);
queue.insert(2);
queue.insert(3);
System.out.println("Item deleted from queue:"+queue.delete());
System.out.println("Item deleted from queue:"+queue.delete());
queue.insert(5);
System.out.println("Item deleted from queue : "+queue.delete()); }
}
OUTPUT:

Item added to queue:2


Item added to queue:3
Item deleted from queue:2 Item deleted from
queue:3 Item added to queue:5
Item deleted from queue : 5
PROGRAM:

import java.util.Scanner;

class Employee {
int Emp_id, Mobile_no;
String Emp_name, address, Mail_id;
Scanner get = new Scanner(System.in);
Employee() {
System.out.println("Enter Name of the Employee:");
Emp_name = get.nextLine();
System.out.println("Enter Mail id of the Employee:");
Mail_id = get.nextLine();
System.out.println("Enter Address of the Employee:");
address = get.nextLine();
System.out.println("Enter Emp_ id:");
Emp_id = get.nextInt();
System.out.println("Enter Mobile no:");
Mobile_no = get.nextInt();
}

void display() {
System.out.println("Employee ld: " + Emp_id);
System.out.println("Employee Name:" + Emp_name);
System.out.println("Phone Number: " + Mobile_no);
System.out.println("Mail ld:" + Mail_id);
System.out.println("Address: " + address); }
}

class Programmer extends Employee {


float bp, salary, hra, da, swf, pf;
Programmer() {
System.out.println("Enter Basic Pay:");
bp = get.nextFloat();
hra = bp * 10 / 100;
da = bp * 97 / 100;
pf = bp * 12 / 100;
swf = (float) (bp * 0.1 / 100);

salary = bp + da + hra - pf - swf;


}

void display() {
System.out.println("Programmer Details" + "\n"); super.display();
System.out.println("Basic Pay: " + bp);
System.out.println("HRA: " + hra);
System.out.println("DA: " + da);
System.out.println("Staff welfare amount:" + swf);
System.out.println("PF: *" + pf);
System.out.println("Salary:" + salary);
}
}

class AssistProf extends Employee {


float bp, salary, hra, da, pf;
double swf;
AssistProf() {
System.out.println("Enter the basic pay:");
bp = get.nextFloat();
hra = bp * 10 / 100;
da = bp * 97 / 100;
pf = bp * 12 / 100;
swf = bp * 0.1 / 100;

salary = (float) (bp + da + hra - pf - swf);


}

void display() {
System.out.println("Assistant Processor Details " + "\n"); super.display();
System.out.println("Basic Payy:" + bp);
System.out.println("HRA: " + hra);
System.out.println("DA:" + da);
System.out.println("Staff welfare amount: " + swf);
System.out.println("P F: " + pf);
System.out.println("Salary: " + salary);
}
}

class AssoProf extends Employee {


float bp, salary, hra, da, swf, pf;

AssoProf() {
System.out.println("Enter Basic Pay:");
bp = get.nextFloat();
hra = bp * 10 / 100;
da = bp * 97 / 100;
pf = bp * 12 / 100;
swf = (float) (bp * 0.1 / 100);
salary = bp + da + hra - pf - swf;
}

void display() {
System.out.println("Associate Professor Details'" + "\n"); super.display();
System.out.println("Basic Pay:" + bp);
System.out.println("HRA: " + hra);
System.out.println("DA:" + da);
System.out.println("Staff welfare amount:" + swf);
System.out.println("PF:" + pf);
System.out.println("Salary:" + salary);
}
}

class Professor extends Employee {


float bp, salary, hra, da, swf, pf;

Professor() {
System.out.println("Enter Basic Pay:");
bp = get.nextFloat();
hra = bp * 10 / 100;
da = bp * 97 / 100;
pf = bp * 12 / 100;
swf = (float) (bp * 0.1 / 100);
salary = bp + da + hra - pf - swf;
}

void display() {
System.out.println("Professor Details" + "\n"); super.display();
System.out.println("Basic Pay: " + bp);
System.out.println("HRA: " + hra);
System.out.println("DA: " + da);
System.out.println("Staff welfare amount:" + swf);
System.out.println("P F: " + pf);
System.out.println("Salary:" + salary);
}
}
class Employees {
public static void main(String args[]) {
System.out.println("Enter Programmer Details" + "\n"); Programmer obj1 =
new Programmer();
obj1.display();
AssistProf obj2 = new AssistProf();
obj2.display();
AssoProf obj3 = new AssoProf();
obj3.display();
Professor obj4 = new Professor();
obj4.display();
}
}
OUTPUT:

Enter Programmer Details

Enter Name of the Employee:


JASMINE
Enter Mail id of the Employee:
[email protected] Enter Address of the Employee:
vanniyambadi
Enter Emp_ id:
53
Enter Mobile no:
1234567890
Enter Basic Pay:
100000

Programmer Details

Employee ld: 53
Employee Name: JASMINE
Phone Number: 1234567890
Mail ld: [email protected] Address: vanniyambadi
Basic Pay: 100000.0
HRA: 10000.0
DA: 97000.0
Staff welfare amount:100.0
PF: *12000.0
Salary:194900.0
PROGRAM:

abstract class shape{


int a=3,b=4;
abstract public void print_area();
}
class rectangle extends shape{
public int area_rect;
public void print_area(){
area_rect=a*b;
System.out.println("The area of rectangle is :"+area_rect); }
}
class triangle extends shape{
int area_tri;
public void print_area(){
area_tri=(int)(0.5*a*b);
System.out.println("The area of triangle is:"+area_tri); }
}
class circle extends shape{
int area_circle;
public void print_area(){
area_circle=(int)(3.14*a*a);
System.out.println("The area of circle is "+area_circle); }
}
public class Main{
public static void main(String[] args) {
rectangle r=new rectangle();
r.print_area();
triangle t=new triangle();
t.print_area();
circle r1=new circle();
r1.print_area();
}
}
OUTPUT:

The area of rectangle is :12 The area


of triangle is:6 The area of circle is 28
PROGRAM:

interface Shape {
void input();

void area();
}
class Circle implements Shape {
int r = 0;
double pi = 3.14, ar = 0;

public void input() {


r = 5;
}

public void area() {


ar = pi * r * r;
System.out.println("Area of circle:" + ar); }
}
class Rectangle extends Circle {
int l = 0, b = 0;
double ar;

public void input() {


super.input();
l = 6;
b = 4;
}

public void area() {


super.area();
ar = l * b;
System.out.println("Area of rectangle:" + ar); }
}
public class Demo {
public static void main(String[] args) {
Rectangle obj = new Rectangle();
obj.input();
obj.area();
}
}
OUTPUT :
Area of circle:78.5
Area of rectangle:24.0
PROGRAM:
public class JavaExceptionExample { public static void

main(String args[]) {

try
{

//code that may raise exception

int data = 100 / 0;


} catch (ArithmeticException e) {

System.out.println(e);

//rest code of the program System.out.println("rest of the

code...");

}
OUTPUT:
java.lang.ArithmeticException: / by zero

rest of the code…


PROGRAM:
class MyException extends Exception { public

MyException(String s) {

super(s);
}

public class Main {

public static void main(String args[]) {

try
{

throw new MyException("exception occurred"); }

catch (MyException ex) {

System.out.println("Caught");

System.out.println(ex.getMessage());

}
OUTPUT:
Caught
exception occurred
PROGRAM:

class RandomGenThread implements Runnable { double num;

public void run() {


try {
SquareThread sqt = new SquareThread();
Thread squareThread = new Thread(sqt);
CubeThread cbt = new CubeThread();
Thread cubeThread = new Thread(cbt);
squareThread.start();
cubeThread.start();
for (int i = 0; i < 10; i++) {
System.out.println("t1-" + i);
if (i % 2 == 0) {
sqt.setNum((double) i);
}
else {
cbt.setNum((double) i);
}
Thread.sleep(1000);
}
}
catch (InterruptedException e) {
e.printStackTrace();
}
}
}
class SquareThread implements Runnable {
Double num;

public void run() {


try {
int i = 0;
do {
i++;
if (num != null && num % 2 == 0) {
System.out.println("t2--->square of " + num + "=" + (num* num)); num = null;
}
Thread.sleep(1000);
} while (i <= 5);
}
catch (Exception e) {
e.printStackTrace();

}
}
public Double getNum() {
return num;
}

public void setNum(Double num) {


this.num = num;
}
}
class CubeThread implements Runnable {
Double num;

public void run() {


try {
int i = 0;
do {
i++;
if (num != null && num % 2 != 0) {
System.out.println("t3--->Cube of " + num + "=" + (num* num*num));num
= null;
}
Thread.sleep(1000);
}
while (i <= 5);
}
catch (Exception e) {
e.printStackTrace();
}
}

public Double getNum() {


return num;
}

public void setNum(Double num) {


this.num = num;
}
}
public class Main {
public static void main(String[] args) throws InterruptedException{Thread
randomThread = new Thread(new RandomGenThread());
randomThread.start();
}
}
OUTPUT:

t1-0
t1-1
t2--->square of 0.0=0.0 t3---
>Cube of 1.0=1.0 t1-2
t1-3
t2--->square of 2.0=4.0 t3---
>Cube of 3.0=27.0 t1-4
t1-5
t2--->square of 4.0=16.0 t1-6
t1-7
t1-8
PROGRAM:

import java.io. *;
public class FileInfo
{
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new
InputStreamReader(System.in));System.out.print(" \nEnter A File Name:");
String fName = br.readLine();
File f = new File(fName);

String result = f.exists() ? " exists." : "does not exist.";


System.out.println("\nThe given file " + result); if (f.exists()) {

System.out.println("The given file is " + result); System.out.println("The


given file is" + result); System.out.println("The given file length is f.length()*
in bytes."); if (fName.endsWith(".jpg") || fName.endsWith(".gif") ||
fName.endsWith(".png"))
{
System.out.println("The given file is an image file.");

}
else if (fName.endsWith(".exe")) {
System.out.println("The given file is an executable file."); }
else if (fName.endsWith(".txt")) {
System.out.println("The given file is a text file.");
} else
{

System.out.println("The file type is unknown.");


}
}
}
}
OUTPUT:

Enter A File Name:

simple.text

The given file does not exist.


PROGRAM:

public class Area<T> {

//T is the Datatype like string,


private void add(T t) {
this.t=t;
}
public T get() {

return t;

public void getArea() {}

public static void main(String args[]) {

//object of generics class Area with parameter Type of aninteger

Area<Integer> rectangle = new Area <Integer> (); //Object of generics class Area

with Parameter Type as DoubleArea<Double> circle=new Area <Double> ();

rectangle.add(10);

circle.add(2.5);
System.out.println(rectangle.get());
System.out.println(circle.get());
}

}
OUTPUT:
10
2.5
PROGRAM:

import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.FlowPane;
import javafx.stage.Stage;

public class Main extends Application {

@Override
public void start(Stage stage) {
stage.setTitle("HTML");
stage.setWidth(500);
stage.setHeight(500);
Scene scene = new Scene(new Group());

FlowPane flow = new FlowPane();


flow.setVgap(8);
flow.setHgap(4);
flow.setPrefWrapLength(300); // preferred width = 300 for (int i = 0; i < 10;
i++) {
flow.getChildren().add(new Button("asdf"));
}
scene.setRoot(flow);

stage.setScene(scene);
stage.show();
}

public static void main(String[] args) {


launch(args);
}
}
OUTPUT:
MENU:

package application;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;
public class MenuExample extends Application { public static
void main(String[] args) {
launch(args);
}

@Override
public void start(Stage primaryStage) throws Exception { // TODO Auto-
generated method stub
BorderPane root = new BorderPane();
Scene scene = new Scene(root,200,300);
MenuBar menubar = new MenuBar();
Menu FileMenu = new Menu("File");
MenuItem filemenu1=new MenuItem("new");
MenuItem filemenu2=new MenuItem("Save");
MenuItem filemenu3=new MenuItem("Exit");
Menu EditMenu=new Menu("Edit");
MenuItem EditMenu1=new MenuItem("Cut");
MenuItem EditMenu2=new MenuItem("Copy");
MenuItem EditMenu3=new MenuItem("Paste");
EditMenu.getItems().addAll(EditMenu1,EditMenu2,EditMenu3);
root.setTop(menubar);
FileMenu.getItems().addAll(filemenu1,filemenu2,filemenu3);
menubar.getMenus().addAll(FileMenu,EditMenu);
primaryStage.setScene(scene);
primaryStage.show();

}
}
OUTPUT:

You might also like