Practical Assignment Java-II (1, 2 & 3)
Practical Assignment Java-II (1, 2 & 3)
Q.1.Write a java program to read ‘N’ names of your friends, store it into
HashSet And display them in ascending order.
ANS.
import java.util.*;
int n = sc.nextInt();
sc.nextLine();
friendsSet.add(sc.nextLine());
Collections.sort(sortedFriends);
System.out.println(friend);
ANS.
import java.util.*;
list.add("Apple");
list.add("Banana");
list.add("Orange");
list.removeFirst();
while (iterator.hasNext())
System.out.println(iterator.next());
Q3. Write a Java program to store city names and their STD codes
using an Appropriate collection and perform following operations:
ANS.
import java.util.*;
citySTDMap.put("Mumbai", "213");
citySTDMap.put("Pune", "312");
while (true)
sc.nextLine();
if (choice == 1)
if (!citySTDMap.containsKey(city))
citySTDMap.put(city, code);
System.out.println("City added.");
else
{
System.out.println("City exists!");
else if (choice == 2)
if (citySTDMap.remove(city) != null)
System.out.println("City removed.");
else
else if (choice == 3)
if (code != null)
else
}
}
else if (choice == 4)
break;
else
System.out.println("Invalid choice.");
sc.close();
Q4. Write a Java Program to create the hash table that will maintain
the mobile number and student name. Display the details of student
using Enumeration interface
ANS.
import java.util.*;
studentTable.put("9876543210", "Yash");
studentTable.put("9123456789", "Raj");
studentTable.put("9988776655", "Abhi");
studentTable.put("9345678901", "Gaurav");
Q5. Write a java program to create a TreeSet, add some colors (String)
and print out the content of TreeSet in ascending order
ANS.
import java.util.*;
colors.add("Blue");
colors.add("Red");
colors.add("Green");
colors.add("Yellow");
colors.add("Purple");
colors.add("Orange");
System.out.println(color);
}
PRACTICAL ASSIGNMENT-2 JAVA-II
Q.1. Write a Java program to display all the alphabets between ‘A’ to ‘Z’
after Every 2 seconds.
ANS.
import java.lang.*;
class Alphabet
System.out.println(letter);
try
Thread.sleep(200);
catch (InterruptedException e)
System.out.println("Thread interrupted");
ANS.
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public TrafficLight()
timer.start();
super.paintComponent(g);
g.setColor(Color.BLACK);
currentLight = (currentLight + 1) % 3;
repaint();
{
JFrame frame = new JFrame("Traffic Light");
frame.add(new TrafficLight());
frame.setSize(200, 350);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
ANS.
import java.util.Random;
import java.lang.*;
while (true)
if (num % 2 == 0)
else
try
Thread.sleep(1000);
catch (InterruptedException e)
Thread.currentThread().interrupt();
});
t1.start();
}
Q.4. Write a Java program to define a thread for printing text on output
screen for ‘n’ number of times. Create 3 threads and run them. Pass the
text ‘n’ parameters to the thread constructor.
ANS.
this.text = text;
this.times = times;
@Override
System.out.println(text);
{
MyThread thread1 = new MyThread("COVID19", 10);
thread1.start();
thread2.start();
thread3.start();
ANS.
import java.lang.*;
class ThreadInfo
};
t.setName("yash");
t.setPriority(7);
t.start();
}
Q.6. Write a Multithreading program in java to display the number’s
between 1 to 100 continuously in a TextField by clicking on button.
(Use Runnable Interface).
ANS.
import java.lang.*;
class Mythread
Thread t=Thread.currentThread();
t.setName("Demo Thread");
try
System.out.println(n);
Thread.sleep(500);
catch(InterruptedException e)
System.out.println("Thread interrupted");
}
PRACTICAL ASSIGNMENT-3 JAVA-II
ANS.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
public EmployeeForm()
setTitle("Employee Form");
setSize(300, 200);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
add(eidField);
add(new JLabel("Name:"));
add(enameField);
add(new JLabel("Designation:"));
add(designationField);
add(new JLabel("Salary:"));
add(salaryField);
add(saveButton);
stmt.setInt(1, Integer.parseInt(eidField.getText()));
stmt.setString(2, enameField.getText());
stmt.setString(3, designationField.getText());
stmt.setDouble(4, Double.parseDouble(salaryField.getText()));
stmt.executeUpdate();
import java.sql.*;
import java.util.Scanner;
String insertQuery = "INSERT INTO Product (Pid, Pname, Price) VALUES (?,
?, ?)";
stmt.executeUpdate(createTableQuery);
while (true)
if (pid == 0) break;
scanner.nextLine();
pstmt.setInt(1, pid);
pstmt.setString(2, pname);
pstmt.executeUpdate();
ResultSet rs = stmt.executeQuery(selectQuery);
while (rs.next()) {
catch (SQLException e)
e.printStackTrace();
OR
import java.sql.*;
String insertQuery = "INSERT INTO Product (Pid, Pname, Price) VALUES (?,
?, ?)";
String selectQuery = "SELECT * FROM Product";
stmt.executeUpdate(createTableQuery);
pstmt.setInt(1, 1);
pstmt.setString(2, "Laptop");
pstmt.executeUpdate();
pstmt.setInt(1, 2);
pstmt.setString(2, "Smartphone");
pstmt.executeUpdate();
pstmt.setInt(1, 3);
pstmt.setString(2, "Tablet");
pstmt.executeUpdate();
pstmt.setInt(1, 4);
pstmt.setString(2, "Headphones");
pstmt.executeUpdate();
pstmt.setInt(1, 5);
pstmt.setString(2, "Monitor");
while (rs.next()) {
System.out.println("Pid: " + pid + ", Pname: " + pname + ", Price: " + price);
catch (SQLException e)
e.printStackTrace();
ANS.
import java.sql.*;
ResultSet rs = stmt.executeQuery(selectQuery))
System.out.println("-----------------------------------");
catch (SQLException e)
e.printStackTrace();
ANS.
import java.sql.*;
while (tables.next())
System.out.println(tables.getString("TABLE_NAME"));
catch (SQLException e)
e.printStackTrace();
}
}
ANS,
import java.sql.*;
insertStmt.setInt(1, 1);
insertStmt.setString(3, "JAVA");
insertStmt.executeUpdate();
insertStmt.setInt(1, 2);
insertStmt.setString(3, "Python");
insertStmt.executeUpdate();
insertStmt.setInt(1, 3);
insertStmt.executeUpdate();
insertStmt.setInt(1, 4);
insertStmt.setString(3, "DA");
insertStmt.executeUpdate();
insertStmt.setInt(1, 5);
insertStmt.setString(3, "STT");
insertStmt.executeUpdate();
selectStmt.setString(1, "JAVA");
while (resultSet.next()) {
System.out.println("TNo: " + tNo + ", TName: " + tName + ", Subject: " +
subject);
}catch (SQLException e)
e.printStackTrace();
}
}
OR
import java.sql.*;
import java.util.Scanner;
insertStmt.setString(1, tNo);
insertStmt.setString(2, tName);
insertStmt.setString(3, subject);
insertStmt.executeUpdate();
selectStmt.setString(1, "JAVA");
while (resultSet.next())
System.out.println("TNo: " + tNo + ", TName: " + tName + ", Subject: " +
subject);
catch (SQLException e)
{e.printStackTrace();
finally
{scanner.close();