Tuesday, September 20, 2022

Advance Java Solve practical Slips

 Slip _1


Q.1. Advanced Java: A) Write a java program to scroll the text from left to right and vice versa continuously.

Slip1A.java

import java.awt.*; 

import java.applet.*;

public class slip1A extends Applet implements Runnable 

 { 

      String banner="Hello "; 

      int state; 

      boolean stopflag; 

            

      public void init() 

      { 

           setBackground(Color.black); 

           setForeground(Color.green); 

      } 

       

      public void start() 

      { 

           Thread t=new Thread(this); 

           stopflag=true; 

           t.start(); 

      } 

       

 public void run() 

      { 

           char ch; 

           try 

           { 

                while(true) 

                { 

                     repaint(); 

                     Thread.sleep(150); 

                     ch=banner.charAt(0); 

                     banner = banner.substring(1,banner.length()); 

                     banner +=ch; 

                      

                } 

           } 

           catch (InterruptedException e) 

           { 

                System.out.println(e); 

           } 

      } 

       

public void stop() 

      { 

           stopflag=false; 

            Thread t=null; 

      } 

       

public void paint(Graphics g) 

      { 

           Font a = new Font("Impact",Font.BOLD,45); 

           g.setFont(a); 

           g.drawString(banner,10,60); 

      } 

       

 }  



Slip1 A html

<html>

<head>

<title>Reverse order</title>

</head>

<body>

<applet code="slip1A.class" height="100" width="350"></applet> 

</body>

</html>


 [15 M] B) Write a socket program in java for chatting application.(Use Swing) [25 M]





 Slip2 :


 A) Write a JSP program to check whether given number is Perfect or not. (Use Include directive). [15 M]

Slip2A.html


<!DOCTYPE html>

<html>

<head>

<title></title>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

</head>

<body>

<form action="Slip2A.jsp" method="post">

Enter Number :

<input type="text" name="num">

<input type="submit" value="submit">

</form>

</body>

</html>



Slip2A.jsp


<%@page contentType="text/html" pageEncoding="UTF-8"%>

<!DOCTYPE html>

<%

int num = Integer.parseInt(request.getParameter("num"));

int n = num;

    // To store sum of divisors

    int sum = 1;

    // Find all divisors and add them

    for (int i = 2; i * i <= n; i++)

    {

        if (n % i==0)

        {

            if(i * i != n)

                sum = sum + i + n / i;

            else

                sum = sum + i;

        }

    }

    // If sum of divisors is equal to

    // n, then n is a perfect number

    if (sum == n && n != 1)

{

out.println("perfect Number");

}

else

{

out.println("Not perfect Number");

}

%>



 B) Write a java program in multithreading using applet for drawing flag. [25 M]

Slip2B.java

import java.awt.*;

import java.applet.*;

public class slip2B extends Applet

{

public void paint(Graphics g)

{

g.setColor(Color.blue);

g.fillRect(50,20,5,300);

g.setColor(Color.black);

g.drawRect(50,18,3,300);

g.setColor(Color.orange);

g.fillRect(55,20,120,30);

g.setColor(Color.black);

g.drawRect(55,20,118,28);

g.setColor(Color.green);

g.fillRect(55,80,119,30);

g.setColor(Color.black);

g.drawRect(55,80,117,28);

g.setColor(Color.black);

g.drawOval(100,50,30,30);

}

}


Slip2B.html


<html>

   <head>

   </head>

   <body>

         <applet code = "slip2B.class" width = "320" height = "480"></applet>

   </body>

</html>

 

Slip  4


A) Write a Java Program to delete details of students whose initial character of their name is ‘S’. [15 M]

Slip4A.java


import java.sql.*;

public class slip4A

