Skip to main content

Assignment No:1

 //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

šŸ‘‡

  1. import java.util.*;
  2. public class city
  3. {
  4. public static void main(String args[])
  5. {
  6. Scanner sc = new Scanner(System.in);
  7. ArrayList<String> al = new  ArrayList<String>();
  8. String c;
  9. System.out.println("How Many City..?");
  10. int n = sc.nextInt();
  11. for(int i=0; i<n; i++)
  12. {
  13. System.out.println("Enter City Name");
  14. c = sc.next();
  15. al.add(c);
  16. }
  17. System.out.println("Citys are"+al);
  18. al.clear();
  19. if(al.isEmpty() == true)
  20. System.out.println("Arraylist is empty");
  21. else
  22. System.out.println("Arraylist is not empty");
  23. }
  24. }

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

CODE

šŸ‘‡

  1. import java.util.*;
  2. public class friends
  3. {
  4. public static void main(String args[])
  5. {
  6. Scanner sc = new Scanner(System.in);
  7. LinkedList<String> ll = new  LinkedList<String>();
  8. String c;
  9. System.out.println("How Many Friends.?");
  10. int n = sc.nextInt();
  11. for(int i=0; i<n; i++)
  12. {
  13. System.out.println("Enter Friends Name");
  14. c = sc.next();
  15. ll.add(c);
  16. }
  17. System.out.println("Friends are"+ll);
  18. }
  19. }

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

CODE
šŸ‘‡
  1. import java.util.*;
  2. public class colors
  3. {
  4. public static void main(String args [])
  5. {
  6. Scanner sc = new Scanner(System.in);
  7. TreeSet<String> ts = new TreeSet<String>();
  8. System.out.println("How many Colors..?");
  9. int n = sc.nextInt();
  10. for(int i=1; i <=n; i++)
  11. {
  12. System.out.println("Enter Color "+i);
  13. String c = sc.next();
  14. ts.add(c);
  15. }
  16. //System.out.println("Colors are"+ts);
  17. Iterator itr = ts.iterator();
  18. while(itr.hasNext())
  19. {
  20. System.out.println("Colors Are : "+itr.next());
  21. }
  22. }
  23. }

d) Create the hash table that will maintain the mobile number and student name. Display the contact list.
CODE
šŸ‘‡
  1. import java.io.*;
  2. import java.util.*;
  3. class hash_table
  4. {
  5. public static void main (String args[]) throws Exception
  6. {
  7. Hashtable<String,Integer> ht = new Hashtable<String,Integer>();
  8. Scanner sc = new Scanner(System.in);
  9. int mno;
  10. String name = null;
  11. System.out.println("\nEnter no of Students");
  12. int n = sc.nextInt();
  13. for(int i=0; i<n; i++)
  14. {
  15. System.out.println("Enter Student Name:");
  16. name = sc.next();
  17. System.out.println("Enter Student Mobile No:");
  18. mno = sc.nextInt();
  19. ht.put(name,mno);
  20. }
  21. System.out.println("Hash Table"+ht);
  22. Enumeration k = ht.keys();
  23. Enumeration v = ht.elements();
  24. System.out.println("Name\tMobile Numbers");
  25. while(k.hasMoreElements())
  26. {
  27. System.out.println(k.nextElement()+"\t"+k.nextElement());
  28. }
  29. }
  30. }

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
šŸ‘‡
  1. import java.util.*;
  2. public class treeset
  3. {
  4. public static void main(String args [])
  5. {
  6. Scanner sc = new Scanner(System.in);
  7. TreeSet<Integer> ts = new TreeSet<Integer>();
  8. System.out.println("How many Numbers");
  9. int n = sc.nextInt();
  10. for(int i=1; i <=n; i++)
  11. {
  12. System.out.println("Enter Number "+i);
  13. int num = sc.nextInt();
  14. ts.add(num);
  15. }
  16. System.out.println("The Sorted Numbers are"+ts);
  17. }
  18. }
b) Write a program to sort HashMap by keys and display the details before sorting and after sorting.

CODE
šŸ‘‡

  1. import  java.util.*;
  2. public class SortHashMap
  3. {
  4. public static void main(String args [])
  5. {
  6. Scanner sc = new Scanner(System.in);
  7. HashMap<Integer, String> hm = new HashMap<Integer, String>();
  8. System.out.println("How many Students");
  9. int n = sc.nextInt();
  10. for(int i=1; i <=n; i++)
  11. {
  12. System.out.println("Enter Roll No "+i+" Student");
  13. int num = sc.nextInt();
  14. System.out.println("Enter Name "+i+" Stdudent");
  15. String name = sc.next();
  16. hm.put(num,name);
  17. }
  18. Iterator <Integer> it = hm.keySet().iterator();
  19. System.out.println("Before Sorting");
  20. while(it.hasNext())
  21. {
  22. int key = (int)it.next();
  23. System.out.println("Rol No: "+key+"Name "+hm.get(key));
  24. }
  25. System.out.println("\nAfter Sorting");
  26. TreeMap <Integer,String> tm = new TreeMap <Integer,String> (hm);
  27. Iterator <Integer> itr = tm.keySet().iterator();
  28. while(itr.hasNext())
  29. {
  30. int key = (int)itr.next();
  31. System.out.println("Roll No: "+key+" Name "+hm.get(key));
  32. }
  33. }
  34. }

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
šŸ‘‡
  1. import java.io.*;
  2. import java.util.*;
  3. public class Phonebook
  4. {
  5. public static void main(String[] args)
  6. {
  7. try
  8. {
  9. FileInputStream fis = new FileInputStream("myfile.txt");
  10. Scanner sc = new Scanner(fis).useDelimiter("\t");
  11. Hashtable<String, String> ht = new Hashtable<String, String> ();
  12. String[] strarray;
  13. String a, str;
  14. while(sc.hasNext())
  15. {
  16. a = sc.nextLine();
  17. strarray = a.split("\t");
  18. ht.put(strarray[0],strarray[1]);
  19. System.out.println("Hash Table Values "+strarray[0]+":"+strarray[1]);
  20. }
  21. Scanner s = new Scanner(System.in);
  22. System.out.println("Enter the name as given in the phone book");
  23. str = s.nextLine();
  24. if(ht.containsKey(str))
  25. {
  26. System.out.println("Phone no is "+ht.get(str));
  27. }
  28. else
  29. {
  30. System.out.println("Name is not matched");
  31. }
  32. }
  33. catch(Exception e)
  34. {
  35. System.out.println(e);
  36. }
  37. }
  38. }