Java Program to Implement Direct Addressing Tables

«
»
This is a Java Program to implement Direct Addressing Tables. Direct Addressing Tables are used when each element has a key drawn from a universe U = {0, 1, . . . ,m − 1} where m isn’t too large and each key is unique. Here Direct Addressing Tables is implemented using an array of strings where array index is the key.

Here is the source code of the Java program to implement Direct Addressing Tables. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.

  1. /*
  2.  *    Java Program to Implement Direct Addressing Tables
  3.  */
  4.  
  5. import java.util.Scanner;
  6.  
  7. class DirectAddressingTable
  8. {
  9.     private String[] arr ;
  10.     private final int DEFAULT_CAPACITY = 10001;
  11.  
  12.     /* Constructor */
  13.     public DirectAddressingTable()
  14.     {
  15.         arr = new String[DEFAULT_CAPACITY];
  16.     }
  17.     /* Constructor */
  18.     public DirectAddressingTable(int capacity)
  19.     {
  20.         arr = new String[capacity + 1];
  21.     }
  22.     /* function to insert */
  23.     public void insert(int k, String d)
  24.     {
  25.         arr[k] = d;
  26.     }
  27.     /* function to delete */
  28.     public void delete(int k)
  29.     {
  30.         arr[k] = null;
  31.     }
  32.     /* function to get value */
  33.     public String get(int k)
  34.     {
  35.         return arr[k];
  36.     }
  37.     /* function to clear */
  38.     public void clear()
  39.     {
  40.         int l = arr.length;
  41.         arr = new String[l];
  42.     }
  43.     /* function to print */
  44.     public void printTable()
  45.     {
  46.         System.out.println("\nDirect Addressing Table : ");
  47.         int l = arr.length;
  48.         for (int i = 0; i < l; i++)
  49.             if (arr[i] != null)
  50.                 System.out.println(i +" "+ arr[i]);
  51.         System.out.println();        
  52.     }
  53. }
  54.  
  55. public class DirectAddressingTableTest
  56. {
  57.     public static void main(String[] args)
  58.     {
  59.         Scanner scan = new Scanner(System.in);
  60.         System.out.println("Direct Addressing Table Test\n\n");
  61.  
  62.         /* Make object of DirectAddressingTable */
  63.         DirectAddressingTable dat = new DirectAddressingTable();
  64.  
  65.         char ch;
  66.         /*  Perform DirectAddressingTable operations  */
  67.         do    
  68.         {
  69.             System.out.println("\nDirect Addressing Table Operations\n");
  70.             System.out.println("1. insert ");
  71.             System.out.println("2. remove");
  72.             System.out.println("3. get");            
  73.             System.out.println("4. clear");
  74.  
  75.  
  76.             int choice = scan.nextInt();            
  77.             switch (choice)
  78.             {
  79.             case 1 : 
  80.                 System.out.println("Enter int key and string value");
  81.                 dat.insert( scan.nextInt(), scan.next() ); 
  82.                 break;                          
  83.             case 2 :                 
  84.                 System.out.println("Enter int key");
  85.                 dat.delete( scan.nextInt() ); 
  86.                 break;                        
  87.             case 3 : 
  88.                 System.out.println("Enter int key");
  89.                 System.out.println("Value = "+ dat.get( scan.nextInt() )); 
  90.                 break;                                   
  91.             case 4 : 
  92.                 dat.clear();
  93.                 System.out.println("Direct Addressing Table Cleared\n");
  94.                 break;
  95.             default : 
  96.                 System.out.println("Wrong Entry \n ");
  97.                 break;   
  98.             }
  99.             /* Display hash table */
  100.             dat.printTable();  
  101.  
  102.             System.out.println("\nDo you want to continue (Type y or n) \n");
  103.             ch = scan.next().charAt(0);                        
  104.         } while (ch == 'Y'|| ch == 'y');  
  105.     }
  106. }

advertisement
Direct Addressing Table Test
 
 
 
Direct Addressing Table Operations
 
1. insert
2. remove
3. get
4. clear
1
Enter int key and string value
6 mango
 
Direct Addressing Table :
6 mango
 
 
Do you want to continue (Type y or n)
 
y
 
Direct Addressing Table Operations
 
1. insert
2. remove
3. get
4. clear
1
Enter int key and string value
24 pineapple
 
Direct Addressing Table :
6 mango
24 pineapple
 
 
Do you want to continue (Type y or n)
 
y
 
Direct Addressing Table Operations
 
1. insert
2. remove
3. get
4. clear
1
Enter int key and string value
17 orange
 
Direct Addressing Table :
6 mango
17 orange
24 pineapple
 
 
Do you want to continue (Type y or n)
 
y
 
Direct Addressing Table Operations
 
1. insert
2. remove
3. get
4. clear
1
Enter int key and string value
1 apple
 
Direct Addressing Table :
1 apple
6 mango
17 orange
24 pineapple
 
 
Do you want to continue (Type y or n)
 
y
 
Direct Addressing Table Operations
 
1. insert
2. remove
3. get
4. clear
3
Enter int key
24
Value = pineapple
 
Direct Addressing Table :
1 apple
6 mango
17 orange
24 pineapple
 
 
Do you want to continue (Type y or n)
 
y
 
Direct Addressing Table Operations
 
1. insert
2. remove
3. get
4. clear
2
Enter int key
17
 
Direct Addressing Table :
1 apple
6 mango
24 pineapple
 
 
Do you want to continue (Type y or n)
 
y
 
Direct Addressing Table Operations
 
1. insert
2. remove
3. get
4. clear
4
Direct Addressing Table Cleared
 
 
Direct Addressing Table :
 
 
Do you want to continue (Type y or n)
 
n

Sanfoundry Global Education & Learning Series – 1000 Java Programs.

Note: Join free Sanfoundry classes at Telegram or Youtube
advertisement
advertisement
If you wish to look at all Java Programming examples, go to Java Programs.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & technical discussions at Telegram SanfoundryClasses.