Question

In: Computer Science

Write the following classes: Class Entry: 1. Implement the class Entry that has a name (String),...

Write the following classes: Class Entry: 1. Implement the class Entry that has a name (String), phoneNumber (String), and address (String). 2. Implement the initialization constructor . 3. Implement the setters and getters for all attributes. 4. Implement the toString() method to display all attributes. 5. Implement the equals (Entry other) to determine if two entries are equal to each other. Two entries are considered equal if they have the same name, phoneNumber, and address. 6. Implement the compareTo (Entry other) method that returns 0 if the two entries have the same number, -1 if this.number is smaller than other.number, and + 1 if this.number is > other.number Class PhoneBook 1. Implement the class PhoneBook that has a city (String), type (String), entryList (an Array of Entry). Assume the array size = 10 2. Implement the constructors to initialize the city and type. 3. Implement the method addEntryToPhoneBook(Entry e) that adds an entry to the phone book. 4. Implement the method toString() that displays all attributes and all enties information of the phone book. 5. Implement the method displayAll() that displays all entries in the phone book. 6. Implement the method checkEntryByNumber(String number) that takes a number and returns the entry with that number if it exists. 7. Implement the method checkEntrisByName(String name) that takes a name, and returns an array of the entries that start with this name or letters. 8. Implement the method removeEntryFromPhoneBook (String number) that removes an entry from the array of entires in the phone book. Class TestPhoneBook 1. Create a phone book and initialize its attributes. 2. Add 5 entries to the phone book list using the method addEntryToPhoneBook() 3. Display all entries in the phone book. 4. Display the entry information for a given phone number if it exists. Give the appropriate message if it does not exist. 5. Display all entries starting with the letters "Abd" 6. Remove a specific entry from the phone book. Display the proper message if it was removed , or if it did not exist. 7. Sort and display the entries of the phone book (Bonus). Java NetBeans.

Solutions

Expert Solution

package phonebook;

public class Entry {

String name;

String phoneNumber;

String address;

public Entry(String name, String phoneNumber, String address) {

super();

this.name = name;

this.phoneNumber = phoneNumber;

this.address = address;

}

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public String getPhoneNumber() {

return phoneNumber;

}

public void setPhoneNumber(String phoneNumber) {

this.phoneNumber = phoneNumber;

}

public String getAddress() {

return address;

}

public void setAddress(String address) {

this.address = address;

}

@Override

public String toString() {

return "Entry [name=" + name + ", phoneNumber=" + phoneNumber + ", address=" + address + "]";

}

public boolean equals(Entry e){

return e.name.equals(this.name) && e.address.equals(this.address) && e.phoneNumber.equals(this.phoneNumber);

}

public int compareTo(Entry e){

if(this.phoneNumber.compareTo(e.phoneNumber)==0)

return 0;

else if(this.phoneNumber.compareTo(e.phoneNumber)==-1)

return -1;

return 1;

}

}

phonebook class

package phonebook;

import java.util.ArrayList;

public class PhoneBook {

String city;

String type;

Entry entryList[]=new Entry[10];

int ind=0;

public PhoneBook(String city, String type) {

super();

this.city = city;

this.type = type;

}

public void addEntryToPhoneBook(Entry e){

if((ind)<entryList.length){

entryList[ind++]=e;

}

else{

System.out.println("Phone book is full");

}

}

@Override

public String toString() {

return "PhoneBook [city=" + city + ", type=" + type + "]\n"+

"Entries"+entryList.toString();

}

public void displayAll(){

for(Entry e:entryList){

if(e!=null)

System.out.println(e);

}

}

public Entry checkEntryByNumber(String number){

for(Entry e:entryList){

if(e.phoneNumber.equals(number))

return e;

}

return null;

}

public Object[] checkEntrisByName(String name) {

ArrayList<Entry> res=new ArrayList<>();

for(Entry e:entryList){

if(e!=null && e.name.startsWith(name))

res.add(e);

}

if(!res.isEmpty()){

return res.toArray();

}

return null;

}

public boolean removeEntryFromPhoneBook(String number){

for(int i=0;i<entryList.length-1;i++){

if(entryList[i].phoneNumber.equals(number)){

for(int j=i+1;j<entryList.length-1;j++){

entryList[j-1]=entryList[j];

}

entryList[entryList.length-1]=null;

return true;

}

}

return false;

}

}

TestPhoneBook class

package phonebook;

import java.util.Collections;

public class TestPhoneBook {

public static void main(String[] args) {

PhoneBook p=new PhoneBook("NYC","Work");

p.addEntryToPhoneBook(new Entry("AbdJhon","12345","CA"));

p.addEntryToPhoneBook(new Entry("Abd","46722","Huston"));

p.addEntryToPhoneBook(new Entry("JJ","23904","DC"));

p.addEntryToPhoneBook(new Entry("Aron","896745","Pasedina"));

p.addEntryToPhoneBook(new Entry("Reid","34566","Hills"));

p.displayAll();

Entry e=p.checkEntryByNumber("12345");

System.out.println("Entry with given Number");

if(e==null){

System.out.println("Entry with this number does not exists");

}

else{

System.out.println(e);

}

System.out.println("Entry with given String");

Object ar[]=p.checkEntrisByName("Abd");

if(ar!=null){

for(Object en:ar){

System.out.println(en);

}

}

else{

System.out.println("Entry with this name does not exists");

}

boolean isRemoved=p.removeEntryFromPhoneBook("46722");

if(isRemoved){

System.out.println("Entry with this number removed");

}

else{

System.out.println("Entry with this number does not exists");

}

p.displayAll();

}

}

