Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

These questions relate to next questions I\'ll post separately (they are confuse

ID: 3585586 • Letter: T

Question

These questions relate to next questions I'll post separately (they are confuse me) Please explain. Thank you very much.

Self-Check Exercise

1. What does the following code fragment do?

ArrayList<Double> myList = new ArrayList<Double>();

myList.add(3.456);

myList.add(5.0);

double result = myList.get(0) + myList.get(1);

System.out.println("Result is " + result);

2. Trace the execution of the following:

int[] anArray = {0, 1, 2, 3, 4, 5, 6, 7};

for (int i = 3; i < anArray.length – 1; i++)

anArray[i + 1] = anArray[i];

and the following:

int[] anArray = {0, 1, 2, 3, 4, 5, 6, 7};

for (int i = anArray.length – 1; i > 3; i--)

anArray[i] = anArray[i – 1];

What are the contents of anArray after the execution of each loop?

Programming Exercise

Try to think about how to implement KWArrayList class. Please implement the following constructor and methods:

public KWArrayList()

public boolean add(E anEntry)

public E get(int index) { public E set(int index, E newValue)

public E remove(int index)

private void reallocate()

public int size()

public int indexOf(Object item)

Study the code for ArrayList implementation (enclosed in the folder) and work on the following exercise

1. Provide a constructor for class KWArrayList that accepts an int argument that represents the initial array capacity.

Use this instead of INITIAL_CAPACITY.

2. Implement the indexOf method of the KWArrayList class.

3.Write statements to remove the middle object from a KWArrayList and place it at the end.

/** Removes middle object and re-inserts it at the end @return E object removed and re-inserted */

public E removeMiddleInsertEnd()

{

}

4. Please define a tester class to verify your code.

Explanation / Answer

1. What does the following code fragment do?
ArrayList<Double> myList = new ArrayList<Double>();   //Creates an empty array list named myList.
myList.add(3.456);   //Appends a value 3.456 to the myList at the end.
myList.add(5.0);   //Appends a value 5.0 to the myList at the end.
double result = myList.get(0) + myList.get(1);   //Adds both the above values (3.456 + 5.0 = 8.456), and stores the result in result.
System.out.println("Result is " + result);   //Prints the statement: Result is 8.456

2. Trace the execution of the following:
int[] anArray = {0, 1, 2, 3, 4, 5, 6, 7};   //Creates an integer array named anArray.
for (int i = 3; i < anArray.length – 1; i++)   //Starting from i value 3 up to anArray.length-2 i.e., 6.
anArray[i + 1] = anArray[i];       //The values will be moved to the right.
So, now the contents are:
{0, 1, 2, 3, 3, 3, 3, 3}.
Note that, the first time the statement is executed for i value 3, the statement is:
anArray[4] = anArray[3];   //So, this statements copies the value from index 3 to index 4.
So, 3 will be copied to index 4.
Next the value in index 4 will be copied to index 5, and all this will simply propagate 3 throughout the array.

and the following:
int[] anArray = {0, 1, 2, 3, 4, 5, 6, 7};   //Creates an integer array named anArray.
for (int i = anArray.length – 1; i > 3; i--)   //Starting from anArray.length-1 i.e., 7 down to 4.
anArray[i] = anArray[i – 1];   //The values will be moved to the right.
So, now the contents are:
{0, 1, 2, 3, 3, 4, 5, 6}.
Note that, the first time the statement is executed for i value 7, the statement is:
anArray[7] = anArray[6];   //So, this statement copies the value 6 to index 7.
Next time it copies, 5 to index 6, and so on, till
anArray[4] = anArray[3];

What are the contents of anArray after the execution of each loop?

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote