Question
A design and the pseudo code for the method import java.util.ArrayList; import java.util.Collections; import java.util.Iterator; public class OrderedList > implements Cloneable, Iterable { private ArrayList
A design and the pseudo code for the method
import java.util.ArrayList; import java.util.Collections; import java.util.Iterator;
public class OrderedList > implements Cloneable, Iterable { private ArrayList list;
/** Construct an empty OrderedList object for objects of the comparable class T. */ public OrderedList() { list = new ArrayList(); }
/** Search for the index where the parameter occurs or belongs in this list. @param element the object sought in this list @return the integer index where the parameter occurs or belongs */ public int search( T element ) { return Collections.binarySearch( list, element ); }
/** Insert the parameter in this list but avoid duplicates and nulls. @param element an object to insert
Postcondition: If the parameter does not occur in the original list, then the revised list contains the parameter in its correct ordered position. If the parameter was in the original list, then no change occurs to the list. */ public void insert( T element ) { if ( element != null ) if ( isEmpty() ) list.add( element ); else { int ndx = search( element ); if ( ndx
/** Insert at position ndx the parameter element. Move the tail back one position. @param ndx a nonnegative index into the list @param element a T object to insert */ public void insert( int ndx, T element ) { list.add( ndx, element ); }
/** Set the value at position ndx to the parameter element. @param ndx a nonnegative index into the list @param element a T object to place at ndx */ public void set( int ndx, T element ) { list.set( ndx, element ); }
/** Delete the parameter from this list if it occurs. @param element a T object to delete
Postcondition: If the parameter does not occur in this list, no change occurs to the list. If the parameter was in the original list, then it no longer occurs in the revised list. */ public void delete( T element ) { int ndx = search( element ); if ( ndx >= 0 ) list.remove( ndx ); }
/** Delete the value at location ndx from this list. @param ndx an index into this list */ public void delete( int ndx ) { list.remove( ndx ); }
/** @return true if list is empty or false otherwise */ public boolean isEmpty() { return list.size() == 0; }
/** @return the number of objects in this list */ public int size() { return list.size(); }
/** @return a String representation of this list */ public String toString() { return list.toString() + " size: " + list.size(); }
/** @return an Object array representation of this list */ public Object [] toArray() { return list.toArray(); }
/** Get the element at location ndx in this list. @param ndx an index in this list @return the T object at location ndx in this list */ public T get( int ndx ) { return list.get( ndx ); }
/** @return a shallow clone of this OrderedList object ? Collections.copy( dest, src ) */ public OrderedList clone() { OrderedList ans = null;
try { ans = ( OrderedList )super.clone(); ans.list = ( ArrayList )list.clone(); // shallow clone } catch( CloneNotSupportedException cne ) { cne.printStackTrace(); }
return ans; }
/** @return the iterator of list from the class ArrayList */ public Iterator iterator() { return list.iterator(); }
@Override /** @return an int value for the hash code of this OrderedList object */ public int hashCode() { return list.hashCode(); }
@Override /** Test whether this ordered list equals the parameter. @param right an Object for comparison @return true if the two lists contain the same values in the same order or false otherwise */ public boolean equals( Object right ) { return ( right instanceof OrderedList ? equals( ( OrderedList )right ) : false ); }
/** Test whether this ordered list equals the parameter. @param right an OrderedList object for comparison @return true if the two lists contain the same values in the same order or false otherwise */ public boolean equals( OrderedList right ) { boolean ans = ( list.size() == right.list.size() );
if ( ans ) for ( int i = 0; ( i
return ans; // return list.equals( right.list ); }
public ArrayList getList() { return list; } }
b) Design an algorithm for the method findRange ) of the class OrderedList of part a that finds all values in an ordered list between T objects low and high, inclusive. Use a paragraph format. The signature for findRange( follows /**Find all values in this OrderedList object between low and high inclusive @param low a T object @param high another T object greater than low @return an OrderedList object with and high inclusive public OrderedList findRange ( all elements of this between low T low, T high )Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started