Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Hey I need help completing a program. The program creates a registry of names an

ID: 3825603 • Letter: H

Question

Hey I need help completing a program. The program creates a registry of names and ID's. It prompts the user to enter however many names and ids as they desire. I need help making it so that when I print out the registry of names that its sorted in alphabetical order. There also needs to be a feature that allows the user to search for a name in the registry using the ID.

Here's the incomplete code:

public interface List<K,V> {

   //abstract methods

public abstract boolean add(K key,V value);

public abstract V remove(K key);

public abstract V remove(int n);

public abstract V remove();

public abstract V lookup(K key);

public abstract int size();

public abstract V get(int n);

public abstract Object[] toArray();

public abstract String toString();

}

public class ListClass<K,V> {

//instance variables

   private K key;

private V value;

//constructor

public ListClass(K key, V value){

this.key = key;

this.value = value;

}

//getKey method that returns the key

public K getKey(){

return key;

}

//getValue method that returns the value

public V getValue(){

return value;

}

//setValue method sets the value

public void setValue(V value){

this.value = value;

}

}

import java.util.*;

public class ListDemo {

   //main method

   public static void main(String args[]){

   //Create an object to the MyList class

   MyList<String, Integer> list = new MyList<String, Integer>();

  

boolean quit = true;

   while(quit) {

   System.out.print("Add Names and ID's to list. Name:");

   Scanner kb = new Scanner(System.in);

   String k = kb.next();

   System.out.print(" ID:");

   int v = kb.nextInt();

   list.addElements(k , v);

   System.out.println("1.Add another Name. 2.Print Names. 3.Search using ID 4.Quit ");

   int choice = kb.nextInt();

   switch(choice){

   case 1: break;

   case 2:

         

       System.out.println(list.toString());//this needs to be alphabetized

   break;

   case 3: //search feature

       break;

   case 4: quit = false;

   break;

  

   }

     

   }

}

}

Explanation / Answer

Hi,

Please see below the classes. Plese comment for any queries/feedbacks.

Thanks

List.java

public interface List<K,V> {
//abstract methods

public abstract boolean add(K key,V value);

public abstract V remove(K key);

public abstract V remove(int n);

public abstract V remove();

public abstract V lookup(K key);

public abstract int size();

public abstract V get(int n);

public abstract Object[] toArray();

public abstract String toString();

}

ListClass.java

public class ListClass<K,V> {

//instance variables

private K key;

private V value;
//constructor

public ListClass() {
       super();
       this.key = key;
       this.value = value;
   }


//getKey method that returns the key

public K getKey(){

return key;
}
//getValue method that returns the value

public V getValue(){

return value;
}
//setValue method sets the value

public void setValue(V value){

this.value = value;
}
//setKeye method sets the key

public void setKey(K   key){

this.key = key;
}



}

MyList.java

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;



public class MyList<K,V> implements List<K,V>{
  
   private ArrayList<ListClass> myListObj ;
  

   public MyList() {
       super();
       this.myListObj = new ArrayList<ListClass>();
   }
  
   public void addElements(String k, int v){
       add((K)k, (V)Integer.valueOf(v));
   }

   @Override
   public boolean add(K key, V value) {
       ListClass node = new ListClass();
       node.setKey(key);
       node.setValue(value);
      
       myListObj.add(node);
       return true;
   }

   @Override
   public V remove(K key) {
       // TODO Auto-generated method stub
       return null;
   }

   @Override
   public V remove(int n) {
       // TODO Auto-generated method stub
       return null;
   }

   @Override
   public V remove() {
       // TODO Auto-generated method stub
       return null;
   }

   @Override
   public V lookup(K key) {
       // TODO Auto-generated method stub
       return null;
   }

   @Override
   public int size() {
       // TODO Auto-generated method stub
       return 0;
   }

   @Override
   public V get(int n) {
       // TODO Auto-generated method stub
       return null;
   }

   @Override
   public Object[] toArray() {
       // TODO Auto-generated method stub
       return null;
   }

   public ArrayList getMyListObj() {
       return myListObj;
   }

   public void setMyListObj(ArrayList myListObj) {
       this.myListObj = myListObj;
   }
  
   public void searchElement(int id){
      
       for(int i=0;i<myListObj.size();i++){
           ListClass<K, V> currObj=myListObj.get(i);
           int currId =(Integer) currObj.getValue();
           if(currId ==id){
               System.out.println(" Found ! Name : "+currObj.getKey()+" ID : "+currObj.getValue());
           }
       }
   }

   @Override
   public String toString() {
       String retString = "";
       Collections.sort(myListObj, new Comparator<ListClass>() {

           @Override
           public int compare(ListClass o1, ListClass o2) {
               // TODO Auto-generated method stub
               String name = (String) o1.getKey();
               String name2 = (String) o2.getKey();
               return name.compareTo(name2);
           }
       });
      
      
       for(int i=0;i<myListObj.size();i++){
           retString = retString +" Name : "+myListObj.get(i).getKey()+" ID : "+myListObj.get(i).getValue();
       }
       return retString;
   }
  
  
}

ListDemo.java

import java.util.*;
public class ListDemo {
   //main method
   public static void main(String args[]){
       //Create an object to the MyList class

       MyList<String, Integer> list = new MyList<String, Integer>();

       boolean quit = true;
       System.out.print("Add Names and ID's to list. Name:");
       Scanner kb = new Scanner(System.in);
       String k = kb.nextLine();
       System.out.print(" ID:");
       int v = Integer.valueOf(kb.nextLine());
       list.addElements(k , v);
      
       while(quit) {
           System.out.println("1.Add another Name. 2.Print Names. 3.Search using ID 4.Quit ");
           String choiceStr = kb.nextLine();
           int choice = Integer.valueOf(choiceStr);
           switch(choice){
           case 1:
               System.out.println("Enter name :");
               String name = kb.nextLine();
               System.out.println("Enter ID :");
               int id = Integer.valueOf(kb.nextLine());
               list.addElements(name , id);
               break;
           case 2:

               System.out.println(list.toString());//this needs to be alphabetized
               break;
           case 3:   
               System.out.println("Enter ID :");
               int Id = Integer.valueOf(kb.nextLine());
               list.searchElement(Id); //search feature
               break;
           case 4:
               quit = false;
           break;

           }

       }
   }
}

Sample output:

Add Names and ID's to list.
Name:Allen
ID:101
1.Add another Name.
2.Print Names.
3.Search using ID
4.Quit
1
Enter name :
Kevin
Enter ID :
102
1.Add another Name.
2.Print Names.
3.Search using ID
4.Quit
2

Name : Allen ID : 101
Name : Kevin ID : 102
1.Add another Name.
2.Print Names.
3.Search using ID
4.Quit
3
Enter ID :
102
Found !
Name : Kevin ID : 102
1.Add another Name.
2.Print Names.
3.Search using ID
4.Quit
4