/**
*
1. Write a class named as ArrayListNum with the appropriate fields, constructor and the following methods:-
isEmpty();
isFull();
insertAtFront();
- Check the array whether it is empty or full
- If it is empty, proceed the insertion process
- If it is full, prompt a message or perform error handling
- If it is not full and not empty, can we proceed with the insertion
process?
- No! You have to shift the elements first from the left to the right of the
array.
insertAtBack();
- Is it empty or full?
- If it is empty, proceed the insertion process.
- If it is full, prompt a message or perform error handling
- If it is not full and not empty? Can we proceed with the insertion
process? Yes! But you have to know the last element by using a
counter.
removeFromFront();
- Is it empty?
- If it is empty, prompt a message or perform error handling.
- If it is not empty, proceed the deletion process but you have to shift
the element to the left of the array.
removeFromBack();
- Is it empty?
- If it is empty, prompt a message or perform error handling.
- If it is not empty, proceed the deletion process.
- Decrease the size by 1.
display();
Notes:
• You are required to implement an array of objects.
Use Integer class.
*
* @author MUHAMMAD AZRI BIN JASNI @ ABDUL RANI
* @version 27 SEPTEMBER 2012
*/
public class ArrayListNum
{
// instance variables
public int length;
protected final int maxSize = 100;// maxSize default at 100
public Integer [] list;
/**
* Constructor for objects of class ArrayListNum
*/
public ArrayListNum(){
this.length = 0;
list = new Integer[maxSize];
}
public ArrayListNum(int length)
{
this.length = length;
list = new Integer[maxSize];
}
/**methods**/
public boolean isEmpty()
{
return (length==0);
}
public boolean isFull()
{
return (length==maxSize);
}
public void insertAtFront(int a)
{
if (isFull())
{
System.err.println("Can\'t insert in full list.");
}
else
{
if (! isEmpty())
{
for(int i = length; i > 0; i--)
list[i] = list[i - 1];
}
//if (isEmpty())
// list = new Integer[1];
list [0] = a;
length++;
}
}//end insertAtFront
public void insertAtBack(int a)
{
if (isFull())
{
System.err.println("Can\'t insert in full list.");
}
else
{
list [length] = a;
length++;
}
}//end insertAtBack
public void removeFromFront()
{
if (isEmpty())
{
System.err.println("Can\'t remove in empty list.");
}
else
{
for(int i = 0; i <length; i++)
list[i] = list[i+1];
length--;
}
}
public void removeFromBack()
{
if (isEmpty())
{
System.err.println("Can\'t remove in empty list.");
}
else
{
list[length] = null;
length--;
}
}
public void display()
{
for(int i = 0; i <length; i++)
{
System.out.print(list[i]+" ");
}
System.out.println("");
}
}