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

Java Problem. Please help 1. Write a class called TelDirectory that implements t

ID: 3671634 • Letter: J

Question

Java Problem. Please help

1. Write a class called TelDirectory that implements the Comparable interface. The class should have two fields name (Strings) and phone number (String). Write the necessary constructors, accessors, mutators, and other methods.

2. Write a client a client that creates an ArrayList of 10 TelDirectory objects called list1.

a. Print the list

b. Call Collection.sort(list1), to sort the elements according to the name. If the names are the same then sort the list using the phone number.

c. Print the sorted list

Explanation / Answer

//*************************************** TelDirectory.java**********************************************************

import java.util.Comparator;                                               // import to use comparator interface
public class TelDirectory implements Comparable<TelDirectory> {

    private String name;                          // field name string
    private String PhoneNumber;             // field name phone number

  
    public String getName() {                       // getter for name
        return name;
    }
    public String PhoneNumber() {               // getter for phone number
        return PhoneNumber;
    }

  

    public TelDirectory( String name, String PhoneNumber) {
        this.name = name;                                                     // setter for name
        this.PhoneNumber = PhoneNumber;                            // setter for phone number
      
    }
  
    @Override
    public int compareTo(TelDirectory o) {                        // compare function which returns the name and sort it, if
                                                                                   // the name is same compare the numbers
        int temp= this.name.compareTo(o.name);               // temp stores the value after comparing names
      
        if(temp!=0)                                                          // if temp is not zero, that means names are different
            return temp;
        else
        {
            int temp2= this.PhoneNumber.compareTo(o.PhoneNumber);   // if name are same find temp2 which is the

                                                                                                   // comparison of phone numbers
            return temp2;
        }
      
    }


    public static Comparator<TelDirectory> NameComparator = new Comparator<TelDirectory>() {
                                                                            
        @Override
        public int compare(TelDirectory e1, TelDirectory e2) {
                return e1.compareTo(e2);                             // calling the compareto function to

                                                                                  // compare the two objects
        }
    };
}

//****************************************** TelDrectory.java end*******************************************************

//******************************************* client.java*******************************************************************

import java.util.*;

public class client {
   public static void main(String[] args) {

   TelDirectory[] list1 = new TelDirectory[10];                                      // making list of 10 names and number

                                                                                                    // and adding the name and phone number
    list1[0] = new TelDirectory("ayush", "1111");                            
    list1[1] = new TelDirectory("ayush", "20000");            // note that two names are same so

                                                                              // they will be sorted based on phone number
    list1[2] = new TelDirectory( "lisa","5000");
    list1[3] = new TelDirectory("ayushi","450000");
    list1[4] = new TelDirectory("rajesh","650000");
    list1[5] = new TelDirectory("inzi"," 502000");
    list1[6] = new TelDirectory("shrinu","508000");
    list1[7] = new TelDirectory("akash","500700");
    list1[8] = new TelDirectory("anuj","500090");
    list1[9] = new TelDirectory("harsh","520000");




    List list = new ArrayList(Arrays.asList(list1));
    Iterator itr1 = list.iterator();                                    // iterator which will iterate thrugh the list and print

    while(itr1.hasNext()){
        TelDirectory element = (TelDirectory) itr1.next();
        System.out.println(element.getName() +" "+element.PhoneNumber()+ " ");
    
    }
    Collections.sort(list);                  // implementing collections

    Iterator itr = list.iterator();           // iterator that will iterate and print the list after sorting
    System.out.println("sorted list ");
    while(itr.hasNext()){
        TelDirectory element = (TelDirectory) itr.next();
        System.out.println(element.getName() +" "+element.PhoneNumber()+ " ");
    
    }
}
}

//********************************************** client.java end********************************************************

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