{

public static void main(String arg[])

{

Connection con;

Statement stmt;

try

{

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

con=DriverManager.getConnection("jdbc:odbc:shamal");

if(con==null)

{

System.out.println("Connection failed");

System.exit(1);

}

System.out.println("Connection established");

stmt=con.createStatement();

int no=stmt.executeUpdate("Delete From Student where sname like'S%' ");

if(no!=0)

System.out.println("data deleted sucessfully");

else

System.out.println("data not deleted");

con.close();

}catch(Exception e)

{

System.out.println(e);

}

}

}


B) Write a SERVLET program that provides information about a HTTP request from a client, such as IP address and browser type. The servlet also provides information about the server on which the servlet is running, such as the operating system type, and the names of currently loaded servlets.



Slip 5


A) Write a JSP program to calculate sum of first and last digit of a given number. Display sum in Red Color with font size 18. [15 M] 

Slip5A.jsp

<!DOCTYPE html>

<html>

<body>

<%! int n,rem,r; %>

<% n=Integer.parseInt(request.getParameter("num"));

if(n<10)

{

out.println("Sum of first and last digit is ");

%><font size=18 color=red><%= n %>

<%

}

else

{

rem=n%10;

do

{

r=n%10;

n=n/10;

}while(n>0);

n=rem+r;

out.println("Sum of first and last digit is ");

%><font size=18 color=red><%= n %>

<%

}

%>

</body>

</html>


Slip5A.html

<html>

<body>

<form method=post action="Slip5A.jsp">

Enter Any Number : <Input type=text name=num>

<input type=submit value=Display>

</form>

</body>

</html>



B) Write a java program in multithreading using applet for Traffic signal. [25 M]


Slip5B.html


<html>

   <head>

   </head>

   <body>

         <applet code = "slip2B.class" width = "320" height = "480"></applet>

   </body>

</html>


Slip5B.java

import java.awt.*;

import java.applet.*;

public class slip5B extends java.applet.Applet implements Runnable

{

int r,g1,y,i;

Thread t;

public void init()

{

r=0;g1=0;y=0;

t=new Thread(this);

t.start();

}

public void run()

{

try

{

for(i=24;i>=1;i--)

{

t.sleep(100);

if(i>=16 && i<24)

{

g1=1;

repaint();

}

if(i>=8 && i<16)

{

y=1;

repaint();

}

if(i>=1 && i<8)

{

r=1;

repaint();

}

}

if(i==0)

{

run();

}

}catch(Exception e){}

}

public void paint(Graphics g)

{

g.drawOval(100,100,100,100);

g.drawOval(100,225,100,100);

g.drawOval(100,350,100,100);

if(r==1)

{

g.setColor(Color.red);

g.fillOval(100,100,100,100);

g.drawOval(100,350,100,100);

r=0;

}

if(g1==1)

{

g.setColor(Color.green);

g.fillOval(100,225,100,100);

g.drawOval(100,225,100,100);

g1=0;

}

if(y==1)

{

g.setColor(Color.yellow);

g.fillOval(100,350,100,100);

g.drawOval(100,350,100,100);

y=0;

}

}

}


Slip  7  


A) Write a JSP script to validate given E-Mail ID. [15 M]

Slip7A.html

<!DOCTYPE html>

<html>

<head>

<meta charset="ISO-8859-1">

<title>Insert title here</title>

</head>

<body>

<form name=f1 action="slip7A.jsp">

<fieldset>

<legend>Enter Email Id...!!!</legend>

        Enter Email Id:<input type="text" name="t1" >

</fieldset>

<div align=center>

<input type="submit" name="Submit" value="Submit">

</div>

</form>


</body>

</html>


Slip7A.jsp

<html>

<body>

<%@ page language="java" %>

<%

        String email=request.getParameter("t1");

        if(email.contains("@") && email.contains("."))

        {

        out.println("Given Email Id is Valid");

        }

        else        {

        out.println("Given Email id is not Valid");

        }

        %>

</body>

</html>




 B) Write a Multithreading program in java to display the number’s between 1 to 100 continuously in a TextField by clicking on button. (use Runnable Interface)



