Pages

Saturday, October 29, 2016

Write a program that perform hashmap

Write a program that perform hashmap

Program/Input:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;

public class hashmap {

    public static void main(String args[]) {
        String s = "A quick brown fox jumps over the lazy dog.";
        HashMap<Character, Integer> map = new HashMap<>();
//        map[1] = a, 2;
//        map[2]=  b, 1;
//        map[3] = c, 4;
        char c = ' ';
        for (int i = 0; i < s.length(); i++) {
            c = s.charAt(i);
            if (map.containsKey(c)) {
                int cc = map.get(c);
                map.put(c, ++cc);
            } else {
                map.put(c, 1);
            }
        }
        Set ss = map.keySet();
        Iterator it = ss.iterator();
        while (it.hasNext()) {
            char ch = (char) it.next();
            int r = map.get(ch);
            System.out.println(ch + " appreas " + r + " time");
            //   System.out.println(it.next());   
        }
    }
}

Result:
A appreas 1 time
  appreas 8 time
a appreas 1 time
b appreas 1 time
c appreas 1 time
d appreas 1 time
e appreas 2 time
f appreas 1 time
g appreas 1 time
h appreas 1 time
i appreas 1 time
j appreas 1 time
k appreas 1 time
l appreas 1 time
m appreas 1 time
n appreas 1 time
. appreas 1 time
o appreas 4 time
p appreas 1 time
q appreas 1 time
r appreas 2 time
s appreas 1 time
t appreas 1 time
u appreas 2 time
v appreas 1 time
w appreas 1 time
x appreas 1 time
y appreas 1 time
z appreas 1 time

Program that generate first 10 prime number by using iterator

Write a program that program that generate first 10 prime number by using iterator

Program/Code:

Num Class -> Prime_Gen:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import java.util.ArrayList;
import java.util.Iterator;

public class num {

    public static Iterator iterator() {
        return new prime_Gen();
    }

    private static class prime_Gen implements Iterator<Object> {

        private final ArrayList<Integer> arr;
        private int p;

        public prime_Gen() {
            this.arr = new ArrayList<>();
            this.p = 2;
        }

        @Override
        public boolean hasNext() {
            return true;
        }

        @Override
        public Object next() {
            if (p == 2) {
                p = 3;
                arr.add(2);
                return 2;
            }
            for (int n = p; true; n += 2) {
                for (int i = 0; i < arr.size(); i++) {
                    int e1 = (int) arr.get(i);
                    if (n % e1 == 0) {
                        break;
                    }
                    if (e1 * e1 > n) {
                        arr.add(n);
                        p = n + 2;
                        return n;
                    }
                }
            }
        }

        @Override
        public void remove() {
            throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
        }
    }
}

Test class:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
package iterator;

import java.util.Iterator;

public class test {

    public static void main(String[] args) {
        Iterator it = num.iterator();
        for(int i = 0; i<10 && it.hasNext(); i++){
            System.out.println(it.next());
        }
    }
}

Result/Run:
2
3
5
7
11
13
17
19
23
29

Perform Exception in Employee class through Generics and by using an ArrayList.

Write a program that perform exception by programming the Employee class through Generics and by using an ArrayList.

Program / Input:

myException Class:
1
2
3
4
5
6
public class myException extends Exception {

    myException(String s) {
        super(s);
    }
}

Generic Class:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package Generic_or_ExceptionHandling;

import java.util.Comparator;

public class Generic_class<u, v> implements Comparator<Generic_class>, Comparable<Generic_class> {

    private u id;
    private v name;

    public Generic_class() {
    }

    public Generic_class(u id, v num) {
        this.id = id;
        this.name = num;
    }

    public u getId() {
        return id;
    }

    public void setId(u id) {
        this.id = id;
    }

    public v getNum() {
        return name;
    }

    public void setNum(v num) {
        this.name = num;
    }

    @Override
    //implements by Comparator<Generic_class>
    public int compare(Generic_class o1, Generic_class o2) {
        int a1 = (int) o1.id;
        int a2 = (int) o2.id;
        return a1 - a2;
    }

