Exp11 15

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 12

Exp:11

import java.util.*;

import java.io.*;

public class Hashtbl

public static void main(String[] args)

try

FileInputStream fs = new FileInputStream("D:\\table.txt");

Scanner sc = new Scanner(fs).useDelimiter("\\s+");

Hashtable<String, String> ht = new Hashtable<String, String>();

String[] arrayList;

String a;

System.out.println("Welcome to St. Martin's Engineering College");

System.out.println("HASH TABLE IS");

System.out.println(" ------------------------- ");

System.out.println("KEY : VALUE");

while (sc.hasNext())

a = sc.nextLine();

arrayList = a.split("\\s+");

ht.put(arrayList[0], arrayList[1]);

System.out.println(arrayList[0] + ":" + arrayList[1]);

}
System.out.println("Welcome to St. Martin's EngineeringTO KG REDDY Eng College");

System.out.println("----MENU ----- ");

System.out.println("----1.Search by Name ----- ");

System.out.println("----2.Search by Mobile ----- ");

System.out.println("----3.Exit ---- ");

String opt = ""; String name, mobile;

Scanner s = new Scanner(System.in);

while (opt != "3")

System.out.println("Enter Your Option 1,2,3");

opt = s.next();

switch (opt)

case "1":

System.out.println("Enter Name");

name = s.next();

if (ht.containsKey(name))

System.out.println("Mobile is " + ht.get(name));

else

System.out.println("Not Found");

}
}

break;

case "2":

System.out.println("Enter mobile");

mobile = s.next();

if (ht.containsValue(mobile))

for (Map.Entry e : ht.entrySet())

if (mobile.equals(e.getValue()))

System.out.println("Name is " + e.getKey());

else

System.out.println("Not Found");

break;

case "3":

opt = "3";
System.out.println("Menu Successfully Exited");

break;

default:

System.out.println("Choose Option betwen 1 and Three");

break;

} catch (Exception ex)

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

Exp:12
class Q

int n;

boolean valueSet=false; synchronized int get()

if(!valueSet) try

wait();

}
catch(InterruptedException e)

System.out.println("Interrupted Exception caught");

System.out.println("Got:"+n); valueSet=false;

notify(); return n;

synchronized void put(int n)

if(valueSet) try

wait();

catch(InterruptedException e)

System.out.println("Interrupted Exception caught");

this.n=n; valueSet=true;

System.out.println("Put:"+n); notify();

class Producer implements Runnable

Q q; Producer(Q q)
{

this.q=q;

new Thread(this,"Producer").start();

public void run()

int i=0; while(true)

q.put(i++);

class Consumer implements Runnable

Q q; Consumer(Q q)

this.q=q;

new Thread(this,"Consumer").start();

public void run()

while(true)

q.get();

}
}

class ProdCons

public static void main(String[] args)

Q q=new Q(); new Producer(q); new Consumer(q);

System.out.println("Press Control-c to stop");

Exp13
// import statement

import java.io.File;

public class DisplayFileExample

public void printFileNames(File[] a, int i, int lvl)

// base case of the recursion

// i == a.length means the directory has

// no more files. Hence, the recursion has to stop

if(i == a.length)

return;

// checking if the encountered object is a file or not


if(a[i].isFile())

System.out.println(a[i].getName());

// recursively printing files from the directory

// i + 1 means look for the next file

printFileNames(a, i + 1, lvl);

// Main Method

public static void main(String[] argvs)

// Providing the full path for the directory

String path = ("D:\\h");

// creating a file object

File fObj = new File(path);

// creating on object of the class DisplayFileExample

DisplayFileExample obj = new DisplayFileExample();

if(fObj.exists() && fObj.isDirectory())

// array for the files of the directory pointed by fObj

File a[] = fObj.listFiles();

// display statements

System.out.println("= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =");

System.out.println("Displaying Files from the directory : " + fObj);

System.out.println("= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =");
// Calling the method

obj.printFileNames(a, 0, 0);

Exp:14
import java.util.*;

class Main {

//partitions the array around pivot=> last element

static int partition(int numArray[], int low, int high) {

int pivot = numArray[high];

// smaller element index

int i = (low - 1);

for (int j = low; j <= high - 1; j++) {

// check if current element is less than or equal to pivot

if (numArray[j] <= pivot) {

i++;

// swap the elements

int temp = numArray[i];

numArray[i] = numArray[j];

numArray[j] = temp;

// swap numArray[i+1] and numArray[high] (or pivot)


int temp = numArray[i + 1];

numArray[i + 1] = numArray[high];

numArray[high] = temp;

return i + 1;

//sort the array using quickSort

static void quickSort(int numArray[], int low, int high)

//auxillary stack

int[] intStack = new int[high - low + 1];

// top of stack initialized to -1

int top = -1;

// push initial values of low and high to stack

intStack[++top] = low;

intStack[++top] = high;

// Keep popping from stack while is not empty

while (top >= 0) {

// Pop h and l

high = intStack[top--];

low = intStack[top--];
// Set pivot element at its correct position

// in sorted array

int pivot = partition(numArray, low, high);

// If there are elements on left side of pivot,

// then push left side to stack

if (pivot - 1 > low) {

intStack[++top] = low;

intStack[++top] = pivot - 1;

// If there are elements on right side of pivot,

// then push right side to stack

if (pivot + 1 < high) {

intStack[++top] = pivot + 1;

intStack[++top] = high;

public static void main(String args[])

//define array to be sorted

int numArray[] = { 3,2,6,-1,9,1,-6,10,5 };


int n = 8;

System.out.println("Original Array:" + Arrays.toString(numArray));

// call quickSort routine to sort the array

quickSort(numArray, 0, n - 1);

//print the sorted array

System.out.println("\nSorted Array:" + Arrays.toString(numArray));

You might also like