Slip 8


A) Write a Java Program to display all the employee names whose initial character of a name is ‘A’. [15 M] 

Slip8A.java

import java.sql.*;

public class slip8A

{

public static void main(String arg[])

{

Connection con;

Statement stmt;

ResultSet rs;

try

{

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

con=DriverManager.getConnection("jdbc:odbc:shamal");

if(con==null)

{

System.out.println("Connection failed");

System.exit(1);

}

System.out.println("Connection established");

stmt=con.createStatement();

rs=stmt.executeQuery("Select * From employee where ename like'A%' ");

while(rs.next())

{

System.out.println("\n"+rs.getInt(1)+" "+rs.getString(2)+" "+rs.getString(3));

}

rs.close();

con.close();

}catch(Exception e)

{

System.out.println(e);

}

}

}




B) Write a java program in multithreading using applet for Digital watch. [25 M] 

Slip8B.html


<html>  

<body>  

<applet code="slip8B.class" width="300" height="300">  

</applet>  

</body>  

</html>  


Slip8.java

import java.applet.*;  

import java.awt.*;  

import java.util.*;  

import java.text.*;  

  

public class slip8B extends Applet implements Runnable {  

  

   Thread t = null;  

   int hours=0, minutes=0, seconds=0;  

   String timeString = "";  

  

   public void init() {  

      setBackground( Color.green);  

   }  

  

   public void start() {  

        t = new Thread( this );  

        t.start();  

   }  

  

    

   public void run() {  

      try {  

         while (true) {  

  

            Calendar cal = Calendar.getInstance();  

            hours = cal.get( Calendar.HOUR_OF_DAY );  

            if ( hours > 12 ) hours -= 12;  

            minutes = cal.get( Calendar.MINUTE );  

            seconds = cal.get( Calendar.SECOND );  

  

            SimpleDateFormat formatter = new SimpleDateFormat("hh:mm:ss");  

            Date date = cal.getTime();  

            timeString = formatter.format( date );  

  

            repaint();  

            t.sleep( 1000 );  // interval given in milliseconds  

         }  

      }  

      catch (Exception e) { }  

   }  

  

    

  public void paint( Graphics g ) {  

      g.setColor( Color.blue );  

      g.drawString( timeString, 50, 50 );  

   }  

}  


Slip 9

A) Write a Java Program to create a Emp (ENo, EName, Sal) table and insert record into it. (Use PreparedStatement Interface) [15 M] 

Slip9.java

import java.io.*;

import java.sql.*;

class slip9A

{

public static void main(String arg[])throws IOException

{

try

{

Connection con;

Statement stmt;

PreparedStatement ps;

ResultSet rs;

BufferedReader br=new BufferedReader(new InputStreamReader(System.in));

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

con=DriverManager.getConnection("jdbc:odbc:shamal");

System.out.println("connection established....");

stmt=con.createStatement();

ps=con.prepareStatement("insert into Emp values(?,?,?)");

System.out.println("Enter the eno:");

int Eno=Integer.parseInt(br.readLine());

ps.setInt(1,Eno);

System.out.println("Enter the ename:");

String Ename=br.readLine();

ps.setString(2,Ename);

System.out.println("Enter the salary:");

int Sal=Integer.parseInt(br.readLine());

ps.setInt(3,Sal);

ps.executeUpdate();

System.out.println("record inserted....");

rs=stmt.executeQuery("select * from Emp");    

while(rs.next())

{

System.out.println("\n"+rs.getInt(1)+" "+rs.getString(2)+" "+rs.getInt(3));

rs.close();

stmt.close();

con.close();

}catch(Exception e)

{

System.out.println(e);

}

}

}


B) Write a JSP program to create an online shopping mall. User must be allowed to do purchase from two pages. Each page should have a page total. The third page should display a bill, which consists of a page total of whatever the purchase has been done and print the total. (Use Session)


