//Assignment No:1
Set A
a) 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.
CODE
š
- import java.util.*;
- public class city
- {
- public static void main(String args[])
- {
- Scanner sc = new Scanner(System.in);
- ArrayList<String> al = new ArrayList<String>();
- String c;
- System.out.println("How Many City..?");
- int n = sc.nextInt();
- for(int i=0; i<n; i++)
- {
- System.out.println("Enter City Name");
- c = sc.next();
- al.add(c);
- }
- System.out.println("Citys are"+al);
- al.clear();
- if(al.isEmpty() == true)
- System.out.println("Arraylist is empty");
- else
- System.out.println("Arraylist is not empty");
- }
- }
b) Write a java program to read ‘n’ names of your friends, store it into linked list, also
display contents of the same.
CODE
š
- import java.util.*;
- public class friends
- {
- public static void main(String args[])
- {
- Scanner sc = new Scanner(System.in);
- LinkedList<String> ll = new LinkedList<String>();
- String c;
- System.out.println("How Many Friends.?");
- int n = sc.nextInt();
- for(int i=0; i<n; i++)
- {
- System.out.println("Enter Friends Name");
- c = sc.next();
- ll.add(c);
- }
- System.out.println("Friends are"+ll);
- }
- }
c) Write a program to create a new tree set, add some colors (string) and print out the
tree set.
CODE
š
- import java.util.*;
- public class colors
- {
- public static void main(String args [])
- {
- Scanner sc = new Scanner(System.in);
- TreeSet<String> ts = new TreeSet<String>();
- System.out.println("How many Colors..?");
- int n = sc.nextInt();
- for(int i=1; i <=n; i++)
- {
- System.out.println("Enter Color "+i);
- String c = sc.next();
- ts.add(c);
- }
- //System.out.println("Colors are"+ts);
- Iterator itr = ts.iterator();
- while(itr.hasNext())
- {
- System.out.println("Colors Are : "+itr.next());
- }
- }
- }
d) Create the hash table that will maintain the mobile number and student name. Display
the contact list.
CODE
š
- import java.io.*;
- import java.util.*;
- class hash_table
- {
- public static void main (String args[]) throws Exception
- {
- Hashtable<String,Integer> ht = new Hashtable<String,Integer>();
- Scanner sc = new Scanner(System.in);
- int mno;
- String name = null;
- System.out.println("\nEnter no of Students");
- int n = sc.nextInt();
- for(int i=0; i<n; i++)
- {
- System.out.println("Enter Student Name:");
- name = sc.next();
- System.out.println("Enter Student Mobile No:");
- mno = sc.nextInt();
- ht.put(name,mno);
- }
- System.out.println("Hash Table"+ht);
- Enumeration k = ht.keys();
- Enumeration v = ht.elements();
- System.out.println("Name\tMobile Numbers");
- while(k.hasMoreElements())
- {
- System.out.println(k.nextElement()+"\t"+k.nextElement());
- }
- }
- }
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.
CODE
š
- import java.util.*;
- public class treeset
- {
- public static void main(String args [])
- {
- Scanner sc = new Scanner(System.in);
- TreeSet<Integer> ts = new TreeSet<Integer>();
- System.out.println("How many Numbers");
- int n = sc.nextInt();
- for(int i=1; i <=n; i++)
- {
- System.out.println("Enter Number "+i);
- int num = sc.nextInt();
- ts.add(num);
- }
- System.out.println("The Sorted Numbers are"+ts);
- }
- }
b) Write a program to sort HashMap by keys and display the details before sorting and
after sorting.
CODE
š
- import java.util.*;
- public class SortHashMap
- {
- public static void main(String args [])
- {
- Scanner sc = new Scanner(System.in);
- HashMap<Integer, String> hm = new HashMap<Integer, String>();
- System.out.println("How many Students");
- int n = sc.nextInt();
- for(int i=1; i <=n; i++)
- {
- System.out.println("Enter Roll No "+i+" Student");
- int num = sc.nextInt();
- System.out.println("Enter Name "+i+" Stdudent");
- String name = sc.next();
- hm.put(num,name);
- }
- Iterator <Integer> it = hm.keySet().iterator();
- System.out.println("Before Sorting");
- while(it.hasNext())
- {
- int key = (int)it.next();
- System.out.println("Rol No: "+key+"Name "+hm.get(key));
- }
- System.out.println("\nAfter Sorting");
- TreeMap <Integer,String> tm = new TreeMap <Integer,String> (hm);
- Iterator <Integer> itr = tm.keySet().iterator();
- while(itr.hasNext())
- {
- int key = (int)itr.next();
- System.out.println("Roll No: "+key+" Name "+hm.get(key));
- }
- }
- }
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).it takes a name or phone number as input and prints the corresponding other value
from the hash table (hint: use hash tables).
CODE
š
- import java.io.*;
- import java.util.*;
- public class Phonebook
- {
- public static void main(String[] args)
- {
- try
- {
- FileInputStream fis = new FileInputStream("myfile.txt");
- Scanner sc = new Scanner(fis).useDelimiter("\t");
- Hashtable<String, String> ht = new Hashtable<String, String> ();
- String[] strarray;
- String a, str;
- while(sc.hasNext())
- {
- a = sc.nextLine();
- strarray = a.split("\t");
- ht.put(strarray[0],strarray[1]);
- System.out.println("Hash Table Values "+strarray[0]+":"+strarray[1]);
- }
- Scanner s = new Scanner(System.in);
- System.out.println("Enter the name as given in the phone book");
- str = s.nextLine();
- if(ht.containsKey(str))
- {
- System.out.println("Phone no is "+ht.get(str));
- }
- else
- {
- System.out.println("Name is not matched");
- }
- }
- catch(Exception e)
- {
- System.out.println(e);
- }
- }
- }