Javascript required

Semester VI


Bsc.Physical Science Computer Science (CBCS) Programs


JAVA Script Programs



Question 1 :  Create a student registration form. Create functions to perform the following checks:
                a. Roll number is a 7-digit numeric value
                b. Name should be an alphabetical value(String)
                c. Non-empty fields like DOB
                                
 Code
                                    <html>
<head>
    <title>REGISTRATION FORM</title>
    <script>
    function validate_form()
    {
        alert("hello");
        if (document.login.rollno.value=="")
        {
            alert("roll no can't be blank");
            document.login.rollno.focus();
        }
        else if(isNaN(document.login.rollno.value))
        {
            alert("roll no should be numeric");
            document.login.rollno.focus();
        }
        else if (document.login.rollno.value.length!=7)
        {
            alert("roll no should be 7 digits");
            document.login.rollno.focus();
        }
        if (document.login.name1.value=="")
        {
            alert("name can't be left blank");
            document.login.name1.focus();
        }
        else if(!login.name1.value.match(/^[a-z A-Z]+$/))
        {
            alert("name must be alphabets");
            login.name1.value="";
            login.name1.focus();
        }
        if (document.login.dob.value=="")
        {
            alert("d.o.b. can't be blank ");
            document.login.dob.focus();
        }
    }
    </script>
</head>
<body>
<b><u><font color=blue><h2> Student Registration form</h2></b></u>
<form name="login">
Rollno:<input type="text" name="rollno"><br><br> 
Name:<input type="text" name="name1"><br><br> 
dob:<input type="date" name="dob"><br><br>
<input type="button" value="Register" onclick="validate_form()">
</form>
</body>
</html>
                                

Question 2 : Implement a static password protection.
                                
 Code
                                    <html>
<head>
    <title> user login </title>
    <script>
    function verify()
    {
        alert("Hello");
        if(document.f.user.value=="" && document.f.pass1.value=="" )
        {
            alert("please enter username");
            document.f.user.focus();
            alert("please enter password");
            document.f.pass1.focus();
        }
        if(document.f.user.value=="admin" && document.f.pass1.value=="1234")
        {
            alert("successfully logged in");
            document.f.user.focus();
            document.f.pass1.focus();
        }
        else
        {
            alert("invalid username or password");
            alert("please try again");
        }
    }
    </script>
</head>
<body>
<b><u><font color=blue><h2>password protection</h2></font></u></b> 
<form name="f">
username:<input type="text" name="user" value=""><br><br> 
password:<input type="password" name="pass1" value=""><br><br>
<input type="reset" name="rst" value="RESET">
<input type="button" name="SIGN_IN" value="sign in" onClick="verify()"><br><br>
</form>
</body>
</html>
                                

Question 3 : Write a java script
               a. To change the colour of text using SetTimeOut()
               b. To move an image across screen using SetInterval()
                                
 3(a) Code
                                    <html>
<body>
<script type="text/javascript">
function changeColor()
{
    var i=document.getElementById("h");
    i.style.backgroundColor="red";
    SetTimeOut("changeColor()",10000);
}
</script>
<h1 id ="h">changetextbackgroundcolor</h1>
<input type="button" name="button1" value="change" onClick="changeColor()">
</body>
</html>
                                
 3(b) Code
                                    <html>
<body>
<script type="text/javascript">
function changeColor()
{
    var i=document.getElementById("h");
    i.style.color="red";
    SetTimeOut("changeColor()",10000);
}
</script>
<h1 id ="h">changetextbackgroundcolor</h1>
<input type="button" name="button1" value="change" onClick="changeColor()">
</body>
</html>
                                

JAVA Programs



Question 1 : WAP to find the largest of n natural numbers.
                                
 Java Code
                                          check.java
                                    public class Check {
public static void main(String[] args) {
    go g=new go();
    g.match();
}
}
                                
 go.java
                                    public class go {
int array[]=new int[10];
int number=0;
int sum1,sum2,sum3;
void match()
{
    System.out.print("Enter the elements of an array : ");
    Scanner s=new Scanner(System.in);
    for(int i=0;i<10;i++)
        {
            array[i]=s.nextInt();
        }
    System.out.println("Enter a Number which you want to match : ");
    Scanner S=new Scanner(System.in);
    number=S.nextInt();
    for(int i=0;i<10;i++)
        {
            if(array[i]==number)
                sum1=sum1+1;
            else if(array[i]>number)
                sum2=sum2+1;
            else
                sum3=sum3+1;
        }
    System.out.println("Number equal to the given number is"+" "+sum1);
    System.out.println("Number greater than to the given number is"+" "+sum2);
    System.out.println("Number less than to the given number is"+" "+sum3);

}
}
                                