    // implements Comparable<Generic_class>
    @Override
    public int compareTo(Generic_class o) {
        String a1 = this.name.toString();
        String a2 = o.getNum().toString();
        int i = a1.compareTo(a2);
        return i;
    }
    
    public void checkType() throws myException {
    Boolean b = id instanceof Integer;
        if (b) {
            System.out.println("true!!! its string");
        } else {
            throw new myException("Its not String");
        }
    }
}

Test Class:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package Generic_or_ExceptionHandling;

import java.util.ArrayList;
import java.util.Collections;

public class test {

    public static void main(String[] args) throws myException {
        ArrayList<Generic_class<Integer, String>> arr = new ArrayList<>();
//        Generic_class<Integer, String> gc0 = new Generic_class<>(2, "Ahmed");
//        Generic_class<Integer, String> gc1 = new Generic_class<>(5, "Nida");
//        Generic_class<Integer, String> gc2 = new Generic_class<>(1, "Maria");
//        Generic_class<Integer, String> gc3 = new Generic_class<>(3, "Anaya");
//        Generic_class<Integer, String> gc4 = new Generic_class<>(4, "Ali");

        arr.add(new Generic_class<>(2, "Ahmed"));
        arr.add(new Generic_class<>(5, "Nida"));
        arr.add(new Generic_class<>(1, "Maria"));
        arr.add(new Generic_class<>(3, "Anaya"));
        arr.add(new Generic_class<>(4, "Ali"));

        // compareTo
        Collections.sort(arr);
        System.out.println("Name \t ID");
        for (int i = 0; i < arr.size(); i++) {
            System.out.println(arr.get(i).getNum() + "   " + arr.get(i).getId());
        }
        System.out.println();
        System.out.println();

        // compare()
        Collections.sort(arr, new Generic_class());
        for (int i = 0; i < arr.size(); i++) {
            System.out.println(arr.get(i).getNum() + "   " + arr.get(i).getId());
        }

        System.out.println("Check its generic class or not???");
        Generic_class g = new Generic_class(1, "1");
        g.checkType();

//        Generic_class obj = new Generic_class("sada", 44);
//       // ArrayList<Generic_class<String, Integer>> aw = new ArrayList<>();
//        try {
//            obj.CheckType1();
//        } catch (Exception e) {
//            System.out.println(e.getMessage());
//
//        }
    }
}

Result/Run:
Name   ID
Ahmed   2
Ali   4
Anaya   3
Maria   1
Nida   5


Maria   1
Ahmed   2
Anaya   3
Ali   4
Nida   5
Check its generic class or not???
true!!! its string

Object Sorting to sort by string or integer by implementing comparable, comparator.

Write a program that use object sorting to sort by string or integer by  implementing comparable, comparator.

Program/Code:

Employee Class:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import java.util.Comparator;

public class employee implements Comparable<employee>, Comparator<employee> {

    // instance variable
    private String name;
    private int Id;

    // constructor
    employee() {
    }

    public employee(String name, int Id) {
        this.name = name;
        this.Id = Id;
    }

    // getter & setter method
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getId() {
        return Id;
    }

    public void setId(int Id) {
        this.Id = Id;
    }

    // compare method 
    @Override
    public int compareTo(employee o) {
        int i;
        i = this.getName().compareTo(o.getName());
        return i;
    }

    @Override
    public int compare(employee o1, employee o2) {
        return o1.Id - o2.Id;
    }
}

EmployeeTest Class:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import java.util.ArrayList;
import java.util.Collections;

public class Main {

    public static void main(String args[]) {
        
        ArrayList<employee> arr = new ArrayList<>();
        arr.add(new employee("Ahmed", 5));
        arr.add(new employee("Akram", 3));
        arr.add(new employee("Shahid", 6));
        arr.add(new employee("Jamshad", 7));
        arr.add(new employee("Sabir", 9));

        Collections.sort(arr);
        System.out.println("Compare by String\n id ---- Name");
        for (int i = 0; i < arr.size(); i++) {
            System.out.println(arr.get(i).getId() + "    " + arr.get(i).getName());
        }

        Collections.sort(arr, new employee());
        System.out.println("\n\nCompare by int\n id ---- Name");
        for (int i = 0; i < arr.size(); i++) {
            System.out.println(arr.get(i).getId() + "    " + arr.get(i).getName());
        }
    }
}

