Friday, 19 April 2019

servlet


JAVA FILE

importjava.io.IOException;
importjava.io.PrintWriter;
importjavax.servlet.Servlet;
importjavax.servlet.ServletException;
importjavax.servlet.annotation.WebServlet;
importjavax.servlet.http.HttpServlet;
importjavax.servlet.http.HttpServletRequest;
importjavax.servlet.http.HttpServletResponse;


publicclassAssignmentextendsHttpServlet  {

     
      protectedvoiddoGet(HttpServletRequestrequest, HttpServletResponseresponse) throwsServletException, IOException {
            PrintWriterout  =response.getWriter();
            String n1 = request.getParameter("txt1");
            String n2 = request.getParameter("txt2");
            String op = request.getParameter("op");
            out.println("Welcome to Servlet......");
            if(op.equals("Addition")){
out.println("Answer is ="+(Integer.parseInt(n1) + Integer.parseInt(n2)));
       }
elseif(op.equals("Subtraction")){
      out.println("Answer is ="+(Integer.parseInt(n1) - Integer.parseInt(n2)));
       }
elseif(op.equals("multiplication")){
      out.println("Answer is ="+(Integer.parseInt(n1) * Integer.parseInt(n2)));
       }
else{
      out.println("Answer is ="+(Integer.parseInt(n1) / Integer.parseInt(n2)));
       }
           
      }
}
-----------------------------------------------------------------------

XML FILE
<?xmlversion="1.0"encoding="UTF-8"?>
<web-appxmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns="http://xmlns.jcp.org/xml/ns/javaee"xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"id="WebApp_ID"version="3.1">
<display-name>Servlet1</display-name>

<servlet>
<servlet-name>Servlet1</servlet-name>
<servlet-class>Assignment</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>Servlet1</servlet-name>
<url-pattern>/Assignment</url-pattern>
</servlet-mapping>

<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
</web-app>

-----------------------------------------------------------------------
HTML FILE
<html>
<head>
<title>Calculator</title>


</head>
<body>
<formaction="Assignment"method="get"name="frm">
<p>
               Enter num1:
<inputname="txt1"type="text"/>
</p>
<p>
                Enter num2:
<inputname="txt2"type="text"/>
</p>
<p>
               Operator

<selectname="op">
<optionvalue="Addition">Addition</option>
<optionvalue="Subtraction">Subtraction</option>
<optionvalue="multiplication">multiplication</option>
<optionvalue="division">division</option>
</select>
</p>
<p>
<inputtype="submit"value="submit"/>
</p>
</form>
</body>
</html>

Sunday, 15 April 2018

Write a function to get the number of vertices in an undirected graph and its edges. You may assume that no edge is input twice.(ADJACENCY MATRIX)

#include<iostream>
#include<queue>
#include<stack>
// Name : Vivek S. Sharma
using namespace std;
class Graph{
int g[10][10];
int v[10];
int n,e;
public:
void create(){
int v1,v2;
cout<<"\nEnter no. of vertices : ";
cin>>n;
cout<<"\nEnter no. of edges : ";
cin>>e;
for(int i=1; i<=n ; i++){
for(int j=1; j<=n ; j++){
g[i][j]=0;
v[i]=0;
}
}
for(int i=1; i<=e ;i++ ){
cout<<"\nEnter the edge : ";
cin>>v1>>v2;
g[v1][v2]=g[v2][v1]=1;
}
}
void bfs(){
int v1,v2;
cout<<"\nEnter the starting vertex : ";
cin>>v1;
queue<int> q;
q.push(v1);
v[v1]=1;
while(!q.empty()){
v1=q.front();
q.pop();
cout<<v1;
for( v2=1; v2<=n ; v2++){
if(g[v1][v2]==1 && v[v2]==0){
q.push(v2);
v[v2]=1;}
}

}
}

void dfs(){
int v1,v2;
cout<<"\nEnter starting vertex : ";
cin>>v1;
stack <int> s;
s.push(v1);
while(!s.empty()){
v1=s.top();
s.pop();
if(v[v1]==0){
cout<<v1;
v[v1]=1;
}
for(v2=1; v2<=n ; v2++){
if(g[v1][v2]==1 && v[v2]==0)
s.push(v2);
}
}
}
void dfs_recurssion(int v1){

int v2;
cout<<v1;
v[v1]=1;
for( v2=1; v2<=n ; v2++){
if(g[v1][v2]==1 && v[v2]==0)
dfs_recurssion(v2);
}
}
};

int main(){
Graph g;
int v;
g.create();
/*cout<<"\nEnter the starting vertex : ";
cin>>v;
g.dfs_recurssion(v);
//g.dfs();*/
g.bfs();
return 0;
}

Saturday, 14 April 2018

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