Quextion 2 : WAP to find whether a given number is prime or not.
                                
 Java Code
                                          prime.java
                                    public class Prime {
    public static void main(String[] args) {
        int num = 29;
        boolean flag = false;
        for(int i = 2; i <= num/2; ++i)
        {
            // condition for nonprime number
            if(num % i == 0)
            {
                flag = true;
                break;
            }
        }
        if (!flag)
            System.out.println(num + " is a prime number.");
        else
            System.out.println(num + " is not a prime number.");
    }
}
                                

Question 3 : WAP to print the sum and product of digits of an Integer and reverse the Integer.
                                
 Java Code
                                          reverse_sum.java
                                    public class reverse_sum
{
    public static void main(String[] args)
    {
        int n, a, m = 0, sum = 0;
        Scanner s = new Scanner(System.in);
        System.out.print("Enter any number:");
        n = s.nextInt();
        do
        {
            a = n % 10;
            m = m * 10 + a;
            sum = sum + a;
            n = n / 10;
        }
        while( n > 0);
        System.out.println("Reverse:"+m);
        System.out.println("Sum of digits:"+sum);
    }
}
                                

Question 4 : Write a program to create an array of 10 integers. Accept values from the user in that
             array. Input another number from the user and find out how many numbers are equal to
             the number passed, how many are greater and how many are less than the number passed.
                                
 Java Code
                                          Question4.java
                                    public class Question4 {
    public static void main(String[] args) {
        int number,equal = 0,less = 0,greater = 0;
        int arr[] = new int[10];
        Scanner s = new Scanner(System.in);
        System.out.println("/t/tEnter 10 elements in an array");
        for(int i=0;i<10;i++)
        {
            System.out.println("Enter "+(i+1)+" Element : ");
            arr[i] = s.nextInt();
        }
        System.out.println("Enter any Number : ");
        number = s.nextInt();
        for(int i=0;i<10;i++)
        {
            if(arr[i]==number)
            {
                equal = equal +1;
            }
            else if(arr[i]<number)
            {
                less = less + 1;
            }
            else if(arr[i]>number)
            {
                greater = greater + 1;
            }
            else
                System.out.println("Wrong Input !!");
        }
        System.out.println(equal+" Numbers are equal to the given number");
        System.out.println(less+" Numbers are there which are less than the given number");
        System.out.println(greater+" Numbers are there which are greater than the given number");
    }
}
                                

Question 5 : Write java program for the following matrix operations:
               a. Addition of two matrices
               b. Summation of two matrices
               c. Transpose of a matrix
               Input the elements of matrices from user
                                
 Java Code
                                          matrix.java
                                    public class Matrix {
    public static void main(String[] args) {
        int n = 0;
        Scanner s  = new Scanner(System.in);
        System.out.println("Enter the size of the matrix : ");
        n = s.nextInt();
        int a[][] = new int[n][n];
        int b[][] = new int[n][n];
        int c[][] = new int[n][n];
        System.out.println("..............Matrix A...........");
        System.out.println("Enter elements of matrix A : ");
        for(int i=0;i<n;i++)
        {
            for(int j=0;j<n;j++)
            {
                a[i][j] = s.nextInt(); 
            }
        }
        System.out.println("..............Matrix B...........");
        System.out.println("Enter elements of matrix B : ");
        for(int i=0;i<n;i++)
        {
            for(int j=0;j<n;j++)
            {
                b[i][j] = s.nextInt(); 
            }
        }
        for(int i=0;i<n;i++)
        {
            for(int j=0;j<n;j++)
            {
                c[i][j] = a[i][j]+b[i][j];
            }
        }
        System.out.println("Addition/Summation of two matrices : ");
        for(int i=0;i<n;i++)
        {
            for(int j=0;j<n;j++)
                System.out.print(c[i][j]+" ");
            System.out.println();
              
        }
    }
}
                                

