Java Sem6 Programs

 Java Sem-6 Programs:




//Write a java program to accept names of ‘n’ cities, insert same into array list
//collection and display the contents of same array list, also remove all these elements

import java.util.ArrayList;
import java.util.Scanner;

public class S2Ass1A1 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
ArrayList a = new ArrayList();

System.out.println("Enter the no. of cities:");
int n = sc.nextInt();
String s[] = new String[n];

for(int i=0; i<n; i++)
{
s[i] = sc.next();
a.add(s[i]);

}
System.out.println("Displaying ArrayList:"+a);
a.clear();
System.out.println("After Removing ArrayList:"+a);
}
}

/*
OUTPUT:
Enter the no. of cities:
5
pune
baramti
patas
supe
mumbai
Displaying ArrayList:[pune, baramti, patas, supe, mumbai]
After Removing ArrayList:[]
*/



//Write a java program to read ‘n’ names of your friends, store it into linked list, also
//display contents of the same

import java.util.LinkedList;
import java.util.Scanner;

public class S2Ass1A2 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);

LinkedList l = new LinkedList();

System.out.println("Enter the no. of friends:");
int n = sc.nextInt();

String s[] = new String[n];

System.out.println("Enter the "+n+" names of your friends:");
for (int i=0; i<n; i++)
{
s[i] = sc.next();
l.add(s[i]);
}

System.out.println(l);
}
}

/*
OUTPUT:
Enter the no. of friends:
5
Enter the 5 names of your friends:
swapnil
raju
om
saurabh
ritik
[swapnil, raju, om, saurabh, ritik]

*/



//Write a program to create a new tree set, add some colors (string) and print out the
//tree set

import java.util.TreeSet;

public class S2Ass1Q3 {
public static void main(String[] args) {
TreeSet<String> t = new TreeSet<String>();
t.add("Pink");
t.add("Blue");
t.add("red");
t.add("green");
t.add("black");
t.add("Yellow");

System.out.println(t);

}
}

/*
OUTPUT:
[Blue, Pink, Yellow, black, green, red]
*/



//) Create the hash table that will maintain the mobile number and student name. Display
//the contact list.

import java.util.Hashtable;

public class S2Ass1Q4 {
public static void main(String[] args) {
Hashtable h = new Hashtable();
h.put("swapnil",123);
h.put("om",345);
h.put("siri",987);
h.put("alexa",324);

System.out.println(h);
}
}

/*
OUTPUT:
{om=345, siri=987, swapnil=123, alexa=324}
*/



// Accept ‘n’ integers from the user. Store and display integers in sorted order having
//proper collection class. The collection should not accept duplicate elements

import java.util.Scanner;
import java.util.TreeSet;

public class S2Ass1B1 {
public static void main(String[] args) {
TreeSet t = new TreeSet();
Scanner sc = new Scanner(System.in);

System.out.println("Enter the no. of Integers:");
int n = sc.nextInt();

System.out.println("Enter the Integers:");
for (int i=0; i<n; i++)
{
t.add(sc.nextInt());
}

System.out.println(t);
}
}

/*
OUTPUT:
Enter the no. of Integers:
5
Enter the Integers:
4
3
8
5
1
[1, 3, 4, 5, 8]
*/



//Write a program to sort HashMap by keys and display the details before sorting and
//after sorting.

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map.Entry;
import java.util.TreeMap;

public class S2Ass1B2 {
public static void main(String[] args) {
HashMap h = new HashMap();
h.put(5,"swapnil");
h.put(1,"seri");
h.put(88,"alexa");
h.put(101,"hena");
h.put(2,"messi");
h.put(3,"otis");

System.out.println("Before Sorting HashMap is:");
Iterator itr = h.keySet().iterator();
while(itr.hasNext())
{
int key = (int)itr.next();
System.out.println(key+" = "+h.get(key));
}

System.out.println("After Sorting HashMap is:");
TreeMap t = new TreeMap(h);
Iterator itr1 = t.keySet().iterator();
while(itr1.hasNext())
{
int key = (int)itr1.next();
System.out.println(key+" = "+h.get(key));
}

}
}
/*OUTPTU:
Before Sorting HashMap is:
1 = seri
2 = messi
3 = otis
5 = swapnil
101 = hena
88 = alexa

After Sorting HashMap is:
1 = seri
2 = messi
3 = otis
5 = swapnil
88 = alexa
101 = hena
*/



import java.io.FileReader;
import java.io.IOException;
import java.util.Hashtable;
import java.util.Scanner;

public class S2Ass1B3 {
public static void main(String[] args) throws IOException
{
FileReader fr = new FileReader("fname.txt");
Scanner sc = new Scanner(fr);
Hashtable h = new Hashtable();

while(sc.hasNextLine())
{
String name = sc.next();
Integer phno = sc.nextInt();
h.put(name,phno);
}

System.out.println(h);



}
}


/* OUTPUT:
PS C:\Users\swapniljamble\IdeaProjects\MyProject\src> javac S2Ass1B3.java
Note: S2Ass1B3.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
PS C:\Users\swapniljamble\IdeaProjects\MyProject\src> java S2Ass1B3
{anu=2348, nil=23487, avi=23048, swapnil=298374, omu=238947}

*/


