You are on page 1of 14

Assignment No.

SetA1 ===>

import java.util.*;
class ArrayListDemo
{
public static void main(String args[])
{
int n;
ArrayList al=new ArrayList();
Scanner sc=new Scanner(System.in);
System.out.println("Enter number of cities :");
n=sc.nextInt();
for(int i=0;i<n;i++)
{
System.out.println("Enter city name:");
String city=sc.next();
al.add(city);
}
System.out.println("ArrayList Data:"+al);
Iterator it=al.iterator();
while(it.hasNext())
{
System.out.println("City :" +it.next());
}
}
}

SetA2 ===>

import java.util.*;
class LinkedListDemo
{
public static void main(String args[])
{
int n;
LinkedList<String> list=new LinkedList<String>();
Scanner sc=new Scanner(System.in);
System.out.println("Enter number of friends :");
n=sc.nextInt();
for(int i=0;i<n;i++)
{
System.out.println("Enter your friends name:");
String name=sc.next();
list.add(name);
}
System.out.println("LinkedList Data:"+list);
Iterator<String> it=list.iterator();
while(it.hasNext())
{
System.out.println("Friends :" +it.next());
}
}
}
SetA3 ===>

import java.util.*;
class TreeSetDemo
{
public static void main(String args[])
{

Set ts=new TreeSet();


ts.add("Red");
ts.add("Blue");
ts.add("Pink");
ts.add("Yellow");
ts.add("Voilet");
System.out.println("Clours from Tree Set is:"+ts);
}
}

SetA4 ====>

import java.util.*;
class ContactList
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
Hashtable ht=new Hashtable();
for(int i=0;i<3;i++)
{
System.out.println("Enter name:");
String name=sc.next();
System.out.println("Enter mobile number:");
int number=sc.nextInt();
ht.put(name,number);

}
System.out.println("Contents are:"+ht);
}
}

SetB1 ===>

import java.util.*;
class Numbers
{
public static void main(String args[])
{
int n;
Set st=new TreeSet();
Scanner sc=new Scanner(System.in);
System.out.println("Enter how many nubers you want:");
n=sc.nextInt();
for(int i=0;i<n;i++)
{
System.out.println("Enter number:");
int number=sc.nextInt();
st.add(number);
}
System.out.println("Tree Elements are :" +st);
}
}

SetB2 ===>

import java.util.*;
class Hash
{
public static void main(String args[])
{
int n;
Scanner sc=new Scanner(System.in);
HashMap hp=new HashMap();
System.out.println("Enter number of subjects:");
n=sc.nextInt();
for(int i=0;i<n;i++)
{
System.out.println("Enter Subject name:");
String name=sc.next();
System.out.println("Enter Subjects Marks:");
int marks=sc.nextInt();
hp.put(name,marks);
}
System.out.println("Details before sorting:"+hp);
TreeMap tp=new TreeMap(hp);
System.out.println("Details after sorting:"+tp);
}
}

SetB3 ====>

import java.io.*;
import java.util.*;
class SetB3
{
public static void main(String arg[])
{
Hashtable ht=new Hashtable();
try
{
Scanner sc=new Scanner(System.in);
FileReader fr=new FileReader("Name.txt");
BufferedReader br=new BufferedReader(fr);
String str ="";
String s[];
while(str!=null)
{
str=br.readLine();
if(str==null)
break;
int pos=str.indexOf("\t");
if(pos!=-1)
{
String name=str.substring(0,pos);
String phone=str.substring(pos+1);
ht.put(name,phone);
System.out.println(name+" "+phone);
}
}
System.out.println("Enter the name you want to search:");
String find=sc.next();
String search=(String)ht.get(find);
if(search==null)
System.out.println("Data not found");
else
System.out.println(search);

}catch(Exception e)
{
System.out.println("Error"+e);
}
}
}

Assignment no. 2

SetA1 ===>

class MyThread extends Thread


{
String msg;
int n;

MyThread(String s,int m)
{
msg=s;
n=m;
}

public void run()


{
try
{
for(int i=0;i<n;i++)
{
System.out.println(msg+ " "+i);
sleep(1000);
}
}catch(Exception e)
{}
}
}

class SetA1
{
public static void main(String arg[])
{
MyThread t1=new MyThread("COVID-19",10);
MyThread t2=new MyThread("LOCKDOWN-2020",20);
MyThread t3=new MyThread("VACCINATED-2021",30);
t1.start();
t2.start();
t3.start();
}
}

SetA2 ===>

class MyThread extends Thread


{
public void run()
{
try
{
for(int i=100;i>=0;i--)
{
System.out.println(i);
sleep(100);
}
}catch(Exception e)
{}
}
}

class SetA2
{
public static void main(String arg[]) throws Exception
{
MyThread t1=new MyThread();
t1.start();
t1.join();
t1.setName("Ramesh");
System.out.println("Thread name after changing name:"+t1.getName());
}
}

SetB1 ===>