Question 6 : Write a java program that computes the area of a circle, rectangle and a Cylinder using
             function overloading.
                                
 Java Code
                                          Overload.java
                                    class Overload
{
    public static void main(String args[])
    {
        OverloadDemo ob = new OverloadDemo();
        ob.area(5);
        ob.area(11,12);
        ob.area(2.5);
    }
}
                                
 
                                         OverloadDemo.java
                                    class OverloadDemo
{
    void area(float x)
    {
        System.out.println("the area of the square is "+Math.pow(x, 2)+" sq units");
    }
    void area(float x, float y)
    {
        System.out.println("the area of the rectangle is "+x*y+" sq units");
    }
    void area(double x)
    {
        double z = 3.14 * x * x;
        System.out.println("the area of the circle is "+z+" sq units");
    }
}
                                

JDBC Program



Question 1 : Create a table 'Student' and 'Teacher' in 'College' database and insert two rows in this
             newly created table using JDBC API and do the following:
               a. Update an already created table 'Teacher' in 'College' database by updating a
                  teacher's name, with "Dr." appended before the name, whose name is "Rita".
                                
 Java Code
                                          Teacher.java
                                    import java.sql.*;
public class Teacher 
{
    public static void main(String[] args) {
    Statement s;
    ResultSet rs = null;
    Connection db;
    try
    {
        Class.forName("net.ucanaccess.jdbc.UcanaccessDriver");
        db=DriverManager.getConnection("jdbc:ucanaccess://C:\\Users\\utteralley\\Documents\\teacher.accd b");
        s=db.createStatement();
        String q="UPDATE teacher SET tname=CONCAT('DR',tname) WHERE tname='rita'";
        String q1="SELECT * FROM teacher";
        s.executeUpdate(q);
        rs=s.executeQuery(q1);
        while(rs.next())
        System.out.println(rs.getString(1)+" "+rs.getString(2)+" "+rs.getString(3));
    }
    catch(Exception e)
    {
        System.out.println(e.getMessage());
    }
}
}
                                

    b. Repeat the same thing for all the teachers using PreparedStatement.
                                
 
                                          Teacher.java
                                    import java.sql.*;
public class Teachercomplile 
{
    public static void main(String[] args) 
    {
        Statement s;
        ResultSet r = null;
        Connection db;
        PreparedStatement pt;
        try
        {
            Class.forName("net.ucanaccess.jdbc.UcanaccessDriver");
            db=DriverManager.getConnection("jdbc:ucanaccess://C:\\Users\\utteralley\\Documents\\teacher.accd b");
            s=db.createStatement();
            String q="UPDATE teacher SET tname=CONCAT('DR',tname) WHERE tname=?";
            r=s.executeQuery("SELECT tname FROM teacher WHERE tname NOT LIKE '%DR%'");
            String q1="SELECT * FROM teacher";
            r=s.executeQuery(q1);
            while(r.next())
            {
                pt=db.prepareStatement(q);
                pt.setString(1,r.getString("tname"));
                pt.executeUpdate();
                System.out.println(r.getInt(1)+" "+r.getString(2)+" "+r.getString(3));
            }
        }
        catch(Exception e)
        {
            System.out.println(e.getMessage());
        }
    }
}
                                

    c. Delete the student with ID=3 from 'Student' database.
                                
 
                                          Db.java
                                    import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
public class Db 
{
    public static void main(String[] args) {
    Statement s;
    ResultSet rs = null;
    Connection db;
    try
    {
        Class.forName("net.ucanaccess.jdbc.UcanaccessDriver");
        db=DriverManager.getConnection("jdbc:ucanaccess://C:\\Users\\utteralley\\Documents\\student.accd b");
        s=db.createStatement();
        String p="DELETE FROM student WHERE sno=3";
        s.executeUpdate(p);
        String q="select * from student";
        rs=s.executeQuery(q);
        while(rs.next())
        System.out.println(rs.getInt(1)+" "+rs.getString(2)+" "+rs.getString(3));
    }
    catch(Exception e)
    {
        System.out.println(e.getMessage());
    }
}
}
                                

    d. Insert two students to the ResultSet returned by the query which selects all students
       with FirstName="Ayush". The database must also get updated along with
       ResultSet.
                                
 
                                          Db_4.java
                                    import java.sql.*;
public class Db_4 
{
    public static void main(String[] args) 
    {
        Statement s;
        ResultSet rs = null;
        Connection db;
        try
        {
            Class.forName("net.ucanaccess.jdbc.UcanaccessDriver");
            db=DriverManager.getConnection("jdbc:ucanaccess://C:\\Users\\utteralley\\Documents\\student.accd b");
            s=db.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
            String q="select * from student WHERE fname = 'ayush'";
            rs=s.executeQuery(q);
            rs.moveToInsertRow();
            rs.updateInt("sno",9);
            rs.updateString("fname","ayush");
            rs.updateString("address","raja garden" );
            rs.insertRow();
            rs.moveToCurrentRow();
            System.out.println("row inserterd");
            String q1="SELECT * FROM student";
            rs = s.executeQuery(q1);
            while(rs.next())
                System.out.println(rs.getString(1)+" "+rs.getString(2)+" "+rs.getString(3));
        }
        catch(Exception e)
        {
            System.out.println(e.getMessage());
        }
    }
}
                                