output

Entry [name=AbdJhon, phoneNumber=12345, address=CA]

Entry [name=Abd, phoneNumber=46722, address=Huston]

Entry [name=JJ, phoneNumber=23904, address=DC]

Entry [name=Aron, phoneNumber=896745, address=Pasedina]

Entry [name=Reid, phoneNumber=34566, address=Hills]

Entry with given Number

Entry [name=AbdJhon, phoneNumber=12345, address=CA]

Entry with given String

Entry [name=AbdJhon, phoneNumber=12345, address=CA]

Entry [name=Abd, phoneNumber=46722, address=Huston]

Entry with this number removed

Entry [name=AbdJhon, phoneNumber=12345, address=CA]

Entry [name=JJ, phoneNumber=23904, address=DC]

Entry [name=Aron, phoneNumber=896745, address=Pasedina]

Entry [name=Reid, phoneNumber=34566, address=Hills]


Related Solutions

java NetBeans Class Entry: Implement the class Entry that has a name (String), phoneNumber (String), and...
java NetBeans Class Entry: Implement the class Entry that has a name (String), phoneNumber (String), and address (String). Implement the initialization constructor . Implement the setters and getters for all attributes. Implement the toString() method to display all attributes. Implement the equals (Entry other) to determine if two entries are equal to each other. Two entries are considered equal if they have the same name, phoneNumber, and address. Implement the compareTo (Entry other) method that returns 0 if the two...
Using the following code write the following instructions in C++ Implementation: 1. Re-implement your String class...
Using the following code write the following instructions in C++ Implementation: 1. Re-implement your String class to use a dynamically allocated array for storage. It will be a NULL terminating charater array. 2. This dynamic version of the String will only allocate exactly the amount of memory necessary to store the characters. That is, the length will be the same as the capacity. However, the size of the dynamic array needs to have an extra char for the NULL terminator....
Implement a class Student, including the following attributes and methods: Two public attributes name(String) and score...
Implement a class Student, including the following attributes and methods: Two public attributes name(String) and score (int). A constructor expects a name as a parameter. A method getLevel to get the level(char) of the student. score level table: A: score >= 90 B: score >= 80 and < 90 C: score >= 60 and < 80 D: score < 60 Example:          Student student = new Student("Zack"); student.score = 10; student.getLevel(); // should be 'D'. student.score = 60; student.getLevel(); //...
Consider the following class: class Person {         String name;         int age;        ...
Consider the following class: class Person {         String name;         int age;         Person(String name, int age){                this.name = name;                this.age = age;         } } Write a java program with two classes “Teacher” and “Student” that inherit the above class “Person”. Each class has three components: extra variable, constructor, and a method to print the student or the teacher info. The output may look like the following (Hint: you may need to use “super”...
In Java, Here is a basic Name class. class Name { private String first; private String...
In Java, Here is a basic Name class. class Name { private String first; private String last; public Name(String first, String last) { this.first = first; this.last = last; } public boolean equals(Name other) { return this.first.equals(other.first) && this.last.equals(other.last); } } Assume we have a program (in another file) that uses this class. As part of the program, we need to write a method with the following header: public static boolean exists(Name[] names, int numNames, Name name) The goal of...
Write a class called Account that contains: • Three privateinstance variables: name (String), account (String),...
Write a class called Account that contains: • Three private instance variables: name (String), account (String), account balance (double). • One constructor, which constructs all instances with the values given. • Getters and setters for all the instance variables. • Debit function that takes an amount from the user and subtract the balance (make sure the balance will not drop below zero after the operation, if this was the case, the function should return false else it should return true)...
JAVAFX LAB 2 1. The Soda class has two fields: a String for the name and...
JAVAFX LAB 2 1. The Soda class has two fields: a String for the name and a double for the price. 2. The Soda class has two constructors. The first is a parameterized constructor that takes a String and a double to be assigned to the fields of the class. The second is a copy constructor that takes a Soda object and assigns the name and price of that object to the newly constructed Soda object. 3. The Soda class...
Write two classes, ToDoList and Driver ToDoList should implement the following UML class diagram: ToDoList -list:String[]...
Write two classes, ToDoList and Driver ToDoList should implement the following UML class diagram: ToDoList -list:String[] -size:int +ToDoList() +add(String item):void +remove(String item): boolean +toString():String add(String item) should add the item as the last element in the list, updating size. remove(String item) should remove the item and return true, or, if the item was not in the list, return false.   To remove the item, check every list element from 0 to size-1, and if that item is equal to the parameter...
Exercise3 Given the following classes: The Holiday class has: Properties of destination (String), duration (int days)...
Exercise3 Given the following classes: The Holiday class has: Properties of destination (String), duration (int days) and cost (int $). All expected getters and setters. A default constructor A fully parameterised constructor (which takes all properties) A toString method which returns a String showing all the properties. The TravelAgent class has: Properties of name (String), postcode (String) and holidays (an ArrayList of Holidays) – complete with getters and setters. A parameterised constructor (name and postcode only) which also initialises the...
Using C#: Write a class named Employee that has the following properties: Name - The Name...
Using C#: Write a class named Employee that has the following properties: Name - The Name property holds the employee's name IdNumber - The IdNumber property holds the employee's ID number Department - The Department property holds the name of the department in which the employee works Position - The Position property holds the employee's job title The class should have the following overloaded constructors: A constructor that accepts the following values as arguments and assigns them to the appropriate...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT