Professional Documents
Culture Documents
SESSION: 2018-2019
INDEX
CONTENT PAGE
1. Print a table of numbers from 5 to 15 and their 1
squares and cubes using alert.
JAVA Script:
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="";
alert(res);
</script>
</head>
<body>
</body>
</html>
Output:
P a g e |2
Ans-
Code:
import java.io. *;
class large {
return x;
return y;
else
return z;
int a, b, c, largest;
a = 50;
b = 10;
c = 32;
Output:
Ans-
Code:
import java.io.*;
class Factorial{
int i,fact=1;
int number=7;
for(i=1;i<=number;i++){
fact=fact*i;
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
int n=5,result=0;
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];
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;
int n = input.nextInt();
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:
Ans-
Code:
<html>
<title>Static password</title>
<script language="javascript">
function cLogin()
var u=document.f1.t1.value;
var p=document.f1.t2.value;
alert("Login Successfully!!");
else
</script>
</head>
<form name="f1">
<br>
<br>
Page |7
<input type="button" name="b1" onClick="cLogin()" value="Login">
</form>
Output:
To change the colour of text using SetTimeOut() b. To move an image across screen using SetInterval().
Ans-
Code:
<html>
<head>
<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>
</body>
</html>
Output:
<html>
P a g e |9
<head>
<title>JavaScript Animation</title>
<!--
var animate ;
function init() {
imgObj = document.getElementById('myImage');
imgObj.style.position= 'relative';
imgObj.style.left = '0px';
function moveRight() {
function stop() {
clearTimeout(animate);
imgObj.style.left = '0px';
window.onload = init;
//-->
</script>
</head>
<body>
<form>
P a g e | 10
</form>
</body>
</html>
Output:
JAVA Programs:
Ans-
Code:
import java.util.Scanner;
/*
*/
P a g e | 14
largest = current;
Output:
Ans-
P a g e | 12
Code:
import java.util.Scanner;
int n = s.nextInt();
if (isPrime(n)) {
} else {
if (n <= 1)
return false;
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;
numbers[i] = input.nextInt();
P a g e | 14
int greater = 0;
int eq = 0;
greater++;
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 {
int rowSize,
int colSize)
System.out.println();
int size)
int i, j;
P a g e | 16
return C;
}
// Driver code
int size = 4;
int A[][] = { { 1, 1, 1, 1 },
{ 2, 2, 2, 2 },
{ 3, 3, 3, 3 },
{ 4, 4, 4, 4 } };
int B[][] = { { 1, 1, 1, 2 },
{ 2, 2, 2, 2 },
{ 3, 3, 3, 3 },
{ 4, 4, 4, 4 } };
System.out.println("\nMatrix B:");
P a g e | 17
Output:
public class MatrixTransposeExample{
int original[][]={{3,3,4},{2,7,3},{3,4,8}};
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
transpose[i][j]=original[j][i];
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
}
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)
void calculateArea(double r)
P a g e | 19
double area = 3.14*r*r;
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
// variables
Statement st = con.createStatement();
ResultSet rs = st.executeQuery(query);
while (rs.next()) {
flag = true;
if (flag == true) {
} else {
P a g e | 21
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;
// SQL query
P a g e | 22
+ "WHERE ID = ? ";
public static void main(String[] args) {
// declare variables
int id = 0;
PreparedStatement ps = null;
int result = 0;
try {
// read input
if(scan != null) {
id = scan.nextInt();
System.out.print("Name:");
name = scan.next();
con = DriverManager.getConnection(
"jdbc:mysql:///college",
"root", "");
if(con != null)
P a g e | 23
ps = con.prepareStatement(UPDATE_PRODUCT_QUERY);
// set input values and execute query
if(ps != null) {
ps.setString(1, name);
ps.setInt(2, id);
result = ps.executeUpdate();
if(result == 0)
else
System.out.println("Records updated"+
" successfully");
} catch(SQLException se) {
se.printStackTrace();
} catch(Exception e) {
e.printStackTrace();
finally {
try {
} catch(SQLException se) {
se.printStackTrace();
P a g e | 24
try {
} catch(SQLException se) {
se.printStackTrace();
try {
} catch(Exception e) {
e.printStackTrace();
} //end of main
(a)Output:
(b)Output:
//drop record
package com.know.jdbc;
import java.sql.*;
P a g e | 25
// JDBC driver name and database URL
// Database credentials
try{
Class.forName("com.mysql.jdbc.Driver");
System.out.println("Creating statement...");
stmt = conn.createStatement();
"WHERE id = 3";
stmt.executeUpdate(sql);
ResultSet rs = stmt.executeQuery(sql);
while(rs.next()){
int id = rs.getInt("id");
//Display values
rs.close();
}catch(SQLException se){
se.printStackTrace();
}catch(Exception e){
e.printStackTrace();
}finally{
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 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:
Ans-
Code:
import java.util.Scanner;
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: