Answer to Question #317297 in Java | JSP | JSF for inder

Question #317297

Implement the java code with data structure supporting listed functionalities using object-oriented principles

.Hash Table

  • Insert
  • delete
  • contains
  • get value by key
  • size
  • iterator
  • traverse
1
Expert's answer
2022-03-25T16:23:19-0400
//---------- Functions.java ---------- abstract class

public abstract class Functions {
    public abstract void Insert(int key, String name);
    public abstract void Delete(int key);
}

//---------- Main.java ----------

import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

class People extends Functions{
    Map<Integer, String> person = new HashMap<Integer, String>();

    @Override
    public void Insert(int key, String name) {
        person.put(key, name);
    }

    @Override
    public void Delete(int key) {
        person.remove(key);
    }

    private void Contains(){
        System.out.println(person.values());
    }
    private void GetValueByKey(int key){
        System.out.println(person.get(key));
    }
    private void Size(){
        System.out.println(person.size());
    }
    public void ShowAll(Scanner in){
        Contains();
        System.out.print("Enter search key: ");
        GetValueByKey(in.nextInt());
        Size();
    }
}

public class Main {
    public static void main(String[] args) {

        Scanner in = new Scanner(System.in);
        People people = new People();

        System.out.print("Enter the number of elements: ");
        int count = in.nextInt();

        for(int i = 0; i < count; i++){
            System.out.print("Enter " + (i+1) + " key: ");
            int key = in.nextInt();
            System.out.print("Enter " + (i+1) + " name: ");
            people.Insert(key,in.next());
        }

        System.out.print("Enter the key to delete: ");
        int keyDel = in.nextInt();
        people.Delete(keyDel);

        people.ShowAll(in);

    }
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS