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

In Java-- Please ensure all classes are complete and instructions are followed c

ID: 3604886 • Letter: I

Question

In Java--

Please ensure all classes are complete and instructions are followed completly.
Please ensure that all code is commented extensively

Using the map ADT

A map is a data structure that provides a collection containing unique keys associated with a single value.

Operations:

Create empty map

Find key in map

Find value of a given key, if key exists

Insert a key with its associated value, if key does not already exist

Remove a key and its associated value

Implement map ADT in file called mapYourLastName.java, to map email addresses (key) to people's names (value). Both can be Strings.

Your driver class should add at least 3 people, search for 2 people by key (email addresses) and remove all keys when done.

Note from discussion board:

Just like Queue, you made a constructor and methods, here you define a class Map, decide how you are storing key, value pairs (in trees and lists we used nodes, queues we used an array)

Then have a Map() constructor, a method returning a boolean called findKey, a method returning the value of a given key getValue(key) if the key exists, ...

Then you will make a MapTester file, you will write your own here.

Explanation / Answer


Given below is the code for the question.
To indent code in eclipse, select code and press Ctrl+a and then Ctrl+i.
Please don't forget to rate the answer if it helped. Post a comment in case of any issues.


public class Map<K, V> {
class Pair
{
K key;
V value;
Pair next;
Pair(K key, V value, Pair next)
{
this.key = key;
this.value = value;
this.next = next;
}
}
private Pair head;
public Map()
{
head = null;
}
public V getValue(K key)
{
Pair p = head;
while(p != null)
{
if(p.key.equals(key))
return p.value;
p = p.next;
}
return null;
}
public boolean containsKey(K key)
{
Pair p = head;
while(p != null)
{
if(p.key.equals(key))
return true;
p = p.next;
}
return false;
}
public boolean remove(K key)
{
Pair curr = head;
Pair prev = null;
while(curr != null)
{
if(curr.key.equals(key))
break;
prev = curr;
curr = curr.next;
}
if(curr == null) //key not found
return false;
else
{
if(curr == head) //deleing 1st entry
head = head.next;
else
prev.next = curr.next;
return true;
}
}
public boolean insert(K key, V value)
{
if(!containsKey(key)) //if not present
{
Pair p = new Pair(key, value, head);
head = p;
return true;
}
else
return false;
}
}

===============

public class Person {
private String name;
private String email;
private String address;
private String phone;
public Person(String name, String email, String address, String phone)
{
this.name = name;
this.email = email;
this.address = address;
this.phone = phone;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String toString()
{
return "Name: " + name + ", Email: " + email + ", Phone: " + phone + ", Address: " + address;
}
}


=============

public class MapTest {
public static void main(String[] args) {
Person p1 = new Person("John Smith", "john.s@gmai.com", "12, stree3", "111-111-111");
Person p2 = new Person("Michael Jackson", "michael.jackson@gmai.com", "32, stree456", "222-222-222");
Person p3 = new Person("Robert Williams", "robert.w@gmai.com", "666, streetXYZ", "333-333-333");
Map<String, Person> map = new Map<String, Person>();
map.insert(p1.getEmail(), p1);
map.insert(p2.getEmail(), p2);
map.insert(p3.getEmail(), p3);
Person p = map.getValue("michael.jackson@gmai.com" );
System.out.println("Fetching value for key michael.jackson@gmai.com");
System.out.println("Got value " + p );
p = map.getValue("Bob@gmai.com" );
System.out.println("Fetching value for key Bob@gmai.com");
System.out.println("Got value " + p );
System.out.println("Removing key michael.jackson@gmai.com");
map.remove("michael.jackson@gmai.com");
if(map.containsKey("michael.jackson@gmai.com"))
System.out.println("key michael.jackson@gmai.com FOUND");
else
System.out.println("key michael.jackson@gmai.com NOT FOUND");
}
}

OUTPUT

Fetching value for key michael.jackson@gmai.com
Got value Name: Michael Jackson, Email: michael.jackson@gmai.com, Phone: 222-222-222, Address: 32, stree456
Fetching value for key Bob@gmai.com
Got value null
Removing key michael.jackson@gmai.com
key michael.jackson@gmai.com NOT FOUND

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote