You are on page 1of 33

UNIVERSITY OF KALYANI

B.SC HONOURS 5TH SEMESTER ASSIGNMENT 2021


REG NO: 017505

ROLL NO: 2115120-180141

SUBJECT: INTERNET TECHNOLOGY


ASSIGNMENT COPY

SESSION: 2018-2019
INDEX
CONTENT PAGE
1. Print a table of numbers from 5 to 15 and their 1
squares and cubes using alert.

2. Print the largest of three numbers. 2-3

3. Find the factorial of a number n. 3

4. Enter a list of positive numbers terminated by 4


Zero. Find the sum and average of these numbers.

5. A person deposits Rs 1000 in a fixed account 4-5


yielding 5% interest. Compute the amount in the
account at the end of each year for n years. Read n
numbers. Count the number of negative numbers,
positive numbers and zeros in the list.

JAVA Script:

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
2. Implement a static password protection.

3. Write a java script a. To change the colour of


text using SetTimeOut() b. To move an image
across screen using SetInterval()
JAVA Programs:

1. WAP to find the largest of n natural numbers

2. WAP to find whether a given number is prime


or not.

3. WAP to print the sum and product of digits of


an Integer and reverse the Integer.

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.
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
6. Write a java program that computes the area of
a circle, rectangle and a Cylinder using function
overloading
JDBC:

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". b. Repeat the same
thing for all the teachers using
PreparedStatement. c. Delete the student with
ID=3 from 'Student' database. 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.
2. Create a procedure in MySQL to count the
number of Rows in table 'Student'. Use Callable
Statement to call this method from Java code.
JSP:

1. Display the patterns of numbers, characters etc.

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)
3. Validate User input entered in a form. The input
must include Name, DOB, Email ID, Lucky Number,
Favorite food etc.
P a g e |1

1. Print a table of numbers from 5 to 15 and their squares and cubes using alert.

Ans-

Code:

<html>

</head>

<title>table </title>

<script>

function at()

var res="";

for (i=5; i<=15; i++)

res +="Num "+(i)+ " : square "+(ii)+" : cube "+(ii*i)+"\n"

alert(res);

</script>

</head>

<body>

<input type="button" onclick="at()" value="check">

</body>

</html>

Output:
P a g e |2

1. Print the largest of three numbers.

Ans-

Code:

// Java Program to Find the Biggest of 3 Numbers

import java.io. *;

