Posts

Showing posts from August, 2019

12. Write a Java program which will demonstrate a concept of cohesion and coupling of the various modules in the program.

 -------------------------Coupling--------------------------- import java.lang.*; import java.util.*; class Box { public int vol; Box( int l , int b , int h) { this.vol = l*b*h; } } class Volume { public static void main(String args[]) { Box b = new Box(5,5,5); System.out.println("VOLUME : " + b.vol); } }     -------------------------Cohesion--------------------------- import java.util.*; import java.lang.*; class Name { String name; public String getName(String name) { this.name = name; return name; } } class Age { int age; public int getAge(int age) { this.age = age; return age; } } class Number { int num; public int getNum(int num) { this.num = num; return num; } } public class Student { public static void main(String args[]) { Name n = new Name(); System.out.println("NAME : " + n.getName("Shreyas")); Age a = new Age(); System.out.println("Age : " + a.getAge(20)...

11. Any application defining scope of Formal parameter, Global parameter, Local parameter accessing mechanism and also relevance to private, public and protected access. Write a Java program which demonstrates the scope rules of the programming mechanism.

import java.util.*; class employee { public String e_name; protected int e_id; String e_add; double salary; employee() { e_name = "abc"; e_id = 0; e_add = "xyz"; salary = 0; } } class Company extends employee { private long ph_no; void get() { Scanner in = new Scanner(System.in); System.out.println("ID : "); e_id = in.nextInt(); System.out.println("NAME : "); e_name = in.next(); System.out.println("ADDRESS : "); e_add = in.next(); System.out.println("SALARY : "); salary = in.nextDouble(); System.out.println("PHONE NO. : "); ph_no = in.nextLong(); } void put() { System.out.println("ID : " + this.e_id); System.out.println("NAME : " + this.e_name); System.out.println("ADDRESS : " + e_add); System.out.println("SALARY : " + salary); System.out.println("PHONE NO. : " + ph_no); } public static void m...

10. Implement the Heap/Shell sort algorithm implemented in C++/Java demonstrating heap/shell data structure with modularity of programming language.

#include<iostream> #include<cstring> using namespace std; #define size 20 class heap { public: void accept() { int n; cout<<".. .. .. .. .. .. .. .. .. .. .. "<<endl; cout<<": ENTER NUMBER OF STUDENTS = "; cin>>n; int a[size]; cout<<": ENTER MARKS OF EACH STUDENTS :"<<endl; for(int i = 1 ; i <= n ; i++) { cout<<"Roll NO. "<<i<<" = "; cin>>a[i]; } heapsort(a,n); display(a,n); } void max_heapify(int a[] , int i , int n) { int temp,left,right,large; large = 0; left = 2*i; right = 2*i+1; if( (left <= n) && (a[left] > a[i])) large = left; else large = i; if( ( right <= n ) && ( a[right] > a[large] ) ) large = right; if(large != i) { temp = a[i]; a[i] = a[large]; a[large] = temp; max_heapify(a,large,n); } } void heapsort(in...

9. Department maintains a student information. The file containsrollnumber, name, division and address. Allow user to add, delete information of student. Display information of particular employee. If record of student does not exist an appropriate message is displayed. If it is, then the system displays the student details.Use sequential file to main the data.

#include<iostream> #include<fstream> using namespace std; class student { int roll_no; string name,div,address; public: student(); void add_record(); void delete_data(string); void modify_record(); void search_data(string); void input_data(); void write_data(); void read_data(); }; student :: student() : roll_no(0) , name(""), address(""),div (""){} void student :: input_data() { cout<<": ENTER STUDENTS DETAILS :"<<endl<<endl; cout<<": ROLL NO = "; cin>>roll_no; cin.ignore(); cout<<": NAME = "; getline(cin,name); cout<<": DIVISION = "; getline(cin , div); cout<<": ADDRESS = "; getline(cin , address); write_data(); } // FUNCTION TO WRITE DATA INTO THE FILE void student :: write_data() { fstream obj; obj.open("student_data.txt" , ios::out | ios::app); obj << roll_no <<" "...

8. Prims Algorithm

#include<iostream> using namespace std; #define infi 9999; class prim { float arr[10][10]; int nodes; char a[6] = {'A','B','C','D','E','F'}; public: void creategraph() { cout<<"ENTER THE NUMBER OF NODES : "; cin>>nodes; for( int i = 0 ; i < nodes ; i++ ) { for( int j = 0 ; j < nodes ; j++) { if( i != j && i < j ) { cout<<"DISTANCE BETWEEN "<<a[i]<<" TO "<<a[j]<<" = "; cin>>arr[i][j]; arr[j][i] = arr[i][j]; } else if(i == j) { arr[i][j] = 0; } } } for(int i = 0 ; i < nodes ; i++) for(int j = 0 ; j < nodes ; j++) if( arr[i][j] == 0 ) arr[i][j] = infi; } void primalgo() { creategraph(); // call to creategraph int visited[nodes]; // defines which nodes are visited and from these nodes we need to find the minimum path ...

7. Optimal Binary Search Tree.

#include<iostream> using namespace std; class obst { int p[10],q[10]; // p -> probability of success : q -> probability of failure int arr[10]; // array of elements int weight[10][10],root[10][10],cost[10][10]; // resultant for obst i.e.,weight , root , cost int n; // n -> no. of elements public: void get() { cout<<"ENTER NUMBER OF ELEMENTS = "; cin>>n; cout<<": INPUT DATA ( ELEMENT , PROBABILITIES ) :"<<endl; for( int i = 1 ; i <= n ; i++ ) { cout<<"ELEMENT "<<i<<" = "; //element cin>>arr[i]; cout<<"p ["<<i<<"] = "; //success probability cin>>p[i]; cout<<"q ["<<i-1<<"] = "; //failure probability cin>>q[i-1]; } cout<<"q ["<<n<<"] = "; ...

6. Implement all the functions of a dictionary (ADT) using hashing.Data: Set of (key, value) pairs, Keys are mapped to values, Keys must be comparable, Keys must be uniqueStandard Operations: Insert(key, value), Find(key), Delete(key)

#include<iostream> using namespace std; #define size 10 class Record { string name; long ph_num; int flag; public: Record() { name = "-"; ph_num = 0; flag = 0; } friend class Phonebook; }; class Phonebook { int capacity; int key; public: Phonebook() : capacity(0) {} void input( Record table[] ) { if( capacity == size ) { cout<<"HASH TABLE FULL !!"<<endl; return; } else { Record r; cout<<"ENTER NAME : "; getline(cin , r.name); cin.ignore(); cout<<"ENTER TELEPHONE NO. : "; cin>>r.ph_num; int index = hash(r.name); while(table[index].flag == 1) { if(table[index].name == r.name) { cout<<" RECORD ALREADY EXIST !!"<<endl; return; } index++; index %= size; } table[index] = r; table[index].flag = 1; capacity++; } } int hash( string name) { int sum = 0; for(int...

5. Implement all the functions of a dictionary (ADT) using hashing.Data: Set of (key, value) pairs, Keys are mapped to values, Keys must be comparable, Keys must be uniqueStandard Operations: Insert(key, value), Find(key), Delete(key)

#include<iostream> using namespace std; #define size 10 class Record { string word; string meaning; int flag; public: Record() { word = "-"; meaning = "-"; flag = 0; } friend class Dictionary; }; class Dictionary { int capacity; int key; public: Dictionary() : capacity(0) {} void input( Record table[] ) { if( capacity == size ) { cout<<"HASH TABLE FULL !!"<<endl; return; } else { Record r; cout<<"ENTER word : "; getline(cin , r.word); cin.ignore(); cout<<"ENTER MEANING : "; cin>>r.meaning; int index = hash(r.word); while(table[index].flag == 1) { if(table[index].word == r.word) { cout<<" RECORD ALREADY EXIST !!"<<endl; return; } index++; index %= size; } table[index] = r; table[index].flag = 1; capacity++; } } int hash( string word) { int sum ...

4. Write a function to get the number of vertices in an undirected graph and its edges. You may assume thatno edge is input twice. i.Use adjacency list representation of the graph and find runtime of the functionii.Use adjacency matrix representation of the graph and find runtime of the function

#include<iostream> using namespace std; class node { public: char data; node *start = NULL; node *next; void list(char ch) { node *ptr = new node; node *temp = new node; ptr->data = ch; if(start == NULL) { start = ptr; start->next = NULL; } else { temp = start; while(temp->next != NULL) { temp = temp->next; } ptr->next = NULL; temp->next = ptr; } } void displaylist() { node *temp = new node; if(start == NULL) { cout<<"LIST IS EMPTY"<<endl; } else { temp = start; while(temp != NULL) { cout<<temp->data<<"->"; temp = temp->next; } cout<<endl; } } }; class graph { int a[10][10]; char arr[5] = {'A','B','C','D','E'}; int vertices; node al[5]; public: graph() { cout<<"ENTER THE NUMBER OF VERTICES IN THE GRAPH \t::\t"; cin>>ve...

3. For given expression eg. a-b*c-d/e+f construct inorder sequence and traverse it using postorder traversal(non recursive).

#include<iostream> #include<string> #include<stack> using namespace std; int getweight(char c) { switch(c) { case '^' : return 3; break; case '*' : case '/' : return 2; break; case '+' : case '-' : return 1; break; default : return 0; } } void infix2postfix(char infix[], char postfix[], int size ,int sizepos) { stack <char> s; int i = 0; int j = 0; int weight = 0; char ch; while( i < size) { ch = infix[i]; //cout<<i<<endl; // cout<<ch<<endl; if ( ch == '(' ) { s.push(ch); i++; continue; } else if( ch == ')') { if(s.top() == '(') { s.pop(); } else { while(s.top() != '(') { postfix[j++] = s.top(); s.pop(); } s.pop(); i++; continue; } } else { weight = getweight(ch); if(weight == 0) { postfix[j++] = ch; } else i...

2. Beginning with an empty binary search tree, Construct binary search tree by inserting the values in the order given. After constructing a binary tree -i.Insert new nodeii.Find number of nodes in longest path iii.Minimum data value found inthe tree iv.Change a tree so that the roles of the left and right pointers are swapped at every node v.Search a value

#include<iostream> #include<math.h> using namespace std; struct Bstnode { int data; Bstnode *left = NULL; Bstnode *right = NULL; }; class Btree { int n; int x; int flag; public: Bstnode * root; Btree() { root = NULL; } Bstnode *GetNewNode(int in_data) { Bstnode * ptr = new Bstnode(); ptr->data = in_data; ptr->left = NULL; ptr->right = NULL; return ptr; } Bstnode *insert( Bstnode *temp , int in_data) { if( temp == NULL ) { temp = GetNewNode(in_data); } else if( temp->data > in_data) { temp->left = insert(temp->left , in_data); } else { temp->right = insert( temp->right , in_data); } return temp; } void input() { cout<<"ENTER NUMBER OF ELEMENTS IN THE BST : "; cin>>n; for(int i = 0 ; i < n ; i++) { cout<<"NUMBER = "; cin>>x; root = insert(root , x); } } int search(Bstnode *temp ,int in_data) { if( te...

1. A book consists of chapters, chapters consist of sections and sections consist of subsections. Construct a tree and print the nodes. Find the time and space requirements of your method.

#include<iostream> using namespace std; struct node { string name; node *B[5]; }; class book { int c,s,sub; public: node *temp = new node; void Getnewnode() { for(int i = 0 ; i<5 ; i++) { temp->B[i] = new node; temp->B[i]->name = "empty"; for(int j=0; j<5; j++) { temp->B[i]->B[j] = new node; temp->B[i]->B[j]->name = "empty"; for(int k = 0 ; k<5; k ++) { temp->B[i]->B[j]->B[k] = new node; temp->B[i]->B[j]->B[k]->name = "empty"; } } } } void add_title() { cout<<": ENTER THE TITLE OF THE BOOK = "; cin>>temp->name; Getnewnode(); } void add_chapter() { string cname; int cnum; cout<<": ENTER NUMBER OF CHAPTERS IN THE BOOK = "; cin>>cnum; c = cnum; for(int i = 0 ; i < cnum ; i++) { cout<<endl<<"Chapter "<...