import java.util.*;
class SumThread extends Thread
{
int num[],start,end,avg;
SumThread(int a[],int s,int e)
{
num=a;
start=s;
end=e;
}
public void run()
{
int sum=0;
for(int i=start;i<=end;i++)
{
sum=sum+num[i];
}
avg=sum/100;
}

}
class setb1
{
public static void main(String arg[]) throws Exception
{

int a[]=new int[1000];


SumThread t1[]=new SumThread[10];
Random r=new Random();
int i;
for( i=0;i<1000;i++)
{
a[i]=r.nextInt(1000);

}
int st=0,end=0;
for(i=0;i<10;i++)
{
end=(i*1)+99;
t1[i]=new SumThread(a,st,end);
t1[i].start();
st=end+1;
t1[i].join();
}
int total=0;
for (i=0;i<10;i++)
{
System.out.println("Average is:"+t1[i].avg);
total=total+t1[i].avg;
}
float avg=total/100;
System.out.println("Average of all numbers :"+avg);

}
}

SetB3 ===>

import java.util.*;
class RandomThread extends Thread
{
int num;
public void run()
{
try
{
while(true)
{
Random r=new Random(num);
int num=r.nextInt(10);
System.out.println("Random number:"+num);
if(num%2==0)
{
SquareThread s=new SquareThread(num);
s.start();
}
else
{
CubeThread c=new CubeThread(num);
c.start();
}
sleep(1000);
}
}catch(Exception e)
{}
}
}

class SquareThread extends Thread


{
int num;
SquareThread(int n)
{
num=n;
}
public void run()
{
System.out.println("Square:"+(num*num));
}
}
class CubeThread extends Thread
{
int num;
CubeThread(int n)
{
num=n;
}
public void run()
{
System.out.println("Cube:"+(num*num*num));
}
}

class setb3
{
public static void main(String arg[])
{
RandomThread rt=new RandomThread();
rt.start();
}
}

Assignment No. 3

SetA1 ===>

import java.sql.*;
import javax.swing.*;
import java.awt.*;
import java.util.*;

class JDBC extends JFrame


{
JDBC()
{
try
{
setSize(300,300);
setTitle("JDBC Window");
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Class.forName("org.postgresql.Driver");
Connection
con=DriverManager.getConnection("jdbc:postgresql:project","postgres"," ");
Statement st=con.createStatement();
ResultSet rs=st.executeQuery("Select * from project");
JTable jt=new JTable(20,4);
int rno=0;
while(rs.next())
{
jt.setValueAt(Integer.toString(rs.getInt(1)),rno,0);
jt.setValueAt(rs.getString(2),rno,1);
jt.setValueAt(rs.getString(3),rno,2);
jt.setValueAt(rs.getString(4),rno,3);
rno++;
}
add(jt);
}catch(Exception e)
{
System.out.println("Error"+e);
}
}

public static void main(String arg[])


{
new JDBC();
}
}

SetA2 ===>

import java.util.*;
import java.sql.*; //step-1
class SetA2
{
public static void main(String args[])
{
try
{
Class.forName("org.postgresql.Driver"); //step-2
Connection
con=DriverManager.getConnection("jdbc:postgresql:project","postgres","");//step-3
Statement st=con.createStatement();
DatabaseMetaData db=con.getMetaData();
ResultSet rs=db.getTables(null,null,null,new String[]{"TABLE"});
System.out.println("JDBC Driver Name: "+db.getDriverName());
System.out.println("DB Version: "+db.getDatabaseProductVersion());
System.out.println("Product Name: "+db.getDatabaseProductName());
System.out.println("User Name: "+db.getUserName());
System.out.println("Catalogs Name: "+db.getCatalogs());
}
catch(Exception e)
{
System.out.println("Error "+e);
}

}
}

SetA3 ===>

import java.util.*;
import java.sql.*; //step-1
class donar
{
public static void main(String args[])
{
try
{
Class.forName("org.postgresql.Driver"); //step-2
Connection
con=DriverManager.getConnection("jdbc:postgresql:project","postgres","");//step-3
Statement st=con.createStatement();
ResultSet rs=st.executeQuery("select * from donar");
ResultSetMetaData rm=rs.getMetaData();
int noOfColumns=rm.getColumnCount();
System.out.println("Number of Columns="+noOfColumns);

for(int i=1;i<=noOfColumns;i++)
{
System.out.println("Column No.: "+i);
System.out.println("Column Name: "+rm.getColumnName(i));
System.out.println("Column Type: "+rm.getColumnTypeName(i));
System.out.println("Column size: "+rm.getColumnDisplaySize(i));
}
}catch(Exception e)
{
System.out.println("Error "+e);
}

}
}

SetB1 ===>

import java.util.*;
import java.sql.*;

