import javax.swing.;
import java.util.;
public class DafoFinals{
public static void main(String args[]){
String display;
String object;
int number; // variable for the number of array //
int t_num; // variable for table number //
int obj_num; // variable for number of of object input as String //
int value; // variable for number objects to be converted to its hash value //
int t_content;
JTextArea displayArea = new JTextArea();
display = "OBJECTS\tVALUE\tRESULT\n\n";
// Dialog box that will prompt the user to enter the number of objects to be hashed //
object=JOptionPane.showInputDialog("Enter number of objects to be hashed."+"\nNOTE: Maximum of 15 objects only.");
number = Integer.parseInt(object);
String input[] = new String[number];
String table[] = new String[15];
for(t_num = 0; t_num < 15; t_num++){
table[t_num] = "";
}
for(obj_num = 0; obj_num < number; obj_num++){
input[obj_num] = JOptionPane.showInputDialog("Object no."+ "" +(obj_num+1));
String askValue = intStr(input[obj_num].toUpperCase());
value = intValue(askValue);
while(value > 15){
String converted = "" + value;
value = intValue(converted);
}
int iVal = value - 1;
if(table[iVal] == "")
table[iVal] = input[obj_num];
else{
for(t_content = iVal; t_content < 15; t_content++){
if(table[t_content] == ""){
table[t_content] = input[obj_num];
break;
}
if(t_content == 14){
t_content = -1;
}
}
}
display += input[obj_num] + "\t" + askValue +"\t" + value + "\n";
}
display += "\n\n\n____________________________________________"+"\n\n\t****HASH TABLE****" + "\n____________________________________________\n\n";
for(int y = 0; y < 15; y++){
display += (y+1) + "\t" + table[y] + "\n";
}
displayArea.setText(display);
JOptionPane.showMessageDialog(null, displayArea, "ITDAFO (Finals) :: Single Hashing", JOptionPane.INFORMATION_MESSAGE);
System.exit(0);
}
static String intStr(String s){
String toValue = "";
int val=0;
for (; val < s.length(); val++){
if (s.charAt(val) == 'A')
toValue += '1';
else if (s.charAt(val) == 'B')
toValue += '2';
else if (s.charAt(val) == 'C')
toValue += '3';
else if (s.charAt(val) == 'D')
toValue += '4';
else if (s.charAt(val) == 'E')
toValue += '5';
else if (s.charAt(val) == 'F')
toValue += '6';
else if (s.charAt(val) == 'G')
toValue += '7';
else if (s.charAt(val) == 'H')
toValue += '8';
else if (s.charAt(val) == 'I')
toValue += '9';
else if (s.charAt(val) == 'J')
toValue += "10";
else if (s.charAt(val) == 'K')
toValue += "11";
else if (s.charAt(val) == 'L')
toValue += "12";
else if (s.charAt(val) == 'M')
toValue += "13";
else if (s.charAt(val) == 'N')
toValue += "14";
else if (s.charAt(val) == 'O')
toValue += "15";
else if (s.charAt(val) == 'P')
toValue += "16";
else if (s.charAt(val) == 'Q')
toValue += "17";
else if (s.charAt(val) == 'R')
toValue += "18";
else if (s.charAt(val) == 'S')
toValue += "19";
else if (s.charAt(val) == 'T')
toValue += "20";
else if (s.charAt(val) == 'U')
toValue += "21";
else if (s.charAt(val) == 'V')
toValue += "22";
else if (s.charAt(val) == 'W')
toValue += "23";
else if (s.charAt(val) == 'X')
toValue += "24";
else if (s.charAt(val) == 'Y')
toValue += "25";
else if (s.charAt(val) == 'Z')
toValue += "26";
}
return toValue;
}
static int intValue(String s){
int value = 0;
int i = 0;
for(; i < s.length(); i++){
if(s.charAt(i) == '1')
value += 1;
else if(s.charAt(i) == '2')
value += 2;
else if(s.charAt(i) == '3')
value += 3;
else if(s.charAt(i) == '4')
value += 4;
else if(s.charAt(i) == '5')
value += 5;
else if(s.charAt(i) == '6')
value += 6;
else if(s.charAt(i) == '7')
value += 7;
else if(s.charAt(i) == '8')
value += 8;
else if(s.charAt(i) == '9')
value += 9;
else if(s.charAt(i) == '0')
value += 0;
}
return value;
}
}