Write a Java program using Multithreading to display all the alphabets between ‘A’ to
‘Z’ after every 2 seconds.
public class Slip26_1 extends Thread{
char c;
public void run(){
for(c = 'A'; c<='Z';c++){
[Link](""+c);
try{
[Link](3000);
}
catch(Exception e){
[Link]();
}}}
public static void main(String args[]){
Slip26_1 t = new Slip26_1();
[Link]();
}}
Write a Java program to accept the details of Employee (Eno, EName, Designation, and
Salary) from a user and store it into the database. (Use Swing)
import [Link].*;
import [Link].*;
class Slip24 extends Frame implements ActionListener{
Label l1,l2,l3,l;
TextField txt1,txt2,txt3;
Button submit,clear;
Panel p1;
Slip24() {
l=new Label("EMPLOYEE INFORMTION");
l1=new Label("Name ");
l2=new Label("Address ");
l3=new Label("Salary ");
txt1=new TextField(20);
txt2=new TextField(20);
txt3=new TextField(20);
submit=new Button("submit");
[Link](this);
clear=new Button("Clear");
[Link](this);
p1=new Panel();
//[Link](new GridLayout(6,2));
[Link](l1);
[Link](txt1);
[Link](l2);
[Link](txt2);
[Link](l3);
[Link](txt3);
[Link](submit);
[Link](clear
add(p1);
setVisible(true);
setSize(400,400);
}
public void actionPerformed(ActionEvent ae) {
if([Link]()==submit) {
new Employee_Detail([Link](),[Link](),[Link]());
}
if([Link]()==clear)
{
[Link]("");
[Link]("");
[Link]("");
}
}
public static void main(String args[]) {
new Slip24();
}
}
Write a Java program using Runnable interface to blink Text on the JFrame (Use Swing)
Import [Link].*;
[Link].*;
class Slip8_1 extends Frame implements Runnable{
Thread t;
Label l1;
int f;
Slip8_1(){
t=new Thread(this);
[Link]();
setLayout(null);
l1=new Label("Hello JAVA");
[Link](100,100,100,40);
add(l1);
setSize(300,300);
setVisible(true);
f=0;
}
public void run(){
try{
if(f==0){
[Link](200);
[Link]("");
f=1;
}
if(f==1){
[Link](200);
[Link]("Hello Java");
f=0;
}}
catch(Exception e){
[Link](e);
}
run();
}
public static void main(String a[]){
new Slip8_1();
}}
Write a Java program to store city names and their STD codes using an appropriate
collection and perform following operations:
i. Add a new city and its code (No duplicates)
ii. Remove a city from the collection
iii. Search for a city name and display the code
import [Link].*;
import [Link].*;
import [Link].*;
import [Link].*;
class Slip16_2 extends JFrame implements ActionListener{
JTextField t1,t2,t3;
JButton b1,b2,b3;
JTextArea t;
JPanel p1,p2;
Hashtable ts;
Slip16_2() {
ts=new Hashtable();
t1=new JTextField(10);
t2=new JTextField(10);
t3=new JTextField(10);
b1=new JButton("Add");
b2=new JButton("Search");
b3=new JButton("Remove");
t=new JTextArea(20,20);
p1=new JPanel();
[Link](t);
p2= new JPanel();
[Link](new GridLayout(2,3));
[Link](t1);
[Link](t2);
[Link](b1);
[Link](t3);
[Link](b2);
[Link](b3);
add(p1);
add(p2);
[Link](this);
[Link](this);
[Link](this);
setLayout(new FlowLayout());
setSize(500,500);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent e) {
if(b1==[Link]()) {
String name = [Link]();
int code = [Link]([Link]());
[Link](name,code);
Enumeration k=[Link]();
Enumeration v=[Link]();
String msg="";
while([Link]()) {
msg=msg+[Link]()+" = "+[Link]()+"\n";
}
[Link](msg);
[Link]("");
[Link]("");
}
else if(b2==[Link]()) {
String name = [Link]();
if([Link](name)) {
[Link]([Link](name).toString());
}
else
[Link](null,"City not found ...");
}
else if(b3==[Link]()) {
String name = [Link]();
if([Link](name)) {
[Link](name);
[Link](null,"City Deleted ...");
}
else
[Link](null,"City not found ...");
} }
public static void main(String a[]) {
new Slip16_2();
}}
Write a java program using multithreading to simulate traffic signal (Use Swing).
import [Link].*;
import [Link].*;
class Slip3_2 extends Applet implements Runnable{
Thread t;
int r,g1,y,i;
public void init(){
T=new Thread(this);
[Link]();
r=0; g1=0;I=0; y=0;
}
public void run(){
try{
for(I =24; I >=1;i--){
if (I >16&& I <=24){
[Link](200);
r=1;
repaint();
}
if (I >8&& I <=16){
[Link](200);
y=1;
repaint();
}
if(I >1&& I <=8){
[Link](200);
g1=1;
repaint();
}}
if (I ==0){
run();
}}
catch(Exception e){
[Link](e);
}}
public void paint(Graphics g){
[Link](100,100,100,300);
if (r==1){
[Link]([Link]);
[Link](100,100,100,100);
[Link]([Link]);
[Link](100,200,100,100);
[Link](100,300,100,100);
r=0;
}
if (y==1){
[Link]([Link]);
[Link](100,100,100,100);
[Link](100,300,100,100);
[Link]([Link]);
[Link](100,200,100,100);
y=0;
}
if (g1==1){
[Link]([Link]);
[Link](100,100,100,100);
[Link](100,200,100,100);
[Link]([Link]);
[Link](100,300,100,100);
g1=0;
}}}
Write a java program that implements a multi-thread application that has three threads. First
thread generates random integer number after every one second, if the number is even; second
thread computes the square of that number and prints it. If the number is odd, the third thread
computes the cube of that number and prints it.
import [Link];
class Square extends Thread{
int x;
Square(int n) {
x = n;
}
public void run() {
int sqr = x * x;
[Link]("Square of " + x + " = " + sqr );
}}
class Cube extends Thread{
int x;
Cube(int n) {
x = n;
}
public void run() {
int cub = x * x * x;
[Link]("Cube of " + x + " = " + cub );
}}
class Number extends Thread{
public void run() {
Random random = new Random();
for(int i =0; i<10; i++) {
int randomInteger = [Link](100);
[Link]("Random Integer generated : " + randomInteger);
Square s = new Square(randomInteger);
[Link]();
Cube c = new Cube(randomInteger);
[Link]();
try {
[Link](1000);
This thread generates random number 10 times
between 1 to 100 for every 1 second. The generated
random number is then passed as argument to
Square and Cube threads.
Output varies each time a program is executed.
} catch (InterruptedException ex) {
[Link](ex);
} } }}
public class LAB3B {
public static void main(String args[]) {
Number n = new Number();
[Link]();
}}
Write a Java program to display information about the database and list all the tables in
the database. (Use DatabaseMetaData).
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
public class GetAllTables {
public static void main(String[] args) throws SQLException {
Connection conn = null;
try {
try {
[Link]("[Link]");
} catch (Exception e) {
[Link](e);
}
conn = (Connection) [Link]("jdbc:mysql://localhost/test",
"Manish", "123456");
[Link]("Connection is created succcessfully:");
} catch (Exception e) {
[Link](e);
}
ResultSet rs = null;
DatabaseMetaData meta = (DatabaseMetaData) [Link]();
rs = [Link](null, null, null, new String[] {
"TABLE"
});
int count = 0;
[Link]("All table names are in test database:");
while ([Link]()) {
String tblName = [Link]("TABLE_NAME");
[Link](tblName);
count++;
}
[Link](count + " Rows in set ");
}}
Write a Java program to show lifecycle (creation, sleep, and dead) of a thread. Program
should print randomly the name of thread and value of sleep time. The name of the thread
should be hard coded through constructor. The sleep time of a thread will be a random
integer in the range 0 to 4999.
Class MyThread extends Thread{
public MyThread(String s){
super(s);
}
public void run(){
[Link](getName()+"thread created.");
while(true){
[Link](this);
int s=(int)([Link]()*5000);
[Link](getName()+"is sleeping for :+s+"msec");
try{
[Link](s);
}
catch(Exception e){
}}}
Class ThreadLifeCycle{
public static void main(String args[]){
MyThread t1=new MyThread("shradha"),t2=new MyThread("pooja");
[Link]();
[Link]();
try{
[Link]();
[Link]();
}
catch(Exception e){}
[Link]([Link]()+"thread dead.");
[Link]([Link]()+"thread dead.");
}}
Write a java program to create a TreeSet, add some colors (String) and print out the
content of TreeSet in ascending order.
import [Link];
public class Exercise1 {
public static void main(String[] args) {
TreeSet<String> tree_set = new TreeSet<String>();
tree_set.add("Red");
tree_set.add("Green");
tree_set.add("Orange");
tree_set.add("White");
tree_set.add("Black");
[Link]("Tree set: ");
[Link](tree_set);
}
}
Write a Java program to accept the details of Teacher (TNo, TName, Subject). Insert at
least 5 Records into Teacher Table and display the details of Teacher who is teaching
“JAVA” Subject.
import [Link].*;
class Slip5_1{
public static void main(String args[]){
Connection con;
try{
[Link]("[Link]");
con=[Link]("jdbc:odbc:dsn");
if(con==null){
[Link]("Connection Failed....");
[Link](1);
}
[Link]("Connection Established...");
Statement stmt=[Link]();
//create a table teacher
String query="create table Teacher"+"(TNoint ,"+" TNamevarchar(20),"+" salint,"+"
desgvarchar(20))";
[Link](query);
[Link]("given table created in database");
//insert record into teacher table
[Link]("insert into Teacher"+"values(1,'NRC',50000,'MCS')");
[Link]("insert into Teacher"+"values(3,'XYZ',40000,'MCA')");
[Link]("insert into Teacher"+"values(4,'PQR',20000,'MCS')");
[Link]("Succesfully inserted in table....");
//display details
ResultSetrs=[Link]("select * From Teacher");
[Link]("TNo\t"+"TName\t"+"sal\t"+"desg");
while([Link]()){
[Link]("\n"+[Link](1)+"\t"+[Link](2)+"\t"+[Link](3)+"\t"+[Link] ing(4));
}}
catch(Exception e){
[Link](e);
}}}
Write a Java program to accept N integers from user. Store and display integers in sorted
order having proper collection class. The collection should not accept duplicate elements.
import [Link].*;
import [Link].*;
class cola1{
public static void main(String[] args) throws Exception {
int num,ele,i;
BufferedReader br=new BufferedReader(new InputStreamReader([Link]));
ArrayList al=new ArrayList();
[Link]("Enter the no for how many element to have:");
num=[Link]([Link]());
for(i=0;i<num;i++) {
[Link]("Enter the elements in position:"+i);
ele=[Link]([Link]());
[Link](ele);
}
[Link]("The elements of ArrayList before:"+al);
[Link](al);
[Link]("The elements of ArrayList after sort:"+al);
}}
Write a java program using Multithreading to display the numbers between 1 to 100
continuously in a JTextField by clicking on Button. (Use Runnable Interface & Swing).
import [Link].*;
import [Link].*;
class Message implements Runnable{
JTextField t;
public void run() {
for(int i =1; i<=100;i++) {
[Link](""+i);
try{
[Link](50);
}
catch(Exception e){
[Link]();
} } }}
class Slip12_1 implements ActionListener{
JFrame f;
JPanel p;
JTextField t;
JButton b;
Thread t1;
Slip12_1(){
f = new JFrame();
p = new JPanel();
t = new JTextField(60);
b = new JButton("Start");
t1 = new Thread(this);
[Link](this);
[Link](t);
[Link](b);
[Link](p);
[Link](400, 400);
[Link](true);
}
public void actionPerformed(ActionEvent e) {
[Link]();
} }
Write a java program to accept ‘N’ subject Names from a user store them into LinkedList
Collection and Display them by using Iterator interface. (Here employee data is mentioned
in below example. User subjects instead)
import [Link].*;
import [Link].*;
public class Slip29{
public static void main(String args[])throws Exception {
int n;
BufferedReader br = new BufferedReader(new InputStreamReader([Link]));
LinkedList li = new LinkedList ();
[Link]("\nEnter number of Employee : ");
n = [Link]([Link]());
[Link]("\nEnter name : ");
for(int i = 1; i <= n; i++) {
[Link]([Link]());
}
[Link]("\nLink List Content : ");
Iterator it = [Link](); {
[Link]([Link]());
}
[Link]("\nReverse order : ");
ListIterator lt = [Link]();
while([Link]()) {
[Link]();
}
while([Link]()) {
[Link]([Link]());
} }}
Write a java program using Multithreading to solve producer consumer problem in which
a producer produces a value and consumer consume the value before producer generate
the next value. (Hint: use thread synchronization)
class shop {
int material;
boolean flag = false;
public synchronized int get() {
while (flag == false) {
try {
wait();
} catch (Exception e) {
[Link]();
} // catch
} // while
flag = false;
notify();
return material;
}// get
public synchronized void put(int value) {
while (flag == true) {
try {
wait();
} catch (Exception e) {
[Link]();
} // catch
} // while
material = value;
flag = true;
notify();
}// put
}// shop
class Consumer extends Thread {
shop sh;
int no;
public Consumer(shop shp, int no) {
sh = shp;
[Link] = no;
}// consumerc
public void run() {
int value = 0;
for (int i = 0; i < 10; i++) {
value = [Link]();
[Link]("Consumer #" + [Link] + " got: " + value);
} // for
}// run
}// Consumer
class Producer extends Thread {
shop sh;
int no;
public Producer(shop s, int no) {
sh = s;
[Link] = no;
}// producerc
public void run() {
for (int i = 0; i < 10; i++) {
[Link](i);
[Link]("Producer #" + [Link] + " put: " + i);
try {
sleep((int) ([Link]() * 1000));
} catch (Exception e) {
[Link]();
} // catch
} // for
}// run
}// Producer
public class A3 {
public static void main(String[] args) {
shop s = new shop();
Producer p = new Producer(s, 1);
Consumer c = new Consumer(s, 1);
[Link]();
[Link]();
}}
Write a java program using Multithreading to accept a String from a user and display
each vowel from a String after every 3 seconds.
import [Link];
class VowelPrinter extends Thread {
private String inputString;
public VowelPrinter(String inputString) {
[Link] = inputString;
}
public void run() {
for (int i = 0; i < [Link](); i++) {
char ch = [Link](i);
if (isVowel(ch)) {
[Link](ch);
try {
[Link](3000); // Sleep for 3 seconds
} catch (InterruptedException e) {
[Link]();
} } } }
private boolean isVowel(char ch) {
ch = [Link](ch);
return ch == 'A' || ch == 'E' || ch == 'I' || ch == 'O' || ch == 'U';
}}
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
[Link]("Enter a string: ");
String inputString = [Link]();
VowelPrinter vowelPrinter = new VowelPrinter(inputString);
[Link]();
}}
Write a java program to accept N student names through command line, store them into
the appropriate Collection and display them by using Iterator and ListIterator interface.
import [Link];
import [Link];
import [Link];
import [Link];
public class Main {
public static void main(String[] args) {
// Check if there are any command line arguments
if ([Link] == 0) {
[Link]("Please provide student names as command line arguments.");
return;
}
List<String> studentNames = new ArrayList<>();
// Store student names into the list
for (String name : args) {
[Link](name);
}
// Display student names using Iterator
[Link]("Displaying student names using Iterator:");
displayNamesUsingIterator(studentNames);
// Display student names in reverse order using ListIterator
[Link]("\nDisplaying student names in reverse order using ListIterator:");
displayNamesInReverseOrder(studentNames);
}
private static void displayNamesUsingIterator(List<String> studentNames) {
Iterator<String> iterator = [Link]();
while ([Link]()) {
[Link]([Link]());
} }
private static void displayNamesInReverseOrder(List<String> studentNames) {
ListIterator<String> listIterator = [Link]([Link]());
while ([Link]()) {
[Link]([Link]());
} }}
Write a Java program to display information about all columns in the DONAR table using
ResultSetMetaData.
import [Link].*;
public class DONOR {
public static void main(String[] args) {
try {
// load a driver
[Link]("[Link]");
// Establish Connection
Connection conn = [Link]("jdbc:postgresql://localhost/postgres",
"postgres", "dsk");
Statement stmt = null;
stmt = [Link]();
ResultSet rs = [Link]("select * from donor");
ResultSetMetaData rsmd = [Link]();
[Link]("\t-------------------------------------------------");
int count = [Link]();
[Link]("\t No. of Columns: " + [Link]());
[Link]("\t-------------------------------------------------");
for (int i = 1; i <= count; i++) {
[Link]("\t\tColumn No : " + i);
[Link]("\t\tColumn Name : " + [Link](i));
[Link]("\t\tColumn Type : " + [Link](i));
[Link]("\t\tColumn Display Size : " + [Link](i));
[Link]();
} // for
[Link]("\t--------------------------------------------------");
[Link]();
[Link]();
[Link]();
} // try
catch (Exception e) {
[Link](e);
} // catch
}}
Write a Java program to create LinkedList of integer objects and perform the following:
i. Add element at first position
ii. Delete last element
iii. Display the size of link list
import [Link];
public class Main {
public static void main(String[] args) {
// Create a LinkedList of integers
LinkedList<Integer> linkedList = new LinkedList<>();
// Add elements at the first position
addFirst(linkedList, 10);
addFirst(linkedList, 20);
addFirst(linkedList, 30);
// Delete the last element
deleteLast(linkedList);
// Display the size of the LinkedList
displaySize(linkedList);
}
private static void addFirst(LinkedList<Integer> linkedList, int element) {
[Link](element);
[Link]("Added " + element + " at the first position.");
}
private static void deleteLast(LinkedList<Integer> linkedList) {
if (![Link]()) {
int removedElement = [Link]();
[Link]("Deleted last element: " + removedElement);
} else {
[Link]("The list is empty. No element to delete.");
} }
private static void displaySize(LinkedList<Integer> linkedList) {
[Link]("Size of the LinkedList: " + [Link]());
}}
Write a java program using Multithreading to demonstrate drawing Indian flag UseSwing)
import [Link].*;
import [Link].*;
import [Link];
class IndianFlag extends JPanel {
private final int WIDTH = 600;
private final int HEIGHT = 400;
private final int GREEN_WIDTH = WIDTH / 3;
private final int ORANGE_WIDTH = WIDTH / 3;
public IndianFlag() {
setPreferredSize(new Dimension(WIDTH, HEIGHT));
}
@Override
protected void paintComponent(Graphics g) {
[Link](g);
// Draw the flag
[Link]([Link]);
[Link](0, 0, ORANGE_WIDTH, HEIGHT);
[Link]([Link]);
[Link](ORANGE_WIDTH, 0, ORANGE_WIDTH, HEIGHT);
[Link](new Color(0, 128, 0));
[Link](ORANGE_WIDTH * 2, 0, GREEN_WIDTH, HEIGHT);
// Draw the Ashoka Chakra
int diameter = [Link](ORANGE_WIDTH, HEIGHT);
int x = ORANGE_WIDTH / 2;
int y = (HEIGHT - diameter) / 2;
[Link]([Link]);
[Link](x, y, diameter, diameter);
// 24 spokes
double angle = [Link](360.0 / 24);
int centerX = x + diameter / 2;
int centerY = y + diameter / 2;
int innerRadius = diameter / 4;
int outerRadius = diameter / 2;
for (int i = 0; i < 24; i++) {
double theta = i * angle;
int innerX = centerX + (int) ([Link](theta) * innerRadius);
int innerY = centerY + (int) ([Link](theta) * innerRadius);
int outerX = centerX + (int) ([Link](theta) * outerRadius);
int outerY = centerY + (int) ([Link](theta) * outerRadius);
[Link](innerX, innerY, outerX, outerY);
}
}
public void waveFlag() {
Thread t = new Thread(() -> {
while (true) {
repaint();
try {
[Link](100); // Adjust the speed of the animation by
changing the sleep time
} catch (InterruptedException e) {
[Link]();
} } });
[Link]();
}}
public class Main {
public static void main(String[] args) {
[Link](() -> {
JFrame frame = new JFrame("Indian Flag");
IndianFlag flag = new IndianFlag();
[Link](flag);
[Link]();
[Link](JFrame.EXIT_ON_CLOSE);
[Link](null);
[Link](true);
[Link](); // Start waving the flag
}); }}
Write a Java Program for the implementation of scrollable ResultSet. Assume Teacher
table with attributes (TID, TName, Salary) is already created.
(here employee example is given please consider this as Teacher).
import [Link].*;
import [Link].*;
import [Link].*;
class Slip25_2{
public static void main(String args[]){
Connection conn= null;
Statement stmt = null;
ResultSet rs = null;
int ch;
Scanner s=new Scanner([Link]);
try{
[Link]("[Link]");
conn=[Link]("jdbc:odbc:dsn");
stmt = [Link](ResultSet.TYPE_SCROLL_SENSITIVE,
ResultSet.CONCUR_UPDATABLE);
rs = [Link]("select * from employee");
int count=0;
while([Link]())
count++;
[Link]("Which Record u want");
[Link]("Records are = "+count);
do{
[Link]("1 First \n2 last \n3 next \n4 prev \n0 Exit");
ch=[Link]();
switch(ch){
case 1: [Link]();
[Link]("Roll :"+[Link](1)+" Name :"+[Link](2)); break;
case 2: [Link]();
[Link]("Roll :"+[Link](1)+" Name :"+[Link](2)); break;
case 3 : [Link]();
if([Link]())
[Link]("can't move forword");
else
[Link]("Roll :"+[Link](1)+" Name :"+[Link](2));
break;
case 4 : [Link]();
NR CLASSES, PUNE (8796064387/90)
if([Link]())
[Link]("can't move backword");
else
[Link]("Roll :"+[Link](1)+" Name :"+[Link](2));
break;
case 0 : break;
default:[Link]("Enter valid operation");
}//switch
}while(ch!=0);
}//end of try
catch(Exception e){
[Link](e);
}}//main}//class