Java language . Write a test program that allows for new items to be inserted an
ID: 3769032 • Letter: J
Question
Java language. Write a test program that allows for new items to be inserted and displayed.
Design an Address Book
1) Implementation of a couple of Object Oriented concepts i.e, polymorphism, encapsulation, exception handling, etc.
2) Implementation of any collection API data structure.
You are going to build a simple phone book that uses two classes, PhoneEntry and PhoneBook.
The PhoneEntry class contains two private fields, name and number. You need to create accessor functions and mutator functions for both of these fields.
Your PhoneEntry class will need a default constructor and a destructor. Note that your destructor may not do anything, but I want you to code one. Make sure you initialize the data in your object in the constructor.
The PhoneBook class is more complicated. You must create a constructor and a destructor for the PhoneBook class. Make sure you initialize the data in your object in the constructor.
In the private data for your PhoneBook class you will have an arrayList of PhoneEntry objects.
The removeByName function will remove the first entry in the phone book it finds that has a name that matches the passed in parameter. If no entry has that name the function does nothing
The removeByNumber function removes the first entry in the phone book that contains the number specified by the input parameter. Like removeByName the function should return and do nothing if the number is not found. You must implement the same removal logic that you used with the removeByName function.
Explanation / Answer
import java.io.*;
import java.util.*;
class PhoneEntry{
private String name;
private long number;
public PhoneEntry(String name,long number){
this.name = name;
this.number = number;
}
void setName(String name){
this.name = name;
}
String getName(){
return name;
}
void setNumber(long number){
this.number = number;
}
long getNumber(){
return number;
}
}
class PhoneBook{
private ArrayList<PhoneEntry> record;
public PhoneBook(){
record = new ArrayList<PhoneEntry>();
}
void add_entry(String name,long number){
PhoneEntry pe = new PhoneEntry(name,number);
record.add(pe);
}
void removeByName(String name){
for (int i = 0; i < record.size(); i++){
if (record[i].name.compareTo(name) == name){
record.remove(i);
return;
}
}
}
void removeByNumber(long number){
for (int i = 0; i < record.size(); i++){
if (record[i].number == number){
record.remove(i);
return;
}
}
}
}
class main{
public static void main(String[] args){
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.