Result:
Compare by String
 id ---- Name
5    Ahmed
3    Akram
7    Jamshad
9    Sabir
6    Shahid


Compare by int
 id ---- Name
3    Akram
5    Ahmed
6    Shahid
7    Jamshad
9    Sabir

Wednesday, June 15, 2016

Single Double Ended Link List / Singly Double Ended Link List

Write a program that demonstrate the use of Single Double Ended Link List / Singly Double Ended Link List


Link List Class:

1. Insert in the List
2. Delete in the List
3. Traverse in the List

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
public class Double_Ended_Single_Link_List {

    Node header;
    Node tail;

    public Double_Ended_Single_Link_List() {// constructor
        header = null;
    }

    public Node getHeader() {
        return header;
    }

    public Node getTail() {
        return tail;
    }

    public void insert_at_Beginning(int data) {
        Node newNode = new Node(data);
        if (header == null) {
            header = newNode;
            tail = newNode;
        } else {
            newNode.setNext(header);
            header = newNode;
        }
    }

    public void insert_at_Position(int data, int pos) {
        Node newNode = new Node(data);
        Node temp = header;
        pos--;
        while (pos > 0) {
            temp = temp.getNext();
            if (temp == null) {
                System.out.println("Not enough elements in the list.");
            } else {
                --pos;
            }
        }
        newNode.setNext(temp.getNext());
        temp.setNext(newNode);
        
    }

    public void insert_at_End(int data) {
        Node newNode = new Node(data);
        Node temp = header;
        if (header == null) {
            header = newNode;
            tail = newNode;
        } else {
            while (temp.getNext() != null) {
                temp = temp.getNext();
            }
            tail.setNext(newNode);
            tail = newNode;
        }
    }

    public int delete_from_Beginning() {
        if (isEmpty()) {
            System.out.println("Error: Invalid operation! List is Empty");
            return -1;
        } else if (header.getNext() == null) {
            header = null;
            tail = null;
            return 0;
        } else {
            header = header.getNext();
            return 0;
        }
    }

    public void delete_from_position(int pos) {
        if (isEmpty()) {
            System.out.println("Error: Invalid operation! List is Empty");
        } else {
            Node temp = header;
            pos--;
            while (--pos != 0) {
                temp = temp.getNext();
            }
            temp.setNext(temp.getNext().getNext());
        }
    }

    public void delete_from_End() {
        Node temp = header.getNext();
        Node prev = header;

        if (header == null) {
            System.out.println("Error: Invalid operation! List is Empty");
        } else {
            while (temp.getNext() != null) {

                temp = temp.getNext();
                prev = prev.getNext();
            }
            prev.setNext(null);
            temp = null;
        }
    }

    public void traverse() {
        Node temp = header;
        while (temp != null) {
            System.out.print(temp.getData() + " ");
            temp = temp.getNext();
        }
    }

    public boolean isEmpty() {
        return header == null;
    }}


Test Class:


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
 public class DE_SLL_Test_Class {

public static void main(String[] args) {
        Double_Ended_Single_Link_List ll = new Double_Ended_Single_Link_List();

        System.out.println("Insert At Begining!");
        ll.insert_at_Beginning(23);
        ll.insert_at_Beginning(56);
        ll.traverse();
        System.out.println("");

        System.out.println("\nInsert At End!");
        ll.insert_at_End(100);
        ll.insert_at_End(123);
        ll.traverse();
        System.out.println("");

        System.out.println("\nInsert At any Position!");
        ll.insert_at_Position(321, 3);
        ll.insert_at_Position(4444, 4);
        ll.traverse();
        System.out.println("");

        System.out.println("\nDelete At Begining!");
        ll.delete_from_Beginning();
        ll.traverse();
        System.out.println("");

        System.out.println("\nDelete At Postion!");
        ll.delete_from_position(3);
        ll.traverse();
        System.out.println("");

        System.out.println("\nDelete At End!");
        ll.delete_from_End();
        ll.traverse();
        System.out.println("");
    }
}

