Assignment 1
Assignment 1
1) Write a java program to accept names of ‘n’ cities, insert same into an array list collection and
display the contents of the same array list, also remove all these elements.
[10]
*/
import java.util.*;
class Q1
{
public static void main ( String args[])
{
Scanner sc = new Scanner(System.in);
System.out.println("How many cities you want store : ");
int n = sc.nextInt();
ArrayList <String> cities = new ArrayList <String> (n);
System.out.println("Enter city names : ");
for ( int i = 0; i < n; i++ )
cities.add(sc.next());
System.out.println ( "City names are : ");
Iterator it = cities.iterator();
while(it.hasNext())
System.out.println(it.next());
cities.clear();
System.out.println("After removing all elements, size = " +cities.size());
sc.close();
}
}
b) 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.*;
public class A2 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
c) Write a program to create a new tree set, add some colors (string) and print out the tree set.
import java.util.Scanner;
import java.util.TreeSet;
public class A3 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
TreeSet<Object> ts = new TreeSet<>();
System.out.println("Enter How many Colours :");
int n = sc.nextInt();
System.out.println("Enter the "+n+" Colours :");
sc.nextLine();
for (int i = 0; i < n; i++) {
String c = sc.nextLine();
ts.add(c);
}
System.out.println("Colours :" + ts);
sc.close();
}
}
d) Create the hash table that will maintain the mobile number and student name.
Display the contact list.
import java.util.*;
class Q2
{
public static void main(String args[])
{
SET B
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.
import java.util.TreeSet;
import java.util.Scanner;
public class B1 {
int n = sc.nextInt();
ts.add(num);
sc.close();
}
b) Write a program to sort HashMap by keys and display the details before sorting and after sorting.
import java.util.HashMap;
import java.util.TreeMap;
public class B2 {
map.put("Prasad", 2002);
map.put("Ashish", 2001);
map.put("Suhas", 2002);
map.put("Swayam", 2001);
map.put("Sanket", 2002);
c) 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)or(:).it takes a name or phone
number as input and prints the corresponding other value from the hash table (hint: use hash tables).
import java.io.*;
import java.util.*;
try
{
String[] strarray;
String a,str;
while(sc.hasNext())
a=sc.nextLine();
strarray=a.split("\t");
ht.put(strarray[0],strarray[1]);
str=s.next();
if(ht.containsKey(str))
System.out.println("phone no is"+ht.get(str));
else
}
catch(Exception e)
System.out.println(e);
Text File
prasad 8796465800
SET C
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
JPanel p1;
C1() {
setSize(700, 500);
setVisible(true);
add(name);
add(txtname);
add(stdcode);
add(txtstd);
add(op);
p1 = new JPanel();
p1.add(btnadd);
btnadd.addActionListener(this);
p1.add(btndelete);
btndelete.addActionListener(this);
p1.add(btnsearch);
btnsearch.addActionListener(this);
add(p1);
}// SETC1
if (ae.getSource() == btnadd) {
// System.out.println(table.containsKey(name) || table.containsValue(std));
if (table.containsKey(name) || table.containsValue(std)) {
JOptionPane.showMessageDialog(null,s2,s2, JOptionPane.ERROR_MESSAGE);
} else {
table.put(name, std);
System.out.println(table);
JOptionPane.INFORMATION_MESSAGE);
txtname.setText("");
txtstd.setText("");
if (ae.getSource() == btndelete) {
table.remove(s1);
JOptionPane.showMessageDialog(null, "Succesfully removed City & STD Code", name,
JOptionPane.INFORMATION_MESSAGE);
if (ae.getSource() == btnsearch) {
if (table.containsKey(s1)) {
JOptionPane.showMessageDialog(null, s2);
} else {
new C1();
import java.util.LinkedList;
import java.util.Scanner;
public class C2 {
ll.add(1);
ll.add(2);
ll.add(3);
ll.addFirst(0);
ll.removeLast();
sc.close();
c) Read a text file, specified by the first command line argument, into a list. The program should then
display a menu which performs the following operations on thelist:
When the user selects Exit, save the contents of the list to the file and end the program.
import java.util.*;
import java.io.*;
public class C3 {
try {
// Here, we have used command line argument , so at the time of run the code use
command java C3 C3.txt
BufferedReader br = null;
int ch;
al.add(line);
} // while
// main logic
do {
ch = sc.nextInt();
switch (ch) {
case 1:
al.add(l1);
break;
case 2:
al.remove(l1);
break;
case 3:
al.add(l1);
break;
case 4:
int n = al.size() - 1;
break;
case 5:
ListIterator<Object> li = al.listIterator();
while (li.hasNext()) {
fout.write(b);
System.exit(1);
break;
br.close();
fout.close();
} catch (Exception e) {
System.out.println(e);
sc.close();
Text File