Slip11


A) Write a java program to display IPAddress and name of client machine.[15 M] 

import java.net.InetAddress;

public class slip10A 

{

    public static void main(String args[]) throws Exception

    {

        InetAddress IP = InetAddress.getLocalHost();

        System.out.println("IP of my system is := "+IP.getHostAddress());

    }    

}


B) Write a Java program to display sales details of Product (PID, PName, Qty, Rate, Amount) between two selected dates. (Assume Sales table is already created).


Slip12

A) Write a java program to count the number of records in a table. [15 M]

Slip12A

import java.sql.*;


class slip12A

{

            public static void main(String args[])

            {

                        Connection con;

                        Statement stmt;                                             

                        try

                        {

                                    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

                                    con=DriverManager.getConnection("jdbc:odbc:shamal");

                                    if(con==null)

                                    {

                                                System.out.println("Connection Failed.......");

                                                System.exit(1);

                                    }

                                    System.out.println("Connection Established......");

                                    stmt=con.createStatement();

                                    ResultSet rs=stmt.executeQuery("select * from Student");


                                    int cnt=0;

                                    while(rs.next())

                                    {

                                                cnt++;

                                    }

                                    System.out.println("Number of records in Table are:"+cnt);

                        }


                        catch(Exception e)

                        {

                                    System.out.println(e);


                        }

            }

}

 B) Write a program in java which will show lifecycle (creation, sleep, and dead) of a thread. Program should print randomly the name of thread and value of sleep time.

The name of the thread should be hard coded through constructor. The sleep time of a thread will be a random integer in the range 0 to 4999.


Slip13


A) Write a java program to display name of currently executing Thread in multithreading. [15 M]

Slip13A

import java.sql.*;

public class slip13A extends Thread

{

public void run()

{

System.out.println(Thread.currentThread().getName());

}

public static void main(String arg[])

{

slip13A t1=new slip13A();

slip13A t2=new slip13A();

t1.start();

t2.start(); 

}

}


 B) Write a JSP program to display the details of College (CollegeID, Coll_Name, Address) in tabular form on browser.


Slip  14

A) Write a JSP program to accept Name and Age of Voter and check whether he is eligible for voting or not. [15 M]

Slip14.html


<html>

<body>

<form action="slip14A.jsp" method="get">

Enter Name:<input type="text" name="name"/>

<br>

Enter Age:<input type="text" name="age"/>

<br>

<input type="submit"/>


</form>

</body>

</html>


Slip14.jsp


<%@page contentType="text/html" pageEncoding="UTF-8"%>

<html>

<body>

<%String name = request.getParameter("name");%>

<%int age = Integer.parseInt(request.getParameter("age"));%>

<%if(age >=18)

{

out.println(name + "\nAllowed to vote");

}

else

{

out.println(name + "\nNot allowed to vote");

}

%>

</body>

</html>


 B) Write a Java program to display given extension files from a specific directory on server machine.


Slip 15

A) Write a java program to display each alphabet after 2 seconds between ‘a’ to ‘z’. [15 M] 

Slip15A.java

public class slip15A extends Thread

{

char c;

public void run()

{

for(c = 'A'; c<='Z';c++)

{

System.out.println(""+c);

try


{

Thread.sleep(3000);

}

catch(Exception e)

{

e.printStackTrace();

}

}

}

public static void main(String args[])

{

slip15A t = new slip15A();

t.start();

}

}

B) Write a Java program to accept the details of Student (RNo, SName, Per, Gender, Class) and store into the database. (Use appropriate Swing Components and PreparedStatement Interface).


Slip18


A) Write a java program to calculate factorial of a number. (Use sleep () method). [15 M] 

Slip18A

import java.io.*;