//////////////////////////////////////////////////////////////
// //
//   Name : Vivek S. Sharma //
// Title : Heap Sort //
// //
// //
//////////////////////////////////////////////////////////////
package vivek_a12;
import java.util.*;
public class vivek_a12 {

private static int N;
    public static void sort(int arr[]){     
heapMethod(arr);       
        for (int i = N; i > 0; i--){
            swap(arr,0, i);
            N = N-1;
            heap(arr, 0);
        }
    }   
    public static void heapMethod(int arr[]){
        N = arr.length-1;
        for (int i = N/2; i >= 0; i--)
            heap(arr, i);       
    }
    public static void heap(int arr[], int i){
        int left = 2*i ;
        int right = 2*i + 1;
        int max = i;
        if (left <= N && arr[left] > arr[i])
            max = left;
if (right <= N && arr[right] > arr[max])       
            max = right;
        if (max != i){
            swap(arr, i, max);
            heap(arr, max);
        }
    }   
    public static void swap(int arr[], int i, int j){
        int tmp = arr[i];
        arr[i] = arr[j];
        arr[j] = tmp;
    }   
    public static void main(String[] args) {
        Scanner in = new Scanner( System.in );     
        int n;   
        System.out.println("Enter the number of elements to be sorted:");
        n = in.nextInt();   
        int arr[] = new int[ n ];
        System.out.println("Enter "+ n +" integer elements");
        for (int i = 0; i < n; i++)
            arr[i] = in.nextInt();
        sort(arr);
        System.out.println("After sorting ");       
        for (int i = 0; i < n; i++)
            System.out.println(arr[i]+" ");           
        System.out.println();           
    }   

}

Department maintains a student information. The file contains roll number, 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>
#include<cstring>
using namespace std;
class tel
 {

 public:
int rollNo,roll1;
char name[10];
char div;
char address[20];
void accept()
{
cout<<"\n\tEnter Roll Number : ";
cin>>rollNo;
cout<<"\n\tEnter the Name : ";
cin>>name;
cout<<"\n\tEnter the Division:";
cin>>div;
cout<<"\n\tEnter the Address:";
cin>>address;
}
        void accept2()
        {
               cout<<"\n\tEnter the Roll No. to modify : ";
               cin>>rollNo;
        }
        void accept3()
        {
              cout<<"\n\tEnter the name to modify : ";
              cin>>name;
        }
        int getRollNo()
        {
        return rollNo;
        }
void show()
{

cout<<"\n\t"<<rollNo<<"\t\t"<<name<<"\t\t"<<div<<"\t\t"<<address;
}
};
int main()
{
int i,n,ch,ch1,rec,start,count,add,n1,add2,start2,n2,y,a,b,on,oname,add3,start3,n3,y1,add4,start4,n4;
char name[20],name2[20];
tel t1;
count=0;
fstream g,f;
do
{
cout<<"\n>>>>>>>>>>>>>>>>>>>>>>MENU<<<<<<<<<<<<<<<<<<<<";
cout<<"\n1.Insert and overwrite\n2.Show\n3.Search & Edit(number)\n4.Search & Edit(name)\n5.Search & Edit(onlynumber)\n6.Search & edit(only name)\n 7.Delete a Student Record\n 8.Exit\n\tEnter the Choice\t:";
cin>>ch;
switch(ch)
{
case 1:
f.open("StuRecord.txt",ios::out);
x:t1.accept();
f.write((char*) &t1,(sizeof(t1)));
cout<<"\nDo you want to enter more records?\n1.Yes\n2.No";
cin>>ch1;
if(ch1==1)
goto x;
else
{
f.close();
break;
}

case 2:
f.open("StuRecord.txt",ios::in);
f.read((char*) &t1,(sizeof(t1)));
//cout<<"\n\tRoll No.\t\tName \t\t Division \t\t Address";
while(f)
{
t1.show();
f.read((char*) &t1,(sizeof(t1)));
}
f.close();
break;
case 3:
cout<<"\nEnter the roll number you want to find";
cin>>rec;
f.open("StuRecord.txt",ios::in|ios::out);
f.read((char*)&t1,(sizeof(t1)));
while(f)
{
if(rec==t1.rollNo)
{
cout<<"\nRecord found";
add=f.tellg();
f.seekg(0,ios::beg);
       start=f.tellg();
n1=(add-start)/(sizeof(t1));
f.seekp((n1-1)*sizeof(t1),ios::beg);
t1.accept();
f.write((char*) &t1,(sizeof(t1)));
f.close();
count++;
break;
}
f.read((char*)&t1,(sizeof(t1)));
     }
if(count==0)
        cout<<"\nRecord not found";
f.close();
break;

case 4:
cout<<"\nEnter the name you want to find and edit";
cin>>name;
f.open("StuRecord.txt",ios::in|ios::out);
f.read((char*)&t1,(sizeof(t1)));
while(f)
{
y=(strcmp(name,t1.name));
if(y==0)
{
cout<<"\nName found";
add2=f.tellg();
f.seekg(0,ios::beg);
start2=f.tellg();
n2=(add2-start2)/(sizeof(t1));
f.seekp((n2-1)*sizeof(t1),ios::beg);
t1.accept();
f.write((char*) &t1,(sizeof(t1)));
f.close();
break;
}
        f.read((char*)&t1,(sizeof(t1)));
}
break;
     case 5:
           cout<<"\n\tEnter the roll number you want to modify";
           cin>>on;
           f.open("StuRecord.txt",ios::in|ios::out);
           f.read((char*) &t1,(sizeof(t1)));
           while(f)
           {
             if(on==t1.rollNo)
             {
               cout<<"\n\tNumber found";
               add3=f.tellg();
               f.seekg(0,ios::beg);
               start3=f.tellg();
               n3=(add3-start3)/(sizeof(t1));
               f.seekp((n3-1)*(sizeof(t1)),ios::beg);
               t1.accept2();
               f.write((char*)&t1,(sizeof(t1)));
               f.close();
               break;
             }
             f.read((char*)&t1,(sizeof(t1)));
          }
          break;
     case 6:
           cout<<"\nEnter the name you want to find and edit";
cin>>name2;
f.open("StuRecord.txt",ios::in|ios::out);
f.read((char*)&t1,(sizeof(t1)));
while(f)
{
y1=(strcmp(name2,t1.name));
if(y1==0)
{
cout<<"\nName found";
add4=f.tellg();
f.seekg(0,ios::beg);
start4=f.tellg();
n4=(add4-start4)/(sizeof(t1));
f.seekp((n4-1)*sizeof(t1),ios::beg);
t1.accept3();
f.write((char*) &t1,(sizeof(t1)));
f.close();
break;
}
        f.read((char*)&t1,(sizeof(t1)));
}
break;
   case 7:
    int roll;
    cout<<"Please Enter the Roll No. of Student Whose Info You Want to Delete: ";
  cin>>roll;
  f.open("StuRecord.txt",ios::in);
  g.open("temp.txt",ios::out);
  f.read((char *)&t1,sizeof(t1));
  while(!f.eof())
  {
    if (t1.getRollNo() != roll)
      g.write((char *)&t1,sizeof(t1));
      f.read((char *)&t1,sizeof(t1));
  }
  cout << "The record with the roll no. " << roll << " has been deleted " << endl;
  f.close();
  g.close();
  remove("StuRecord.txt");
  rename("temp.txt","StuRecord.txt");
    break;
   case 8:
      cout<<"\n\tThank you";
      break;


        }
  }while(ch!=8);
}