Tuesday, June 14, 2016

Single Link List / Singly Link List

Write a program that demonstrate the use of Single Link List / Singly Link List.

Node Class:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
public class Node {
    // instance variable
    public int data; // data item, can be of any type e.g. student, book etc.
    public Node next; // next link in list

    public int getData() {
        return data;
    }

    public void setData(int data) {
        this.data = data;
    }

    public Node getNext() {
        return next;
    }

    public void setNext(Node next) {
        this.next = next;
    }

    public Node(int id) // constructor
    {
        data = id; // initialize data
        next = null; // (if not initialized next is automatically set to null)
    }

    public void displayLink() // display the link
    {
        System.out.print("Data : " + data);
    }
}

Link List Class:

1. Insert in the List
2. Delete in the List
3. Traverse in the List

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
public class Single_Link_List {
    // instance variable

    Node header;

    // constructor
    public Single_Link_List() {
        header = null;
    }

    public Node getHeader() {
        return header;
    }

    public void insert_at_Beginning(int data) {
        Node newNode = new Node(data);
        if (header == null) {
            header = newNode;
        } else {
            newNode.setNext(header);
            header = newNode;
        }
    }

    public void insert_at_Position(int data, int pos) {
        Node newNode = new Node(data);
        Node temp = header;
            pos--;
        if (pos == 1) {
            insert_at_Beginning(data);
        }
        while (pos > 1) {
            if (temp == null) {
                throw new NoSuchElementException("Not enough elements in the list.");
            } else {
                --pos;
            }
            temp = temp.getNext();
        }
        newNode.setNext(temp.getNext());
        temp.setNext(newNode);
    }

    public void insert_at_End(int data) {
        Node newNode = new Node(data);
        Node temp;
        if (header == null) {
            header = newNode;
        } else {
            temp = header;
            while (temp.getNext() != null) {
                temp = temp.getNext();
            }
            temp.setNext(newNode);
        }
    }

    public int delete_at_Beginning() {
        if (isEmpty()) {
            System.out.println("Error: Invalid operation! List is Empty");
            return -1;
        } else {
            int temp = header.getData();
            header = header.getNext();
            return temp;
        }
    }

    public void delete_at_Position(int pos) {
        Node temp = header;
        if (isEmpty()) {
            System.out.println("Error: Invalid operation! List is Empty");
        } else {
            pos--;
            while (--pos != 0) {
                temp = temp.getNext();
            }
            temp.setNext(temp.getNext().getNext());
        }
    }

    public void delete_at_End() {
        Node temp = header.getNext();
        Node prev = header;

        if (header == null) {
            System.out.println("Error: Invalid operation! List is Empty");
        } else {
            while (temp.getNext() != null) {
                temp = temp.getNext();
                prev = prev.getNext();
            }
            prev.setNext(null);
            temp = null;
        }
    }

    public void traverse() {
        Node temp = header;
        while (temp != null) {
            System.out.print(temp.getData() + " ");
            temp = temp.getNext();
        }
    }

    public boolean isEmpty() {
        return header == null;
    }
}

Test Class:


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
public class test_class {
   
    public static void main(String[] args) {
        Single_Link_List ll = new Single_Link_List();

        System.out.println("Insert At Begining!");
        ll.insert_at_Beginning(23);
        ll.insert_at_Beginning(56);
        ll.traverse();
        System.out.println("");

        System.out.println("\nInsert At End!");
        ll.insert_at_End(100);
        ll.insert_at_End(123);
        ll.traverse();
        System.out.println("");

        System.out.println("\nInsert At any Position!");
        ll.insert_at_Position(321, 3);
        ll.insert_at_Position(4444, 4);
        ll.traverse();
        System.out.println("");

        System.out.println("\nDelete At Begining!");
        ll.delete_at_Beginning();
        ll.traverse();
        System.out.println("");
        
        System.out.println("\nDelete At Postion!");
        ll.delete_at_Position(3);
        ll.traverse();
        System.out.println("");

        System.out.println("\nDelete At End!");
        ll.delete_at_End();
        ll.traverse();
        System.out.println("");

    }
}

