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

Add to the Phone class a header such that it implements the Comparable interface

ID: 3912515 • Letter: A

Question

Add to the Phone class a header such that it implements the Comparable interface (so you will change the header to public class Phone implements Comparable; instead of T you need to specify the type of objects that will be compared, in this exercise objects of class Phone.

Add a int compareTo(Phone obj) method to the Phone class , such that the Phone entries are compared by name.

Method compareTo compares this object with the specified object obj for order.

Returns a negative integer, zero, or a positive integer as this object is less than, equal to, or greater than the specified object.

Hint: you will need to use the compareTo method of the String class in your code.

----------------------------------ArrayIterator.java--------------------------

/**

* ArrayIterator represents an iterator over the elements of an array.

*

* @author Dr. Lewis

* @author Dr. Chase

* @version 1.0, 8/19/08

*/

import java.util.*;

public class ArrayIterator implements Iterator

{

private int count; // the number of elements in the collection

private int current; // the current position in the iteration

private T[] items;

/**

* Sets up this iterator using the specified items.

*

* @param collection the collection for which the iterator will be created

* @param size the size of the collection

*/

public ArrayIterator (T[] collection, int size)

{

items = collection;

count = size;

current = 0;

}

/**

* Returns true if this iterator has at least one more element

* to deliver in the iteraion.

*

* @return true if this iterator has at least one more element to deliver

*/

public boolean hasNext()

{

return (current < count);

}

/**

* Returns the next element in the iteration. If there are no

* more elements in this itertion, a NoSuchElementException is

* thrown.

*

* @return the next element in the iteration

* @throws NoSuchElementException if a no such element exception occurs

*/

public T next()

{

if (! hasNext())

throw new NoSuchElementException();

current++;

  

return items[current - 1];

}

/**

* The remove operation is not supported in this collection.

*

* @throws UnsupportedOperationException if an unsupported operation

* exception occurs

*/

public void remove() throws UnsupportedOperationException

{

throw new UnsupportedOperationException();

}

}

--------------------------ArrayList.java-------------------------

//********************************************************************
// ArrayList.java Authors: Lewis/Chase
// Mods : JCD
// Represents an array implementation of a list. The front of
// the list is kept at array index 0. This class will be extended
// to create a specific kind of list.
//********************************************************************

import java.util.Iterator;

public class ArrayList implements ListADT
{
protected final int DEFAULT_CAPACITY = 100;
private final int NOT_FOUND = -1;
protected int rear;
protected T[] list;

//-----------------------------------------------------------------
// Creates an empty list using the default capacity.
//-----------------------------------------------------------------
public ArrayList()
{
rear = 0;
list = (T[])(new Object[DEFAULT_CAPACITY]);
}

//-----------------------------------------------------------------
// Creates an empty list using the specified capacity.
//-----------------------------------------------------------------
public ArrayList (int initialCapacity)
{
rear = 0;
list = (T[])(new Object[initialCapacity]);
}

//-----------------------------------------------------------------
// Removes and returns the last element in the list.
//-----------------------------------------------------------------
public T removeLast () throws EmptyCollectionException
{
T result;

if (isEmpty())
throw new EmptyCollectionException ("list");

rear--;
result = list[rear];
list[rear] = null;

return result;
}

//-----------------------------------------------------------------
// Removes and returns the first element in the list.
//-----------------------------------------------------------------
public T removeFirst() throws EmptyCollectionException
{
if (isEmpty())
throw new EmptyCollectionException ("list");

T result = list[0];
rear--;
// shift the elements
for (int scan=0; scan < rear; scan++)
list[scan] = list[scan+1];


list[rear] = null;

return result;
}

//-----------------------------------------------------------------
// Removes and returns the specified element.
//-----------------------------------------------------------------
public T remove (T element)
{
T result;
int index = find (element);

if (index == NOT_FOUND)
throw new ElementNotFoundException ("list");

result = list[index];
rear--;
// shift the appropriate elements
for (int scan=index; scan < rear; scan++)
list[scan] = list[scan+1];


list[rear] = null;

return result;
}


//-----------------------------------------------------------------
// Returns a reference to the element at the front of the list.
// The element is not removed from the list. Throws an
// EmptyCollectionException if the list is empty.  
//-----------------------------------------------------------------
public T first() throws EmptyCollectionException
{
if (isEmpty())
throw new EmptyCollectionException ("list");

return list[0];
}

//-----------------------------------------------------------------
// Returns a reference to the element at the rear of the list.
// The element is not removed from the list. Throws an
// EmptyCollectionException if the list is empty.  
//-----------------------------------------------------------------
public T last() throws EmptyCollectionException
{
if (isEmpty())
throw new EmptyCollectionException ("list");

return list[rear-1];
}

//-----------------------------------------------------------------
// Returns true if this list contains the specified element.
//-----------------------------------------------------------------
public boolean contains (T target)
{
return (find(target) != NOT_FOUND);
}

//-----------------------------------------------------------------
// Returns the array index of the specified element, or the
// constant NOT_FOUND if it is not found.
//-----------------------------------------------------------------
private int find (T target)
{
int scan = 0, result = NOT_FOUND;
boolean found = false;

if (! isEmpty())
while (! found && scan < rear)
if (target.equals(list[scan]))
found = true;
else
scan++;

if (found)
result = scan;

return result;
}

//-----------------------------------------------------------------
// Returns true if this list is empty and false otherwise.
//-----------------------------------------------------------------
public boolean isEmpty()
{
return (rear == 0);
}

//-----------------------------------------------------------------
// Returns the number of elements currently in this list.
//-----------------------------------------------------------------
public int size()
{
return rear;
}

//-----------------------------------------------------------------
// Returns an iterator for the elements currently in this list.
//-----------------------------------------------------------------
public Iterator iterator()
{
return new ArrayIterator (list, rear);
}

//-----------------------------------------------------------------
// Returns a string representation of this list.
//-----------------------------------------------------------------
public String toString()
{
String result = "";

for (int scan=0; scan < rear; scan++)
result = result + list[scan].toString() + " ";

return result;
}

//-----------------------------------------------------------------
// Creates a new array to store the contents of the list with
// twice the capacity of the old one.
//-----------------------------------------------------------------
protected void expandCapacity()
{
T[] larger = (T[])(new Object[list.length*2]);

for (int scan=0; scan < list.length; scan++)
larger[scan] = list[scan];

list = larger;
}
}

-------------ArrayOrderedList.java----------------------------

public class ArrayOrderedList extends ArrayList

   implements OrderedListADT

{

   /**

* Creates an empty list using the default capacity.

*/

   public ArrayOrderedList()

   {

super();

   }

   /**

* Creates an empty list using the specified capacity.

*

* @param initialCapacity the integer initial size of the list

*/

   public ArrayOrderedList (int initialCapacity)

   {

super(initialCapacity);

   }

   /**

* Adds the specified Comparable element to this list, keeping

* the elements in sorted order.

*

* @param element the element to be added to this list

*/

   public void add (T element)

   {

if (size() == list.length)

   expandCapacity();

Comparable temp = (Comparable)element;

int scan = 0;

   while (scan < rear && temp.compareTo(list[scan]) > 0)

scan++;

for (int scan2=rear; scan2 > scan; scan2--)

   list[scan2] = list[scan2-1];

list[scan] = element;

rear++;

   }

}

----------ElementNotFoundException-----------

public class ElementNotFoundException extends RuntimeException

{

   /******************************************************************

   Sets up this exception with an appropriate message.

   ******************************************************************/

   public ElementNotFoundException (String collection)

   {

super ("The target element is not in this " + collection);

   }

}

----------EmptyCollectionException-------

public class EmptyCollectionException extends RuntimeException

{

/**

   * Sets up this exception with an appropriate message.

   * @param collection String representing the name of the collection

   */

public EmptyCollectionException (String collection)

{

super ("The " + collection + " is empty.");

}

}

--------------ListADT---------------

import java.util.Iterator;

public interface ListADT extends Iterable

{

   /**

* Removes and returns the first element from this list.

*

* @return the first element from this list

*/

   public T removeFirst ();

   /**

* Removes and returns the last element from this list.

*

* @return the last element from this list

*/

   public T removeLast ();

   /**

* Removes and returns the specified element from this list.

*

* @param element the element to be removed from the list

*/

   public T remove (T element);

   /**

* Returns a reference to the first element in this list.

*

* @return a reference to the first element in this list

*/

   public T first ();

   /**

* Returns a reference to the last element in this list.

*

* @return a reference to the last element in this list

*/

   public T last ();

   /**

* Returns true if this list contains the specified target element.

*

* @param target the target that is being sought in the list

* @return true if the list contains this element

*/

   public boolean contains (T target);

   /**

* Returns true if this list contains no elements.

*

* @return true if this list contains no elements

*/

   public boolean isEmpty();

   /**

* Returns the number of elements in this list.

*

* @return the integer representation of number of elements in this list

*/

   public int size();

   /**

* Returns an iterator for the elements in this list.

*

* @return an iterator over the elements in this list

*/

   public Iterator iterator();

   /**

* Returns a string representation of this list.

*

* @return a string representation of this list

*/

   public String toString();

}

------------OrderedList.java---------------------

public interface OrderedListADT extends ListADT

{

   /**

* Adds the specified element to this list at the proper location

*

* @param element the element to be added to this list

*/

   public void add (T element);

}

------------Phone.java-------------

public class Phone{

  

private String name;

private String phone;

  

public Phone(){

name = "";

phone = "";

}

public Phone(String name, String phone){

this.name = name;

this.phone = phone;

}

public String getName(){

return name;

}

public String getPhone(){

return phone;

}

public void setName(String name){

this.name = name;

}

public void setPhone(String phone){

this.phone = phone;

}

public String toString(){

return (name + " " + phone);

}

public boolean equals(Phone other)

{

      return (name == other.name)&&(phone == other.phone);

  

}

}

------ArrayOderedList.java

public class ArrayOrderedList<T> extends ArrayList<T>
implements OrderedListADT<T>
{
/**
* Creates an empty list using the default capacity.
*/
public ArrayOrderedList()
{
super();
}

/**
* Creates an empty list using the specified capacity.
*
* @param initialCapacity the integer initial size of the list
*/
public ArrayOrderedList (int initialCapacity)
{
super(initialCapacity);
}

/**
* Adds the specified Comparable element to this list, keeping
* the elements in sorted order.
*
* @param element the element to be added to this list
*/
public void add (T element)
{
if (size() == list.length)
expandCapacity();

Comparable<T> temp = (Comparable<T>)element;

int scan = 0;
while (scan < rear && temp.compareTo(list[scan]) > 0)
scan++;

for (int scan2=rear; scan2 > scan; scan2--)
list[scan2] = list[scan2-1];

list[scan] = element;
rear++;
}
}

Explanation / Answer

import java.util.Iterator;

import java.util.NoSuchElementException;

public class ArrayIterator<T> implements Iterator

{

private int count; // the number of elements in the collection

private int current; // the current position in the iteration

private T[] items;

/**

*

* Sets up this iterator using the specified items.

*

*

*

* @param collection

* the collection for which the iterator will be created

*

* @param size

* the size of the collection

*

*/

public ArrayIterator(T[] collection, int size)

{

items = collection;

count = size;

current = 0;

}

/**

*

* Returns true if this iterator has at least one more element

*

* to deliver in the iteraion.

*

*

*

* @return true if this iterator has at least one more element to deliver

*

*/

public boolean hasNext()

{

return (current < count);

}

/**

*

* Returns the next element in the iteration. If there are no

*

* more elements in this itertion, a NoSuchElementException is

*

* thrown.

*

*

*

* @return the next element in the iteration

*

* @throws NoSuchElementException

* if a no such element exception occurs

*

*/

public T next()

{

if (!hasNext())

throw new NoSuchElementException();

current++;

return items[current - 1];

}

/**

*

* The remove operation is not supported in this collection.

*

*

*

* @throws UnsupportedOperationException

* if an unsupported operation

*

* exception occurs

*

*/

public void remove() throws UnsupportedOperationException

{

throw new UnsupportedOperationException();

}

}

interface ListADT<T> extends Iterable<T>

{

/**

* Removes and returns the first element from this list.

*

* @return the first element from this list

*/

public T removeFirst ();

/**

* Removes and returns the last element from this list.

*

* @return the last element from this list

*/

public T removeLast ();

/**

* Removes and returns the specified element from this list.

*

* @param element the element to be removed from the list

*/

public T remove (T element);

/**

* Returns a reference to the first element in this list.

*

* @return a reference to the first element in this list

*/

public T first ();

/**

* Returns a reference to the last element in this list.

*

* @return a reference to the last element in this list

*/

public T last ();

/**

* Returns true if this list contains the specified target element.

*

* @param target the target that is being sought in the list

* @return true if the list contains this element

*/

public boolean contains (T target);

/**

* Returns true if this list contains no elements.

*

* @return true if this list contains no elements

*/

public boolean isEmpty();

/**

* Returns the number of elements in this list.

*

* @return the integer representation of number of elements in this list

*/

public int size();

/**

* Returns an iterator for the elements in this list.

*

* @return an iterator over the elements in this list

*/

public Iterator<T> iterator();

/**

* Returns a string representation of this list.

*

* @return a string representation of this list

*/

public String toString();

}

interface OrderedListADT<T> extends ListADT<T>

{

/**

*

* Adds the specified element to this list at the proper location

*

*

*

* @param element

* the element to be added to this list

*

*/

public void add(T element);

}

class Phone implements Comparable<Phone>{

  

private String name;

private String phone;

  

public Phone(){

name = "";

phone = "";

}

public Phone(String name, String phone){

this.name = name;

this.phone = phone;

}

public String getName(){

return name;

}

public String getPhone(){

return phone;

}

public void setName(String name){

this.name = name;

}

public void setPhone(String phone){

this.phone = phone;

}

public String toString(){

return (name + " " + phone);

}

public boolean equals(Phone other)

{

return (name == other.name)&&(phone == other.phone);

  

}

@Override

public int compareTo(Phone aO) {

return this.getName().compareTo(aO.getName());

}

}

Note: Changed the Phone class to implement the comparable interfaces and implemented sorting based on name

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