Documente Academic
Documente Profesional
Documente Cultură
Experiment 1
Aim: Write a program of Binary Search
Code:
import java.util.Scanner;
class BinarySearch
{
public static void main(String args[])
{
int c, first, last, middle, n, search, array[];
Scanner in = new Scanner(System.in);
System.out.println("Enter number of elements");
n = in.nextInt();
array = new int[n];
System.out.println("Enter " + n + " integers");
13113027
Output:
13113027
Experiment 2
Aim: Write a program of Linear Search
Code:
import java.util.Scanner;
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];
System.out.println("Enter " + n + " integers");
for (c = 0; c < n; c++)
array[c] = in.nextInt();
System.out.println("Enter value to find");
search = in.nextInt();
for (c = 0; c < n; c++)
{
if (array[c] == search) /* Searching element is present */
{
System.out.println(search + " is present at location " + (c + 1) + ".");
break;
}
}
if (c == n) /* Searching element is absent */
System.out.println(search + " is not present in array.");
}
}
13113027
Output:
13113027
Experiment 3
Aim: Write a program of Quick Sort
Code:
import java.util.Scanner;
while (i <= j)
{
while (arr[i] < pivot)
i++;
while (arr[j] > pivot)
j--;
if (i <= j)
{
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
i++;
j--;
}
}
13113027
13113027
Output:
13113027
Experiment 4
Aim: Write a program of Heap Sort
Code:
public class HeapSort
{
public void sort(int arr[])
{
int n = arr.length;
heapify(arr, i, 0);
}
}
13113027
if (largest != i)
{
int swap = arr[i];
arr[i] = arr[largest];
arr[largest] = swap;
heapify(arr, n, largest);
}
}
static void printArray(int arr[])
{
int n = arr.length;
for (int i=0; i<n; ++i)
System.out.print(arr[i]+" ");
System.out.println();
}
13113027
Output:
10
13113027
Experiment 5
Aim: Write a program of Merge Sort
Code:
import java.util.Scanner;
public class MergeSort
{
public static void sort(int[] a, int low, int high)
{
int N = high - low;
if (N <= 1)
return;
int mid = low + N/2;
sort(a, low, mid);
sort(a, mid, high);
int[] temp = new int[N];
int i = low, j = mid;
for (int k = 0; k < N; k++)
{
if (i == mid)
temp[k] = a[j++];
else if (j == high)
temp[k] = a[i++];
else if (a[j]<a[i])
temp[k] = a[j++];
else
temp[k] = a[i++];
}
for (int k = 0; k < N; k++)
a[low + k] = temp[k];
}
public static void main(String[] args)
{
Scanner scan = new Scanner( System.in );
System.out.println("Merge Sort Test\n");
int n, i;
System.out.println("Enter number of integer elements");
n = scan.nextInt();
int arr[] = new int[ n ];
System.out.println("\nEnter "+ n +" integer elements");
for (i = 0; i < n; i++)
arr[i] = scan.nextInt();
sort(arr, 0, n);
System.out.println("\nElements after sorting ");
for (i = 0; i < n; i++)
System.out.print(arr[i]+" ");
System.out.println();
}
}
11
13113027
Output:
12
13113027
Experiment 6
Aim: Write a program of Text Editor
Code:
import javax.swing.*;
import javax.swing.text.*;
import java.awt.*;
import java.io.*;
import java.awt.event.*;
import java.util.Hashtable;
public class SimpleEditor extends JFrame {
private Action openAction = new OpenAction();
private Action saveAction = new SaveAction();
private JTextComponent textComp;
private Hashtable actionHash = new Hashtable();
public static void main(String[] args) {
SimpleEditor editor = new SimpleEditor();
editor.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
editor.setVisible(true);
}
public SimpleEditor() {
super("Swing Editor");
textComp = createTextComponent();
makeActionsPretty();
Container content = getContentPane();
content.add(textComp, BorderLayout.CENTER);
content.add(createToolBar(), BorderLayout.NORTH);
setJMenuBar(createMenuBar());
setSize(320, 240);
}
protected JTextComponent createTextComponent() {
JTextArea ta = new JTextArea();
ta.setLineWrap(true);
return ta;
}
protected void makeActionsPretty() {
Action a;
a = textComp.getActionMap().get(DefaultEditorKit.cutAction);
a.putValue(Action.SMALL_ICON, new ImageIcon("icons/cut.gif"));
a.putValue(Action.NAME, "Cut");
a = textComp.getActionMap().get(DefaultEditorKit.copyAction);
a.putValue(Action.SMALL_ICON, new ImageIcon("icons/copy.gif"));
a.putValue(Action.NAME, "Copy");
a = textComp.getActionMap().get(DefaultEditorKit.pasteAction);
a.putValue(Action.SMALL_ICON, new ImageIcon("icons/paste.gif"));
a.putValue(Action.NAME, "Paste");
a = textComp.getActionMap().get(DefaultEditorKit.selectAllAction);
13
13113027
14
13113027
15
13113027
Output:
16
13113027
17
13113027
Experiment 7
Aim: Write a program of Syntax Checker
Code:
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Formatter;
import java.util.List;
import java.util.Locale;
import javax.tools.Diagnostic;
import javax.tools.DiagnosticCollector;
import javax.tools.JavaCompiler;
import javax.tools.JavaFileObject;
import javax.tools.StandardJavaFileManager;
import javax.tools.ToolProvider;
18
13113027
return messages;
}
}
File to Check
public class HelloBuggyWorld {
String test // missing a semicolon
Output:
19
13113027
Experiment 8
Aim: Write a program of Scheduling algorithm
Code:
import java.io. * ;
class fcfs {
public static void main(String args[]) throws Exception {
int n,
AT[],
BT[],
WT[],
TAT[];
float AWT = 0;
InputStreamReader isr = new InputStreamReader(System. in );
BufferedReader br = new BufferedReader(isr);
System.out.println("Enter no of process");
n = Integer.parseInt(br.readLine());
BT = new int[n];
WT = new int[n];
TAT = new int[n];
AT = new int[n];
System.out.println("Enter Burst time for each process\n");
for (int i = 0; i < n; i++) {
System.out.println("Enter BT for process " + (i + 1));
BT[i] = Integer.parseInt(br.readLine());
}
for (int i = 0; i < n; i++) {
System.out.println("Enter AT for process" + i);
AT[i] = Integer.parseInt(br.readLine());
}
WT[0] = 0;
for (int i = 1; i < n; i++) {
WT[i] = WT[i - 1] + BT[i - 1];
WT[i] = WT[i] - AT[i];
}
for (int i = 0; i < n; i++) {
TAT[i] = WT[i] + BT[i];
AWT = AWT + WT[i];
}
System.out.println(" PROCESS BT WT TAT ");
for (int i = 0; i < n; i++) {
System.out.println(" " + i + " " + BT[i] + " " + WT[i] +
" " + TAT[i]);
}
AWT = AWT / n;
System.out.println("Avg waiting time=" + AWT + "\n");
}
}
20
13113027
Output:
21
13113027
Experiment 9
Aim: Write a program of Round Robin
Code:
import java.io.*;
import java.util.*;
class Round {
public static void main(String args[]) throws IOException {
Scanner scan=new Scanner(System.in);
int i,
j,
k,
q,
sum = 0;
System.out.println("Enter number of process:");
int n = scan.nextInt();
int bt[] = new int[n];
int wt[] = new int[n];
int tat[] = new int[n];
int a[] = new int[n];
System.out.println("Enter brust Time:");
for (i = 0; i < n; i++) {
System.out.println("Enter brust Time for " + (i + 1));
bt[i] = scan.nextInt();
}
System.out.println("Enter Time quantum:");
q = scan.nextInt();
for (i = 0; i < n; i++)
a[i] = bt[i];
for (i = 0; i < n; i++) {
wt[i] = 0;
}
do {
for (i = 0; i < n; i++) {
if (bt[i] > q) {
bt[i] -= q;
for (j = 0; j < n; j++) {
if ((j != i) && (bt[j] != 0)) wt[j] += q;
}
} else {
for (j = 0; j < n; j++) {
if ((j != i) && (bt[j] != 0)) wt[j] += bt[i];
}
bt[i] = 0;
}
}
sum = 0;
for (k = 0; k < n; k++)
sum = sum + bt[k];
}
while ( sum != 0 );
for (i = 0; i < n; i++) {
tat[i] = wt[i] + a[i];
22
13113027
Output:
23
13113027
24
13113027
Experiment 10
Aim: Write a program of STG
Code:
import java.util. * ;
class STG {
public static void main(String args[]) {
Scanner sc = new Scanner(System. in );
int n,
BT[],
WT[],
TAT[];
System.out.println("Enter no of process");
n = sc.nextInt();
BT = new int[n + 1];
WT = new int[n + 1];
TAT = new int[n + 1];
float AWT = 0;
System.out.println("Enter Burst time for each process");
for (int i = 0; i < n; i++) {
System.out.println("Enter BT for process " + (i + 1));
BT[i] = sc.nextInt();
}
for (int i = 0; i < n; i++) {
WT[i] = 0;
TAT[i] = 0;
}
int temp;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n - 1; j++) {
if (BT[j] > BT[j + 1]) {
temp = BT[j];
BT[j] = BT[j + 1];
BT[j + 1] = temp;
temp = WT[j];
WT[j] = WT[j + 1];
WT[j + 1] = temp;
}
}
}
for (int i = 0; i < n; i++) {
TAT[i] = BT[i] + WT[i];
WT[i + 1] = TAT[i];
}
TAT[n] = WT[n] + BT[n];
System.out.println(" PROCESS BT WT TAT ");
for (int i = 0; i < n; i++)
System.out.println(" " + i + " " + BT[i] + " " + WT[i] + " " + TAT[i]);
for (int j = 0; j < n; j++)
AWT += WT[j];
25
13113027
Output:
26
13113027
Experiment 11
Aim: Write a program of 2 pass assembler
Code:
import java.util.*;
import java.io.*;
class Tuple {
String mnemonic, bin_opcode, type;
int length;
Tuple() {}
class SymTuple {
String symbol, ra;
int value, length;
class LitTuple {
27
13113027
LitTuple() {}
class TwoPassAssembler {
static int lc;
static List<Tuple> mot;
static List<String> pot;
static List<SymTuple> symtable;
static List<LitTuple> littable;
static List<Integer> lclist;
static Map<Integer, Integer> basetable;
static PrintWriter out_pass2;
static PrintWriter out_pass1;
static int line_no;
28
13113027
for(SymTuple i : symtable) {
output = i.symbol;
for(j=i.symbol.length() ; j < 10 ; j++) {
output += " ";
}
output += i.value;
for(j=new Integer(i.value).toString().length() ; j < 7 ; j++) {
output += " ";
}
output += i.length + "
" + i.ra;
System.out.println(output);
out_symtable.println(output);
}
29
13113027
" + i.ra;
System.out.println(output);
out_littable.println(output);
}
}
30
13113027
if(s.length == 3) {
i = 1;
}
s = tokenizeOperands(s);
if(s[i].equalsIgnoreCase("DS") || s[i].equalsIgnoreCase("DC")) {
potval = 1;
}
if(s[i].equalsIgnoreCase("EQU")) {
potval = 2;
}
if(s[i].equalsIgnoreCase("START")) {
potval = 3;
}
if(s[i].equalsIgnoreCase("LTORG")) {
potval = 4;
}
if(s[i].equalsIgnoreCase("END")) {
potval = 5;
}
31
13113027
case 2:
// EQU statement
if(!s[2].equals("*")) {
symtable.add(new SymTuple(s[0], Integer.parseInt(s[2]), 1, "A"));
} else {
symtable.add(new SymTuple(s[0], lc, 1, "R"));
}
return true;
case 3:
// START statement
symtable.add(new SymTuple(s[0], Integer.parseInt(s[2]), 1, "R"));
return true;
32
13113027
case 4:
// LTORG statement
ltorg(false);
return true;
case 5:
// END statement
ltorg(true);
return true;
}
return false;
}
33
13113027
34
13113027
if(s.length == 3) {
i = 1;
}
if(Collections.binarySearch(pot, s[i]) >= 0) {
if(s[i].equalsIgnoreCase("USING")) {
s = tokenizeOperands(s);
if(s[i+1].equals("*")) {
s[i+1] = lclist.get(line_no) + "";
} else {
for(int j=i+1 ; j<s.length ; j++) {
int value = getSymbolValue(s[j]);
if(value != -1) {
s[j] = value + "";
}
}
}
basetable.put(new Integer(s[i+2].trim()), new Integer(s[i+1].trim()));
}
return true;
}
return false;
}
if(s.length == 3) {
i = 1;
}
s = tokenizeOperands(s);
35
13113027
for(Tuple x : mot) {
if(s[i].equals(x.mnemonic)) {
t = x;
break;
}
}
36
13113027
37
13113027
38
13113027
39
13113027
40
13113027