Question

In: Computer Science

java NetBeans Class Entry: Implement the class Entry that has a name (String), phoneNumber (String), and...

java NetBeans

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 ArrayList of Entry).
  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 list of the entries that start with this name or letters.
  8. Implement the method removeEntryFromPhoneBook (String number) that removes an entry from the arrayList 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

Solutions

Expert Solution

Hello,

Please find the below code :


public class Entry implements Comparable<Entry> {
   private String name;
   private String phoneNumber;
   private String address;

   public Entry(String name, String phoneNumber, String address) {
       super();
       this.name = name;
       this.phoneNumber = phoneNumber;
       this.address = address;
   }

   @Override
   public int hashCode() {
       final int prime = 31;
       int result = 1;
       result = prime * result + ((address == null) ? 0 : address.hashCode());
       result = prime * result + ((name == null) ? 0 : name.hashCode());
       result = prime * result + ((phoneNumber == null) ? 0 : phoneNumber.hashCode());
       return result;
   }

   @Override
   public boolean equals(Object obj) {
       if (this == obj)
           return true;
       if (obj == null)
           return false;
       if (getClass() != obj.getClass())
           return false;
       Entry other = (Entry) obj;
       if (address == null) {
           if (other.address != null)
               return false;
       } else if (!address.equals(other.address))
           return false;
       if (name == null) {
           if (other.name != null)
               return false;
       } else if (!name.equals(other.name))
           return false;
       if (phoneNumber == null) {
           if (other.phoneNumber != null)
               return false;
       } else if (!phoneNumber.equals(other.phoneNumber))
           return false;
       return true;
   }

   @Override
   public String toString() {
       return "Entry [name=" + name + ", phoneNumber=" + phoneNumber + ", 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 int compareTo(Entry o) {
       return (Integer.valueOf(this.phoneNumber) < Integer.valueOf(o.phoneNumber)) ? -1
               : (Integer.valueOf(this.phoneNumber) > Integer.valueOf(o.phoneNumber)) ? 1 : 0;

   }

}

import java.util.ArrayList;
import java.util.List;

public class Phonebook {
   private String city;
   private String type;
   public List<Entry> entryList;

   public Phonebook(String city, String type) {
       super();
       this.city = city;
       this.type = type;
       entryList = new ArrayList<Entry>();
   }

   public void addEntryToPhoneBook(Entry entry) {
       entryList.add(entry);
   }

   @Override
   public String toString() {
       return "Phonebook [entryList=" + entryList + "]";
   }

   public List<Entry> displayAll() {
       return entryList;
   }

   /*
   * checkEntryByNumber
   */
   public Entry checkEntryByNumber(String number) {
       for (int index = 0; index < entryList.size(); index++) {
           if (entryList.get(index).getPhoneNumber() == number) {
               return entryList.get(index);
           }
       }
       System.out.println("Phone number does not exists");
       // Returning null if Phone number doesn't exists
       return null;
   }

   /*
   * checkEntrisByName
   */
   public List<Entry> checkEntrisByName(String name) {
       List<Entry> list = new ArrayList<Entry>();
       for (int index = 0; index < entryList.size(); index++) {
           if (entryList.get(index).getName().startsWith(name)) {
               list.add(entryList.get(index));
           }
       }
       return list;
   }

   /*
   * removeEntryFromPhoneBook
   */
   public void removeEntryFromPhoneBook(String number) {

       for (int index = 0; index < entryList.size(); index++) {
           if (entryList.get(index).getPhoneNumber() == number) {
               System.out.println("Removed the entry : " + entryList.get(index).getName());
               entryList.remove(index);
           }
       }
   }
}

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

public class TestPhonebook {
   public static void main(String[] args) {
       // Create a phone book and initialize its attributes.

       Phonebook book = new Phonebook("Kolkata", "Unknown");

       // Add 5 entries to the phone book list using the method addEntryToPhoneBook()

       Entry e1 = new Entry("Abdite", "1234567891", "C/O abc");
       Entry e2 = new Entry("Jibran", "6783644", "C/O weew");
       Entry e3 = new Entry("Abdlice", "876574", "C/O wewe");
       Entry e4 = new Entry("Smith", "9275454", "C/O wewewe");
       Entry e5 = new Entry("John", "846333", "C/O weweew");

       book.addEntryToPhoneBook(e1);
       book.addEntryToPhoneBook(e2);
       book.addEntryToPhoneBook(e3);
       book.addEntryToPhoneBook(e4);
       book.addEntryToPhoneBook(e5);

       // Display all entries in the phone book.

       List<Entry> list = book.displayAll();
       for (int index = 0; index < list.size(); index++) {
           System.out.println(list.get(index).toString());
       }

       System.out.println("===================================");

       System.out.println(book.checkEntryByNumber("1234567891"));
       System.out.println(book.checkEntryByNumber("123"));

       System.out.println("===================================");

       // Display all entries starting with the letters "Abd"
       List<Entry> list1 = book.checkEntrisByName("Abd");
       System.out.println(list1);

       System.out.println("===================================");

       // Remove a specific entry from the phone book. Display the proper message if it
       // was removed , or if it did not exist.
       book.removeEntryFromPhoneBook("1234567891");

       System.out.println("===================================");

       // Sort and display the entries of the phone book
       Collections.sort(book.displayAll());
       System.out.println(book.displayAll());
   }
}