Read the marks obtained by students of second year in an online examination of particular subject. Find out maximum and minimum marks obtained in that subject. Use heap data structure. Analyze the algorithm.

#include <iostream>
//////////////////////////////////////////////////////////////
// //
//   Name : Vivek S. Sharma //
// Title : Heap Sort //
// //
// //
//////////////////////////////////////////////////////////////
using namespace std;


void MaxHeapify(int a[], int i, int n)
{
int j, temp;
temp = a[i];
j = 2*i;

  while (j <= n)
{
if (j < n && a[j+1] > a[j])
j = j+1;

if (temp > a[j])
break;

else if (temp <= a[j])
{
a[j/2] = a[j];
j = 2*j;
}
}
a[j/2] = temp;
return;
}
void MinHeapify(int a[], int i, int n)
{
int j, temp;
temp = a[i];
j = 2*i;

  while (j <= n)
{
if (j < n && a[j+1] < a[j])
j = j+1;

if (temp < a[j])
break;

else if (temp >= a[j])
{
a[j/2] = a[j];
j = 2*j;
}
}
a[j/2] = temp;
return;
}
void MaxHeapSort(int a[], int n)
{
int i, temp;
for (i = n; i >= 2; i--)
{

temp = a[i];
a[i] = a[1];
a[1] = temp;

MaxHeapify(a, 1, i - 1);
}
}
void MinHeapSort(int a[], int n)
{
int i, temp;
for (i = n; i >= 2; i--)
{

temp = a[i];
a[i] = a[1];
a[1] = temp;

MinHeapify(a, 1, i - 1);
}
}
void Build_MaxHeap(int a[], int n)
{
int i;
for(i = n/2; i >= 1; i--)
MaxHeapify(a, i, n);
}
void Build_MinHeap(int a[], int n)
{
int i;
for(i = n/2; i >= 1; i--)
MinHeapify(a, i, n);
}
int main()
{
int n, i;
cout<<"\nEnter the number of Students : ";
cin>>n;
n++;
int arr[n];
for(i = 1; i < n; i++)
{
cout<<"Enter the marks :  "<<i<<": ";
cin>>arr[i];
}

Build_MaxHeap(arr, n-1);
MaxHeapSort(arr, n-1);


int max,min;
cout<<"\nSorted Data : ASCENDING : ";

for (i = 1; i < n; i++)
cout<<"->"<<arr[i];
min=arr[1];
Build_MinHeap(arr, n-1);
MinHeapSort(arr, n-1);
cout<<"\nSorted Data : DESCENDING: ";
max=arr[1];
for (i = 1; i < n; i++)
cout<<"->"<<arr[i];
cout<<"\nMaximum Marks : "<<max<<"\nMinimum marks : "<<min;
return 0;
}