3 Arrays (JAVA)

Status
Not open for further replies.
Level 2
Joined
Jun 13, 2011
Messages
15
Hi guys, I need some serious help in relation to Java Programming.

I need to do the following:

In this lab you will modify the SimpleListADT so that it provides an iterator() method for use by a client, alter the implementation so that it uses arrays, and change the sort method.

Since I suck at JAVA, I tried to do this in C Language. Can someone look over my C Code and Convert it to JAVA, please??

data = [two][one][four][][three][];
links = [4][0][-1][][2][];
freelist = [][][][5][][-1];



full(){ //checks whether data/links array are full
int full = 1;
for(int i = 0; i < freelist.length; i++){
if(freelist != NULL)
full = 0;

return full; //1 = full and 0 = not full
}

empty(){
int empty = 1;
for(int i = 0; i < data.length; i++){
if(data != NULL)
empty = 0;

return full; //1 = empty and 0 = not empty

}


equals() //not really sure what this function is supposed to do


iterator(){ //iterates through arrays and prints out data
int i = 1; //start of data array
print data; //print first array
while(links != -1){

i = links; //assign index to i
print data; //print data

} //should ouput: one two three four


}



insertItem(Comparable item){ //because no position specified, i will assume to add to first free space
if(full())
print "error, array is full";
else {
for(int i = 0; i < freekist.length; i++){ //find first free location
if(freelist != NULL)
break;
}
links = freelist; //put location in links
data[links] = item; //put item into data
temp = links;
links[data[temp]] = -1; //show where end of array is

}
}





insertItem(Comparable item, int position){
if(full())
print "error, array is full";
else {
if(data[position] == NULL){
links[position] = freelist[position]; //put location in links
data[links[position]] = item; //put item into data
temp = links[position];
links[data[temp]] = -1; //show where end of array is
else
print "error, location already in use";
}



}



removeItem(int position){

if (data[position] == NULL)
print "error,position already empty";
else {
data[position] = NULL; //remove data
for(int i = 0; i < links.length; i++){
if(links == position){
links == NULL; //remove link to data
break;
}

}
freelist[position] = i; //add to freelist

}


}



Or feel free to do the entire program and I will "repay" you back with my Web Designing experties such as HTML, CSS, Flash, etc.

ps. I attached my previous lab so you can refer to that if need be. It uses nodes.

Thank you so much!
 

Attachments

  • old.txt
    6 KB · Views: 90

Dr Super Good

Spell Reviewer
Level 64
Joined
Jan 18, 2005
Messages
27,241
Can someone look over my C Code and Convert it to JAVA, please??
This is not possible, seeing that C is procedural while Java is Object Orientated. I can however attempt to create the nescescary code as I have already completed a simlar course to what you apparently are doing.

You also failed to provide us with the SimpleListADT interface.
In this lab you will modify the SimpleListADT so that it provides an iterator() method for use by a client,
Thus you missed out an important file...

and change the sort method
To what? Bubble sort? Quick sort? Merge sort? Selection sort? Gnome sort?

You need to explain more carefully what you need done.

EDIT

Your code is terriable, it is unclear what it is meant to do... Iterator must return a class that impliments the Iterator interface not just run through all elements.

EDIT 2

Well here is a class which should do sort of what you are after. I do not garuntee it will work as I had no way to test it due to you forgeting all the needed classes but you should atleast be able to fix it so it does.

I advise you do your own homework in future. People like me will not be able to help you during your exams on this subject.
 

Attachments

  • SimpleStringList.txt
    5.4 KB · Views: 86
Last edited:
Level 2
Joined
Jun 13, 2011
Messages
15
Yes, I understand. Here is my Java attempt for it.

Code:
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package util;

/* @author D3158 */
public final class SimpleComparableList implements SimpleListADT<java.lang.Comparable> {
    private int size;
    private int currentPosition;
    private int capacity;
    private Object data;
    private final int DEFAULT_CAPACITY = 100; 
    private String[] dataArray;
    private int[] linksArray;
    private int[] freeListArray;
            
    public static void main(String [ ] args){          
    }

    
    public SimpleComparableList(){
        this.capacity = DEFAULT_CAPACITY;
        this.currentPosition = 1;
        this.dataArray = new String[capacity];
        this.linksArray = new int[capacity];
        this.freeListArray = new int[capacity];
    }
    
    public SimpleComparableList(int capacity){       
    // Constructs and initializes a list that is empty. Current list size is 
    // zero and current position is set to one.        
        this.capacity = capacity;                 
        this.currentPosition = 1; 
        this.dataArray = new String[capacity];
        this.linksArray = new int[capacity];
        this.freeListArray = new int[capacity];        
    }

    public SimpleComparableList(SimpleComparableList otherCopy){  
    // Copy constructor.          
        this.capacity = otherCopy.getCurrentMaxSize(); // Maximum value copy
        this.size = 0;  
        currentPosition = 1;  
        int temp = otherCopy.getCurrentPosition(); // Current position copy
        for (int i = 0; i < otherCopy.getCurrentListSize(); i++) {
            otherCopy.setCurrentPosition(i + 1);
            setCurrentPosition(i + 1);
            this.insertItem(otherCopy.getItem(), i + 1);
        }
        otherCopy.setCurrentPosition(temp); // Updates the current position 
        setCurrentPosition(temp);       // Sets the current position
        }    
    
    @Override
    public boolean empty() {
        //throw new UnsupportedOperationException("Not supported yet.");
        return (this.size == 0);        
    }

    @Override
    public boolean full() {
        //throw new UnsupportedOperationException("Not supported yet.");
        return (this.size >= this.capacity);        
    }

    
    @Override
    public void insertItem(Comparable item) {
        this.insertItem(item, currentPosition);
    }

    @Override
    public void insertItem(Comparable item, int position) {
        //throw new UnsupportedOperationException("Not supported yet.");
        if (this.full()){
            throw new SimpleListException("Full list");
        }
        else{
                for(int i = 0; i < freeListArray.length; i++){ //find first free location
                    if(freeListArray[i] != NULL){
			break;
                    }
                }
		linksArray[i] = freeListArray[i]; //put location in links
		dataArray[linksArray[i]] = item;  //put item into data
		temp = linksArray[i];
		linksArray[dataArray[temp]] = -1; //show where end of array is        
            }
    }
    


    @Override
    public void removeItem() {
        //throw new UnsupportedOperationException("Not supported yet.");
        this.removeItem(this.currentPosition);        
    }

    @Override
    public void removeItem(int position) {
        //throw new UnsupportedOperationException("Not supported yet.");
        
    }

    @Override
    public void removeAll() {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public int getCurrentListSize() {
        return this.size;
    }

    @Override
    public int getCurrentMaxSize() {
        return this.capacity;
    }

    @Override
    public int getCurrentPosition() {
        return this.currentPosition;
    }

    @Override
    public String getItem() {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public void setCurrentPosition(int position) {
        //throw new UnsupportedOperationException("Not supported yet.");
        if (position > this.size) 
            position = this.size;
        if (position < 1) position = 1;
        currentPosition = position;        
    }

    @Override
    public void next() {
     //throw new UnsupportedOperationException("Not supported yet.");
    // Increment only the current position of this list if it is not already the 
    // last position.         
        if (this.currentPosition < this.size){
            currentPosition++;
        }else{
            throw new SimpleListException
                    ("Can't increase current on the list");
        }        
    }

    @Override
    public void prev() {
        //throw new UnsupportedOperationException("Not supported yet.");
        if (this.currentPosition > 1){
            this.currentPosition--;
        }else{
            throw new SimpleListException
                    ("Can't decrease current on the list");
        }        
    }

    @Override
    public void sortList() {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public void quickSort(int array[], int start, int end){
        int i = start;   // index of left-to-right scan
        int k = end;     // index of right-to-left scan

        if (end - start >= 1){                   // check that there are at least two elements to sort
            int pivot = array[start];       // set the pivot as the first element in the partition
            while (k > i){                   // while the scan indices from left and right have not met,
              while (array[i] <= pivot && i <= end && k > i)  // from the left, look for the first
                i++;                                    // element greater than the pivot
                  while (array[k] > pivot && k >= start && k >= i) // from the right, look for the first
                    k--;                                        // element not greater than the pivot
                      if(k > i)                                       // if the left seekindex is still smaller than
                        swap(array, i, k);                      // the right index, swap the corresponding elements
            }
                
            swap(array, start, k);          // after the indices have crossed, swap the last element in
                                            // the left partition with the pivot 
            quickSort(array, start, k - 1); // quicksort the left partition
            quickSort(array, k + 1, end);   // quicksort the right partition
        }
        else{return;}    // if there is only one element in the partition, do not do any sorting
                        // the array is sorted, so exit        
}

public void swap(int array[], int index1, int index2) {
// pre: array is full and index1, index2 < array.length
// post: the values at indices 1 and 2 have been swapped

	int temp = array[index1];           // store the first value in a temp
	array[index1] = array[index2];      // copy the value of the second into the first
	array[index2] = temp;               // copy the value of the temp into the second
}

    
    
}

Here is Simple List ADT

Code:
package util;

/**
 * Defines a SimpleListADT.
 */
public interface SimpleListADT<T> {

  /**
   * Determines if this list is empty.
   * @return <code>true</code> if the list is currently empty, false otherwise.
   */
  public boolean empty();

  /**
   * Determines if this list is full.
   * @return <code>true</code> if the list is currently full, or
   *         <code>false</code> otherwise.
   */
  public boolean full();

  /**
   * Inserts an <code>item</code> into the list at the current position. If
   * an element occupies the current position before this call, that element
   * will be the new item's successor after the call. The list's current
   * position does not change. The first element in the list is defined to be
   * at position 1 (one) <em>not</em> at position zero (0). To insert an
   * item after the last element in the list (i.e., insert an item which will
   * become the last one and will not have a successor) you must use the
   * <code>insertItem(T, int)</code> method.
   * @see SimpleListADT insertItem(T, int)
   * @param item the item to be inserted.
   * @throws <code>SimpleListException</code> if the list is full.
   */
  public void insertItem(T item);

  /**
   * Inserts an <code>item</code> into the list at a specified
   * <code>position</code>. If the list is empty or the <code>position</code>
   * value is less than 1 (one), the item becomes the first one. If
   * <code>position</code> is greater than the current list size, the item
   * becomes the last one. If an element already occupies the specified
   * position, that element will be the new item's successor after the call.
   * Before returning, the current position is updated to <code>item</code>'s
   * position in the list. The current position will not be greater than the
   * current list size.
   * @see SimpleListADT insertItem(T)
   * @param item the item to be inserted.
   * @param position the position in the list that the new item will occupy
   *        after insertion.
   * @throws <code>SimpleListException</code> if the list is full.
   */
  public void insertItem(T item, int position);

  /**
   * The element at the current position is removed from the list. If the
   * removed element had a successor then that successor will be at the
   * current position after the removal. The current position is not changed
   * unless the list becomes empty (in that case the current position is set
   * to one) or the last element in the list is removed (in that case the
   * current position is set to the current number of items in the list, after
   * the removal).
   * @see #removeAll()
   * @see #removeItem(int)
   * @throws <code>SimpleListException</code> if the list is empty or the
   *         current position is not set.
   */
  public void removeItem();

  /**
   * The element at the specified <code> position </code> is removed from the
   * list. If the removed element had a successor then that successor will be
   * at the vacated position after the removal.  The current position is not
   * changed unless the current position exceeds the length of the list after
   * the removal (in that case the current position is set to the length of the
   * list or 1 (one) if the list becomes empty.
   * @param position the position of the element to be removed.
   * @see #removeAll()
   * @see #removeItem()
   * @throws <code>SimpleListException</code> if the list is empty or the
   *         current position is not set.
   */
  public void removeItem(int position);

  /**
   * The list is made empty.
   * @see #removeItem()
   * @see #removeItem(int position)
   * @throws <code>SimpleListException</code> if the list is empty.
   */
  public void removeAll();

  /**
   * Obtain the size of the list.
   * @return the current number of elements in the list.
   */
  public int getCurrentListSize();

  /**
   * Obtain the maximum size of the list.
   * @return the maximum number of elements that can be stored in the list.
   */
  public int getCurrentMaxSize();

  /**
   * Obtain the current position for the list. The list's position indicator is
   * '1-based' <em>not</em> '0-based'. That is, the first position is numbered
   * 1, the second numbered 2, etc. The last position is 'n' where n is the
   * number of items in this list.
   * @return the current position.
   */
  public int getCurrentPosition();

  /**
   * Obtain the element at the current position.
   * @return the element stored at the current position in the list.
   * @throws <code>SimpleListException</code> if the list is empty.
   */
  public T getItem();

  /**
   * Sets the current position value for the list.  The position value must be
   * between 1 (one) and the number of elements in the list.  If the provided
   * value is less than one (1), the position value will be set to one (1).  If
   * the provided value is greater than the number of elements in the list, the
   * position value will be set to the number of elements in the list.
   * @param position the value for the list's current position.
   */
  public void setCurrentPosition(int position);

  /**
   * Increments the current position value for the list if the current
   * position is not already the last position; otherwise, the current position
   * value is not changed.
   * @throws <code>SimpleListException</code> if there is no next element
   * or if the list is empty.
   */
  public void next();

  /**
   * Decrements the current position value for the list if the current
   * position is not already the first position; otherwise, the current
   * position value is not changed.
   * @throws <code>SimpleListException</code> if there is no previous element
   * or if the list is empty.
   */
  public void prev();

  /**
   * Sorts the list into ascending order according to the natural ordering of
   * the elements. The natural ordering can be determined by the
   * <code>compareTo()</code> method. Before returning to the caller, the
   * current position will be set at the last (end of list) position.
   */
  public void sortList();
}

It's quick sort I need to implement.

I noticed you attached the completed version of the code, which I am only referring to. As I am tracing through the code, I noticed you created an empty array by doing private String[] array;? I need 3 arrays. I am sure it's right there in front of me but where is it?

Thank you Dr Super Good so much. I hope you can continue to help me out.
 

Dr Super Good

Spell Reviewer
Level 64
Joined
Jan 18, 2005
Messages
27,241
There are more than 1 ways to impliment a list...
You were using a linked list which you do primarilly via nodes. I was using an array list which is backed by a single array strcture (usually dynamic array but in this case it was a normal array).
This has its advantages such as O(1) read, replace, append and remove top efficiency.
This also has its disadvantages such as O(n) remove and add efficiency.

You missed out the simple list exception... How can you be helped if you do not give us everything or what you need to do. I am not psycic.
 
Level 2
Joined
Jun 13, 2011
Messages
15
Here is Simple List Exception. Sorry about that.

Code:
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package util;

/**
 *
 * @author D3158
 */
public class SimpleListException extends RuntimeException  {
    
    public SimpleListException(String message){
        super(message);
    }


}
 
Level 2
Joined
Jun 13, 2011
Messages
15
Okay. I implemented your code and made changes.

Code:
package util;
/* @author D3158 */
public final class SimpleComparableList implements SimpleListADT<java.lang.Comparable> {
    private int size;
    private int currentPosition;
    private int capacity;
    private String[] array;      
    //private Object data;
    //private final int DEFAULT_CAPACITY = 100; 
    //private String[] dataArray;
    //private int[] linksArray;
    //private int[] freeListArray;
  
            
    public static void main(String [ ] args){          
    }

    
    public SimpleComparableList(){
        this.capacity = 100;
        this.array = new String[this.capacity];        
        this.currentPosition = 1;
        this.size = 0;        
        //this.dataArray = new String[capacity];
        //this.linksArray = new int[capacity];
        //this.freeListArray = new int[capacity];
    }
    
    public SimpleComparableList(int capacity){       
    // Constructs and initializes a list that is empty. Current list size is 
    // zero and current position is set to one.        
        this.capacity = capacity;                 
        this.currentPosition = 1; 
        //this.dataArray = new String[capacity];
        //this.linksArray = new int[capacity];
        //this.freeListArray = new int[capacti];        
        array = new String[capacity];
        this.size = 0;        
    }

    @Override
    public boolean empty() {
        //throw new UnsupportedOperationException("Not supported yet.");
        return (size == 0);
    	
    }

    @Override
    public boolean full() {
        //throw new UnsupportedOperationException("Not supported yet.");
        return (size >= capacity);        
    }

    
    @Override
    public void insertItem(Comparable item) {
        //throw new UnsupportedOperationException("Not supported yet.");
        insertItem(item, currentPosition);        
    }

    @Override
    public void insertItem(Comparable item, int positionOnTheList) {
        //throw new UnsupportedOperationException("Not supported yet.");
    	if (full()) throw new SimpleListException("Full list");
    	int target = positionOnTheList-1;
    	if (target > size) target = size;
        for(int i = size ; i>target ; i--) array[i] = array[i-1];
        array[target] = (String) item;
        size++;
        currentPosition = positionOnTheList;                
    }


    @Override
    public void removeItem() {
        //throw new UnsupportedOperationException("Not supported yet.");
        removeItem(currentPosition);        
    }

    @Override
    public void removeItem(int positionOnTheList) {
        //throw new UnsupportedOperationException("Not supported yet.");
        if(empty()) throw new SimpleListException("Empty List");
    	size--;
        for(int i = --positionOnTheList ; i<size ; i++) array[i] = array[i+1];
        if (currentPosition > size) currentPosition = size+1;        
    }

    @Override
    public void removeAll() {
        //throw new UnsupportedOperationException("Not supported yet.");
        if(empty()) throw new SimpleListException("Empty List");
        size = 0;
        currentPosition = 1;        
    }

    @Override
    public int getCurrentListSize() {
        //throw new UnsupportedOperationException("Not supported yet.");
        return this.size;        
    }

    @Override
    public int getCurrentMaxSize() {
        //throw new UnsupportedOperationException("Not supported yet.");
        return this.capacity;
    }

    @Override
    public int getCurrentPosition() {
        //throw new UnsupportedOperationException("Not supported yet.");
        return this.currentPosition;        
    }

    @Override
    public String getItem() {
        //throw new UnsupportedOperationException("Not supported yet.");
        if (empty()) throw new SimpleListException("Empty List");
        return array[currentPosition-1];        
    }

    @Override
    public void setCurrentPosition(int position) {
        //throw new UnsupportedOperationException("Not supported yet.");
        if (position > size) position = size+1;
        else if (position < 1) position = 1;
        currentPosition = position;        
    }

    @Override
    public void next() {
        //throw new UnsupportedOperationException("Not supported yet.");
        if (currentPosition > size)
        	throw new SimpleListException ("Can't increase current on the list");	
        currentPosition++;        
    }

    @Override
    public void prev() {
        //throw new UnsupportedOperationException("Not supported yet.");
        if (currentPosition < 2)
        	throw new SimpleListException("Can't decrease current on the list");
        this.currentPosition--;        
    }

    @Override
    public void sortList() {
     
    }


    
    
}

When I run the program, i don't get a compilation error (which is obviously good) and it runs the GUI interface:

Default Constructor OK
Constructor with given max size OK
iterator()
insertItem(Comparable item)
insertItem(Comparable item, int position)
removeItem()
removeItem(int position)


The unchecked ones are the problematic issues. My curiosity is do I need to "fix" the iterator() first so then others will follow or insertItem?
 

Attachments

  • lab3tester.jpg
    lab3tester.jpg
    83.4 KB · Views: 83

Dr Super Good

Spell Reviewer
Level 64
Joined
Jan 18, 2005
Messages
27,241
Why are you not giving us all the needed information and materials...

In this lab you will modify the SimpleListADT so that it provides an iterator() method for use by a client, alter the implementation so that it uses arrays, and change the sort method
Like I mentioned, the interface needs to be made to extend iterable of some type and the implimentation needs to make an iterator that matches the iterator specifications (sort of like I tried to do).
 
Level 2
Joined
Jun 13, 2011
Messages
15
I need to pay better attention. Gah!

Anyways, here is the updated code( I hope) for SimpleListADT

Code:
package util;

import java.util.Iterator;

/**
 * Defines a SimpleListADT.
 */
public interface SimpleListADT<T> {

  /**
   * Determines if this list is empty.
   * @return <code>true</code> if the list is currently empty, false otherwise.
   */
  public boolean empty();

  /**
   * Determines if this list is full.
   * @return <code>true</code> if the list is currently full, or
   *         <code>false</code> otherwise.
   */
  public boolean full();

  /**
   * Inserts an <code>item</code> into the list at the current position. If
   * an element occupies the current position before this call, that element
   * will be the new item's successor after the call. The list's current
   * position does not change. The first element in the list is defined to be
   * at position 1 (one) <em>not</em> at position zero (0). To insert an
   * item after the last element in the list (i.e., insert an item which will
   * become the last one and will not have a successor) you must use the
   * <code>insertItem(T, int)</code> method.
   * @see SimpleListADT insertItem(T, int)
   * @param item the item to be inserted.
   * @throws <code>SimpleListException</code> if the list is full.
   */
  public void insertItem(Comparable item);

  /**
   * Inserts an <code>item</code> into the list at a specified
   * <code>position</code>. If the list is empty or the <code>position</code>
   * value is less than 1 (one), the item becomes the first one. If
   * <code>position</code> is greater than the current list size, the item
   * becomes the last one. If an element already occupies the specified
   * position, that element will be the new item's successor after the call.
   * Before returning, the current position is updated to <code>item</code>'s
   * position in the list. The current position will not be greater than the
   * current list size.
   * @see SimpleListADT insertItem(T)
   * @param item the item to be inserted.
   * @param position the position in the list that the new item will occupy
   *        after insertion.
   * @throws <code>SimpleListException</code> if the list is full.
   */
  public void insertItem(Comparable item, int positionOnTheList);

  /**
   * The element at the current position is removed from the list. If the
   * removed element had a successor then that successor will be at the
   * current position after the removal. The current position is not changed
   * unless the list becomes empty (in that case the current position is set
   * to one) or the last element in the list is removed (in that case the
   * current position is set to the current number of items in the list, after
   * the removal).
   * @see #removeAll()
   * @see #removeItem(int)
   * @throws <code>SimpleListException</code> if the list is empty or the
   *         current position is not set.
   */
  public void removeItem();

  /**
   * The element at the specified <code> position </code> is removed from the
   * list. If the removed element had a successor then that successor will be
   * at the vacated position after the removal.  The current position is not
   * changed unless the current position exceeds the length of the list after
   * the removal (in that case the current position is set to the length of the
   * list or 1 (one) if the list becomes empty.
   * @param position the position of the element to be removed.
   * @see #removeAll()
   * @see #removeItem()
   * @throws <code>SimpleListException</code> if the list is empty or the
   *         current position is not set.
   */
  public void removeItem(int positionOnTheList);

  /**
   * The list is made empty.
   * @see #removeItem()
   * @see #removeItem(int position)
   * @throws <code>SimpleListException</code> if the list is empty.
   */
  public void removeAll();

  /**
   * Obtain the size of the list.
   * @return the current number of elements in the list.
   */
  public int getCurrentListSize();

  /**
   * Obtain the maximum size of the list.
   * @return the maximum number of elements that can be stored in the list.
   */
  public int getCurrentMaxSize();

  /**
   * Obtain the current position for the list. The list's position indicator is
   * '1-based' <em>not</em> '0-based'. That is, the first position is numbered
   * 1, the second numbered 2, etc. The last position is 'n' where n is the
   * number of items in this list.
   * @return the current position.
   */
  public int getCurrentPosition();

  /**
   * Obtain the element at the current position.
   * @return the element stored at the current position in the list.
   * @throws <code>SimpleListException</code> if the list is empty.
   */
  public T getItem();

  /**
   * Sets the current position value for the list.  The position value must be
   * between 1 (one) and the number of elements in the list.  If the provided
   * value is less than one (1), the position value will be set to one (1).  If
   * the provided value is greater than the number of elements in the list, the
   * position value will be set to the number of elements in the list.
   * @param position the value for the list's current position.
   */
  public void setCurrentPosition(int position);

  /**
   * Increments the current position value for the list if the current
   * position is not already the last position; otherwise, the current position
   * value is not changed.
   * @throws <code>SimpleListException</code> if there is no next element
   * or if the list is empty.
   */
  public void next();

  /**
   * Decrements the current position value for the list if the current
   * position is not already the first position; otherwise, the current
   * position value is not changed.
   * @throws <code>SimpleListException</code> if there is no previous element
   * or if the list is empty.
   */
  public void prev();

  /**
   * Sorts the list into ascending order according to the natural ordering of
   * the elements. The natural ordering can be determined by the
   * <code>compareTo()</code> method. Before returning to the caller, the
   * current position will be set at the last (end of list) position.
   */
  public void sortList();
  
  public Iterator<String> iterator();
}
 
Level 2
Joined
Jun 13, 2011
Messages
15
I have given you the entire directory with the driver_classes, utils, etc.

Everything is in there including the tester program.

At the end, if you get everything to work, can you please forward me all the way I did that, please?

Thank you man!

I will be sure to help you in whatever way I can Web Design related.

Cheers!
 

Attachments

  • Comp139Lab3.rar
    57 KB · Views: 51
Level 2
Joined
Jun 13, 2011
Messages
15
I can probably see why. I am using NetBeans, not Eclipse, unfortunately. :(

Not my choice. I prefer Eclipse just like you do.
 

Dr Super Good

Spell Reviewer
Level 64
Joined
Jan 18, 2005
Messages
27,241
Ok I think I might have it, this code is untested and has warnings but should be very close to what you are after I hope.

Code:
package util;

import java.util.Iterator;
import java.util.NoSuchElementException;

/* @author D3158 */
public final class SimpleComparableList implements SimpleListADT<Comparable> {
    private int size;
    private int currentPosition;
    private int capacity;
    private Comparable[] array;      
    
    private final static int DEFAULT_CAPACITY = 100; 
  
            
    public static void main(String [ ] args){          
    }

    
    public SimpleComparableList(){
        capacity = DEFAULT_CAPACITY;
        array = new Comparable[capacity];        
        currentPosition = 1;
        size = 0;        
    }
    
    public SimpleComparableList(int capacity){       
    // Constructs and initializes a list that is empty. Current list size is 
    // zero and current position is set to one.        
        this.capacity = capacity;                 
        currentPosition = 1; 
        array = new Comparable[capacity];      
        size = 0;        
    }

    @Override
    public boolean empty() {
        return size == 0;
    	
    }

    @Override
    public boolean full() {
        return size == capacity;        
    }
    
    @Override
    public void insertItem(Comparable item) {
        insertItem(item, currentPosition);        
    }

    @Override
    public void insertItem(Comparable item, int positionOnTheList) {
    	if (full()) throw new SimpleListException("Full list");
    	int target = positionOnTheList-1;
    	if (target > size) target = size;
    	else if (target < 0) target = 0;
        for(int i = size++ ; i>target ; i--) array[i] = array[i-1];
        array[target] = item;
        currentPosition = target+1;                
    }


    @Override
    public void removeItem() {
        removeItem(currentPosition);        
    }

    @Override
    public void removeItem(int positionOnTheList) {
        if(empty()) throw new SimpleListException("Empty List");
        int target = positionOnTheList-1;
    	if (target > size) target = size;
    	else if (target < 0) target = 0;
    	size--;
        for(int i = target ; i<size ; i++) array[i] = array[i+1];
        if (size == 0) currentPosition = 1;
        if (currentPosition > size) currentPosition = size;
        
    }

    @Override
    public void removeAll() {
        if(empty()) throw new SimpleListException("Empty List");
        size = 0;
        currentPosition = 1;        
    }

    @Override
    public int getCurrentListSize() {
        return size;        
    }

    @Override
    public int getCurrentMaxSize() {
        return capacity;
    }

    @Override
    public int getCurrentPosition() {
        return currentPosition;        
    }

    @Override
    public Comparable getItem() {
        if (empty()) throw new SimpleListException("Empty List");
        return array[currentPosition-1];        
    }

    @Override
    public void setCurrentPosition(int position) {
        if (position > size) position = size;
        if (position < 1) position = 1;
        currentPosition = position;        
    }

    @Override
    public void next() {
    	if (empty()) throw new SimpleListException("Empty List");
        if (currentPosition == size) throw new SimpleListException ("No Next Element");	
        currentPosition++;        
    }

    @Override
    public void prev() {
    	if (empty()) throw new SimpleListException("Empty List");
        if (currentPosition == 1) throw new SimpleListException ("No Previous Element");	
        currentPosition--;        
    }
    
    
    @Override
    public void sortList() {
     
    }

    public void quicksort(int start, int end) {
        if (start-end <= 1) return;
        Comparable pivot = array[end];
        int big = end-1;
        int small = 0;
        while(big != small){
        	if(pivot.compareTo(array[big]) > 0){
        		Comparable temp = array[big];
        		array[big] = array[small];
        		array[small++] = temp;
        	}else big--;
        }
        array[end] = array[big];
        array[big] = pivot;
        quicksort(start,big-1);
        quicksort(big+1,end);
    }


	@Override
	public Iterator<Comparable> iterator() {
		return new SimpleComparableListIterator();
	}
	
	private class SimpleComparableListIterator implements Iterator<Comparable>{
		private int position;
		
		public SimpleComparableListIterator(){
			position = 0;
		}

		@Override
		public boolean hasNext() {
			return position < size;
		}

		@Override
		public Comparable next() {
			if (!hasNext()) throw new NoSuchElementException("Element out of bounds");
			return array[position];
		}

		@Override
		public void remove() {
			throw new UnsupportedOperationException("SimpleComparableListIterator does not support remove");
		}
		
	}
    
}
Code:
package util;

/**
 * Defines a SimpleListADT.
 */
public interface SimpleListADT<T> extends Iterable<T> {

  /**
   * Determines if this list is empty.
   * @return <code>true</code> if the list is currently empty, false otherwise.
   */
  public boolean empty();

  /**
   * Determines if this list is full.
   * @return <code>true</code> if the list is currently full, or
   *         <code>false</code> otherwise.
   */
  public boolean full();

  /**
   * Inserts an <code>item</code> into the list at the current position. If
   * an element occupies the current position before this call, that element
   * will be the new item's successor after the call. The list's current
   * position does not change. The first element in the list is defined to be
   * at position 1 (one) <em>not</em> at position zero (0). To insert an
   * item after the last element in the list (i.e., insert an item which will
   * become the last one and will not have a successor) you must use the
   * <code>insertItem(T, int)</code> method.
   * @see SimpleListADT insertItem(T, int)
   * @param item the item to be inserted.
   * @throws <code>SimpleListException</code> if the list is full.
   */
  public void insertItem(T item);

  /**
   * Inserts an <code>item</code> into the list at a specified
   * <code>position</code>. If the list is empty or the <code>position</code>
   * value is less than 1 (one), the item becomes the first one. If
   * <code>position</code> is greater than the current list size, the item
   * becomes the last one. If an element already occupies the specified
   * position, that element will be the new item's successor after the call.
   * Before returning, the current position is updated to <code>item</code>'s
   * position in the list. The current position will not be greater than the
   * current list size.
   * @see SimpleListADT insertItem(T)
   * @param item the item to be inserted.
   * @param position the position in the list that the new item will occupy
   *        after insertion.
   * @throws <code>SimpleListException</code> if the list is full.
   */
  public void insertItem(T item, int positionOnTheList);

  /**
   * The element at the current position is removed from the list. If the
   * removed element had a successor then that successor will be at the
   * current position after the removal. The current position is not changed
   * unless the list becomes empty (in that case the current position is set
   * to one) or the last element in the list is removed (in that case the
   * current position is set to the current number of items in the list, after
   * the removal).
   * @see #removeAll()
   * @see #removeItem(int)
   * @throws <code>SimpleListException</code> if the list is empty or the
   *         current position is not set.
   */
  public void removeItem();

  /**
   * The element at the specified <code> position </code> is removed from the
   * list. If the removed element had a successor then that successor will be
   * at the vacated position after the removal.  The current position is not
   * changed unless the current position exceeds the length of the list after
   * the removal (in that case the current position is set to the length of the
   * list or 1 (one) if the list becomes empty.
   * @param position the position of the element to be removed.
   * @see #removeAll()
   * @see #removeItem()
   * @throws <code>SimpleListException</code> if the list is empty or the
   *         current position is not set.
   */
  public void removeItem(int positionOnTheList);

  /**
   * The list is made empty.
   * @see #removeItem()
   * @see #removeItem(int position)
   * @throws <code>SimpleListException</code> if the list is empty.
   */
  public void removeAll();

  /**
   * Obtain the size of the list.
   * @return the current number of elements in the list.
   */
  public int getCurrentListSize();

  /**
   * Obtain the maximum size of the list.
   * @return the maximum number of elements that can be stored in the list.
   */
  public int getCurrentMaxSize();

  /**
   * Obtain the current position for the list. The list's position indicator is
   * '1-based' <em>not</em> '0-based'. That is, the first position is numbered
   * 1, the second numbered 2, etc. The last position is 'n' where n is the
   * number of items in this list.
   * @return the current position.
   */
  public int getCurrentPosition();

  /**
   * Obtain the element at the current position.
   * @return the element stored at the current position in the list.
   * @throws <code>SimpleListException</code> if the list is empty.
   */
  public T getItem();

  /**
   * Sets the current position value for the list.  The position value must be
   * between 1 (one) and the number of elements in the list.  If the provided
   * value is less than one (1), the position value will be set to one (1).  If
   * the provided value is greater than the number of elements in the list, the
   * position value will be set to the number of elements in the list.
   * @param position the value for the list's current position.
   */
  public void setCurrentPosition(int position);

  /**
   * Increments the current position value for the list if the current
   * position is not already the last position; otherwise, the current position
   * value is not changed.
   * @throws <code>SimpleListException</code> if there is no next element
   * or if the list is empty.
   */
  public void next();

  /**
   * Decrements the current position value for the list if the current
   * position is not already the first position; otherwise, the current
   * position value is not changed.
   * @throws <code>SimpleListException</code> if there is no previous element
   * or if the list is empty.
   */
  public void prev();

  /**
   * Sorts the list into ascending order according to the natural ordering of
   * the elements. The natural ordering can be determined by the
   * <code>compareTo()</code> method. Before returning to the caller, the
   * current position will be set at the last (end of list) position.
   */
  public void sortList();
}

You may have to debug it yourself a bit as I have never implimented an iterator before but it seems to make some sense.
 
Level 2
Joined
Jun 13, 2011
Messages
15
You, my friend, are AWESOME!

Everything works except 2 tests: "Constructor with given max size" and "iterator()".

I am trying to fix it. If I come up with a solution, I will post it. I hope you do as well.

Thank you so much! :)
 

Attachments

  • only_2_more_2_go!.jpg
    only_2_more_2_go!.jpg
    80.2 KB · Views: 143

Dr Super Good

Spell Reviewer
Level 64
Joined
Jan 18, 2005
Messages
27,241
No idea why the constructor with given max size failed...

The iterator might have failed due to the lack of a remove method. Refer to the Iterator interface documentation to see what the remove is meant to do. It was not clear weather remove was required or not so I obeyed the specification in declaring that it was not supported but the test might require it.

That is what happens without good problem specifications.
 
Level 2
Joined
Jun 13, 2011
Messages
15
No results thus far. I tried to change the iterator from comparable to string but it does not work. Still shows same message as in the attached picture
 
Level 2
Joined
Jun 13, 2011
Messages
15
Yeah. I did. It compiles and everything. But yet the output is still the same as before.

Code:
        public void remove() {
            //throw new UnsupportedOperationException("Not supported yet.");
            SimpleComparableList aList = new SimpleComparableList();            
            Iterator itr = aList.iterator();
            String strElement = "";
                while(itr.hasNext()){    
                    strElement = (String)itr.next();
                    if(strElement.equals("2")){
                        itr.remove();
                        break;
                    }                
                }        
       }
 
Level 2
Joined
Jun 13, 2011
Messages
15
Okay. I managed to fix the "constructor with given max size". Gonna again try to get the iterator to work and follow the advice you suggested.

ps. What time do you have there? I got 9:34 PM
 
Level 2
Joined
Jun 13, 2011
Messages
15
Had to make changes to the constructors.

Code:
    public SimpleComparableList(){
        capacity = 100;
        array = new Comparable[capacity];
        currentPosition = 1; 
    }
    
    public SimpleComparableList(int capacity){       
        // Constructs and initializes a list that is empty. Current list size is 
        // zero and current position is set to one.        
        this.capacity = capacity;
        array = new Comparable[capacity];
        currentPosition = 1;       
    }
 
Level 2
Joined
Jun 13, 2011
Messages
15
I wish I had a technical answer - but I don't. However, I am doing some research on why it did not work before for a bit until I have to jump along with the "Trees".
 
Status
Not open for further replies.
Top