class slip18A{

public static void main(String arg[])throws InterruptedException

{

int i,fact=1;

int number=5;

for(i=1;i<=number;i++)

{

Thread.sleep(2000);

fact=fact*i;

}

System.out.println("factorial of "+number+" is:"+fact);

}

}


B) Write a java program for simple standalone chatting application. [25 M]


Slip   20

A) Write a JDBC program to delete the details of given employee (ENo EName Salary). Accept employee ID through command line. [15 M] 

Slip20 A

import java.sql.*;

class slip20A

{

public static void main(String a[])

{

Connection con;

PreparedStatement ps;

ResultSet rs;

try

{

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

con=DriverManager.getConnection("jdbc:odbc:shamal");

if(con==null)

{

System.out.println("Connection Failed....");

System.exit(1);

}

System.out.println("Connection Established...");

ps=con.prepareStatement("select * from employee where eid=?");

int id = Integer.parseInt(a[0]);

ps.setInt(1,id);


rs=ps.executeQuery();

System.out.println("eno\t"+"ename\t"+"department\t"+"sal"); 

while(rs.next())

{

System.out.println("\n"+rs.getInt(1)+"\t"+rs.getString(2)+"\t"+rs.getString(3)+"\t"+rs.getInt(4));

}

con.close();

}

catch(Exception e)

{

System.out.println(e);

}

}

}


B) Write a java program in multithreading using applet for drawing temple.

Slip20B.java

import java.applet.Applet;

import java.awt.*;

     

public class slip20B extends Applet

{

      public void paint(Graphics g)

      {

            g.drawRect(100,150,90,120);

            g.drawRect(130,230,20,40);

            g.drawLine(150,100,100,150);

            g.drawLine(150,100,190,150);

            g.drawLine(150,50,150,100);

            g.drawRect(150,50,20,20);



      }

}


Slip20 html

<html>

<head>

<title>Insert here</title>

</head>

<body>

<applet code=slip20B.class width=300 height=300></applet>

</body>

</html>


Slip21 

A) Write a java program to display name and priority of a Thread. [15 M] 

Slip21A.java

import java.io.*;

import java.sql.*;

public class slip21A implements Runnable

{

public void run()

{

  System.out.println(Thread.currentThread()); // This method is static.

}

public static void main(String[] args) 

{

slip21A a = new slip21A();

 Thread t = new Thread(a, "NewThread");

 t.setPriority(2); // Setting the priority of thread.

 

 System.out.println("Priority of Thread: " +t.getPriority());

 System.out.println("Name of Thread: " +t.getName());

 t.start();

  }

}


B) Write a SERVLET program in java to accept details of student (SeatNo, Stud_Name, Class, Total_Marks). Calculate percentage and grade obtained and display details on page.


Slip22

A) Write a java program to display Date and Time of Server machine on client machine. [15 M] 


B) Write a JSP program to accept the details of Account (ANo, Type, Bal) and store it into database and display it in tabular form.

Slip22.html

<html>

<body>

<form method=get action="slip22B.jsp">

Enter Account No. : <input type=text name=ano><br><br>

Enter Account Type:<input type=text name=type><br><br>

Enter Balance : <input type=text name=bal><br><br>

<input type=submit value="Save">

</form>

</body>

</html>


Slip22B.jsp

<html>

<body>

<%@ page import="java.sql.*;" %>

<%! int ano,bal;

       String type;  %>

<%

      ano=Integer.parseInt(request.getParameter("ano"));

      type=request.getParameter("type");

      bal=Integer.parseInt(request.getParameter("bal"));

      try{

            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

            Connection cn=DriverManager.getConnection("jdbc:odbc:shamal");

            PreparedStatement s=cn.prepareStatement("insert into Account values(?,?,?)");

            s.setInt(1,ano);

            s.setString(2,type);

            s.setInt(3,bal);

            s.executeUpdate();

            out.println("Record is saved");

            Statement st=cn.createStatement();

            ResultSet rs=st.executeQuery("select * from Account");

%>

<table border="1" width="40%">

<%      while(rs.next())

            {

%>

<tr> <td><%= rs.getInt("ano") %></td>

            <td><%= rs.getString("type") %></td>

            <td><%= rs.getInt("bal") %></td>

</tr>

<%

            }

            cn.close();

          }catch(Exception e)

            {      

                   out.println(e);      

            }