/*  Create a java application to store city names and their STD codes using an
appropriate collection. The GUI should allow the 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 java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;


public class S2Ass1C1 extends JFrame implements ActionListener
{
JTextField t1,t2,t3;
JButton b1,b2,b3;
JTextArea t;
JPanel p1,p2;

Hashtable ts;
S2Ass1C1()
{
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();
p1.add(t);

p2= new JPanel();
p2.setLayout(new GridLayout(2,3));
p2.add(t1);
p2.add(t2);
p2.add(b1);
p2.add(t3);
p2.add(b2);
p2.add(b3);

add(p1);
add(p2);

b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);

setLayout(new FlowLayout());
setSize(500,500);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


}
public void actionPerformed(ActionEvent e)
{
if(b1==e.getSource())
{
String name = t1.getText();
int code = Integer.parseInt(t2.getText());
ts.put(name,code);
Enumeration k=ts.keys();
Enumeration v=ts.elements();
String msg="";
while(k.hasMoreElements())
{
msg=msg+k.nextElement()+" = "+v.nextElement()+"\n";
}
t.setText(msg);
t1.setText("");
t2.setText("");
}
else if(b2==e.getSource())
{
String name = t3.getText();

if(ts.containsKey(name))
{
t.setText(ts.get(name).toString());
}

else
JOptionPane.showMessageDialog(null,"City not found ...");
}
else if(b3==e.getSource())
{
String name = t3.getText();

if(ts.containsKey(name))
{
ts.remove(name);
JOptionPane.showMessageDialog(null,"City Deleted ...");
}

else
JOptionPane.showMessageDialog(null,"City not found ...");
}
}
public static void main(String a[])
{
new S2Ass1C1();
}
}


/* Write a program to create link list of integer objects. Do the following:
i. add element at first position
ii. delete last element
iii. display the size of link list
*/

import java.util.LinkedList;

public class S2Ass1C2 {

public static void main(String[] args) {
LinkedList l = new LinkedList();
l.addFirst(5);
l.add(9);
l.add(53);
l.add(34);
l.add(3);
l.add(93);
l.addLast(66);
System.out.println(l);
l.addFirst(88);
System.out.println("After Adding element in first position:"+l);
l.removeLast();
System.out.println("After removing last element in link list:"+l);
System.out.println("The size of given Linked list is: "+l.size());

}
}

/* OUTPUT:
[5, 9, 53, 34, 3, 93, 66]
After Adding element in first position:[88, 5, 9, 53, 34, 3, 93, 66]
After removing last element in link list:[88, 5, 9, 53, 34, 3, 93]
The size of given Linked list is: 7
*/







Assignment 2




class A extends Thread
{
public void run()
{
for(int i=1;i<=10;i++)
{
System.out.println("COVID19...."+i);
}
}
}

class B extends Thread
{
public void run()
{
for(int i=1;i<=20;i++)
{
System.out.println("LOCKDOWN2020...."+i);
}
}
}


class C extends Thread
{
public void run()
{
for(int i=1;i<=30;i++)
{
System.out.println("VACCINATED2021...."+i);
}
}
}

public class S2Ass2A1 {

public static void main(String args[])
{
A obj=new A();
obj.start();

B obj1=new B();
obj1.start();

C obj2=new C();
obj2.start();
}
}

/* OUTPUT:
COVID19....1
COVID19....2
COVID19....3
COVID19....4
COVID19....5
COVID19....6
COVID19....7
LOCKDOWN2020....1
LOCKDOWN2020....2
LOCKDOWN2020....3
LOCKDOWN2020....4
LOCKDOWN2020....5
LOCKDOWN2020....6
LOCKDOWN2020....7
LOCKDOWN2020....8
LOCKDOWN2020....9
LOCKDOWN2020....10
LOCKDOWN2020....11
LOCKDOWN2020....12
LOCKDOWN2020....13
LOCKDOWN2020....14
LOCKDOWN2020....15
LOCKDOWN2020....16
LOCKDOWN2020....17
LOCKDOWN2020....18
LOCKDOWN2020....19
LOCKDOWN2020....20
VACCINATED2021....1
VACCINATED2021....2
VACCINATED2021....3
VACCINATED2021....4
VACCINATED2021....5
COVID19....8
VACCINATED2021....6
COVID19....9
VACCINATED2021....7
COVID19....10
VACCINATED2021....8
VACCINATED2021....9
VACCINATED2021....10
VACCINATED2021....11
VACCINATED2021....12
VACCINATED2021....13
VACCINATED2021....14
VACCINATED2021....15
VACCINATED2021....16
VACCINATED2021....17
VACCINATED2021....18
VACCINATED2021....19
VACCINATED2021....20
VACCINATED2021....21
VACCINATED2021....22
VACCINATED2021....23
VACCINATED2021....24
VACCINATED2021....25
VACCINATED2021....26
VACCINATED2021....27
VACCINATED2021....28
VACCINATED2021....29
VACCINATED2021....30

*/












d











d





















d











d

Comments

Popular posts from this blog

Practical slips programs : Machine Learning

Full Stack Developement Practical Slips Programs

Android App Developement Practicals Programs