Sunday, January 17, 2016

Unconditional Jumps

Write a program that demonstrate unconditional Jumps

Program/Code:


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
org 100h
mov AX, 35
JMP YES
NO:
MOV AH, 2
INT 21H
JMP EXIT_
YES:
MOV DL, 'A'
JMP NO
EXIT_:
ret
; Always Print: A

Convert capital alphabet to small and small to capital alphabet

Write a program that convert capital alphabet to small and small to capital alphabet

Program/Code:


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
org 100h
MOV AH, 1
INT 21H
add al,20h
mov ah, 2
mov dl, al ;take output a character ;and show it by dl
int 21h

mov dl, 0Dh
int 21h
mov dl, 0Ah
int 21h


mov ah, 1
int 21h
sub al,20h
mov ah, 2
mov dl, al ;take output a character ;and show it by dl
int 21h
ret

Output/Result:




Simple output of string

Write a program that display output on screen

Program/Code:


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
Org 100h
.data
Str db 'hello!', 0ah, 0dh,'$'
Str2 db 48h, 45h,4ch, 4ch, 4fh,0Ah, 0Dh, ' abcd$', 24h
 ;$ is use for terminate string
 ;0ah is use for new line feed
 ;0dh is use for carriage return / start line  
.code
Mov ah, 9
Lea dx, str
int 21h
Mov ah, 9
Lea dx, str2
Int 21h
ret

Output/Result:


Addition of two number by taking input and display output

Write a program that takes two number by user input and display output

Program/Code:


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
org 100h
mov ah, 1
int 21h
 ;take input a character
 ;and store it in al
mov bl, al
int 21h ;take input a character
 ;and store it in al
sub al, 30h
sub bl, 30h
add al, bl
mov ah, 2
add al, 30h
mov dl, al ;take output a character
 ;and show it by dl
int 21h
ret
;Output 437 first two is input by user third one is output

Simple Define Double Word Addition

Write a program that take three variable and sum the first two variable and store its value in third one variable

Program/Code:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
org 100h
.data
A dd 0x12345678
B dd 0x87654321
C dd 0
.code
mov ax, A+0
add ax, B+0
mov bx, A+2
adc bx, B+2
mov c+0, ax
mov c+2, bx
ret

Wednesday, January 13, 2016

Write a program that make a triangle

Program/Code:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
org 100h
mov Cx, 5
mov Bx, 0
outer:
mov Bx, Cx
mov Ah, 2
mov Dl, '*'
line_print:
int 21h
Loop line_print
mov dl, 0AH
int 21h
mov dl, 0DH
int 21h
mov Cx, Bx
Loop outer
ret

OR

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
org 100h
mov Cx, 5
mov Bx, 0
outer:
mov Bx, Cx
mov Ah, 2
mov Dl, '*'
line_print:
int 21h
Loop line_print
mov dl, 0AH
int 21h
mov dl, 0DH
int 21h
mov Cx, Bx
Loop outer
ret

Output:


*****
****
***
**
*

C Language

Assembly Language





Assalam O Alikum 
Dear Programmer Here is some code in Assembly Language 8086. These program are complied by emu8086 emulator easily download by given link. I hope these program guide you learn Assembly Language.

Write a program that print A 5 times in output by loop


Program/Code:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
mov Bx, 5
mov ah, 2
mov dl,'A'
loopLabel:
int 21h
sub Bx, 1
Cmp Bx, 0
JNE LoopLabel

Output:
AAAAA

Print * 5 times in output by loop keyword

Program/Code:

1
2
3
4
5
6
mov Cx, 5
mov Ah, 2
Label:
mov dl, '*'
int 21h
loop label

Output:
*****