%>

</body>

</html>


Slip24

A) Create a JSP page to accept a number from a user and display it in words: Example: 123 – One Two Three. The output should be in red color. [15 M] 

Slip24A.html

<html>

<body>

<form method=get action="slip24.jsp">

Enter Any Number : <input type=text name=num><br><br>

<input type=submit value="Display">

</form>

<body>

</html>


Slip24A.jsp

<html>

<body>

<font color=red>

<%! int i,n;

       String s1;

%>

<%   s1=request.getParameter("num");

         n=s1.length();

         i=0;

         do

         {

           char ch=s1.charAt(i);

           switch(ch)

            {

                case '0': out.println("Zero  ");break;

                case '1': out.println("One  ");break;

                case '2': out.println("Two  ");break;

                case '3': out.println("Three  ");break;

                case '4': out.println("Four ");break;

                case '5': out.println("Five  ");break;

               case '6': out.println("Six  ");break;

               case '7': out.println("Seven  ");break;

               case '8': out.println("Eight  ");break;

               case '9': out.println("Nine  ");break;

           }

           i++;

         }while(i<n);

%>

</font>

</body>

</html>

B) Write a menu driven program in Java for the following: Assume Emp table with attributes ( ENo, EName, salary, Desg ) is already created. 1. Insert 2. Update 3. Delete 4. Search 5. Display 6. Exit.

Slip25B

import java.io.*;

import java.awt.*;

import javax.swing.*;

import java.sql.*;

import java.awt.event.*;

class slip25B extends JFrame implements ActionListener

{

JLabel l1;

JTextArea t1,t2;

JButton b1,b2,b3;

public static void main(String arg[])throws IOException

{

System.out.println("hello world");

new slip25B( );

}

slip25B()

{

l1=new JLabel("Type DDL Query");

t1=new JTextArea(30,30);

t2=new JTextArea(30,30);

b1=new JButton("Create Table");

b2=new JButton("Alter Table");

b3=new JButton("Drop Table");

b1.addActionListener(this);

b2.addActionListener(this);

b3.addActionListener(this);

setVisible(true);

setLayout(new GridLayout(2,3));

setSize(500,500);

add(l1);

add(t1);

add(t2);

add(b1);

add(b2);

add(b3);

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

}

public void actionPerformed(ActionEvent ae)

{

if(ae.getSource()==b1)

{

try

{

Connection con;

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

con=DriverManager.getConnection("jdbc:odbc:shamal");

String query=t1.getText();

Statement stmt=con.createStatement();

stmt.executeUpdate(query);

JOptionPane.showMessageDialog(null,"Table created");

stmt.close();

con.close();

}

catch(Exception e)

{

System.out.println(e);

}

}

if(ae.getSource()==b2)

{

try

{

Connection con;

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

con=DriverManager.getConnection("jdbc:odbc:shamal");

String query=t1.getText();

Statement stmt=con.createStatement();

stmt.executeUpdate(query);

JOptionPane.showMessageDialog(null,"Table Altered");

stmt.close();

con.close();

}

catch(Exception e)

{

System.out.println(e);

}

}

if(ae.getSource()==b3)

{

try

{

Connection con;

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

con=DriverManager.getConnection("jdbc:odbc:shamal");

String query=t1.getText();

Statement stmt=con.createStatement();

stmt.executeUpdate(query);

JOptionPane.showMessageDialog(null,"Table drop");

stmt.close();

con.close();

}

catch(Exception e)

{

System.out.println(e);

}

}

}

}


No comments:

Post a Comment