JSP Programs



Question 1 : Display the pattern:
               1
               1 2
               1 2 3
             Take 'n' in a textbox from user. Display this pattern using
             a. Scriptlets
             b. <c:forEach> loop
                                
 Using Scriplets
                                         pattern.html 
                                    <!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Pattern</title>
</head>
<body>
<form action = "access.jsp" method = "POST">
Input N: <input type = "text" name = "val">
<br />
<input type = "submit" value = "Submit" />
</form>
</body>
</html>
                                
 
                                         access.jsp 
                                    <!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Pattern</title>
</head>
<body>
<h2>Pattern </h2>
 <%
  int val=Integer.parseInt(request.getParameter("val"));
  for(int i=1;i<=val;i++)
  {
    for(int j=1;j<=i;j++)
    {
      out.print(j+" ");
    }
    out.println("<br />");
  }
%>
</html>
                                
 Using <c:forEach> loop
                                         pattern.html 
                                    <!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Pattern</title>
</head>
<body>
<form action = "access.jsp" method = "POST">
Input N: <input type = "text" name = "val">
<br />
<input type = "submit" value = "Submit" />
</form>
</body>
</html>
                                
 
                                         access.jsp 
                                    <!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Pattern</title>
</head>
<body>
<h2>Pattern </h2>
 <%
  int val=Integer.parseInt(request.getParameter("val"));
 %>
 <c:forEach var = "i" begin = "1" end = "${val}">
      <c:forEach var = "j" begin = "&{i}" end = "&{val}">
          <c:out value = "${j}" />
       </c:forEach>
       <br />
  </c:forEach>
</html>
                                

Question 2 :  Make two files as follows:
                a. main.html: shows 2 text boxes and 3 radio buttons with values "addition",
                   "subtraction" and "multiplication"
                b. operate.jsp: depending on what the user selects perform the corresponding function
                   (Give two implementations: using request.getParameter() and using expression
                   language)
                                
 Code
                                          main.html
                                    <html>
<head>
    <title>welcome</title>
</head>
<body>
<form method=POST action="operators.jsp">
Enter first Number:<input type=text name=num1><br><br> Enter second Number:<input type=text name=num2>
<br><br>
<input type="radio" name="operation" value="add">addition<br>
<input type="radio" name="operation" value="multiply">multiplication<br>
<input type="radio" name="operation" value="subtract">subtraction<br>
<input type=submit value="submit">
</form>
<body>
</html>
                                
 
                                         operate.jsp
                                    <html>
<head>
    <title>welcome</title>
</head>
<body>
<%
int result=0;
String s1=request.getParameter("num1");
int n1=Integer.parseInt(s1);
String s2=request.getParameter("num2");
int n2=Integer.parseInt(s2);
if(request.getParameter("operation")!=null)
    {
        if(request.getParameter("operation").equals("add"))
        {
            out.println(n1+n2);
        }
        else if(request.getParameter("operation").equals("multiply"))
        {
            out.println(n1*n2);
        }
        else
        {
            out.println(n1-n2);
        }
    }
%>
</body>
</html>
                                

Question 4 : Display Good Morning <uname>, Good Afternoon <uname> or Good Evening <uname>
             based on the current time of the day.
                                
 Html Code
                                          main.html
                                    <html>
<body>
<form method=post action="date.jsp"> Name:<input type=text name=username><br><br>
<input type=submit value="submit"> </form>
</body>
</html>
                                
 
                                         date.jsp
                                    <%@ page import="java.io.* , java.util.*,javax.servlet.*"%> <%@page import="java.util.*"%>
<html>
<head>
<title>Welcome</title>
<body>
<%Date time=new Date();%>
<%out.println(time.getHours());%>
<% int hour=time.getHours();
if(hour<12)
out.println("Good Morning :" + request.getParameter("username")); else if(hour<=17)
out.println("Good Afternoon :" + request.getParameter("username")); else
out.println("Good Evening :" + request.getParameter("username"));
%>
</body>
</html>