class large {

static int biggestOfThree (int x, int y, int z)

// Comparing all 3 numbers

if (x >= y && x >= z)

// Returning 1st number if largest

return x;

// Comparing 2nd, no with 1st and 3rd no

else if (y >= x && y >= z)

// Return z if the above conditions are false

return y;

else

// Returning 3rd, no, it’s sure it is greatest

return z;

public static void main(String[] args)


Page |3

int a, b, c, largest;

// Considering random integers three numbers

a = 50;

b = 10;

c = 32;

// Calling the function in main () body

largest = biggestOfThree(a, b, c);

System.out.println(largest + " is the largest number.");

Output:

2. Find the factorial of a number n.

Ans-

Code:

import java.io.*;

class Factorial{

public static void main(String args[]){

int i,fact=1;

int number=7;

//It is the number to calculate factorial

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

fact=fact*i;

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

Output:
P a g e |4

3. Enter a list of positive numbers terminated by Zero. Find the sum and average of these numbers.

Ans-

Code:

class Average

public static void main(String arg[])

int n=5,result=0;

int a[]=new int[5];

a[0]=10;

a[1]=20;

a[2]=30;

a[3]=40;

a[4]=50;

for(int i=0;i<n;i++)

result=result+a[i];

System.out.println("sum of ("+a[0]+","+a[1]+","+a[2]+","+a[3]+","+a[4]+") is ="+result);

System.out.println("average of ("+a[0]+","+a[1]+","+a[2]+","+a[3]+","+a[4]+") is ="+result/n);

Output:

4. A person deposits Rs 1000 in a fixed account yielding 5% interest. Compute the amount in the account at the end of
each year for n years. Read n numbers. Count the number of negative numbers, positive numbers and zeros in the list.

P a g e |5
Ans-

Code:

import java.util.Scanner;

public class Java5

private static final int PRINCIPAL = 1000;

private static final int RATE = 5;

public static void main(String[] args)

Scanner input = new Scanner(System.in);

System.out.print("Enter year: ");

int n = input.nextInt();

for (int i = 1; i <= n; i++)

double interest = (PRINCIPAL * i * RATE) / 100f;

System.out.printf("End of year %d amount: %.2f\n", i, PRINCIPAL +interest);

Output:

JAVA Script:

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

P a g e |6
Ans-

Code:

Output:

2. Implement a static password protection.

Ans-

Code:

<html>

<title>Static password</title>

<head><h1> Login Page </h1>

<script language="javascript">

function cLogin()

var u=document.f1.t1.value;

var p=document.f1.t2.value;

if(u=="ABCD" && p=="AAAA")

alert("Login Successfully!!");

else

alert("User name or Password is wrong!!");

</script>

</head>

<form name="f1">

User Name <input type="text" name="t1">

<br>

Password <input type="password" name="t2">

<br>

Page |7
<input type="button" name="b1" onClick="cLogin()" value="Login">

<input type="reset" name="b2" value="Reset">

<body bgcolor = "DodgerBlue"> </body>

</form>

Output:

3. Write a java script

To change the colour of text using SetTimeOut() b. To move an image across screen using SetInterval().

Ans-

Code:

<html>

<head>

<title> New Document </title>

<script language="javascript">

function ChangeColor()

document.getElementById('btn1').style.backgroundColor='Gray';

setTimeout("ChangeColor2()",4000);

function ChangeColor2()

document.getElementById('btn1').style.backgroundColor='Pink';

Page |8

setTimeout("ChangeColor3()",4000);
}

function ChangeColor3()

document.getElementById('btn1').style.backgroundColor='blue';

setTimeout("ChangeColor4()",4000);

function ChangeColor4()

document.getElementById('btn1').style.backgroundColor='Red';

</script>

</head>

<body>

<input type="text" onmouseover="ChangeColor()" value="its a javscript program"


id="btn1" />

</body>

</html>

Output:

<html>

P a g e |9

<head>
<title>JavaScript Animation</title>

<script type = "text/javascript">

<!--

var imgObj = null;

var animate ;

function init() {

imgObj = document.getElementById('myImage');

imgObj.style.position= 'relative';

imgObj.style.left = '0px';

function moveRight() {

imgObj.style.left = parseInt(imgObj.style.left) + 5 + 'px';

animate = setTimeout(moveRight,6); // call moveRight in 6msec

function stop() {

clearTimeout(animate);

imgObj.style.left = '0px';

window.onload = init;

//-->

</script>

</head>

<body>

<form>

<img id = "myImage" src = "F:\2.jpg" />

<p>Click the buttons below to handle animation</p>

<input type = "button" value = "Start" onclick = "moveRight();" />

<input type = "button" value = "Stop" onclick = "stop();" />

P a g e | 10

</form>

</body>
</html>

Output:

JAVA Programs:

1. WAP to find the largest of n natural numbers.

Ans-

Code:

import java.util.Scanner;

/*

Java Program to find the largest of N numbers

*/

public class LargestOfN {

public static void main(String[] args) {

System.out.println("Welcome to Java Program to find "

+ "largest and smallest number without using array");

P a g e | 14

System.out.println("Please enter value of N: ");

Scanner sc = new Scanner(System.in);


int n = sc.nextInt();

int largest = Integer.MIN_VALUE;

System.out.printf("Please enter %d numbers %n", n);

for (int i = 0; i < n; i++) {

int current = sc.nextInt();

if (current > largest) {

largest = current;

System.out.println("largest of N number is : " + largest);

Output:

2. WAP to find whether a given number is prime or not.

Ans-

P a g e | 12

Code:

import java.util.Scanner;

public class PrimeExample3 {


public static void main(String[] args)

Scanner s = new Scanner(System.in);

System.out.print("Enter a number : ");

int n = s.nextInt();

if (isPrime(n)) {

System.out.println(n + " is a prime number");

} else {

System.out.println(n + " is not a prime number");

public static boolean isPrime(int n)

if (n <= 1)

return false;

for (int i = 2; i < Math.sqrt(n); i++)

if (n % i == 0)

return false;

return true;

Output:

P a g e | 13
3. WAP to print the sum and product of digits of an Integer and reverse the Integer.

Ans-

Code:

Output:

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.

Ans-

Code:

import java.util.Scanner;

public class Java6

public static void main(String[] args)

Scanner input = new Scanner(System.in);

int[] numbers = new int[10];

for (int i = 0; i < 10; i++)

System.out.print("Enter " + (i + 1) + " : ");

numbers[i] = input.nextInt();

System.out.print("Enter number to compare: ");

P a g e | 14

int num = input.nextInt();


int less = 0;

int greater = 0;

int eq = 0;

for (int i = 0; i < 10; i++)

if (numbers[i] > num)

greater++;

else if (numbers[i] < num)

less++;

else

eq++;

System.out.println("Greater: " + greater + " Less: " + less + " Equal: " + eq);

Output:

P a g e | 15
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.

Ans-

Code:

import java.io.*;

class matrix {

// Function to print Matrix

static void printMatrix(int M[][],

int rowSize,

int colSize)

for (int i = 0; i < rowSize; i++) {

for (int j = 0; j < colSize; j++)

System.out.print(M[i][j] + " ");

System.out.println();

// Function to add the two matrices

// and store in matrix C

static int[][] add(int A[][], int B[][],

int size)

int i, j;

int C[][] = new int[size][size];

for (i = 0; i < size; i++)

for (j = 0; j < size; j++)

C[i][j] = A[i][j] + B[i][j];

P a g e | 16

return C;
}

// Driver code

public static void main(String[] args)

int size = 4;

int A[][] = { { 1, 1, 1, 1 },

{ 2, 2, 2, 2 },

{ 3, 3, 3, 3 },

{ 4, 4, 4, 4 } };

// Print the matrices A

System.out.println("\n \n Matrix A:");

printMatrix(A, size, size);

int B[][] = { { 1, 1, 1, 2 },

{ 2, 2, 2, 2 },

{ 3, 3, 3, 3 },

{ 4, 4, 4, 4 } };

// Print the matrices B

System.out.println("\nMatrix B:");

printMatrix(B, size, size);

// Add the two matrices

int C[][] = add(A, B, size);

// Print the result

System.out.println("\n\n Resultant Matrix:");

printMatrix(C, size, size);

P a g e | 17

 Output:
public class MatrixTransposeExample{

public static void main(String args[]){

int original[][]={{3,3,4},{2,7,3},{3,4,8}};

//creating another matrix to store transpose of a matrix

int transpose[][]=new int[3][3]; //3 rows and 3 columns

//Code to transpose a matrix

for(int i=0;i<3;i++){

for(int j=0;j<3;j++){

transpose[i][j]=original[j][i];

System.out.println("Printing Matrix without transpose:");

for(int i=0;i<3;i++)

for(int j=0;j<3;j++){

System.out.print(original[i][j]+" ");

System.out.println();//new line

P a g e | 18
}

System.out.println("Printing Matrix After Transpose:");

for(int i=0;i<3;i++){

for(int j=0;j<3;j++){

System.out.print(transpose[i][j]+" ");

System.out.println();//new line

 Output:

6. Write a java program that computes the area of a circle, rectangle and a Cylinder using function overloading.

Ans-

Code:

class Java

void calculateArea(float x)

System.out.println("Area of the square: "+x*x+" sq units");

void calculateArea(float x, float y)

System.out.println("\n Area of the rectangle: "+x*y+" sq units");

void calculateArea(double r)

P a g e | 19
double area = 3.14*r*r;

System.out.println("\n Area of the circle: "+area+" sq units");

public static void main(String args[])

Java obj = new Java();

obj.calculateArea(6.1f);

obj.calculateArea(10,22);

obj.calculateArea(6.1);

 Output:

JDBC:

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".
b. Repeat the same thing for all the teachers using PreparedStatement.
c. Delete the student with ID=3 from 'Student' database.
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.

Ans-

 Code:
 //show_tble

package com.know.jdbc;

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.ResultSet;

import java.sql.Statement;

P a g e | 20

public class SelectTest {


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

// variables

final String url = "jdbc:mysql:///college";

final String user = "root";

final String password = "";

// establish the connection

Connection con = DriverManager.getConnection(url, user, password);

// create JDBC statement object

Statement st = con.createStatement();

// prepare SQL query

String query = "SELECT ID, FIRSTNAME, LASTNAME FROM TEACHER";

// send and execute SQL query in Database software

ResultSet rs = st.executeQuery(query);

// process the ResultSet object

boolean flag = false;

while (rs.next()) {

flag = true;

System.out.println(rs.getInt(1) + " " + rs.getString(2) +

" " + rs.getString(3));

if (flag == true) {

System.out.println("\nRecords retrieved and displayed");

} else {

P a g e | 21

System.out.println("Record not found");


}

// close JDBC objects

rs.close();

st.close();

con.close();

 Output:

a. //update

package com.know.jdbc;

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.PreparedStatement;

import java.sql.SQLException;

import java.util.Scanner;

public class SelectTest {

// SQL query

private static final String UPDATE_PRODUCT_QUERY =

"UPDATE TEACHER SET FIRSTNAME = ?"

P a g e | 22

+ "WHERE ID = ? ";
public static void main(String[] args) {

// declare variables

Scanner scan = null;

int id = 0;

String name = null;

Connection con = null;

PreparedStatement ps = null;

int result = 0;

try {

// read input

scan = new Scanner(System.in);

if(scan != null) {

System.out.println("Enter the existing ID"

+ " to update: ");

id = scan.nextInt();

System.out.println("Enter new details,");

System.out.print("Name:");

name = scan.next();

// establish the connection

con = DriverManager.getConnection(

"jdbc:mysql:///college",

"root", "");

// compile SQL query and

// store it in PreparedStatemet object

if(con != null)

P a g e | 23

ps = con.prepareStatement(UPDATE_PRODUCT_QUERY);
// set input values and execute query

if(ps != null) {

// set input values to query parameters

ps.setString(1, name);

ps.setInt(2, id);

// execute the query

result = ps.executeUpdate();

// process the result

if(result == 0)

System.out.println("Records not updated");

else

System.out.println("Records updated"+

" successfully");

} catch(SQLException se) {

se.printStackTrace();

} catch(Exception e) {

e.printStackTrace();

} // end of try-catch block

finally {

// close JDBC objects

try {

if(ps != null) ps.close();

} catch(SQLException se) {

se.printStackTrace();

P a g e | 24
try {

if(con != null) con.close();

} catch(SQLException se) {

se.printStackTrace();

try {

if(scan != null) scan.close();

} catch(Exception e) {

e.printStackTrace();

} //end of main

 (a)Output:

 (b)Output:

 //drop record

package com.know.jdbc;

import java.sql.*;

public class SelectTest {

P a g e | 25
// JDBC driver name and database URL

static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";

static final String DB_URL = "jdbc:mysql:///college";

// Database credentials

static final String USER = "root";

static final String PASS = "";

private static String Firstname;

private static String Lastname;

public static void main(String[] args) {

Connection conn = null;

Statement stmt = null;

try{

//STEP 2: Register JDBC driver

Class.forName("com.mysql.jdbc.Driver");

//STEP 3: Open a connection

System.out.println("Connecting to a selected database...");

conn = DriverManager.getConnection(DB_URL, USER, PASS);

System.out.println("Connected database successfully...");

//STEP 4: Execute a query

System.out.println("Creating statement...");

stmt = conn.createStatement();

String sql = "DELETE FROM STUDENT " +

"WHERE id = 3";

stmt.executeUpdate(sql);

// Now you can extract all the records

// to see the remaining records

sql = "SELECT ID,Firstname,Lastname FROM student";


P a g e | 26

ResultSet rs = stmt.executeQuery(sql);

while(rs.next()){

//Retrieve by column name

int id = rs.getInt("id");

String first = rs.getString("Firstname");

String last = rs.getString("Lastname");

//Display values

System.out.print("ID: " + id);

System.out.print(", First: " + Firstname);

System.out.println(", Last: " + Lastname);

rs.close();

}catch(SQLException se){

//Handle errors for JDBC

se.printStackTrace();

}catch(Exception e){

//Handle errors for Class.forName

e.printStackTrace();

}finally{

//finally block used to close resources

try{

if(stmt!=null)

conn.close();

}catch(SQLException se){

}// do nothing

try{

if(conn!=null)
conn.close();

P a g e | 27

}catch(SQLException se){

se.printStackTrace();

}//end finally try

}//end try

System.out.println("Goodbye!");

}//end main

}//end JDBCExample

 Output:

 //insert student

package com.know.jdbc;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
public class SelectTest {
public static void main(String[] args) throws Exception {
//Registering the Driver
DriverManager.registerDriver(new com.mysql.jdbc.Driver());
//Getting the connection
String url = "jdbc:mysql:///college";
Connection con = DriverManager.getConnection(url, "root", "");
//Creating a Statement object
Statement stmt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
//Retrieving the data
ResultSet rs = stmt.executeQuery("select * from student");
//Printing the contents of the Result Set
System.out.println("Contents of the Result Set");
while(rs.next()) {
System.out.print("ID: " + rs.getInt("id"));
System.out.print(", FirstName: " + rs.getString("Firstname"));
System.out.print(", LastName: " + rs.getString("Lastname")); }
System.out.println();
rs.moveToInsertRow();
rs.updateInt(1, 3);
rs.updateString(2, "Sayak");
rs.updateString(3, "Pal");
rs.insertRow();
//Retrieving the contents of result set again
System.out.println("Contents of the ResultSet after inserting another row in to it");
P a g e | 28

rs.beforeFirst();
while(rs.next()) {
System.out.print("ID: " + rs.getInt("id"));
System.out.print(", FirstName: " + rs.getString("Firstname"));
System.out.print(", LastName: " + rs.getString("Lastname"));
}
}
}} //end of class

 Output:

2. Create a procedure in MySQL to count the number of Rows in table 'Student'. Use Callable Statement to call this
method from Java code.

Ans-

Code:

Output:

JSP:

1. Display the patterns of numbers, characters etc.

Ans-

Code:

import java.util.Scanner;

public class pattern


{           
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in); //Taking rows value from the user   
System.out.println("Enter the number of rows: ");   
int rows = sc.nextInt();        
for (int i = 1; i <= rows; i++)
{
for (int j = 1; j <= i; j++)
{
System.out.print(i+" ");
}

System.out.println();
}        
P a g e | 29

sc.close();
}
}

Output:

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).

Ans-

Code:

Output:

3. Validate User input entered in a form. The input must include Name, DOB, Email ID, Lucky Number, Favorite food
etc.

Ans-

Code:

Output:

You might also like