java problem! Show code and step! A dictionary or an associative array is a comm
ID: 3781911 • Letter: J
Question
java problem!
Show code and step!
A dictionary or an associative array is a commonly used data structure that is composed of a collection of (key, value) pairs. Each key is unique and may appear only once within the dictionary. This assignment requires you to create your own abstract data type for a dictionary. The ADT should contain the following methods: Insert Insert a new (key, value) pair. GetValue Returns the value of the given key. GetKey Returns all keys of the given value. Remove Remove the (key, value) pair of the given key. Compare Returns true if values of two keys are the same. Contains Checks the existence of the given key. Count Returns the total number of (key, value) pairs currently in the dictionary. IsEmpty Returns whether the Dictionary is empty or not. printKeys Prints out all keys in the following format: {key1, key2, ..., keyN}_ The ADT should be called "dictionary" and contain two Arrays. The first array is used to store keys and should be an array of Integers. The second array is used to contain the value and should be an array of Strings. The elements of the same index in the two arrays make a (key, value) pair.Explanation / Answer
Assumption:
As we already know the size of the dictionary (from user input), the key and value arrays are initialized to 0 and " " respectively (in the constructor). That means for a blank entry key[i] will be 0 and value[i] will be " ". This added a limitation to the dictionary that no entry with key=0 can be present. To remove this limitation, we can initialize the key[] array with some value that can never be used as a key (i.e. -1). For this particular case, we are assuming that 0 can never be a key.
Dictionary implementation explanation:
Creating a Class-Dictionary will the following variable:
a. int [] key: An array of integers that stores unique key
b. String [] value: An array of Strings that stores the string value
c. int count: It counts the current number of entries in the Dictionary
Dictionary(int n): This is a parameterized constructor and is called from the main() to create Dictionary object. Parameter "n" is the size of the dictionary input by the user. Dictionary constructor initializes the variables of class Dictionary and also allocate the memory where required (i.e. key and value are allocated size equal to the size of the dictionary, that's "n").
void Insert(int k, String v): This method takes two parameters "k" (key) and "v" (value), where "v" is the value for this key "k". It first checks if the key is already present in the dictionary using Contains(k) call. If the key is present then this new key-value pair can not be added to the dictionary. But, if the key is not present then a new key-value pair is added to the empty location and "count" is incremented to 1(ONE).
String GetValue(int k): This method returns the value corresponding to the key "k". First, we perform a linear search in the key[] array and if "k" is present we get the index (i.e. i) of that entry. The same index (i.e. i) will give us the value corresponding to key "k".
int [] GetKey(String str): This method returns an array of keys corresponding to given value "str". To get all the keys we first count the number of keys that matches the value "str". We again run the same loop to insert the matched key to the new array of size equal to the count of the number of keys calculated in the previous loop.
void Remove(int k): This method removes the key "k" (if present) from the array key[] and corresponding value from array value[]. After the key-value pair is removed successfully, the count is also decremented to update the current number of entries in the dictionary.
boolean Compare(int k1, int k2): This method compares the values corresponding to two keys "k1" and "k2". First, we check if "k1" and "k2" are present in the array key[]. If they are present, we check the corresponding values in array value[]. If two values are equal then this method will return "true" else it will return "false".
boolean Contains(int k): This method checks if the key "k" is present in the array key[] and return "true" if "k" is present, otherwise "false".
int Count(): This will give us the number of current entries in the dictionary. This method simply returns the count variable which is already updated after every Insert() and Remove() call.
boolean IsEmpty(): This return "false" if the dictionary is not empty (count>0), otherwise "true".
void printKeys(): This method prints all the keys present in the array key[] in the format mentioned in the question.
--------------------------------------------------------------------------------------------------------------------------------------
HERE IS THE COMPLETE CODE:
import java.util.*;
import java.io.*;
import java.lang.*;
public class Dictionary{
int [] key;
String [] value;
int count; // count the number of entries in the dictionary
// Constructor
Dictionary(int n){
key = new int[n];
value = new String[n];
for(int i=0; i<n; i++)
value[i]=" ";
count=0;
}
void Insert(int k, String v){
// Check if Key is already present.
boolean ret = Contains(k);
if(ret==true){
System.out.println(" Failed! Key "+k+" is already present!");
return;
}
// Insert new key-value pair
int i=0;
for(int kk: key){
if(kk==0){
key[i]=k;
value[i]=v;
count++;
System.out.println(" Key: "+k+", value: "+v+" inserted.");
return;
}
i++;
}
if(i>=key.length){
System.out.println(" Overflow. Dictionary is full!");
}
}
String GetValue(int k){
String str=" ";
for(int i=0; i<key.length; i++){
if(key[i]==k){
str=value[i];
break;
}
}
return str;
}
int [] GetKey(String str){
int cnt=0;
for(int i=0; i<value.length;i++){
if(value[i].equals(str)){
cnt++;
}
}
int [] v=new int[cnt];
int j=0;
for(int i=0; i<value.length;i++){
if(value[i].equals(str)){
v[j]=key[i];
j++;
}
}
return v;
}
void Remove(int k){
boolean ret=false;
// check if key is present. If yes, mark key=0, and value=" "
for(int i=0; i<key.length; i++){
if(key[i]==k){
key[i]=0;
value[i]=" ";
ret=true;
System.out.println(" Key "+k+" and corresponding value is removed.");
count--;
break;
}
}
if(ret==false){
System.out.println(" Failed! Key "+k+" is not present!");
}
}
boolean Compare(int k1, int k2){
boolean ret=false;
boolean foundKey1=false;
boolean foundKey2=false;
int index1=-1;
int index2=-1;
for(int i=0; i<key.length; i++){
if(key[i]==k1){
foundKey1=true;
index1=i;
}
if(key[i]==k2){
foundKey2=true;
index2=i;
}
}
if(foundKey1==true && foundKey2==true){
if(value[index1].equals(value[index2])){ // check if values are equal
ret=true;
}
}else{
if(foundKey1==true && foundKey2==false)
System.out.println(" Key: "+k2+" is NOT present in the dictionary.");
if(foundKey1==false && foundKey2==false)
System.out.println(" Key: "+k1+" and "+k2+" are NOT present in the dictionary.");
if(foundKey1==false && foundKey2==true)
System.out.println(" Key: "+k1+" is NOT present in the dictionary.");
ret=false;
}
return ret;
}
boolean Contains(int k){
boolean ret=false;
// traverse the key array.
// Return true if key already present, else return false
for(int i=0; i<key.length; i++){
if(key[i]==k){
ret=true;
break;
}
}
return ret;
}
int Count(){
return count;
}
boolean IsEmpty(){
boolean ret=true;
if(count>0)
ret=false;
return ret;
}
void printKeys(){
System.out.println(" Printing all keys:");
String separator="";
System.out.print("{");
for (int kk : key) {
if(kk!=0){
System.out.print(separator + kk);
separator = ",";
}
}
System.out.println("}");
}
public static void main(String args[]){
System.out.println("DICTIONARY - JAVA");
System.out.println("--------------------------");
Scanner input = new Scanner(System.in);
System.out.println("Enter the size of dictionary:");
int dSize = input.nextInt();
Dictionary dictionary = new Dictionary(dSize);
int option=0;
do{
System.out.println("--------------------------");
System.out.println(" Press any key to continue...");
try{
System.in.read();
}catch(Exception e){
}
System.out.println("Enter 1 to Insert a Key-Value pair.");
System.out.println("Enter 2 to Get a Value corresponding to a Key.");
System.out.println("Enter 3 to Get all Keys corresponding to a Value.");
System.out.println("Enter 4 to Remove a Key-Value pair.");
System.out.println("Enter 5 to Compare values corresponding to two Keys.");
System.out.println("Enter 6 to check if dictionary Contains a particular Key.");
System.out.println("Enter 7 to Count the current number of entries in the dictionary.");
System.out.println("Enter 8 to check if the dictionary is Empty.");
System.out.println("Enter 9 to print all keys in the dictionary.");
System.out.println("Enter 0 to EXIT.");
System.out.println("");
option = input.nextInt();
input.nextLine();
if(option==0)
break; // EXIT from the loop
int k=0;
String v=" ";
boolean ret=false;
switch(option){
case 1:
System.out.println("--------------------------");
System.out.println("---Insert Operation.---");
System.out.print("Enter Key: ");
k = input.nextInt();
v = input.nextLine();
System.out.print("Enter Value: ");
v = input.nextLine();
System.out.println(" OUTPUT:");
dictionary.Insert(k,v);
break;
case 2:
System.out.println("--------------------------");
System.out.println("---GetValue Operation.---");
System.out.print("Enter Key: ");
k = input.nextInt();
input.nextLine();
System.out.println(" OUTPUT:");
String str = dictionary.GetValue(k);
if(str==" "){
System.out.println("Key: "+k+" is not present.");
}else{
System.out.println(" Value for key: "+k+" is: "+str);
}
break;
case 3:
System.out.println("--------------------------");
System.out.println("---GetKey Operation.---");
System.out.print("Enter Value: ");
v = input.nextLine();
System.out.println(" OUTPUT:");
int kSameVal[] = dictionary.GetKey(v);
if(kSameVal.length>0){
System.out.print(" Key(s) corresponding to value: "+v+ " is/are: ");
for(int i=0; i<kSameVal.length;i++)
System.out.print(kSameVal[i]+" ");
}else{
System.out.print(" There is no Key(s) corresponding to value: "+v+".");
}
System.out.println("");
break;
case 4:
System.out.println("--------------------------");
System.out.println("---Remove Operation.---");
System.out.print("Enter Key: ");
k = input.nextInt();
input.nextLine();
System.out.println(" OUTPUT:");
dictionary.Remove(k);
break;
case 5:
System.out.println("--------------------------");
System.out.println("---Compare Operation.---");
System.out.print("Enter First Key: ");
int k1 = input.nextInt();
input.nextLine();
System.out.print("Enter Second Key: ");
int k2 = input.nextInt();
input.nextLine();
System.out.println(" OUTPUT:");
boolean valEqual = dictionary.Compare(k1,k2);
if(valEqual==true){
System.out.println(" Values of key: "+k1+" and key: "+k2+" are EQUAL.");
}
break;
case 6:
System.out.println("--------------------------");
System.out.println("---Contains Operation.---");
System.out.print("Enter Key: ");
k = input.nextInt();
input.nextLine();
System.out.println(" OUTPUT:");
ret = dictionary.Contains(k);
if(ret==true){
System.out.println(" Yes. Dictionary contains the Key: "+k);
}else{
System.out.println(" No. Dictionary does not contain the Key: "+k);
}
break;
case 7:
System.out.println("--------------------------");
System.out.println("---Count Operation.---");
System.out.println(" OUTPUT:");
int c=dictionary.Count();
System.out.println(" Number of entries in the dictionary are: "+c);
break;
case 8:
System.out.println("--------------------------");
System.out.println("---IsEmpty Operation.---");
System.out.println(" OUTPUT:");
ret = dictionary.IsEmpty();
if(ret==true){
System.out.println(" Yes. Dictionary is Empty.");
}else{
System.out.println(" No. Dictionary is NOT Empty.");
}
break;
case 9:
System.out.println("--------------------------");
System.out.println("---printKey Operation.---");
System.out.println(" OUTPUT:");
dictionary.printKeys();
break;
default:
System.out.println("--------------------------");
System.out.println("Invalid choice!");
}
}while(option!=0);
System.out.println("CLOSING DICTIONARY");
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.