Adrijeet Deb 17bci0185 Cse1007 - Java Programming - L33+L34 Lab Assessment 3 Question No: 1

Download as pdf or txt
Download as pdf or txt
You are on page 1of 18

ADRIJEET DEB 17BCI0185

CSE1007 - JAVA PROGRAMMING – L33+L34


LAB ASSESSMENT 3

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;

class Task1 extends Thread

private String str;

private String string = new String();

private boolean flag;

Task1(String str)

this.str = str;

public void run()

for(int i=0;i<str.length();i++)

if(str.charAt(i)=='z')

{
string += 'a';

else if(str.charAt(i)=='Z')

string += 'A';

else

string += (char)(str.charAt(i) + 1);

flag = true;

synchronized (this)

this.notifyAll();

public String getString()

if(!flag)

try

synchronized (this)

this.wait();

}
catch (InterruptedException e)

e.printStackTrace();

return string;

class Task2 extends Thread

String str;

String string = new String();

private boolean flag;

Task2(String str)

this.str = str;

public void run()

for(int i=0;i<str.length();i++)

if(str.charAt(i)=='a')

string += 'z';

else if(str.charAt(i)=='A')

{
string += 'Z';

else

string += (char)(str.charAt(i) - 1);

flag = true;

synchronized (this)

this.notifyAll();

public String getString()

if(!flag)

try

synchronized (this)

this.wait();

catch (InterruptedException e)

e.printStackTrace();

}
}

return string;

public class Main

public static void main(String args[])

Scanner scanner = new Scanner(System.in);

System.out.println("Enter a string to generate cipher text: ");

String str;

str = scanner.next();

Task1 t1 = new Task1(str);

Task2 t2 = new Task2(str);

t1.start();

t2.start();

String x = t1.getString();

String y = t2.getString();

try

FileWriter myWriter = new FileWriter("new.txt");

myWriter.write(x);

// myWriter.write(" ");

myWriter.write(y);

myWriter.close();
System.out.println("Successfully wrote to the file.");

catch (IOException e)

System.out.println("An error occurred.");

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;

class Sample implements Serializable

public String pid;

public int diameter;

public int length;

public int weight;

// Default constructor

public Sample(String pid, int diameter, int length, int weight)

this.pid = pid;

this.diameter = diameter;

this.length = length;

this.weight = weight;

public class Main


{

public static void main(String[] args)

Scanner scanner = new Scanner(System.in);

String pid;

int diameter;

int length;

int weight;

ArrayList<String> list = new ArrayList<>();

int i = 0;

int n;

System.out.println("Enter the number of product samples: ");

n = scanner.nextInt();

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

System.out.println("Enter the details of product "+(l+1));

pid = scanner.next();

length = scanner.nextInt();

diameter = scanner.nextInt();

weight = scanner.nextInt();

Sample object = new Sample(pid, diameter, length, weight);

String filename = "file.txt";


// Serialization

try

//Saving of object in a file

FileOutputStream file = new FileOutputStream(filename);

ObjectOutputStream out = new ObjectOutputStream(file);

// Method for serialization of object

out.writeObject(object);

out.close();

file.close();

catch (IOException ex)

System.out.println("IOException is caught");

Sample object2 = null;

// Deserialization

try {

// Reading the object from a file

FileInputStream file = new FileInputStream(filename);

ObjectInputStream in = new ObjectInputStream(file);

// Method for deserialization of object

object2 = (Sample) in.readObject();


in.close();

file.close();

if (object2.length != 10 || object2.diameter != 3 || object2.weight != 100)

list.add((object2.pid).toString());

catch (IOException ex)

System.out.println("IOException is caught");

catch (ClassNotFoundException ex)

System.out.println("ClassNotFoundException is caught");

System.out.println("The product id of defective sample is:");

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.

a) List of students whose joined in 2018.


b) List of students whose age is between 18-20
c) List of students with CGPA less than 5.
d) List of students who stay in hostel.
e) List of 2019 batch students who are dayscholars.

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;

/**

* @author Adrijeet Deb

*/

public class Student {

/**

* @param args the command line arguments

*/

public static void main(String[] args) {

try

Class.forName("oracle.jdbc.driver.OracleDriver");

Connection con =
DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl","system","system");

Statement stmt = con.createStatement();


Scanner sc = new Scanner(System.in);

String sql = "create table students (name varchar(25), regno varchar(25), cgpa number(4,2),
age int, dayscholar char(1))";

stmt.executeUpdate(sql);

System.out.println("Enter the number of students: ");

int n = sc.nextInt();

String name,newsql,regno;

double cgpa;

int age;

char dayscholar;

PreparedStatement pstmt;

System.out.println("Enter their names");

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

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

name = sc.next();

System.out.println("Registration No: ");

regno = sc.next();

System.out.println("CGPA: ");

cgpa = sc.nextDouble();

System.out.println("Age: ");

age = sc.nextInt();

System.out.println("Dayscholar (Y/N): ");

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%'";

System.out.println("List of students who joined in 2018: ");

ResultSet rs1 = stmt.executeQuery(query1);

int i = 1;

while(rs1.next())

System.out.println(i+" "+rs1.getString(1)+" "+rs1.getString(2));

i++;

String query2 = "select name,age from students where age>=18 and age<=20";

System.out.println("List of students whose age is between 18 and 20: ");


ResultSet rs2 = stmt.executeQuery(query2);

i = 1;

while(rs2.next())

System.out.println(i+" "+rs2.getString(1)+" "+rs2.getInt(2));

i++;

String query3 = "select name,cgpa from students where cgpa<5";

System.out.println("List of students whose CGPA is less than 5");

ResultSet rs3 = stmt.executeQuery(query3);

i = 1;

while(rs3.next())

System.out.println(i+" "+rs3.getString(1)+" "+rs3.getInt(2));

i++;

String query4 = "select name,dayscholar from students where dayscholar='N'";

System.out.println("List of students who stay in hostel: ");

ResultSet rs4 = stmt.executeQuery(query4);

i = 1;

while(rs4.next())

System.out.println(i+" "+rs4.getString(1)+" "+rs4.getString(2));

i++;

}
String query5 = "select name,regno from students where dayscholar='Y' and regno like '19%'";

System.out.println("List of 2019 batch students who are day scholars: ");

ResultSet rs5 = stmt.executeQuery(query5);

i = 1;

while(rs5.next())

System.out.println(i+" "+rs5.getString(1)+" "+rs5.getString(2));

i++;

con.close();

catch(Exception e)

System.out.println(e);

Output in next page


Output:

You might also like