Create a class named Person that contains a string data field named ID for the p
ID: 3755974 • Letter: C
Question
Create a class named Person that contains a string data field named ID for the person's ID and a LinkedList (from java.util.LinkedList) data field named phoneNums for all the numbers that the person possesses (the numbers saved in the LinkedList should be String). Provide an abstract data type PBList, which stands for phone book list, that can be used to represent sequences of objects of class Person Note: The IDs are unique for each person but a person can have multiple entries in the phonebook. In other words there can be multiple entries in PBList with the same ID which belong to the same person The abstract data type PBList must support the following operations int size() - Returns the current size of the list (number of entries in the phonebook) void addPerson(int i, Person person) - Adds a new element before the i-th element of the list (the index of the first entry is 0). If i is greater than the number of the elements in the list, adds it to the end boolean addNumber(String ID, String phoneNum) - Adds a new phone number to the first occurrence of ID in the list and returns true. Returns false if ID does not exist in the list. . boolean delete(int i) - Deletes the i-th element of the list and returns true. In the case when the list has less than i elements returns false . Person search(int i) - Returns the i-th element of the list. In the case when the sequence has less than i elements, it returns null e void merge0 Merges the phone numbers of all the entries with the same ID Provide two implementations of this abstract data structure: a class named PBArrayList and another class named PBLinkedList. The first one must use an array to store the sequence of persons and the second a singly linked list. Notes regarding the implementation of these ADT Using built-in Java classes, such as ArrayList and LinkedList, is not allowed (LinkedList is allowed only for the implementation and usage of class Person) . . Do not forget the issue of managing the "real estate" (i.e., the fixed size of the array) in the case of the PBArrayList implementation (e.g. you will need to internally re-allocate the array as it grows too much) Write a demo application utilizing this abstract data structure. Your application should include the following components . Two static methods: int prefixCountArrayList(PBArrayList list, String prefix) and int prefixCountLinkedList(PBLinkedList list, String prefix) which return the total number of entries in the phonebook with the phone number matching the prefix. Note: The running time of this method should be linear in the size of the phone book.Explanation / Answer
1) person.java
package phonebookproject;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
//person class
public class Person {
String ID; // persons id
LinkedList<String> phoneNums; // list of numbers instrng form
//default constructor
public Person(){
this.phoneNums = new LinkedList<>();
}
//constructor with id
public Person(String iD) {
super();
this.phoneNums = new LinkedList<>();
ID = iD;
}
//constructor taking both id and list of numbers
public Person(String iD, LinkedList<String> phoneNums) {
super();
ID = iD;
this.phoneNums = phoneNums;
}
//setters and getters for properties
public String getID() {
return ID;
}
public void setID(String iD) {
ID = iD;
}
public LinkedList<String> getPhoneNums() {
return phoneNums;
}
public void setPhoneNums(LinkedList<String> phoneNums) {
this.phoneNums = phoneNums;
}
//to add new num into list
public void addPhoneNum(String num){
phoneNums.add(num);
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((ID == null) ? 0 : ID.hashCode());
return result;
}
//equal to persons based on id
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Person other = (Person) obj;
if (ID == null) {
if (other.ID != null)
return false;
} else if (!ID.equals(other.ID))
return false;
return true;
}
//returns list of numbrs atarting with key
public List<String> matching(String key){
ArrayList<String> found = new ArrayList<>();
for(String num:phoneNums){
if(num.startsWith(key))
found.add(num);
}
return found;
}
}
2) pblist.java
package phonebookproject;
public abstract class PBList {
//returns size of phonebook
public abstract int size();
// using starting index = 0
//adds new person to the list before the index i of book list
public abstract void addPerson(int i, Person persong);
//adds new phone number to the first occurence of this id
public abstract boolean addNumber(String Id, String phoneNum);
//deletes item at index 0 starting 0
public abstract boolean delete(int i);
//returns person object at index i
public abstract Person search(int i);
//merges all phone numbers into single id
// for duplicate items
public abstract void merge();
}
3) pbarraylist.java
package phonebookproject;
import java.util.Arrays;
import java.util.List;
public class PBArrayList extends PBList {
private int size; //no of elements
private Person[] persons; //array to hold
private static int MIN_CAPACITY = 5; //min capacity of array
private int capacity; //current capacity of array
// default constructor
public PBArrayList() {
super();
size = 0;
capacity = MIN_CAPACITY;
persons = new Person[MIN_CAPACITY]; // intialising with min capacity
}
// for dynamically increases array size
private void increaseCapacity() {
capacity = persons.length + MIN_CAPACITY; // increasing capacity
persons = Arrays.copyOf(persons, capacity); // increasing arraysize
}
// return no of elements
@Override
public int size() {
return size;
}
// ad person at index
@Override
public void addPerson(int i, Person person) {
if (size >= capacity) { // increasing capacity if full
increaseCapacity();
}
if (i >= size) { // if index is greater than current index
persons[size] = person; // adding at last
size++;
return;
}
Person temp = persons[i]; // here we just swapping this index i to last
persons[i] = person;
persons[size] = temp;
size++;
}
//just ading new persong
public void addPerson(Person person) {
if (size >= capacity) { // increasing capacity if full
increaseCapacity();
}
persons[size] = person;
size++;
}
// get index of person of this id
// -1 not found
public int getIndex(String id) {
Person key = new Person(id);
for (int i = 0; i < size; i++) {
if (persons[i].equals(key))
return i;
}
return -1;
}
// adds new num to this id
@Override
public boolean addNumber(String id, String phoneNum) {
int index = getIndex(id); // getting index
if (index != -1) {// if found
Person person = persons[index]; // adding
person.addPhoneNum(phoneNum);
return true;
}
return false;
}
// deleting at index
@Override
public boolean delete(int index) {
if (index >= size) {
return false;
}
persons[index] = persons[size - 1]; // replacing it with last element
// decreasing size
size--;
return true;
}
// returns person object at index
@Override
public Person search(int index) {
if (index >= size) {
return null;
}
return persons[index];
}
// combines adding persons phone numbers to person numbers
public void combine(Person person, Person adding) {
List<String> x = person.getPhoneNums();
List<String> y = person.getPhoneNums();
for (String ynum : y) {
if (!x.contains(ynum)) {
x.add(ynum);
}
}
}
// merging two lists
@Override
public void merge() {
for (int i = 0; i < size; i++) { //loop till end
for (int j = i+1; j < size; j++) { // loop from i to till end
if (persons[i].equals(persons[j])) { //if j equals i
combine(persons[i],persons[j]); //adding j list into i
delete(j); //now deleting object at j
}
}
}
}
//tosting overriding
public String toString(){
String output = "[ ";
for (int i = 0; i < size; i++) { //loop till end{
output = output + persons[i].getID()+", ";
}
output = output + " ]";
return output;
}
//getnum of matching num
public int matching(String key){
int count = 0;
for (int i = 0; i < size; i++) { //loop till end{
count = count + persons[i].matching(key).size();
}
return count;
}
}
4) pblinkedlist.java
package phonebookproject;
import java.util.List;
public class PBLinkedList extends PBList {
private Node<Person> head; // head node
private Node<Person> tail; // tail node
private int size; // number of elements in linked list
// constructor
public PBLinkedList() {
super();
head = null;
tail = null;
size = 0;
}
// returns num of elements
@Override
public int size() {
return size;
}
// adds new person object at index i
@Override
public void addPerson(int i, Person person) {
add(i, person);
}
@Override
public boolean addNumber(String Id, String phoneNum) {
Node<Person> temp = head;
Person element = new Person(Id);
if (isEmpty()) {
return false;
}
if (head.getElement().equals(element)) {
Person found = head.getElement();
found.addPhoneNum(phoneNum);
return true;
}
while (temp != tail) {
if (temp.getElement().equals(element)) {
return true;
}
temp = temp.next;
}
return false;
}
// deletes item at index index
@Override
public boolean delete(int index) {
if (index == 0) { // removing head
head = head.getNext();
size--;
return true;
}
if (index == size - 1) { // removing tail
Node<Person> s = head;
Node<Person> t = head;
while (s != tail) {
t = s;
s = s.getNext();
}
tail = t;
tail.setNext(null);
size--;
return true;
}
Node<Person> ptr = head;
for (int i = 1; i < size - 1; i++) {
if (i == index) {
Node<Person> tmp = ptr.getNext();
tmp = tmp.getNext();
ptr.setNext(tmp);
size--;
return true;
}
ptr = ptr.getNext();
}
return false;
}
// search and returns person at index i
@Override
public Person search(int index) {
Node<Person> temp = head;
if (isEmpty()) {
return null;
}
int count = 0;
while (count < index) {
temp = temp.next;
count++;
}
return temp.getElement();
}
// search and returns the index of this person
public int indexOf(Person element) {
Node<Person> temp = head;
int index = -1;
if (isEmpty()) {
return index;
}
if (head.getElement().equals(element))
index = 0;
int count = 0;
while (temp != tail) {
if (temp.getElement().equals(element))
return count;
temp = temp.next;
count++;
}
return index;
}
// combines adding persons phone numbers to person numbers
public void combine(Person person, Person adding) {
List<String> x = person.getPhoneNums();
List<String> y = person.getPhoneNums();
for (String ynum : y) {
if (!x.contains(ynum)) {
x.add(ynum);
}
}
}
// merges two equal id's into one
@Override
public void merge() {
Node<Person> p = head;
int pindex = 0;
while (p != null) { // till end of loop
Node<Person> k = p.getNext();
int qindex = pindex + 1;
while (k != null) { // till end of loop starting from p
if (p.equals(k)) { // if both id's equal
combine(p.getElement(), k.getElement()); // combining
delete(qindex); // deleting node at qindex
}
k = k.getNext();
qindex++;
}
p = p.getNext();
pindex++;
}
}
// returns linked list first to last of ID's
public String toString() {
Node<Person> p = head;
String output = "[ ";
while (p != null) {
output = output + p.getElement().getID() + "--> ";
p = p.getNext();
}
output = output + " ]";
return output;
}
// getnum of matching num
public int matching(String key) {
int count = 0;
Node<Person> p = head;
while (p != null) {
count = count + p.getElement().matching(key).size();
p = p.getNext();
}
return count;
}
// checks if list is empty
public boolean isEmpty() {
return size == 0;
}
// return first element
public Person first() {
if (isEmpty())
return null;
return head.getElement();
}
// get last element
public Person last() {
if (isEmpty())
return null;
return tail.getElement();
}
// add element to the set, return true if element is added
public boolean add(Person element) {
Node<Person> nptr = new Node<>(element, null);
if (head == null) {
head = nptr;
tail = head;
} else {
tail.setNext(nptr);
tail = nptr;
}
size++;
return true;
}
// add element at first
public void addFirst(Person e) {
Node<Person> nptr = new Node<>(e, null);
size++;
if (head == null) {
head = nptr;
tail = head;
} else {
nptr.setNext(head);
head = nptr;
}
}
// insert at given index
// by shifting presented element
public void add(int index, Person element) {
if (isEmpty() || index >= size) {
add(element);
return;
}
if (index == 0) {
addFirst(element);
return;
}
Node<Person> nptr = new Node<>(element, null);
Node<Person> ptr = head;
for (int i = 0; i < size; i++) {
if (i == index - 1) {
Node<Person> tmp = ptr.getNext();
ptr.setNext(nptr);
nptr.setNext(tmp);
break;
}
ptr = ptr.getNext();
}
size++;
}
// inner class for Node
private static class Node<E> {
// elements for Node
private E element;
private Node<E> next;
// ARGUMENTED constructor
public Node(E e, Node<E> n) {
element = e;
next = n;
}
// getters and setters
public E getElement() {
return element;
}
public Node<E> getNext() {
return next;
}
public void setElement(E element) {
this.element = element;
}
public void setNext(Node<E> n) {
next = n;
}
}
}
5) Tst.java
package phonebookproject;
import java.util.Arrays;
import java.util.LinkedList;
public class Test {
public static void main(String[] args) {
String[] p1 = new String[] { "899999999", "8888888", "777777777" };
Person person1 = new Person("100", new LinkedList(Arrays.asList(p1)));
p1 = new String[] { "55555555", "89666666666", "777777777" };
Person person2 = new Person("101", new LinkedList(Arrays.asList(p1)));
p1 = new String[] { "3333333", "111111111", "00000000" };
Person person3 = new Person("102", new LinkedList(Arrays.asList(p1)));
PBArrayList arraylist = new PBArrayList();
arraylist.addPerson(person1);
arraylist.addPerson(person2);
arraylist.addPerson(person3);
PBLinkedList linedlist = new PBLinkedList();
linedlist.add(person1);
linedlist.add(person2);
linedlist.add(person3);
System.out.println("arraylist : "+arraylist.toString());
System.out.println("linedlist : "+linedlist.toString());
System.out.println(prefixCountArrayList(arraylist,"89"));
System.out.println(prefixCountLinedList(linedlist,"89"));
}
//searching for prefix in arraylist
public static int prefixCountArrayList(PBArrayList list, String prefix) {
return list.matching(prefix);
}
//searching for prefix in linked list
public static int prefixCountLinedList(PBLinkedList list, String prefix) {
return list.matching(prefix);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.