java-assignments
java-assignments
display the
contents of same array list, also remove all these elements
1. import java.util.Scanner;
2. import java.util.ArrayList;
3. public class a1
4. {
5. public static void main(String args[])
6. {
7. Scanner sc = new Scanner(System.in);
8. System.out.println("Enter number of cities:");
9. int n = sc.nextInt();
10. ArrayList<String> a1 = new ArrayList<String>();
11. sc.nextLine();
12. for(int i = 0;i<n;i++)
13. {
a. System.out.print("Enter city "+(i+1)+":");
b. String city = sc.nextLine();
c. a1.add(city);
14. }
15. System.out.println("The cities are:");
16. System.out.println(a1);
17. a1.clear();
18. System.out.println("After Clearing");
19. System.out.println(a1);
20. }
21. }
#A2 b) Write a java program to read 'n' names of your friends, store it into linked list, also display contents of
the same.
1. import java.util.Scanner;
2. import java.util.*;
3. public class a2
4. {
5. public static void main(String args[])
6. {
7. Scanner sc = new Scanner(System.in);
8. LinkedList<String> friends = new LinkedList<String>();
9. System.out.print("Enter number of friends: ");
10. int n = sc.nextInt();
11. sc.nextLine();
12. for(int i = 0;i<n;i++)
13. {
a. System.out.print("Enter Friend "+(i+1)+":");
b. String friend = sc.nextLine();
c. friends.add(friend);
14. }
15. System.out.println("Friends:");
16. Iterator<String>itr = friends.iterator();
17. while(itr.hasNext())
18. {
a. System.out.println(itr.next());
19. }
20. }
21. }
#A3 c) Write a program to create a new tree set, add some colors (string) and print out the tree set
1. import java.util.Scanner;
2. import java.util.Set;
3. import java.util.TreeSet;
4. public class a3
5. {
6. public static void main(String args[])
7. {
8. Scanner sc = new Scanner(System.in);
9. Set<String> ts = new TreeSet<String>();
10. System.out.print("Enter number of colors: ");
11. int n = sc.nextInt();
12. sc.nextLine();
13. for(int i = 0;i<n;i++)
14. {
a. System.out.print("Enter color "+(i+1)+":");
b. String color = sc.nextLine();
c. ts.add(color);
15. }
16. System.out.println("Colors:");
17. System.out.println(ts);
18. }
19. }
#A4 d) Create the hash table that will maintain the mobile number and student name. Display the contact list.
1. import java.util.Scanner;
2. import java.util.*;
3. public class a4
4. {
5. public static void main(String args[])
6. {
7. Scanner sc = new Scanner(System.in);
8. Hashtable<String,String> ht = new Hashtable<String,String>();
9. System.out.print("Enter number of students: ");
10. int n = sc.nextInt();
11. sc.nextLine();
12. for(int i = 0;i<n;i++)
13. {
#B1 a) Accept 'n' integers from the user. Store and display integers in sorted order having proper collection class.
The collection should not accept duplicate elements.
1. import java.util.Scanner;
2. import java.util.Set;
3. import java.util.TreeSet;
4. public class b1
5. {
6. public static void main(String args[])
7. {
8. Scanner sc = new Scanner(System.in);
9. Set<Integer> ts = new TreeSet<Integer>();
10. System.out.print("Enter how many numbers: ");
11. int n = sc.nextInt();
12. sc.nextLine();
13. for(int i = 0;i<n;i++)
14. {
a. System.out.print("Enter number "+(i+1)+":");
b. int num = sc.nextInt();
c. ts.add(num);
15. }
16. System.out.println("Sorted Numbers:");
17. System.out.println(ts)
18. }
19. }
#B2 Write a program to sort HashMap by keys and display the details before sorting and after sorting
1. import java.util.*;
2. public class b2
3. {
4. public static void main(String args[])
5. {
6. if(args.length == 0)
7. {
a. System.out.println("khudse likh loollolllololol");
b. return;
8. }
9. Scanner sc = new Scanner(System.in);
10. HashMap<String, Integer> hashmap = new HashMap<>();
#B3 Write a program that loads names and phone numbers from a text file where the data is organized as one
line per record and each field in a record are separated by a tab (\t). it takes a name or phone number as input
and prints the corresponding other value from the hash table (hint: use hash tables)
1. import java.util.*;
2. import java.io.BufferedReader;
3. import java.io.File;
4. import java.io.*;
5. import java.util.Scanner;
6. import java.util.Scanner;
7. public class b3
8. {
9. public static void main(String args[]){
10. try{
11. File f = new File("B3.txt");
12. BufferedReader br = null;
13. br = new BufferedReader(new FileReader(f));
14. Hashtable<String, String>table = new Hashtable<>();
15. Scanner sc = new Scanner(System.in);
16. String line = "";
#C2 b) Write a program to create link list of integer objects. Do the following:
1. import java.util.*;
2. import java.util.Scanner;
3. import java.util.Iterator;
4. public class c2
5. {
6. public static void displayList(LinkedList a1)
7. {
8. Iterator<Integer>itr = a1.iterator();
9. while(itr.hasNext())
10. {
a. System.out.println(itr.next());
11. }
12. }
13. public static void main(String[] args)
14. {
15. LinkedList<Integer> a1 = new LinkedList<Integer>();
16. Scanner sc = new Scanner(System.in);
17. a1.add(1);
18. int choice = 0;
19. do
20. {
21. System.out.println("1.Enter Element at first position:");
22. System.out.println("2.Delete Last Element:");
23. System.out.println("3.Display the size of LinkedList:");
24. System.out.print("Enter your choice:");
25. choice = sc.nextInt();
26. switch(choice)
27. {
a. case 1: System.out.print("Enter Element:");
b. int el = sc.nextInt();
c. a1.addFirst(el);
d. System.out.println("Element "+el+" Entered in the LinkedList");
e. displayList(a1);
f. break;
ASIIGNMENT 2
#A1 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.
Example:
public class a1
{
public static void main(String args[])
{
Thread t1 = new PrintThread("Covid-19", 10);
Thread t2 = new PrintThread("LOCKDOWN2020", 20);
Thread t3 = new PrintThread("VACCINATED2021", 30);
t1.start();
t2.start();
t3.start();
}
}
#A2 b) Write a program in which thread sleep for 6 sec in the loop in reverse order from 100 to 1 and change the
name of thread.
13. }
14. }
15. }
23. t1.start();
24. t2.start();
25. t3.start();
26. if(!t1.isAlive() && !t2.isAlive() && !t3.isAlive())
27. {
a. System.out.print("|-----------------By Tanmay Waghmare-----------------
----|");
28. }
29. }
30. }
#A3 c) Write a program 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)
1. class SharedResources
2. {
3. private int value;
4. private boolean available = false;
5. public synchronized void produce(int newValue)
6. {
7. while(available)
8. {
a. try
b. {
c. wait();
d. }
e. catch(InterruptedException e)
f. {
g. System.out.println("Producer interrupted:" + e.getMessage());
h. }
9. }
10. value = newValue;
11. available = true;
23. }
24. }
//Set B 1 a) Write a program to calculate the sum and average of an array of 1000 integers (generated
randomly) using 10 threads. Each thread calculates the sum of 100 integers. Use these values to calculate average
[Use join method ].
import java.util.Random;
import java.util.*;
class SumThread extends Thread
{
private int[] array;
private int startIndex;
private int sum=0;
for(int i=0;i<10;i++)
{
thread[i]=new SumThread(array,i*100);
thread[i].start();
}
int total=0;
for(int i=0;i<10;i++)
{
thread[i].join();
total+=thread[i].getSum();
}
double avg=total/1000.0;
System.out.println("Total sum: "+total);
System.out.println("Average: "+avg);
}
}
import java.util.*;
import java.io.*;
class FileSearch extends Thread
{
private File File;
private String search;
import java.util.Random;
class SharedData
{
private int number;
private boolean isNewNumber = false;
public synchronized void setNumber(int number)
{
while(isNewNumber)
{
try{
wait();
}catch(InterruptedException e)
{
Thread.currentThread().interrupt();
System.out.println("Thread Interrupted!");
}
}
this.number = number;
isNewNumber = true;
notifyAll();
}
isNewNumber = false;
notifyAll();
return number;
}
}
}
}
}
}
public class b3
{
public static void main(String[] args)
{
SharedData sharedData = new SharedData();
generator.start();
even.start();
odd.start();
}
}
ASSIGNMENT 3
#A1 a) Create a PROJECT table with fields project_id, Project_name,
Project_description, Project Status, etc. Insert values in the table. Display all the
details of the PROJECT table in a tabular format on the screen (using swing).
1. import javax.swing.*;
2. import javax.swing.table.DefaultTableModel;
3. import java.awt.*;
4. import java.sql.*;
5. public class a1 {
#A2 b) Write a program to display information about the database and list all the
tables in the database. (Use DatabaseMetaData).
1. import java.sql.*;
2. public class a2 {
3. private static final String URL = "jdbc:postgresql://192.168.0.10/tydb180";
4. private static final String USER = "ty180";
5. private static final String PASSWORD = "";
#A3 c) Write a program to display information about all columns in the DONAR
table using ResultSetMetaData
import java.sql.*;
public class a3 {
private static final String URL = "jdbc:postgresql://192.168.0.10/tydb180";
private static final String USER = "ty180";
private static final String PASSWORD = "";
#B1 a) Create a MOBILE table with fields Model Number, Model Name,
Model_Color, Sim Type, Network Type, BatteryCapacity, Internal Storage, RAM and
Processor Type. Insert values in the table. Write a menu driven program to pass the
input using Command line argument to perform the following operations on
MOBILE table.
1. Insert 2. Modify 3. Delete 4. Search 5. View All 6. Exit
import java.sql.*;
public class b1 {
static final String URL = "jdbc:postgresql://192.168.0.10/tydb180";
static final String USER = "ty180";
static final String PASSWORD = "your_password";
case "Modify":
if (args.length < 3) {
System.out.println("Usage: Modify <Model_Number>
<Column_Name> <New_Value>");
return;
}
modifyMobile(conn, args);
break;
case "Delete":
if (args.length < 2) {
System.out.println("Usage: Delete <Model_Number>");
return;
}
deleteMobile(conn, args[1]);
break;
case "Search":
if (args.length < 2) {
System.out.println("Usage: Search <Model_Number>");
return;
}
searchMobile(conn, args[1]);
break;
case "ViewAll":
viewAllMobiles(conn);
break;
case "Exit":
System.out.println("Exiting program...");
break;
default:
System.out.println("Invalid operation. Use Insert, Modify, Delete,
Search, ViewAll, or Exit.");
}
} catch (SQLException e) {
e.printStackTrace();
}
}
if (rs.next()) {
System.out.println("Model Number: " +
rs.getString("Model_Number"));
System.out.println("Model Name: " + rs.getString("Model_Name"));
System.out.println("Color: " + rs.getString("Model_Color"));
System.out.println("Sim Type: " + rs.getString("Sim_Type"));
System.out.println("Network Type: " + rs.getString("NetworkType"));
System.out.println("Battery: " + rs.getInt("BatteryCapacity") + "mAh");
System.out.println("Storage: " + rs.getInt("InternalStorage") + "GB");
System.out.println("RAM: " + rs.getInt("RAM") + "GB");
System.out.println("Processor: " + rs.getString("ProcessorType"));
} else {
System.out.println("No record found.");
}
}
}
while (rs.next()) {
System.out.println("Model Number: " + rs.getString("Model_Number")
+
", Name: " + rs.getString("Model_Name") +
", Color: " + rs.getString("Model_Color") +
", Sim: " + rs.getString("Sim_Type") +
", Network: " + rs.getString("NetworkType") +
", Battery: " + rs.getInt("BatteryCapacity") + "mAh" +
", Storage: " + rs.getInt("InternalStorage") + "GB" +
", RAM: " + rs.getInt("RAM") + "GB" +
", Processor: " + rs.getString("ProcessorType"));
}
}
}
}
ASSIGNMENT 4
a) Design a servlet that provides information about a HTTP request from a client,
such as IP address and browser type. The servlet also provides information about
the server on which the servlet is running, such as the operating system type, and
the names of currently loaded servlets.
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse
response)
throws ServletException, IOException {
out.println("<html><body>");
out.println("<h2>Client Information</h2>");
out.println("<p><strong>IP Address:</strong> " + clientIP + "</p>");
out.println("<p><strong>Browser (User-Agent):</strong> " + userAgent +
"</p>");
out.println("<h2>Server Information</h2>");
out.println("<p><strong>Operating System:</strong> " + osName +
"</p>");
out.println("<h2>Loaded Servlets</h2>");
if (servletNames != null) {
out.println("<ul>");
while (servletNames.hasMoreElements()) {
out.println("<li>" + servletNames.nextElement() + "</li>");
}
out.println("</ul>");
} else {
out.println("<p>Servlet names not available (Check Servlet API
version)</p>");
}
out.println("</body></html>");
}
}
#DATABASEINFO.JAVA
import java.sql.*;
#DONORTABLEINFO.java
import java.sql.*;
#projecttabledisplay.java
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.sql.*;
public ProjectTableDisplay() {
JFrame frame = new JFrame("Project Details");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(600, 400);
frame.setLayout(new BorderLayout());
// Database Operations
try (Connection conn = DriverManager.getConnection(URL, USER,
PASSWORD);
Statement stmt = conn.createStatement()) {
// Add components
JScrollPane scrollPane = new JScrollPane(table);
frame.add(scrollPane, BorderLayout.CENTER);
frame.setVisible(true);
}
}