Adrijeet Deb 17bci0185 Cse1007 - Java Programming - L33+L34 Lab Assessment 3 Question No: 1
Adrijeet Deb 17bci0185 Cse1007 - Java Programming - L33+L34 Lab Assessment 3 Question No: 1
Adrijeet Deb 17bci0185 Cse1007 - Java Programming - L33+L34 Lab Assessment 3 Question No: 1
Question No: 1:
Given a String as input, perform the following 2 tasks by 2 threads simultaneously without
interrupting each other. Find the cipher text by substituting every letter in the String by the
next letter in the alphabet list Eg., If the input string is “program”, output should be
“qsphsbn”. Find the cipher text by substituting every letter in the String by the previous letter
in the alphabet list Eg., If the input string is “program”, output should be “oqnfqzl”. Both
cipher text has to appended to the same file one after the other.
Code:
import java.io.*;
import java.util.Scanner;
Task1(String str)
this.str = str;
for(int i=0;i<str.length();i++)
if(str.charAt(i)=='z')
{
string += 'a';
else if(str.charAt(i)=='Z')
string += 'A';
else
flag = true;
synchronized (this)
this.notifyAll();
if(!flag)
try
synchronized (this)
this.wait();
}
catch (InterruptedException e)
e.printStackTrace();
return string;
String str;
Task2(String str)
this.str = str;
for(int i=0;i<str.length();i++)
if(str.charAt(i)=='a')
string += 'z';
else if(str.charAt(i)=='A')
{
string += 'Z';
else
flag = true;
synchronized (this)
this.notifyAll();
if(!flag)
try
synchronized (this)
this.wait();
catch (InterruptedException e)
e.printStackTrace();
}
}
return string;
String str;
str = scanner.next();
t1.start();
t2.start();
String x = t1.getString();
String y = t2.getString();
try
myWriter.write(x);
// myWriter.write(" ");
myWriter.write(y);
myWriter.close();
System.out.println("Successfully wrote to the file.");
catch (IOException e)
e.printStackTrace();
Output:
Question No: 2:
An Industry collects the product sample measurements (product id, diameter, length, weight)
for quality test and sends it to the quality assurance (QA) department in a serialized manner.
The QA departments deserialize the samples and checks if the length=10cm, diameter=3cm,
weight=100gms. The product id of defective samples is stored in a list for later corrections.
Code:
import java.io.*;
import java.util.ArrayList;
import java.util.Scanner;
// Default constructor
this.pid = pid;
this.diameter = diameter;
this.length = length;
this.weight = weight;
String pid;
int diameter;
int length;
int weight;
int i = 0;
int n;
n = scanner.nextInt();
pid = scanner.next();
length = scanner.nextInt();
diameter = scanner.nextInt();
weight = scanner.nextInt();
try
out.writeObject(object);
out.close();
file.close();
System.out.println("IOException is caught");
// Deserialization
try {
file.close();
list.add((object2.pid).toString());
System.out.println("IOException is caught");
System.out.println("ClassNotFoundException is caught");
for(String j:list)
System.out.println(j);
}
Output:
Question No: 3:
Create database of the student with the details such as (name, registernumber, cgpa, age,
dayscholar/hosteller).
Create a class student with the needed attributes. Use array of objects to store ‘n’ number of students’
details into the database.
Write a java program to fetch the following details from the database.
Code:
package pkg17bci0185q1;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Scanner;
/**
*/
/**
*/
try
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection con =
DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl","system","system");
String sql = "create table students (name varchar(25), regno varchar(25), cgpa number(4,2),
age int, dayscholar char(1))";
stmt.executeUpdate(sql);
int n = sc.nextInt();
String name,newsql,regno;
double cgpa;
int age;
char dayscholar;
PreparedStatement pstmt;
for(int i=0;i<n;i++)
System.out.println("Name: ");
name = sc.next();
regno = sc.next();
System.out.println("CGPA: ");
cgpa = sc.nextDouble();
System.out.println("Age: ");
age = sc.nextInt();
dayscholar = sc.next().charAt(0);
newsql = "insert into students (name, regno, cgpa, age, dayscholar) values (?,?,?,?,?)";
pstmt = con.prepareStatement(newsql);
pstmt.setString(1,name);
pstmt.setString(2,regno);
pstmt.setDouble(3,cgpa);
pstmt.setInt(4,age);
pstmt.setString(5,String.valueOf(dayscholar));
pstmt.executeUpdate();
System.out.println();
String query1 = "select name,regno from students where regno like '18%'";
int i = 1;
while(rs1.next())
i++;
String query2 = "select name,age from students where age>=18 and age<=20";
i = 1;
while(rs2.next())
i++;
i = 1;
while(rs3.next())
i++;
i = 1;
while(rs4.next())
i++;
}
String query5 = "select name,regno from students where dayscholar='Y' and regno like '19%'";
i = 1;
while(rs5.next())
i++;
con.close();
catch(Exception e)
System.out.println(e);