Documentation: 1. Explain the purpose of the program as detail as possible. 2. D
ID: 3752637 • Letter: D
Question
Documentation:
1. Explain the purpose of the program as detail as possible.
2. Develop a solution for the problem and mention algorithms to be used.
3. List data structures to be used in solution.
4. Give a description of how to use the program and expected input/output.
5. Explain the purpose of each class you develop in the program.
Programming:
1. For each method, give the pre and post conditions and invariant, if any.
2. Program execution according to the requirements given.
3. Naming of program as required.
Description of Program
You are to write a program name ArrayList.java that create/build the ArrayList data Structure that exist in the java library. The class must be written to accept any type of Objects. The following must be implemented i.e. YOU must write the code (do not import them from the Java Library):
1. One default constructor that will create an ArrayList object with a default size (capacity) of 10 ------ public ArrayList();
2. Another constructor that accepts a parameter of type int and sets the size to this parameter ---- public ArrayList(int n);
3. A method that allows you to place a value at the end of the ArrayList ---- public void add(Object x);
4 .A method that allows you to place a value at a given location ---- public void add(int index, Object x);
5. A method that allows you to retrieve a value from a given location ---- public Object get(int index);
6. A method that allows you the number of elements in the the ArrayList ---- public int size();
7. A method would test to see if the is empty ---- public boolean isEmpty();
8. A method that see if a particular object exist in the --- public boolean isIn(Object ob);
9. A method that will return the location of first occurrence of an Object starting from location 0 ----- public int find (Object n);
10. A method that will remove the first occurrence of an Object starting from location 0 ----- public void remove (Object n);
Now, write a driver program (the class with the public static void main(String[] args) method) name testarray.java to test the ArrayList data structure you just created. That is, you must test all ten (including the default constructor) of the above methods. For those that are returning a value, print out the returned value to the screen and for those that are not returning a value, print a message that indicate if it successfully completed its task.
To test the ArrayList, you must randomly generate 15 integer numbers ranging from 1 to 25 and add to the two ArrayList you created (One using the no-parameter constructor and the other using the one-parameter constructor). Of course, to use the one-parameter constructor, you must prompt the user for an initial size of the ArrayList.
Remember --- The size function should be based upon the actual number of elements you have in the ArrayList at any given time.
PLEASE SHOW ALL CODE, and documentation.
Thank you!
Explanation / Answer
1. Explain the purpose of the program as detail as possible.
sol: the purpose of the program is to understand the implementation of collection data structures , collections is the most important concept of java programming and plays an very important role in coding real time scenarios , as the object data type can store any kind of value so collections are used
2. Develop a solution for the problem and mention algorithms to be used.
sol: we can use collection for implementation of ArrayList and other data structure as these can be implemeted in generic format its easy to use them
3. List data structures to be used in solution.
sol:yes list data structured is implemented using the array data structure which makes it more performance oriented
4. Give a description of how to use the program and expected input/output.
we can simply run the file testarray.java as an java application on any IDE such as Eclipse/STS etc
5. Explain the purpose of each class you develop in the program.
there are two class one testarray.java is the main class calling all the methods of its parent class
and arrayList.java include all the methods of listarray
please find below code
ArrayList.java
import java.util.Arrays;
//no librabry is imported only array is used
public class ArrayList {
private Object[] elementData;// object to store array values
private int Size = 0; // initial Size
public ArrayList(){ //point 1 implementation
elementData = new Object[10]; // setting the value of object to 10
}
//point 2 implementation
public ArrayList(int n){ //point 1 implementation
elementData = new Object[n]; // setting the value of object to 10
}
// point 5 implementation
public Object get(int index){
if(index < Size){
return elementData[index];
} else {
throw new ArrayIndexOutOfBoundsException();
}
}
//point 3 implementation
public void add(Object x){
if(elementData.length-Size <= 5){
increaseListSize();
}
elementData[Size++] = x;
}
//point 4 implementation
public void add(int index ,Object x){
if(elementData.length-Size <= 5){
increaseListSize();
}
elementData[Size++] = x;
}
//point 10
public Object remove(int index){
if(index < Size){
Object obj = elementData[index];
elementData[index] = null;
int tmp = index;
while(tmp < Size){
elementData[tmp] = elementData[tmp+1];
elementData[tmp+1] = null;
tmp++;
}
Size--;
return obj;
} else {
throw new ArrayIndexOutOfBoundsException();
}
}
//point 6 implementation
public int Size(){
return Size;
}
//point 7 implementation
public boolean isEmpty(){
return Size == 0;
}
//point 8
public boolean isIn(Object ob)
{
for (int i = 0 ; i < Size ; i++)
if (elementData[i].equals(ob))
return true;
return false;
}
//point 9
public Object find (Object n){
for (int i = 0 ; i < Size ; i++)
if (elementData[i].equals(n))
return elementData[i];
}
private void increaseListSize(){
elementData = Arrays.copyOf(elementData, elementData.length*2);
System.out.println(" length: "+elementData.length);
}
}
testarray.java
import java.util.Arrays;
//no librabry is imported only array is used
public class ArrayList {
private Object[] elementData;// object to store array values
private int Size = 0; // initial Size
public ArrayList(){ //point 1 implementation
elementData = new Object[10]; // setting the value of object to 10
}
//point 2 implementation
public ArrayList(int n){ //point 1 implementation
elementData = new Object[n]; // setting the value of object to 10
}
// point 5 implementation
public Object get(int index){
if(index < Size){
return elementData[index];
} else {
throw new ArrayIndexOutOfBoundsException();
}
}
//point 3 implementation
public void add(Object x){
if(elementData.length-Size <= 5){
increaseListSize();
}
elementData[Size++] = x;
}
//point 4 implementation
public void add(int index ,Object x){
if(elementData.length-Size <= 5){
increaseListSize();
}
elementData[Size++] = x;
}
//point 10
public Object remove(int index){
if(index < Size){
Object obj = elementData[index];
elementData[index] = null;
int tmp = index;
while(tmp < Size){
elementData[tmp] = elementData[tmp+1];
elementData[tmp+1] = null;
tmp++;
}
Size--;
return obj;
} else {
throw new ArrayIndexOutOfBoundsException();
}
}
//point 6 implementation
public int Size(){
return Size;
}
//point 7 implementation
public boolean isEmpty(){
return Size == 0;
}
//point 8
public boolean isIn(Object ob)
{
for (int i = 0 ; i < Size ; i++)
if (elementData[i].equals(ob))
return true;
return false;
}
//point 9
public Object find (Object n){
for (int i = 0 ; i < Size ; i++)
if (elementData[i].equals(n))
return elementData[i];
}
private void increaseListSize(){
elementData = Arrays.copyOf(elementData, elementData.length*2);
System.out.println(" length: "+elementData.length);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.