CSC212 Implementing the Python List in Java: Version 1

From dftwiki3
Jump to: navigation, search

--D. Thiebaut (talk) 11:12, 16 September 2014 (EDT)


To Be Done in a Group


Starting Point


  1. Refresh your memory about how Python Lists work (Python.org page on Lists)
  2. Come up with a Java class that would use an array (or several), and that would implement the same functionality as a Python list. In particular, we are interested in:
    • Creating a new list object (we're interested in numbers)
    • Appending a collection of integers to it
    • Displaying the contents of the object.


Output


  1. List of methods needed
  2. List of member variables/fields
  3. Which variables/fields should be private, which should be public?
  4. Sketch the code for each method
  5. Sketch how to fully test the class created
  6. Write down potential problem areas
  7. Make a note of the limitations discovered
  8. Anything that we need to learn in order to make it work smoothly/better?









Possible Solution


public class PythonList {
	int[] array = null;
	int maxdim;
	int end = 0;

	PythonList(int m) {
		maxdim = m;
		end = 0;
		array = new int[maxdim];
	}

	PythonList() {
		this(100);
	}

	public void append(int n) {
		if (end <= array.length - 1) {
			array[end++] = n;
			return;
		}
		int[] temp = new int[maxdim * 2];
		for (int i = 0; i < end; i++)
			temp[i] = array[i];
		array = temp;
		array[end++] = n;
		maxdim = maxdim * 2;
	}

	public int length() {
		return end;
	}

	public int at(int index) {

		if (index >= 0 && index < end)
			return array[index];
		// this is bad... we'll fix it later...
		else
			return -1;
	}

	public static void main(String[] args) {
		// --- Test List ---
		PythonList L = new PythonList(5);

		for (int i = 0; i < 11; i++)
			L.append(i);

		for (int i = 0; i < L.length(); i++)
			System.out.println(L.at(i));
	}
}








Possible Solution (With Javadoc)


/**
 * PythonList: a first approach to creating a data structure Limitations: no
 * exceptions treated.
 * 
 * @author thiebaut
 * 
 */
public class PythonList {
	int[] array = null;
	int maxdim;
	int end = 0;

	/**
	 * Constructor.
	 * 
	 * @param m
	 *            Integer that defines the initial size of the list.
	 */
	PythonList(int m) {
		maxdim = m;
		end = 0;
		array = new int[maxdim];
	}

	/**
	 * Constructor. No size specified, we default it to 100.
	 */
	PythonList() {
		this(100);
	}

	/**
	 * appends an int to the end of the list.
	 * 
	 * @param n
	 */
	public void append(int n) {
		if (end <= array.length - 1) {
			array[end++] = n;
			return;
		}
		int[] temp = new int[maxdim * 2];
		for (int i = 0; i < end; i++)
			temp[i] = array[i];
		array = temp;
		array[end++] = n;
		maxdim = maxdim * 2;
	}

	/**
	 * returns the length of the list.
	 * 
	 * @return an int, which is the length of the list, 0 if empty.
	 */
	public int length() {
		return end;
	}

	/**
	 * returns element at index specified.
	 * 
	 * @param index
	 *            the integer index where item is located.
	 * @return the item at the specified index, or -1 if invalid index.
	 */
	public int at(int index) {

		if (index >= 0 && index < end)
			return array[index];
		// this is bad... we'll fix it later...
		else
			return -1;
	}

	/**
	 * main program. Tests an object of type PythonList.
	 * 
	 * @param args
	 */
	public static void main(String[] args) {
		// --- Test List ---
		PythonList L = new PythonList(5);

		for (int i = 0; i < 11; i++)
			L.append(i);

		for (int i = 0; i < L.length(); i++)
			System.out.println(L.at(i));
	}
}