class SetB1
{
public static void main(String arg[]) throws Exception
{
Class.forName("org.postgresql.Driver");
Connection
con=DriverManager.getConnection("jdbc:postgresql:project","postgres"," ");
Statement st=null;
PreparedStatement pst=null;
Scanner sc=new Scanner(System.in);
while(true)
{
System.out.println("Operations to be perform on Mobile :");
System.out.println(" 1.INSERT\n 2.MODIFY\n 3.DELETE\n 4.SEARCH\n 5.VIEW\n
6.EXIT\n");
System.out.println("Enter your choice:");
int ch=sc.nextInt();
switch(ch)
{
case 1:
System.out.println("Enter the Model Number:");
int mn=sc.nextInt();
System.out.println("Enter the Model Name:");
String mna=sc.next();
System.out.println("Enter the Model Color:");
String mc=sc.next();
System.out.println("Enter the Sim Type:");
String sm=sc.next();
System.out.println("Enter the Network Type:");
String nt=sc.next();
System.out.println("Enter the Battery Capacity:");
String bc=sc.next();
System.out.println("Enter the Internal Storage:");
String is=sc.next();
System.out.println("Enter the RAM:");
String ram=sc.next();
System.out.println("Enter the Processor Type:");
String pt=sc.next();

pst=con.prepareStatement("Insert into Mobile


values(?,?,?,?,?,?,?,?,?)");
pst.setInt(1,mn);
pst.setString(2,mna);
pst.setString(3,mc);
pst.setString(4,sm);
pst.setString(5,nt);
pst.setString(6,bc);
pst.setString(7,is);
pst.setString(8,ram);
pst.setString(9,pt);

pst.executeUpdate();
System.out.println("Record Insereted !!!");
break;
case 2:
System.out.println("Enter the Model Number:");
mn=sc.nextInt();
System.out.println("Enter the Network Type:");
nt=sc.next();

pst=con.prepareStatement("update mobile set network_type=? where


model_number=?");
pst.setString(1,nt);
pst.setInt(2,mn);
int n=pst.executeUpdate();
System.out.println("No. of Records updated:"+n);
break;
case 3:
System.out.println("Enter the Model Number you want to delete:");
mn=sc.nextInt();
pst=con.prepareStatement("delete from mobile where model_number=?");
pst.setInt(1,mn);
n=pst.executeUpdate();
System.out.println("No. of Records deleted:"+n);
break;
case 4:
System.out.println("Enter the Model Number you want to search:");
mn=sc.nextInt();
pst=con.prepareStatement("select * from mobile where model_number=?");
pst.setInt(1,mn);
ResultSet rs1=pst.executeQuery();
if(rs1.next())
{
System.out.println(rs1.getInt(1)+"\t"+rs1.getString(2)+"\
t"+rs1.getString(3)+"\t"+rs1.getString(4)+"\t"+rs1.getString(5) +"\
t"+rs1.getString(6)+"\t"+rs1.getString(7)+"\t"+rs1.getString(8)+"\
t"+rs1.getString(9));
}
else
System.out.println("Records Not Found !!!");
break;
case 5:
pst=con.prepareStatement("select * from mobile");
rs1=pst.executeQuery();
while(rs1.next())
{
System.out.println(rs1.getInt(1)+"\t"+rs1.getString(2)+"\
t"+rs1.getString(3)+"\t"+rs1.getString(4)+"\t"+rs1.getString(5) +"\
t"+rs1.getString(6)+"\t"+rs1.getString(7)+"\t"+rs1.getString(8)+"\
t"+rs1.getString(9));
}
break;

case 6:
System.exit(0);
break;
}
}
}
}

Assignment No. 4

Ex.1 ===>

import java.util.*;
import java.io.*;
import javax.servlet.*;
public class Servlet1 extends GenericServlet
{
public void service(ServletRequest req,ServletResponse res)
{
try
{
PrintWriter out=res.getWriter();
System.out.println("Welcome to Servlet Programming");
}
catch(Exception e)
{
System.out.println("Error"+e);
}
}
}

ex.2 ===>

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.*;
import java.sql.*;
import java.text.*;

public class ServletJdbc extends HttpServlet


{
public void doGet(HttpServletRequest req,HttpServletResponse res) throws
ServletException,IOException
{
res.setContentType("text/html");
PrintWriter out=res.getWriter();
try
{
out.println("<html>");
out.println("<body>");

Class.forName("org.postgresql.Driver");
out.println("<h1>Driver Loaded</h1>");

Connection
con=DriverManager.getConnection("jdbc:postgresql:project","postgres","");
out.println("<h1>Connection Created</h1>");

Statement st=con.createStatement();
ResultSet rs=st.executeQuery("Select * from project");
while(rs.next())
{

out.print("<h3>"+rs.getInt(1)+"-"+rs.getString(2)+"-"+rs.getString(3)+"-"+rs.getStr
ing(4)+"</h3>");

out.println("<br>");
}
}
catch(Exception e)
{
}

out.println("<h1> Hii Ramesh !!!!</h1>");


out.println("</body>");
out.println("</html>");
}
}

ex3 ===>

import java.util.*;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class SubServlet extends HttpServlet


{
public void doGet(HttpServletRequest req,HttpServletResponse res) throws
ServletException,IOException
{
int num1=Integer.parseInt(req.getParameter("no1"));
int num2=Integer.parseInt(req.getParameter("no2"));
int sub=num1-num2;
res.setContentType("text/html");
PrintWriter pw=res.getWriter();
pw.println("<h1>Substraction</h1><h3>"+sub+"</h3>");
pw.close();
}
}

You might also like