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

1. Write a class called TelDirectory that implements the comparable interface. T

ID: 3671750 • Letter: 1

Question

1. Write a class called TelDirectory that implements the comparable interface. The class should contain tow fields name (String) and phone number (String).
Write the necessary constructors, accessors, mutors and other methods.
2. Write client class that creates an ArrayList of 10 TelDirectory objects called list.
-Point the list.
-Call Collections.sort(list), to sort the elements according to the name. If the names are the same the sort the list using the phone number.
-Print the sorted list.

1. Write a class called TelDirectory that implements the comparable interface. The class should contain tow fields name (String) and phone number (String).
Write the necessary constructors, accessors, mutors and other methods.
2. Write client class that creates an ArrayList of 10 TelDirectory objects called list.
-Point the list.
-Call Collections.sort(list), to sort the elements according to the name. If the names are the same the sort the list using the phone number.
-Print the sorted list.

Explanation / Answer

/**
* @author Srinivas Palli
*
*/
public class TelDirectory implements Comparable<TelDirectory> {

   String name, phoneNumber;

   /**
   * @param name
   * @param phoneNumber
   */
   public TelDirectory(String name, String phoneNumber) {
       this.name = name;
       this.phoneNumber = phoneNumber;
   }

   /**
   * @return the name
   */
   public String getName() {
       return name;
   }

   /**
   * @return the phoneNumber
   */
   public String getPhoneNumber() {
       return phoneNumber;
   }

   /**
   * @param name
   * the name to set
   */
   public void setName(String name) {
       this.name = name;
   }

   /**
   * @param phoneNumber
   * the phoneNumber to set
   */
   public void setPhoneNumber(String phoneNumber) {
       this.phoneNumber = phoneNumber;
   }

   /*
   * (non-Javadoc)
   *
   * @see java.lang.Comparable#compareTo(java.lang.Object)
   */
   @Override
   public int compareTo(TelDirectory obj) {
       // TODO Auto-generated method stub
       // if the name is equal then sort based on phone number
       if (getName().equalsIgnoreCase(obj.getName())) {

           if (Long.parseLong(getPhoneNumber()) > Long.parseLong(obj
                   .getPhoneNumber()))
               return 1;
           else
               return -1;

       } else if (getName().compareToIgnoreCase(obj.getName()) > 0)
           return 1;
       else
           return -1;

   }

}

import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;

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

       try {
           List<TelDirectory> list = new ArrayList<TelDirectory>();
           list.add(new TelDirectory("Srinivas", "9255354333"));
           list.add(new TelDirectory("Rajesh", "97586123"));
           list.add(new TelDirectory("Pavan", "9638521470"));
           list.add(new TelDirectory("Kishore", "695847123"));
           list.add(new TelDirectory("Akshaya", "859674123"));
           list.add(new TelDirectory("Neehaal", "4567891230"));
           list.add(new TelDirectory("Ajay", "9255354333"));
           list.add(new TelDirectory("Vijay", "9255354333"));
           list.add(new TelDirectory("Geetha", "87545699"));
           list.add(new TelDirectory("Srinivas", "9255354334"));
           list.add(new TelDirectory("Srinivas", "9255354332"));

           Collections.sort(list);

           Iterator<TelDirectory> itr = list.iterator();
           while (itr.hasNext()) {
               TelDirectory directory = itr.next();
               System.out.println(directory.getName() + " "
                       + directory.getPhoneNumber());
           }

       } catch (Exception e) {
           // TODO: handle exception
           e.printStackTrace();
       }
   }
}

OUTPUT:

Ajay 9255354333
Akshaya 859674123
Geetha 87545699
Kishore 695847123
Neehaal 4567891230
Pavan 9638521470
Rajesh 97586123
Srinivas 9255354332
Srinivas 9255354333
Srinivas 9255354334
Vijay 9255354333

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