How to write test code for this java class import java.util.Scanner; class HashT
ID: 3844368 • Letter: H
Question
How to write test code for this java class
import java.util.Scanner;
class HashTable{
LList keyList[]; //keylist
public HashTable(){
keyList=new LList[15];//15slot to keylist
for(int i=0;i<15;i++){
keyList[i]=new LList();
}
String key;
int option;
Scanner sc=new Scanner(System.in);
while(true){
menu();
option=sc.nextInt();
if(option==5){
break;
}
switch(option){
case 1:
System.out.println("Enter key to insert");
key=sc.next();
insertInfo(key);
break;
case 2:
System.out.println("Enter key to delete");
key=sc.next();
deleteInfo(key);
break;
case 3:
System.out.println("Enter key to search");
key=sc.next();
LList l=findInfo(key);
if(l==null){System.out.println("key no found");}
else{
System.out.println("Key found");
System.out.println("Reference::"+l.travers());
}
break;
case 4:
printTable();
break;
default:
System.out.println("Invalid Option");
break;
}
}
}
public void menu(){
System.out.printf(" Enter 1->insert 2->delete 3->search 4->display 5->exit Enter your option::");
}
//function to calculate hashvalue of a key
public int hashToKey(String key){
return ((int)key.charAt(0)+(int)key.charAt(1))%15;
}
//insertinfo() to insert a string in hashtable
public void insertInfo(String infoToStore){
int val=hashToKey(infoToStore); //calulating hashvalue
keyList[val].insert(infoToStore); //inserting info in corresponding list
}
//findInfo
public LList findInfo(String infoToFind){
int val=hashToKey(infoToFind); //calulating hashvalue
return keyList[val].findInfo(infoToFind);//returning reference to LList object
}
//deleteInfo
public void deleteInfo(String infoToRemove){
if(findInfo(infoToRemove)!=null){
int val=hashToKey(infoToRemove); //calulating hashvalue
keyList[val].deleteInfo(infoToRemove);//removing info
System.out.println(infoToRemove+" removed at location "+val);
}
else{
System.out.println("Key not found in hashTable");
}
}
//printing hashtable
public void printTable(){
System.out.println("Location|Value");
for(int i=0;i<15;i++){
System.out.println(""+i+" | "+keyList[i].travers());
System.out.println("--------------------------------------------");
}
}
public static void main(String args[]){
new HashTable();
};
}
class LList {
String info;
LList nextList;
boolean isEmpty() {
return(this.nextList == null && this.info == null);
}
void insert(String inInfo) {
if (this.isEmpty()) {
this.info = inInfo;
this.nextList = new LList();
return;
}
this.nextList.insert(this.info);
this.info = inInfo;
}
LList nextList() {
return(this.nextList);
}
int lSize() {
if (this.isEmpty()) {
return(0);
}
return (this.nextList.lSize() + 1); // add one for me
}
void deleteInfo(String inInfo) {
if (this.isEmpty()) {
return;
}
if (!this.info.equals(inInfo)) {
this.nextList.deleteInfo(inInfo);
} else { // I have the info
this.info = this.nextList.info;
this.nextList.deleteInfo(this.info); // may be null
if (this.info == null) {
this.nextList = null; // I am empty now too.
}
}
return;
}
String travers() {
if (this.isEmpty()) {
return("");
}
return (this.info + ", " + this.nextList.travers()); // don't care about the comma
}
LList findInfo(String inInfo) {
if (this.isEmpty()) {return (null);}
if (this.info.equals(inInfo)) { return (this); }
return (this.nextList.findInfo(inInfo));
}
LList cloneLList() {
if (this.isEmpty()) { return (new LList()); }
LList newList = new LList();
newList.nextList = this.nextList.cloneLList();
newList.info = this.info;
return (newList);
}
}
Explanation / Answer
Test that your method handles Null inputs gracefully, if not your hashtable must be able to raise exception accordingly.Here exception handling and try and catch would be useful.
You can use assertEquals() inbuilt method of java to check output of a certain method, if output not as excepted this will throw error accordingly.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.