In: Computer Science
Create a List
Create a class that holds an ordered list of items. This list should have a variable size, most importantly this class should implement the interface SimpleArrayList. Feel free to add as many other functions and methods as needed to your class to accomplish this task. In other words, you have to write the code for each of the functions specified in the SimpleArrayList interface.
You are not allowed to use any 3rd party data structures or libraries such as Java.Utils.ArrayList or Java.awt.ArrayList
Please use the class SimpleArrayListUnitTest.java to test your code, it is a JUnit test that contains 49 different tests that test all the functions of your class.
(using Eclipse.. I don't have any idea :()
/**********************SimpleArrayList.java**************/
/**
* The Interface SimpleArrayList.
*/
public interface SimpleArrayList {
/**
* Size.
*
* @return the int
*/
public int size();
/**
* Checks if is empty.
*
* @return true, if is empty
*/
public boolean isEmpty();
/**
* Index of.
*
* @param obj the obj
* @return the int
*/
public int indexOf(Object obj);
/**
* Contains.
*
* @param obj the obj
* @return true, if successful
*/
public boolean contains(Object obj);
/**
* Adds the.
*
* @param obj the obj
* @return true, if successful
*/
public boolean add(Object obj);
/**
* Clear.
*/
public void clear();
/**
* Gets the.
*
* @param pos the pos
* @return the object
*/
public Object get(int pos);
/**
* Sets the.
*
* @param pos the pos
* @param obj the obj
* @return the object
*/
public Object set(int pos, Object obj);
/**
* Removes the.
*
* @param pos the pos
* @return the object
*/
public Object remove(int pos);
/**
* Equals.
*
* @param obj the obj
* @return true, if successful
*/
public boolean equals(Object obj);
}
/*************************MyArrayList.java*********************/
/**
* The Class MyArrayList.
*/
public class MyArrayList implements SimpleArrayList {
/** The items. */
private Object[] items;
/** The size. */
private int size;
/** The Constant CAPACITY. */
private static final int CAPACITY = 5;
/**
* Instantiates a new my array list.
*/
public MyArrayList() {
this(CAPACITY);
}
/**
* Instantiates a new my array list.
*
* @param capacity2 the capacity 2
*/
public MyArrayList(int capacity2) {
items = new
Object[capacity2];
size = 0;
}
/*
* (non-Javadoc)
*
* @see SimpleArrayList#size()
*/
@Override
public int size() {
return size;
}
/*
* (non-Javadoc)
*
* @see SimpleArrayList#isEmpty()
*/
@Override
public boolean isEmpty() {
return size() == 0;
}
/*
* (non-Javadoc)
*
* @see SimpleArrayList#indexOf(java.lang.Object)
*/
@Override
public int indexOf(Object obj) {
for (int i = 0; i < size(); i++)
{
if
(items[i].equals(obj)) {
return i;
}
}
return -1;
}
/*
* (non-Javadoc)
*
* @see
SimpleArrayList#contains(java.lang.Object)
*/
@Override
public boolean contains(Object obj) {
return indexOf(obj) != -1;
}
/*
* (non-Javadoc)
*
* @see SimpleArrayList#add(java.lang.Object)
*/
@Override
public boolean add(Object obj) {
if (obj == null) {
throw new
IllegalArgumentException();
}
ensureSpareCapacity(1);
items[size] = obj;
size++;
return true;
}
/**
* Ensure spare capacity.
*
* @param i the i
*/
private void ensureSpareCapacity(int i) {
if (size() + i <= items.length)
{
return;
}
Object[] newItems = new
Object[items.length * 2 + i];
for (int k = 0; k < size(); k++)
{
newItems[k] =
items[k];
}
items = newItems;
}
/*
* (non-Javadoc)
*
* @see SimpleArrayList#clear()
*/
@Override
public void clear() {
for (int k = 0; k < size; k++)
{
items[k] =
null;
}
size = 0;
}
/*
* (non-Javadoc)
*
* @see SimpleArrayList#get(int)
*/
@Override
public Object get(int pos) {
checkPosition(pos);
return items[pos];
}
/**
* Check position.
*
* @param pos the pos
*/
private void checkPosition(int pos) {
if (pos < 0 || pos >= size())
{
throw new
IndexOutOfBoundsException();
}
}
/*
* (non-Javadoc)
*
* @see SimpleArrayList#set(int,
java.lang.Object)
*/
@Override
public Object set(int pos, Object obj) {
checkPosition(pos);
Object result = items[pos];
items[pos] = obj;
return result;
}
/*
* (non-Javadoc)
*
* @see SimpleArrayList#remove(int)
*/
@Override
public Object remove(int pos) {
checkPosition(pos);
Object result = items[pos];
size--;
// shift to the left
for (int k = pos; k < size; k++)
{
items[k] =
items[k + 1];
}
items[size] = null;
return result;
}
public boolean equals(Object obj) {
if (!(obj instanceof SimpleArrayList)) {
return false;
}
MyArrayList other = (MyArrayList) obj;
if (this.size() != other.size()) {
return false;
}
for (int k = 0; k < size(); k++) {
if (!this.items[k].equals(other.items[k])) {
return false;
}
}
return true;
}
@Override
public String toString() {
return
Arrays.toString(items);
}
}
/*********************TestMyArrayList.java************/
public class TestMyArrayList {
public static void main(String[] args) {
SimpleArrayList ar = new
MyArrayList();
ar.add("JKS");
ar.add(12);
ar.add(new String("Virat"));
ar.add(56);
ar.remove(2);
ar.add(67);
ar.add("MS");
System.out.println(ar.size());
System.out.println(ar.indexOf(56));
System.out.println(ar.toString());
}
}
/**************output****************/
5
2
[JKS, 12, 56, 67, MS]
Please let me know if you have any doubt or modify the answer, Thanks :)