22MIC0044JAVA
22MIC0044JAVA
CYCLE SHEET -2
1. Create a Thread class with name "GreetThread" and override the run method to say
greeting message for 5 Times. Create Two objects of the thread and start each of such
thread objects.
SOURCE CODE:
SOURCE CODE:
@Override
public void run() {
for (int i = 0; i < 5; i++) {
System.out.println("Greetings from " + name + "'s Thread");
try {
Thread.sleep(1200); // Changed sleep time for variation
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
OUTPUT:
3. Create a Class name AddTable that will implement a method printTable(int n) to print first
5 entries of nth ADDITION table. Try to create a thread class that overides the run() method
and then calls the printTable(int n) method throgh the objects of the display class. Create
two thread objects such that one will print 5th Table and other one will print 7th Table.
Demonstrate the execution of the threads with synchronized and without synchronized
keyword.
SOURCE CODE:
class AdditionTablePrinter {
synchronized void printTable(int n) {
System.out.println("Printing the first 5 entries of the " + n + "th Addition Table:");
for (int i = 1; i <= 5; i++) {
System.out.println(n + " + " + i + " = " + (n + i));
}
System.out.println();
}
}
@Override
public void run() {
tablePrinter.printTable(n);
}
}
public class SynchronizedTablePrinting {
public static void main(String[] args) {
AdditionTablePrinter tablePrinter = new AdditionTablePrinter();
System.out.println("Without Synchronization:");
TablePrintingThread thread1 = new TablePrintingThread(tablePrinter, 5);
TablePrintingThread thread2 = new TablePrintingThread(tablePrinter, 7);
thread1.start();
thread2.start();
try {
thread1.join();
thread2.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("With Synchronization:");
TablePrintingThread synchronizedThread1 = new TablePrintingThread(tablePrinter, 5);
TablePrintingThread synchronizedThread2 = new TablePrintingThread(tablePrinter, 7);
synchronizedThread1.start();
synchronizedThread2.start();
}
}
OUTPUT:
4. With the help of two separate thread , implement the following formula to determine
SOURCE CODE:
@Override
public void run() {
for (int i = startIndex; i < endIndex; i++) {
partialSum += values[i] * frequencies[i];
}
}
}
double totalSum = 0;
try {
for (PartialSumCalculator thread : threads) {
thread.join();
totalSum += thread.getPartialSum();
}
} catch (InterruptedException e) {
e.printStackTrace();
}
Q5. Create a BankAccount class that would implement deposit(int amount) and withdraw(int
amount) and also declare the instance variables such as accno, accountname and balance
Create an object of this account and pass it to two separate threads such that one is invoking
deposit() method meanwhile other one is invoking withdraw() method. Demonstrate the
execution of the threads with synchronized and without synchronized keyword.
Use wait() and notify() method to be implemented over the BankAccount object . Assume that
this account is jointly used by both parent and his son . The son thread has to wait if there is
no available money to withdraw . Meanwhile if parent thread sends notification to wake of
the son thread once amount is deposited into the account
SOURCE CODE:
class BankAccount {
private int accountNumber;
private String accountHolderName;
private int balance;
@Override
public void run() {
account.deposit(amount);
}
}
@Override
public void run() {
account.withdraw(amount);
}
}
System.out.println("Without Synchronization:");
DepositTask depositTask = new DepositTask(account, 500);
WithdrawTask withdrawTask = new WithdrawTask(account, 200);
depositTask.start();
withdrawTask.start();
try {
depositTask.join();
withdrawTask.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("With Synchronization:");
DepositTask synchronizedDepositTask = new DepositTask(account, 500);
WithdrawTask synchronizedWithdrawTask = new WithdrawTask(account, 200);
synchronizedDepositTask.start();
synchronizedWithdrawTask.start();
}
}
OUTPUT:
SOURCE CODE:
class BankAccount {
private int accno;
private String accountName;
private int balance;
notify();
}
@Override
public void run() {
account.deposit(amount);
}
}
@Override
public void run() {
account.withdraw(amount);
}
}
OUTPUT:
Q6. Write the following data to a datafile item.dat and again read it and find out the total
amount paid by the all customers.
SOURCE CODE:
import java.io.*;
private static void writeDataToFile(String fileName, double[] cost, int[] quantity, String[] items) {
try (DataOutputStream out = new DataOutputStream(new FileOutputStream(fileName))) {
for (int i = 0; i < cost.length; i++) {
out.writeDouble(cost[i]); // Write double instead of float
out.writeInt(quantity[i]);
out.writeUTF(items[i]);
}
System.out.println("Data written to " + fileName);
} catch (IOException e) {
System.err.println("Error writing data to file: " + e.getMessage());
}
}
OUTPUT:
Q7. Write a Java Program to create two threads such that one thread uses to push the uppercase
letters of a sentence onto a piped output stream and another thread pull the data through the
piped input stream and converts all received letters into lowercase letters.
SOURCE CODE:
import java.io.IOException;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;
import java.util.Scanner;
try {
PipedOutputStream pipedOutputStream = new PipedOutputStream();
PipedInputStream pipedInputStream = new PipedInputStream(pipedOutputStream);
uppercaseThread.start();
lowercaseThread.start();
uppercaseThread.join();
lowercaseThread.join();
} catch (IOException | InterruptedException e) {
e.printStackTrace();
}
}
}
OUTPUT:
Q8. Assume that you have two text files that stored the following details . The first text file stores
text A and second file stores Text B
Read all the files one by one and count the frequency count of each word together with all files using
sequence input stream.
SOURCE CODE:
import java.io.*;
import java.util.*;
public class WordFrequencyCounter {
processFile("file1.txt", wordFrequencyMap);
processFile("file2.txt", wordFrequencyMap);
while (scanner.hasNext()) {
String word = scanner.next().toLowerCase();
FILE 1:
FILE 2:
OUTPUT:
Q9. Define a class “Customer” with following members where as the objects of this class could be
serializabe onto a file. Create 5 different objects of customer and seralize them into a file with
“youregno.ser”. During serialization you should mask the value of the gender. After that deserialize
the count of customers whose name started “Ram”..
SOURCE CODE:
import java.io.*;
import java.util.ArrayList;
import java.util.List;
@Override
public String toString() {
return name + "(" + id + ")";
}
Q1O. Read a Sample CSV File and Print a particular column using BufferedReader and
FileReader classes.
SOURCE CODE:
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
// Split the header line to identify the column index for the desired column
String[] headers = headerLine.split(",");
int columnIndex = -1;
// Read and print the desired column from each subsequent line
String line;
while ((line = br.readLine()) != null) {
String[] data = line.split(",");
if (data.length > columnIndex) {
String columnValue = data[columnIndex];
System.out.println("Age: " + columnValue);
}
}
} catch (IOException e) {
System.err.println("Error reading CSV file: " + e.getMessage());
}
}
}FILE:
OUTPUT: