0% found this document useful (0 votes)
76 views

Lab Assignment - 5: Fall Semester 2020-21

The document is a lab assignment submission that includes the student's name, registration number, course details, and solutions to two programming questions. For the first question, the student writes a Java program to create three threads that each display a message at different time intervals. For the second question, the student writes a program to create a sequential file that stores employee details entered through keyboard, including ID, name, age, department, and salary.

Uploaded by

Tanay Dubey
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
76 views

Lab Assignment - 5: Fall Semester 2020-21

The document is a lab assignment submission that includes the student's name, registration number, course details, and solutions to two programming questions. For the first question, the student writes a Java program to create three threads that each display a message at different time intervals. For the second question, the student writes a program to create a sequential file that stores employee details entered through keyboard, including ID, name, age, department, and salary.

Uploaded by

Tanay Dubey
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Lab Assignment – 5

Fall Semester 2020-21

Name – Tanay Dubey


Registration Number – 19BCT0185
Course Code and Name – CSE1007 Java Programming
Faculty Name – Gopichand G
Slot – L27 + L28
Question -:
Write a Java program that creates three threads. First
thread displays “Good Morning” every one second, the
second thread displays “Hello” every two seconds and
the third thread displays “Welcome” every three
seconds.
Solution -:
Code
class A extends Thread
{
synchronized public void run()
{
try
{
while(true)
{
sleep(1000);
System.out.println("good morning");
}
}
catch(Exception e)
{ }
}
}
class B extends Thread
{
synchronized public void run()
{
try
{
while(true)
{
sleep(2000);
System.out.println("hello");
}
}
catch(Exception e)
{ }
}
}
class C extends Thread
{
synchronized public void run()
{
try
{
while(true)
{
sleep(3000);
System.out.println("welcome");
}
}
catch(Exception e)
{ }
}
}
class ThreadDemo
{
public static void main(String args[])
{
A t1=new A();
B t2=new B();
C t3=new C();
t1.start();
t2.start();
t3.start();
}
}
Output
Question -:
Write a program to create a sequential fi le that could
store details about the employees of an organization.
Details include empId, empName, empAge, empDept,
and empSal. These are provided through keyboard.
Solution -:
Code
import java.io.*;

class Emp
{
int emp_id;
String emp_name;
int emp_age;
String emp_dept;
double emp_sal;

public Emp(int id, int age, double sal, String dept,


String name)
{
emp_id=id;
emp_age=age;
emp_sal=sal;
emp_dept=dept;
emp_name=name;
}

public static Emp getEmp(BufferedReader br)throws


IOException
{
return new
Emp(Integer.parseInt(br.readLine()),Integer.parseInt(br.r
eadLine()),Double.parseDouble(br.readLine()),br.readLine(
),br.readLine());
}
public void disp(PrintStream ps)
{

ps.println(emp_id+"\n"+emp_age+"\n"+emp_sal+"\n"+emp_dept
+"\n"+emp_name+"\n\n");
}
}
public class Sol
{
public static void main(String[] args)
{
try
{
InputStreamReader isr=new
InputStreamReader(System.in);
BufferedReader br=new BufferedReader(isr);
System.out.println("ENTER THE NUMBER OF
RECORDS TO BE ENTERED");
int n=Integer.parseInt(br.readLine());
System.out.println("ENTER ID, AGE, SALARY,
DEPT, NAME IN EACH LINE");
Emp[] e=new Emp[n];
for(int i=0,j=0;i<n;++i)
e[i]=Emp.getEmp(br);
System.out.println("ENTER FILE NAME");
PrintStream ps=new
PrintStream(br.readLine());
for(int i=0;i<n;++i)
e[i].disp(ps);
br.close();
isr.close();
}
catch(IOException e1)
{
System.err.println(e1.getMessage());
}
catch(SecurityException e2)
{
System.err.println(e2.getMessage());
}
}
}

Output

You might also like