I need help with this question please add comments about what each method does t
ID: 3567725 • Letter: I
Question
I need help with this question please add comments about what each method does to get the result and do include pre and post conditions:
import java.util.ArrayList;
public class ArrayIntList
{
private int[] elementData;
private int size;
// constructor
public int ArrayIntList()
{
elementData = new int[100];
size = 0;
}
// method adds element into array
public void add(int value)
{
elementData[size] = value;
size++;
}
// Displays the list of elements
public void print()
{
if (size == 0)
{
System.out.println("[]");
}
else
{
System.out.print("[" + elementData[0]);
for (int i = 1; i < size; i++)
System.out.print(", " + elementData[i]);
System.out.println("]");
}
}
Explanation / Answer
//The class ArrayIntList implements the metods
//and runningTotal method which do not change the
//original array and creates a new array as specified
//in the question.
//ArrayIntList.java
public class ArrayIntList
{
private int [] elementData;
private int size;
//constructor to create a array of maximum size 100
//and initilize the size variable to zero
public ArrayIntList()
{
elementData = new int[100];
size = 0;
}
//The method runningTotal that creates and temporary
//array and add the first element to the temp list from the elementData
//and add the element from tempList and consecutive element from the elementData
//array and add the final value to the templist.
//Returns the tempList as a return type value.
public ArrayIntList runningTotal()
{
ArrayIntList tempList=new ArrayIntList();
tempList.add(elementData[0]);
for (int index = 0; index < size-1; index++)
{
tempList.add(tempList.get(index)+elementData[index+1]);
}
return tempList;
}
//The add method accept an integer value
//and stores the element into the array list.
public void add(int value)
{
if(size<elementData.length)
elementData[size]= value;
size++;
}
//The method add accepts two argumets index an value
//and check whether the index is out of array lenght
//And stores the value at the given index position in the array.
public void add(int index, int value)
{
if(index<elementData.length)
elementData[index]=value;
}
//The method get accepts a index value and returns
//element stroed at the index position in the array.
public int get(int index)
{
return elementData[index];
}
//The method contains accepts a integer value as
//its argument and returns true if the value is
//found in the array list otherwise returns false
public boolean contains(int value)
{
boolean found=false;
for (int index = 0; index < size && !found; index++)
{
if(elementData[index]==value)
found=true;
}
return found;
}
//The method indexOf accepts a value that returns
//the position of value in the array list
public int indexOf(int value)
{
int found=-1;
for(int i=0; i<size;i++)
if(elementData[i]==found)
return i;
return found;
}
//Override the roString method to return the list
//of elements in the array as a string value.
public String toString()
{
if(size ==0)
{
return "[]";
}
else
{
String print="["+elementData[0];
for(int i =1; i<size;i++)
print= print+","+elementData[i];
print+="]";
return print;
}
}
//The method returns the current size of the array list.
public int size()
{
return size;
}
}
------------------------------------------------------------------------------------------------------
//Tester class
//TestArrayIntList.java
public class TestArrayIntList
{
public static void main(String[] args) {
//create an object of class ArrayIntList
ArrayIntList list1=new ArrayIntList();
//add elements to the list1 object calling add method
list1.add(2);
list1.add(3);
list1.add(5);
list1.add(4);
list1.add(7);
list1.add(15);
list1.add(20);
list1.add(7);
//print the elements in the list1
System.out.println(list1);
//call the method runningTotal on the list1 object
//and store the result ArrayIntList object
ArrayIntList list2=list1.runningTotal();
System.out.println("After calling runningTotal method");
////print the elements in the list2
System.out.println(list2);
}
}//end of the tester class
---------------------------------------------------------------------------------------
Sample output:
[2,3,5,4,7,15,20,7]
After calling runningTotal method
[2,5,10,14,21,36,56,63]
Hope this helps you.