Test Result:

Code ScreenShots:


Please let me know if you any questions regarding this.

Thanks


Related Solutions

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...
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...
Language: Java(Netbeans) Implement a simple calculator.
Language: Java(Netbeans) Implement a simple calculator.
java/netbeans 1.Design an abstract class Employee that stores: employee name, unique auto-generated id, hire year. Implement...
java/netbeans 1.Design an abstract class Employee that stores: employee name, unique auto-generated id, hire year. Implement any necessary methods in addition to: a.The toString and equals methods. The equals method should NOT compare the id. b.The copy constructor (should copy the same id too) c.An abstract method float getWeeklyCheckAmount() d.Implement the appropriate interface to be able to sort employee names in alphabetical order. Subclasses should NOT be allowed to override this implementation. 2.Design an abstract class HourlyWorker that extends Employee...
Create a Netbeans project with a Java main class. (It does not matter what you name...
Create a Netbeans project with a Java main class. (It does not matter what you name your project or class.) Your Java main class will have a main method and a method named "subtractTwoNumbers()". Your subtractTwoNumbers() method should require three integers to be sent from the main method. The second and third numbers should be subtracted from the first number. The answer should be returned to the main method and then displayed on the screen. Your main() method will prove...
JAVA/Netbeans •Design a class named Person with fields for holding a person’s name, address and phone...
JAVA/Netbeans •Design a class named Person with fields for holding a person’s name, address and phone number (all Strings) –Write a constructor that takes all the required information. –Write a constructor that only takes the name of the person and uses this to call the first constructor you wrote. –Implement the accessor and mutator methods. Make them final so subclasses cannot override them –Implement the toString() method •Design a class named Customer, which extends the Person class. It should have...
Java problem Implement class Course containing: String name, enumeration Type (Foundamental, Specialization, Discipline), enumeration Stream (English,...
Java problem Implement class Course containing: String name, enumeration Type (Foundamental, Specialization, Discipline), enumeration Stream (English, French, German), int creditPoints.  Class Contract has an array of courses with methods addCourse(Course), deleteCourse(type, stream, name) sort(), display().  Courses are sorted by stream, type, name.  If 2 courses are equal raise a custom exception in method sort().  Make Contract implement Storable.
In java Implement the class Book. It has the following instance variables: name, subject, year, maximumLoanPeriod,...
In java Implement the class Book. It has the following instance variables: name, subject, year, maximumLoanPeriod, and loanPeoriod. The following methods should be included: • Constructor(s), Accessors and Mutators as needed. • public double computeFine() => calculates the fine due on this item The fine is calculated as follows: • If the loanPeriod <= maximumLoanPeriod, there is no fine on the book. • If loanPeriod > maximumLoanPeriod o If the subject of the book is "CS" the fine is 10.00...
1. Circle: Implement a Java class with the name Circle. It should be in the package...
1. Circle: Implement a Java class with the name Circle. It should be in the package edu.gcccd.csis. The class has two private instance variables: radius (of the type double) and color (of the type String). The class also has a private static variable: numOfCircles (of the type long) which at all times will keep track of the number of Circle objects that were instantiated. Construction: A constructor that constructs a circle with the given color and sets the radius to...
java/netbeans Write a recursive method, reverseString, that accepts a String and returns the String reversed. Write...
java/netbeans Write a recursive method, reverseString, that accepts a String and returns the String reversed. Write a recursive method, reverseArrayList, that accepts an ArrayList of Strings and returns an ArrayList in reserve order of the input ArrayList. Write a main method that asks the user for a series of Strings, until the user enters “Done” and puts them in an ArrayList. Main should make use to reverseArrayList and reverseString to reverse each String in the ArrayList and then reverse the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT